For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS (Depth First Search) and BFS (Breadth First Search). 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 }
#include <stdio.h> #include <stdlib.h> #include<algorithm> #include<iostream> #include<stack> #include<queue> using namespace std; #define MaxVertices 10 /* maximum number of vertices */ typedef int Vertex; /* vertices are numbered from 0 to MaxVertices-1 */ typedef struct VNode *PtrToVNode; struct VNode { Vertex Vert; PtrToVNode Next; }; int edge[15][15]={0}; stack<int>st; queue<int>qu; typedef struct GNode *Graph; struct GNode { int NumOfVertices; int NumOfEdges; PtrToVNode *Array; }; int count1=-1; int min1[100]={999999}; Graph ReadG() { int N, M, i, t1, t2; scanf("%d %d", &N, &M); int isread[MaxVertices]={-1}; Graph G; G = (Graph)malloc(sizeof(GNode)); PtrToVNode a,b; G->NumOfEdges = M; G->NumOfVertices = N; (G->Array) = (PtrToVNode*)malloc(N * sizeof(PtrToVNode)); for (i = 0; i < N; i++) { G->Array[i] = (PtrToVNode)malloc(sizeof(VNode)); G->Array[i]->Vert = -1; G->Array[i]->Next = NULL; } for (i = 0; i < M; i++) { scanf("%d %d", &t1, &t2); //cin >> t1 >> t2; // if(isread[t1]==-1){ // G->Array[t1]->Vert=t2; // isread[t1]=1; a = G->Array[t1]; while (a->Next != NULL) a = a->Next; a->Next = (PtrToVNode)malloc(sizeof(VNode)); a->Next->Vert = t2; a->Next->Next = NULL; swap(t1,t2); a = G->Array[t1]; while (a->Next != NULL) a = a->Next; a->Next = (PtrToVNode)malloc(sizeof(VNode)); a->Next->Vert = t2; a->Next->Next = NULL; edge[t1][t2]=1; edge[t2][t1]=1; } return G; } void PrintV(Vertex V) { printf("%d ", V); } void StronglyConnectedComponents(Graph G, void(*visit)(Vertex V)); int isread[15] = { 0 }; void DFS(Graph G,int i){ cout<<" "<<i; isread[i]=1; for(int j=0;j<15;j++){ if(edge[i][j]==1&&isread[j]==0){ DFS(G,j); } } } void BFS(Graph G,int i){ qu.push(i); //cout<<i<<"*****"; PtrToVNode b; int k; int isread[15] = { 0 }; isread[i]=1; int fir=0; cout<<"{ "; int j; //cout<<qu.front(); while(!qu.empty()){ //cout<<"*"; j=qu.front(); qu.pop(); if(fir==0) fir=1; else cout<<" "; cout<<j; for(k=0;k<15;k++){ if(edge[j][k]==1&&isread[k]==0){ isread[k]=1; qu.push(k); } } } cout<<" }"<<endl; //cout<<edge[0][1]; } int main() { Graph G = ReadG(); for(int i=0;i<100;i++) min1[i]=999999999; StronglyConnectedComponents(G, PrintV); for(int i=0;i<=count1;i++){ cout<<"{"; DFS(G,min1[i]); cout<<" }"<<endl; } for(int i=0;i<=count1;i++){ BFS(G,min1[i]); } return 0; } void StronglyConnectedComponents(Graph G, void(*visit)(Vertex V)) { int isconect[15][15] = { 0 }; int i, j; for (i = 0; i < G->NumOfVertices; i++) { isconect[i][i] = 1; int isread[15] = { 0 }; int t; int stack[15]; t = 0; stack[++t] = i; while (t!=0) { j = stack[t]; t--; PtrToVNode b; b = G->Array[j]; while (b->Next!= NULL) { b = b->Next; isconect[i][b->Vert] = 1; if (isread[b->Vert] == 0) { isread[b->Vert] = 1; stack[++t] = b->Vert; } //b = b->Next; } } } int *a = (int*)malloc(G->NumOfVertices*(sizeof(int))); for (i = 0; i < G->NumOfVertices ; i++) a[i] = 0; i = 0; while (i < G->NumOfVertices) { int j; min1[++count1]=i; //int fir = 0; for (j = i; j < G->NumOfVertices; j++) { if (isconect[i][j] == 1 && isconect[j][i] == 1) { a[j] = 1; } } //printf("\n"); while (a[i] != 0) { i++; if (i > G->NumOfVertices) break; } } }
感想:
Q:为什么写这么复杂,邻接表又用,邻接矩阵又用?
A:因为上一道题用过了邻接表,这道题和上道题类似就搬过来了
Q:DFS能不能用栈实现?
A:树是可以的,但是图我做过了,答案是只要有回路就不可以,还是老老实实用递归吧
Q:为什么没有注释?
A:上一篇博客里有。
Q:这道题告诉我要记住什么东西?
A:DFS和BFS的区别可不是用队列和栈实现这么简单,DFS在树里可以用栈,但是为了方便,以后都用递归吧