06-图1 列出连通集 (25 分)

给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

输入格式:

输入第1行给出2个整数N(0)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。

输出格式:

按照"{ v1​​ v2​​ ... vk​​ }"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。

输入样例:

8 6
0 7
0 1
2 0
4 1
2 4
3 5

输出样例:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

  1 #define MaxVertexNum 120
  2 #include "stdio.h"
  3 #include <stdlib.h>
  4 #include <queue>
  5 using namespace std;
  6 
  7 
  8 typedef int WeightType;
  9 typedef int DataType;
 10 typedef int Vertex;
 11 
 12 typedef  struct GNode* PtrToGNode;
 13 struct GNode {
 14     int Nv;
 15     int Ne;
 16     WeightType G[MaxVertexNum][MaxVertexNum];
 17     DataType Data[MaxVertexNum];
 18 };
 19 typedef PtrToGNode MGraph;
 20 
 21 typedef struct ENode* PtrToENode;
 22 struct ENode {
 23     Vertex V1, V2;
 24     WeightType Weight;
 25 };
 26 typedef PtrToENode Edge;
 27 
 28 MGraph createGraph( int VertexNum ) {
 29     Vertex V, M;
 30     MGraph Graph;
 31     Graph = (MGraph) malloc(sizeof(struct GNode));
 32     
 33     Graph->Ne = 0;
 34     Graph->Nv = VertexNum;
 35     
 36     for (V = 0; V<VertexNum; V++) {
 37         for(M = 0; M<VertexNum; M++) {
 38             Graph->G[V][M] = 0;
 39         }    
 40     }
 41     return Graph;
 42 }
 43 
 44 void InsertEdge(MGraph Graph, Edge E) {
 45     Graph->G[E->V1][E->V2] = E->Weight;
 46     //undirected graph
 47     Graph->G[E->V2][E->V1] = E->Weight;
 48 }
 49 
 50 MGraph buildGraph() {
 51     MGraph Graph;
 52     int Nv;
 53     Vertex V;
 54     Edge E;
 55     
 56     scanf( "%d", &Nv );
 57     Graph = createGraph(Nv);
 58     
 59     scanf( "%d", &(Graph->Ne) );
 60     if(Graph->Ne){
 61         E = (Edge) malloc(sizeof(struct ENode));
 62         for(int i=0; i<Graph->Ne; i++) {
 63             scanf("%d %d", &(E->V1), &(E->V2));
 64             E->Weight = 1;
 65             InsertEdge(Graph, E);
 66         }
 67     }
 68     
 69     /*
 70     for(V = 0; V < Graph->Nv; V++) {
 71         scanf("%d", Graph->Data[V]);
 72     }
 73     */
 74     return Graph;
 75 }
 76 
 77 int Visted[MaxVertexNum] = {0};
 78 void reset(int a[], int bound) {
 79     for (int i=0;i<bound;i++) {
 80         a[i] = 0;
 81     }
 82 }
 83 
 84 void DFS(MGraph Graph, Vertex V) {
 85     
 86     printf("%d ", V);
 87     Visted[V] = 1;
 88     Vertex M;
 89     
 90     for (M = 0; M<Graph->Nv; M++) {
 91         if (Graph->G[V][M] and !Visted[M]) {
 92             DFS(Graph, M);
 93         }
 94     }
 95     
 96 }
 97 
 98 
 99 void BFS(MGraph Graph, Vertex V) {
100     queue< Vertex > Q;
101     Vertex E;
102     
103     printf("%d ", V);
104     Visted[V] = 1;
105     Q.push(V);
106     
107     while(!Q.empty()) {
108         E = Q.front();
109         Q.pop();
110         
111         for(Vertex W = 0; W < Graph->Nv; W++) {
112             if(!Visted[W] and Graph->G[E][W] == 1) {
113                 printf("%d ", W);
114                 Visted[W] = 1;
115                 Q.push(W);
116             }
117         }
118     }
119     
120     
121 }
122 
123 void search(MGraph Graph, Vertex V, void(*f)(MGraph Graph, Vertex V)) {
124     reset(Visted, MaxVertexNum);
125     for(V=0; V<Graph->Nv; V++) {
126         if (!Visted[V]) {
127             printf("{ ");
128             f(Graph, V);
129             printf("}\n");
130             
131         }
132     }
133     
134 }
135 
136 
137 int main() {
138     MGraph Graph = buildGraph();
139     Vertex V;
140     
141     search(Graph, V, DFS);
142     search(Graph, V, BFS);
143 }

猜你喜欢

转载自www.cnblogs.com/acoccus/p/10935640.html