ACM Learning-cutting Point and bridge. CPP: The entry point that defines the console application.
//
#include "stdafx.h"
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace Std;
const int v = 13;
int Edge[v][v] = {
{0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}
};
BOOL Vis[v] = {false};
Char t[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ',
' I ', ' J ', ' K ', ' L ', ' M '};
Queue<int> Q;
Determine if I-point to J-Point has two paths
BOOL Is_can_shuang_lian_tong (int i, int j) {
for (int i = 0; i<v; i++) {
Vis[i] = false;
}
Queue<int> Q;
Q.push (i);
Vis[i] = true;
int count = 0;
while (Q.size () > 0) {
int k = Q.front ();
Q.pop ();
if (Edge[k][j]) {
count++;
VIS[J] = true;
if (count = = 2)
return true;
}
for (int i = 0; i < V; i++) {
if (Edge[k][i] &&!vis[i])
{
Vis[i] = true;
Q.push (i);
}
}
}
return false;
}
void print (int i)
{
cout << T[i] << ends;
}
void Bian_zi_tu () {//breadth-first traversal
Queue<int> Q;
Vector<int> Vnum;
int yy[v][v];//This variable can be removed, useless
memcpy (yy, Edge, v*v * sizeof (int));
for (int i = 0; i<v; i++) {
Vis[i] = false;
}
for (int i = 0; i<v; i++) {
if (!vis[i])
{
Vis[i] = true;
Vnum.clear ();
Q.push (i);
Vnum.push_back (i);
while (Q.size () > 0) {
int k = Q.front ();
Q.pop ();
for (int i = 0; i < V; i++) {
if (Yy[k][i]&&!vis[i])
{
Yy[k][i] = 0;
Yy[i][k] = 0;
Vis[i] = true;
Q.push (i);
Vnum.push_back (i);
}
}
}//while
For_each (Vnum.begin (), Vnum.end (), print);
cout << Endl;
}
}
}
Get a graph with two connected sub-graphs
void Get_shuang_lian_tong () {
bool Iscontinue = true;
while (iscontinue) {
int count = 0;
int index = 0;
Iscontinue = false;//Because the node relationship was deleted, it may have to be deleted again
for (int i = 0; i < V; i++) {//If a node has only one node connected to it, delete its relationship to the attached node
Count = 0;
for (int j = 0; J < V; j + +) {
if (Edge[i][j]) {
++count;
index = j;
}
}
if (Count ==1)
{
Iscontinue = true;
Edge[i][index] = 0;
Edge[index][i] = 0;
}
}
}
for (int i = 0; i < V; i++) {
for (int j = 0; J < V; j + +) {
if (Edge[i][j]) {
if (!is_can_shuang_lian_tong (I,J))//Determine if I and J have two paths
{//If their relationship is not deleted
EDGE[I][J] = 0;
Edge[j][i] = 0;
}
}
}
}
}
int _tmain (int argc, _tchar* argv[])
{
Get_shuang_lian_tong ();
/*for (int i = 0; i < V; i++) {
for (int j = 0; J < V; j + +) {
if (Edge[i][j])
{
cout << T[i] << "and" << t[j "<<" attached "<< Endl;
}
}
}*/
Bian_zi_tu ();
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ACM Learning-Graph Dual connected sub-graph