06-图1 列出连通集(25 分)邻接矩阵
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。
输入格式:
输入第1行给出2个整数N(0<N≤10)和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 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 5 #define MaxVertexNum 100 6 #define INFINITY 65535 7 #define MaxSize 100 8 9 typedef int Vertex; 10 typedef int WeightType; 11 typedef char DataType; 12 13 typedef struct ENode *PtrToENode; 14 struct ENode { 15 Vertex V1, V2; 16 WeightType Weight; 17 }; 18 typedef PtrToENode Edge; 19 20 typedef struct GNode *PtrToGNode; 21 struct GNode { 22 int Nv; 23 int Ne; 24 WeightType G[MaxVertexNum][MaxVertexNum]; 25 DataType Data[MaxVertexNum]; 26 }; 27 typedef PtrToGNode MGraph; 28 29 struct QNode { 30 Vertex Data[MaxSize]; 31 int rear; 32 int front; 33 }; 34 typedef struct QNode *Queue; 35 36 MGraph CreateGraph(int VertexNum) 37 { 38 Vertex V, W; 39 MGraph Graph; 40 41 Graph = (MGraph)malloc(sizeof(struct GNode)); 42 Graph->Nv = VertexNum; 43 Graph->Ne = 0; 44 for(V=0;V<Graph->Nv;V++) 45 for(W=0;W<Graph->Nv;W++) 46 Graph->G[V][W] = INFINITY; 47 48 return Graph; 49 } 50 51 void InsertEdge(MGraph Graph, Edge E) 52 { 53 Graph->G[E->V1][E->V2] = E->Weight; 54 Graph->G[E->V2][E->V1] = E->Weight; 55 } 56 57 MGraph BuildGraph() 58 { 59 MGraph Graph; 60 Edge E; 61 // Vertex V; 62 int Nv, i; 63 64 scanf("%d ", &Nv); 65 Graph = CreateGraph(Nv); 66 67 scanf("%d\n", &(Graph->Ne)); 68 if(Graph->Ne != 0) { 69 E = (Edge)malloc(sizeof(struct ENode)); 70 for(i=0;i<Graph->Ne;i++) { 71 scanf("%d %d\n", &E->V1, &E->V2); 72 E->Weight = 1; 73 InsertEdge(Graph, E); 74 } 75 } 76 77 // for(V=0;V<Graph->Nv;V++) 78 // scanf(" %c", &(Graph->Data[V])); 79 80 return Graph; 81 } 82 83 //DFS 84 void Visit(Vertex V) 85 { 86 // printf("Now visit Vertex %d\n", V); 87 printf(" %d", V); 88 } 89 90 void DFS(MGraph Graph, Vertex V, int *Visited) 91 { 92 Vertex W; 93 94 Visit(V); 95 Visited[V] = 1; //已访问 96 97 for(W=0;W<Graph->Nv;W++) 98 if(Graph->G[V][W]==1 && Visited[W]==0) 99 DFS(Graph, W, Visited); 100 } 101 102 //BFS 103 int IsEmpty(Queue Q) 104 { 105 return (Q->rear == Q->front); //1:empty 0:not empty 106 } 107 108 void AddQ(Queue PtrQ, Vertex item) 109 { 110 if((PtrQ->rear+1)%MaxSize == PtrQ->front) { 111 printf("Queue full"); 112 return; 113 } 114 PtrQ->rear = (PtrQ->rear+1)%MaxSize; 115 PtrQ->Data[PtrQ->rear] = item; 116 } 117 118 Vertex DeleteQ(Queue PtrQ) 119 { 120 if(PtrQ->front == PtrQ->rear) { 121 printf("Queue empty"); 122 return -1; 123 } else { 124 PtrQ->front = (PtrQ->front+1)%MaxSize; 125 return PtrQ->Data[PtrQ->front]; 126 } 127 } 128 129 int IsEdge(MGraph Graph, Vertex V, Vertex W) 130 { 131 return Graph->G[V][W]<INFINITY?1:0; 132 } 133 134 void BFS(MGraph Graph, Vertex S, int *Visited) 135 { 136 Queue Q; 137 Vertex V, W; 138 Q = (Queue)malloc(sizeof(struct QNode)); 139 Visit(S); 140 Visited[S] = 1; 141 AddQ(Q, S); 142 143 while(!IsEmpty(Q)) { 144 V = DeleteQ(Q); 145 for(W=0;W<Graph->Nv;W++) 146 if(!Visited[W] && IsEdge(Graph, V, W)) { 147 Visit(W); 148 Visited[W] = 1; 149 AddQ(Q, W); 150 } 151 } 152 } 153 154 void PrintGraph(MGraph Graph) 155 { 156 Vertex V, W; 157 printf("Graph:\n"); 158 for(V=0;V<Graph->Nv;V++) { 159 for(W=0;W<Graph->Nv;W++) 160 printf("%5d " ,Graph->G[V][W]); 161 printf("\n"); 162 } 163 printf("-----------------------\n"); 164 } 165 166 int main() 167 { 168 MGraph Graph; 169 int *Visited, i; 170 171 Graph = BuildGraph(); 172 Visited = (int *)malloc(sizeof(int)*(Graph->Nv)); 173 for(i=0;i<Graph->Nv;i++) 174 Visited[i] = 0; 175 // PrintGraph(Graph); 176 177 //DFS 178 for(i=0;i<Graph->Nv;i++) { 179 if(Visited[i] == 0) { 180 printf("{"); 181 DFS(Graph, i, Visited); 182 printf(" }\n"); 183 } 184 } 185 186 //BFS 187 for(i=0;i<Graph->Nv;i++) 188 Visited[i] = 0; 189 for(i=0;i<Graph->Nv;i++) { 190 if(!Visited[i]) { 191 printf("{"); 192 BFS(Graph, i, Visited); 193 printf(" }\n"); 194 } 195 } 196 197 return 0; 198 }
无欲速,无见小利。欲速,则不达;见小利,则大事不成。