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 5Sample Output:
{ 0 1 4 2 7 } { 3 5 } { 6 } { 0 1 2 7 4 } { 3 5 } { 6 }
题意:给出图中各个顶点的连通关系,要求输出DFS与BFS的遍历结果
解题思路:根据输入建立邻接矩阵,再进行图的遍历,注意:图中可能不是所有顶点都连通的,因此需要对图中每个顶点进行遍历
#include <iostream> #include <string> #include <memory.h> using namespace std; typedef struct Graph{ int *graphMatrix; bool *visited; int vertexNumber; }*pGraph, nGraph; typedef struct{ int *queData; int queFront; int queEnd; int MaxSize; }*pQue, nQue; pGraph CreateGraph( int vertexNumber ); pGraph InsertEdge( pGraph pG, int edgeStart, int edgeEnd ); void DFS( pGraph pG, int vertex ); pQue CreateQueue( int queSize ); bool IsEmptyQ( pQue pQ ); void AddQ( pQue pQ, int item ); int DeleteQ( pQue pQ ); void BFS( pGraph pG, int vertex, int vertexNum ); int main() { int N, E; cin >> N >> E; pGraph pG; pG = CreateGraph( N ); int i; int edgeStart, edgeEnd; for ( i = 0; i < E; i++ ) { cin >> edgeStart >> edgeEnd; InsertEdge( pG, edgeStart, edgeEnd ); } for ( i = 0; i < N; i++ ) { if ( pG->visited[i] == false ) { cout << "{ "; DFS( pG, i); cout << "}" << endl; } } for ( i = 0; i < N; i++ ) //清空visited状态 { pG->visited[i] = false; } for ( i = 0; i < N; i++ ) { if ( pG->visited[i] == false ) { cout << "{ "; BFS( pG, i, N); cout << "}" << endl; } } return 0; } pGraph CreateGraph( int vertexNumber ) { pGraph pG; pG = ( pGraph )malloc( sizeof( nGraph ) ); pG->vertexNumber = vertexNumber; int len = ( vertexNumber * ( vertexNumber + 1 ) / 2 ); pG->graphMatrix = ( int* )malloc( len * sizeof( int ) ); memset( pG->graphMatrix, 0, sizeof( int ) * len); pG->visited = ( bool* )malloc( vertexNumber * sizeof( bool ) ); memset( pG->visited, false, sizeof( bool ) * vertexNumber ); return pG; } pGraph InsertEdge( pGraph pG, int edgeStart, int edgeEnd ) { if ( edgeStart >= pG->vertexNumber || edgeEnd >= pG->vertexNumber ) { cout << "Error Edge!" << endl; return pG; } int tmp; if ( edgeStart < edgeEnd ) { tmp = edgeStart; edgeStart = edgeEnd; edgeEnd = tmp; } int item = edgeStart * ( edgeStart + 1 ) / 2 + edgeEnd; pG->graphMatrix[item] = 1; return pG; } void DFS( pGraph pG, int vertex ) { if ( pG->visited[vertex] == false ) { cout << vertex << ' '; } pG->visited[vertex] = true; int rows, cols; int materixIndex; for ( cols = 0; cols < vertex; cols++ ) //扫描行 { materixIndex = vertex * ( vertex + 1 ) / 2 + cols; if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ cols ] == false ) { DFS( pG, cols ); } } for ( rows = vertex; rows < pG->vertexNumber; rows++ ) { materixIndex = rows * ( rows + 1 ) / 2 + vertex; if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ rows ] == false ) { DFS( pG, rows ); } } } pQue CreateQueue( int queSize ) { pQue pQ; pQ = ( pQue )malloc( sizeof( nQue ) ); pQ->MaxSize = queSize + 1; pQ->queData = ( int * )malloc( pQ->MaxSize * sizeof( int ) ); pQ->queFront = pQ->queEnd = 0; return pQ; } bool IsEmptyQ( pQue pQ ) { if ( pQ->queFront == pQ->queEnd ) //队列为空 { return true; } else { return false; } } void AddQ( pQue pQ, int item ) { if ( ( pQ->queEnd + 1 ) % pQ->MaxSize == pQ->queFront ) { cout << "Queue Is Full!" << endl; return; } pQ->queEnd = ( pQ->queEnd + 1 ) % pQ->MaxSize; pQ->queData[ pQ->queEnd ] = item; } int DeleteQ( pQue pQ ) { if ( pQ->queFront == pQ->queEnd ) { cout << "Queue Is Empty!" << endl; return -1; } else { pQ->queFront = ( pQ->queFront + 1 ) % pQ->MaxSize; return pQ->queData[ pQ->queFront ]; } } void BFS( pGraph pG, int vertex, int vertexNum ) { cout << vertex << ' '; pG->visited[vertex] = true; pQue pQ; pQ = CreateQueue( vertexNum ); AddQ( pQ, vertex); int cols, rows; int materixIndex; while ( IsEmptyQ( pQ ) != true ) { vertex = DeleteQ( pQ ); for ( cols = 0; cols < vertex; cols++ ) //扫描行 { materixIndex = vertex * ( vertex + 1 ) / 2 + cols; if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ cols ] == false ) { cout << cols << ' '; pG->visited[cols] = true; AddQ( pQ, cols ); } } for ( rows = vertex; rows < pG->vertexNumber; rows++ ) { materixIndex = rows * ( rows + 1 ) / 2 + vertex; if ( pG->graphMatrix[materixIndex] == 1 && pG->visited[ rows ] == false ) { cout << rows << ' '; pG->visited[rows] = true; AddQ( pQ, rows ); } } } }