06-图1 List Components

这题主要涉及到了队列,无向图的邻接矩阵表示,图的深度和广度优先搜索。又是糙哥,参考了他的程序(http://www.cnblogs.com/liangchao/p/4288807.html),主要是BFS那块,课件上的不太明白。有一点不太明白,图的初始化那块,利用传指向图的指针而不是通过函数返回值为什么不行?代码及题目如下

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdbool.h>
  5 
  6 typedef struct MGraph
  7 {
  8     int vertice;
  9     int * edge;
 10     bool * visited;
 11 }MGraph, * pMGraph;
 12 typedef struct Queue
 13 {
 14     int * elem;
 15     int head;
 16     int tail;
 17     int size;
 18 }Queue, * pQueue;
 19 
 20 pMGraph initGraph(int vn);
 21 void link(pMGraph pG, int v1, int v2);
 22 void DFS(pMGraph pG, int v);
 23 
 24 void BFS(pMGraph pG, int v);
 25 pQueue createQueue(int vn);
 26 bool isEmpty(pQueue pQ);
 27 void inQueue(pQueue, int v);
 28 int outQueue(pQueue);
 29 
 30 int main()
 31 {
 32 //    freopen("in.txt", "r", stdin); // for test
 33     int i, N, E;
 34     scanf("%d%d", &N, &E);
 35     pMGraph pG;
 36     pG = initGraph(N);
 37     
 38     int v1, v2;
 39     for(i = 0; i < E; i++)
 40     {
 41         scanf("%d%d", &v1, &v2);
 42         link(pG, v1, v2);
 43     }
 44     
 45     for(i = 0; i < N; i++)
 46     {
 47         if(!pG->visited[i])
 48         {
 49             printf("{ ");
 50             DFS(pG, i);
 51             printf("}
");
 52         }
 53     }
 54     memset(pG->visited, false, pG->vertice * sizeof(bool));
 55     for(i = 0; i < N; i++)
 56     {
 57         if(!pG->visited[i])
 58         {
 59             printf("{ ");
 60             BFS(pG, i);
 61             printf("}
");
 62         }
 63     }
 64 //    fclose(stdin); // for test
 65     return 0;
 66 }
 67 
 68 pMGraph initGraph(int vn)
 69 {
 70     int len;
 71     len = vn * (vn - 1) / 2;
 72     
 73     pMGraph pG;
 74     pG = (pMGraph)malloc(sizeof(MGraph));
 75     pG->vertice = vn;
 76     pG->edge = (int *)malloc(len * sizeof(int));
 77     memset(pG->edge, 0, len * sizeof(int));
 78     pG->visited = (bool *)malloc(vn * sizeof(bool));
 79     memset(pG->visited, false, vn * sizeof(bool));
 80     
 81     return pG;
 82 }
 83 
 84 void link(pMGraph pG, int v1, int v2)
 85 {
 86     int index;
 87     
 88     if(v1 > v2)
 89     {
 90         v1 += v2;
 91         v2 = v1 - v2;
 92         v1 -= v2;
 93     }
 94     index = v2 * (v2 - 1) / 2 + v1;
 95     pG->edge[index] = 1;
 96 }
 97 
 98 void DFS(pMGraph pG, int v)
 99 {
100     int row, col, index;
101     
102     pG->visited[v] = true;
103     printf("%d ", v);
104     for(col = 0; col < v; col++)
105     {
106         index = v * (v - 1) / 2 + col;
107         if(pG->edge[index] && pG->visited[col] == false)
108             DFS(pG, col);
109     }
110     for(row = v + 1; row < pG->vertice; row++)
111     {
112         index = row * (row - 1) / 2 + v;
113         if(pG->edge[index] && pG->visited[row] == false)
114             DFS(pG, row);
115     }
116 }
117 
118 void BFS(pMGraph pG, int v)
119 {
120     pQueue pQ;
121     pQ = createQueue(pG->vertice);
122     
123     int row, col, index;
124     pG->visited[v] = true;
125     printf("%d ", v);
126     inQueue(pQ, v);
127     while(!isEmpty(pQ))
128     {
129         v = outQueue(pQ);
130         for(col = 0; col < v; col++)
131         {
132             index = v * (v - 1) / 2 + col;
133             if(pG->edge[index] && pG->visited[col] == false)
134             {
135                 pG->visited[col] = true;
136                 printf("%d ", col);
137                 inQueue(pQ, col);
138             }
139         }
140         for(row = v + 1; row < pG->vertice; row++)
141         {
142             index = row * (row - 1) / 2 + v;
143             if(pG->edge[index] && pG->visited[row] == false)
144             {
145                 pG->visited[row] = true;
146                 printf("%d ", row);
147                 inQueue(pQ, row);
148             }
149         }
150     }
151 }
152 
153 pQueue createQueue(int vn)
154 {
155     pQueue pQ;
156     pQ = (pQueue)malloc(sizeof(Queue));
157     pQ->size = vn + 1;
158     pQ->head = pQ->tail = 0;
159     pQ->elem = (int *)malloc(pQ->size * sizeof(int));
160     
161     return pQ;
162 }
163 
164 bool isEmpty(pQueue pQ)
165 {
166     if(pQ->head != pQ->tail)
167         return false;
168     else
169         return true;
170 }
171 
172 void inQueue(pQueue pQ, int v)
173 {
174     pQ->tail = (pQ->tail + 1) % pQ->size;
175     pQ->elem[pQ->tail] = v;
176 }
177 
178 int outQueue(pQueue pQ)
179 {
180     pQ->head = (pQ->head + 1) % pQ->size;
181     
182     return pQ->elem[pQ->head];
183 }

For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index, and visit its adjacent vertices in ascending order of their indices.

Input Specification:

Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{ v1 v2 ... vk }". First print the result obtained by DFS, then by BFS.

Sample Input:

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

Sample Output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }
原文地址:https://www.cnblogs.com/qingkai/p/4439302.html