06-图1 List Components

时间:2023-03-09 00:18:04
06-图1 List Components

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

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct MGraph
{
int vertice;
int * edge;
bool * visited;
}MGraph, * pMGraph;
typedef struct Queue
{
int * elem;
int head;
int tail;
int size;
}Queue, * pQueue; pMGraph initGraph(int vn);
void link(pMGraph pG, int v1, int v2);
void DFS(pMGraph pG, int v); void BFS(pMGraph pG, int v);
pQueue createQueue(int vn);
bool isEmpty(pQueue pQ);
void inQueue(pQueue, int v);
int outQueue(pQueue); int main()
{
// freopen("in.txt", "r", stdin); // for test
int i, N, E;
scanf("%d%d", &N, &E);
pMGraph pG;
pG = initGraph(N); int v1, v2;
for(i = ; i < E; i++)
{
scanf("%d%d", &v1, &v2);
link(pG, v1, v2);
} for(i = ; i < N; i++)
{
if(!pG->visited[i])
{
printf("{ ");
DFS(pG, i);
printf("}\n");
}
}
memset(pG->visited, false, pG->vertice * sizeof(bool));
for(i = ; i < N; i++)
{
if(!pG->visited[i])
{
printf("{ ");
BFS(pG, i);
printf("}\n");
}
}
// fclose(stdin); // for test
return ;
} pMGraph initGraph(int vn)
{
int len;
len = vn * (vn - ) / ; pMGraph pG;
pG = (pMGraph)malloc(sizeof(MGraph));
pG->vertice = vn;
pG->edge = (int *)malloc(len * sizeof(int));
memset(pG->edge, , len * sizeof(int));
pG->visited = (bool *)malloc(vn * sizeof(bool));
memset(pG->visited, false, vn * sizeof(bool)); return pG;
} void link(pMGraph pG, int v1, int v2)
{
int index; if(v1 > v2)
{
v1 += v2;
v2 = v1 - v2;
v1 -= v2;
}
index = v2 * (v2 - ) / + v1;
pG->edge[index] = ;
} void DFS(pMGraph pG, int v)
{
int row, col, index; pG->visited[v] = true;
printf("%d ", v);
for(col = ; col < v; col++)
{
index = v * (v - ) / + col;
if(pG->edge[index] && pG->visited[col] == false)
DFS(pG, col);
}
for(row = v + ; row < pG->vertice; row++)
{
index = row * (row - ) / + v;
if(pG->edge[index] && pG->visited[row] == false)
DFS(pG, row);
}
} void BFS(pMGraph pG, int v)
{
pQueue pQ;
pQ = createQueue(pG->vertice); int row, col, index;
pG->visited[v] = true;
printf("%d ", v);
inQueue(pQ, v);
while(!isEmpty(pQ))
{
v = outQueue(pQ);
for(col = ; col < v; col++)
{
index = v * (v - ) / + col;
if(pG->edge[index] && pG->visited[col] == false)
{
pG->visited[col] = true;
printf("%d ", col);
inQueue(pQ, col);
}
}
for(row = v + ; row < pG->vertice; row++)
{
index = row * (row - ) / + v;
if(pG->edge[index] && pG->visited[row] == false)
{
pG->visited[row] = true;
printf("%d ", row);
inQueue(pQ, row);
}
}
}
} pQueue createQueue(int vn)
{
pQueue pQ;
pQ = (pQueue)malloc(sizeof(Queue));
pQ->size = vn + ;
pQ->head = pQ->tail = ;
pQ->elem = (int *)malloc(pQ->size * sizeof(int)); return pQ;
} bool isEmpty(pQueue pQ)
{
if(pQ->head != pQ->tail)
return false;
else
return true;
} void inQueue(pQueue pQ, int v)
{
pQ->tail = (pQ->tail + ) % pQ->size;
pQ->elem[pQ->tail] = v;
} int outQueue(pQueue pQ)
{
pQ->head = (pQ->head + ) % pQ->size; return pQ->elem[pQ->head];
}

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 }