给定一个有NNN个顶点和EEE条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1N-1N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。
输入格式:
输入第1行给出2个整数NNN(0<N≤100<N\le 100<N≤10)和EEE,分别是图的顶点数和边数。随后EEE行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。
输出格式:
按照"{ v1v_1v1 v2v_2v2 ... vkv_kvk }"的格式,每行输出一个连通集。先输出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行给出2个整数NNN(0<N≤100<N\le 100<N≤10)和EEE,分别是图的顶点数和边数。随后EEE行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。
输出格式:
按照"{ v1v_1v1 v2v_2v2 ... vkv_kvk }"的格式,每行输出一个连通集。先输出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 }
//邻接矩阵实现 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 10 #define INF 0 int visit[MAXN]; typedef int vertex; typedef struct GNode* Graph; struct GNode { int Data[MAXN][MAXN]; int Nv, Ne; }; typedef struct ENode* Edge; struct ENode { vertex V1, V2; }; Graph CreateGraph(int n) { vertex v, w; Graph G = (Graph)malloc(sizeof(struct GNode)); G->Ne = 0; G->Nv = n; for(v = 0; v < G->Nv; v++) for(w = 0; w < G->Nv; w++) G->Data[v][w] = INF; return G; } void InsertEdge(Graph G, Edge E) { G->Data[E->V1][E->V2] = 1; G->Data[E->V2][E->V1] = 1; } void PrintGraph(Graph G) { vertex v, w; for(v = 0; v < G->Nv; v++) { for(w = 0; w < G->Nv; w++) { printf("%3d", G->Data[v][w]); } printf("\n"); } } int IsEdge(Graph G, vertex v1, vertex v2) { return G->Data[v1][v2] != 0 ? 1 : 0; } void Visit(vertex v) { printf("%d ", v); visit[v] = 1; } void BFS(Graph G, vertex s) { int Queue[MAXN], rear, front; vertex v, w; front = rear = 0; Visit(s); Queue[++rear] = s; while(rear != front) { v = Queue[++front]; for(w = 0; w < G->Nv; w++) { if(IsEdge(G, v, w) && !visit[w]) { Visit(w); Queue[++rear] = w; } } } } void DFS(Graph G, vertex s) { vertex v; Visit(s); for(v = 0; v < G->Nv; v++) if(IsEdge(G, v, s) && !visit[v]) DFS(G, v); } void ListComponents(Graph G, int tag) { vertex v; for(v = 0; v < G->Nv; v++) { if(!visit[v]) { printf("{ "); if(tag) BFS(G, v); else DFS(G, v); printf("}\n"); } } } int main() { int N, i; Graph G; Edge E; //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); memset(visit, 0, sizeof(visit)); scanf("%d", &N); G = CreateGraph(N); scanf("%d", &G->Ne); if(G->Ne) { E = (Edge)malloc(sizeof(struct ENode)); for(i = 0; i < G->Ne; i++) { scanf("%d%d", &E->V1, &E->V2); InsertEdge(G, E); } }//PrintGraph(G); ListComponents(G, 0); memset(visit, 0, sizeof(visit)); ListComponents(G, 1); return 0; }
//邻接表 #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXN 10 int visit[MAXN]; typedef int Vertex; typedef struct ENode* Edge; struct ENode { Vertex V1, V2; }; typedef struct AdjNode* PtrA; struct AdjNode { Vertex V; PtrA Next; }; typedef struct VNode { PtrA FirstEdge; }AdjList[MAXN]; typedef struct GNode *Graph; struct GNode { int Nv, Ne; AdjList GList; }; Graph CreateGraph(Vertex n) { Vertex v; Graph G = (Graph)malloc(sizeof(struct GNode)); G->Nv = n; G->Ne = 0; for(v = 0; v < n; v++) G->GList[v].FirstEdge = NULL; return G; } void InsertEdge(Graph G, Edge E) { PtrA newnode = (PtrA)malloc(sizeof(struct AdjNode)); newnode->V = E->V2; newnode->Next = G->GList[E->V1].FirstEdge; G->GList[E->V1].FirstEdge = newnode; newnode = (PtrA)malloc(sizeof(struct AdjNode)); newnode->V = E->V1; newnode->Next = G->GList[E->V2].FirstEdge; G->GList[E->V2].FirstEdge = newnode; } void PrintGraph(Graph G) { Vertex v; PtrA p; for(v = 0; v < G->Nv; v++) { printf("%3d", v); p = G->GList[v].FirstEdge; while(p) { printf("%3d", p->V); p = p->Next; } printf("\n"); } } void Visit(Vertex V) { visit[V] = 1; printf("正在访问定点%d\n", V); } void DFS(Graph G, Vertex V) { PtrA W; Visit(V); for(W = G->GList[V].FirstEdge; W; W = W->Next) if(!visit[W->V]) DFS(G, W->V); }; int main() { int n, i; Graph G; Edge E; memset(visit, 0, sizeof(visit)); scanf("%d", &n); G = CreateGraph(n); scanf("%d", &G->Ne); if(G->Ne) { E = (Edge)malloc(sizeof(struct ENode)); for(i = 0; i < G->Ne; i++) { scanf("%d%d", &E->V1, &E->V2); InsertEdge(G, E); } }PrintGraph(G); DFS(G, 0); return 0; }