PTA || 06-图1 列出连通集

给定一个有NNN个顶点和EEE条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1N-1N1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

输入格式:

输入第1行给出2个整数NNN(0<N≤100<N\le 100<N10)和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 }

BFS(广度优先搜索)

void BFS(MGraph Graph, Vertex V)
//BFS遍历 ,只遍历一个连通分量 
{
	Vertex W;
	Queue Q;
	Q = CreateQ(Graph->Nv );//建立一个容量为MaxVertexNum的空队列 
	if (Visited[V])
		return;
	Visited[V] = true;
	printf("%d ", V);
	AddQ(Q, V);
	while(!IsEmpty(Q)){ 
		V = DeleteQ(Q);
		for (W = 0; W<Graph->Nv ; W++){
			if (!Visited[W] && Graph->G[V][W] ==1){
				Visited[W] = true;
				printf("%d ", W);
				AddQ(Q, W);
			}
		}
	}//队列为空,表示一个连通分量访问结束 

DFS(深度优先搜索)

void DFS(MGraph Graph, Vertex V)
//DFS遍历,只遍历一个连通分量 
{
	Vertex W;
	if (Visited[V])
		return;
	Visited[V] = true;
	printf("%d ", V);
	for (W = 0; W < Graph->Nv ; W++){
		if (!Visited[W] && Graph->G[V][W] == 1)
			DFS(Graph, W);
	}
}
  上面两个标准代码段均是针对一个连通分量而言,搜索程序调用一次即遍历图中的一个连通分量。如果要遍历图中的每一个连通分量,则可以对图中的每一个顶点都调用一次搜索程序。
/*
	Name: 图1 列出连通集.cpp 
	Copyright: 
	Author: xuuyann
	Date: 04/11/18 21:06
	Description: 
*/

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 10
#define Vertex int
#define WeightType int
#define DataType int
#define ElementType int
#include <queue>


//用邻接矩阵存储图
//图结点的定义
typedef struct GNode *PtrToGNode;
struct GNode {
	int Nv;//顶点数 
	int Ne;//边数
	WeightType G[MaxVertexNum][MaxVertexNum];//邻接矩阵,表示顶点间的相邻关系 
	//DataType Data[MaxVertexNum];//存顶点数据 
};
typedef PtrToGNode MGraph;
//边的定义
typedef struct ENode *PtrToENode;
struct ENode {
	Vertex V1;
	Vertex V2;//边的两个端点(顶点) 
}; 
typedef PtrToENode Edge;
//队列定义
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
	ElementType *Data;
	Position front, rear;
	int MaxSize;
}; 
typedef PtrToQNode Queue;
bool Visited[MaxVertexNum];//全局变量。初始化为false 

Queue CreateQ(int MaxSize)
//建队列
{
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));
	Q->front = Q->rear = 0;
	Q->MaxSize = MaxSize;
} 
void AddQ(Queue Q, ElementType V)
//入队列
{
	Q->rear = (Q->rear + 1)%Q->MaxSize ;
	Q->Data[Q->rear ] = V;
 } 
 
ElementType DeleteQ(Queue Q)
//出队列
{
	Q->front = (Q->front +1)%Q->MaxSize;
	return Q->Data[Q->front ];
 }
bool IsEmpty(Queue Q)
//判断队列是否为空
{
	return (Q->rear ==Q->front );
 } 
 
MGraph CreateM(int V)
//初始化图,建立一个顶点数为V、边数E为0的空图
{
	int i, j;
	MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
	Graph->Nv = V;
	Graph->Ne = 0;
	for (i = 0; i<Graph->Nv ; i++){
		for (j = 0; j<Graph->Nv ; j++)
			Graph->G[i][j] = 0;//任意两顶点间无边相连 
	}
	
	return Graph;
} 

void InsertEdge(MGraph Graph, Edge E)
//插入边 
{
	Graph->G[E->V1 ][E->V2 ] = 1;
	Graph->G[E->V2 ][E->V1 ] = 1;//无向图 
} 

MGraph BuildGraph()
//建立图
{
	MGraph Graph;
	Edge E;
	int VertexNum, EdgeNum, i;
	scanf("%d %d", &VertexNum, &EdgeNum);
	Graph = CreateM(VertexNum);
	Graph->Ne = EdgeNum;
	if (Graph->Ne != 0){
		E =(Edge)malloc(sizeof(struct ENode)); 
		for (i = 0; i < Graph->Ne; i++){
			scanf("%d %d", &E->V1 ,&E->V2 );
			InsertEdge(Graph, E);
		}
	}
	
	return Graph;
}

void DFS(MGraph Graph, Vertex V)
//DFS遍历,只遍历一个连通分量 
{
	Vertex W;
	if (Visited[V])
		return;
	Visited[V] = true;
	printf("%d ", V);
	for (W = 0; W < Graph->Nv ; W++){
		if (!Visited[W] && Graph->G[V][W] == 1)
			DFS(Graph, W);
	}
}

void BFS(MGraph Graph, Vertex V)
//BFS遍历 ,只遍历一个连通分量 
{
	Vertex W;
	Queue Q;
	Q = CreateQ(Graph->Nv );//建立一个容量为MaxVertexNum的空队列 
	if (Visited[V])
		return;
	Visited[V] = true;
	printf("%d ", V);
	AddQ(Q, V);
	while(!IsEmpty(Q)){ 
		V = DeleteQ(Q);
		for (W = 0; W<Graph->Nv ; W++){
			if (!Visited[W] && Graph->G[V][W] ==1){
				Visited[W] = true;
				printf("%d ", W);
				AddQ(Q, W);
			}
		}
	}//队列为空,表示一个连通分量访问结束 
}

void ListDFS(MGraph Graph)
{
	int i;
	for (i = 0; i<Graph->Nv ; i++){
		if (!Visited[i]){
			printf("{ "); 
			DFS(Graph, i);
			printf("}\n");
		}
	}
}

void ListBFS(MGraph Graph)
{
	int i;
	for (i = 0; i<Graph->Nv ; i++){
		if (!Visited[i]){
			printf("{ ");
			BFS(Graph, i);
			printf("}\n");
		}
	}
}


//主体程序框架 
int main()
{
	int i;
	MGraph G;
	G = BuildGraph();//建立图
	for (i = 0; i < G->Nv ; i++)
		Visited[i] = false;
	ListDFS(G);//DFS(MGraph Graph, Vertex V)//DFS
	for (i = 0; i < G->Nv ; i++)
		Visited[i] = false;
	ListBFS(G);//void BFS(MGraph Graph, Vertex V)//BFS
	
	return 0; 
}


/*


//图的邻接矩阵表示简化法

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

#define MaxSive 10

int  graph[MaxSive][MaxSive];
int Nv, Ne;
int check[MaxSive];

void buildGraph()
{
    int V1, V2;
    scanf("%d %d",&Nv, &Ne);
    //CreateGraph
    for (int i = 0; i < Nv;i++)
    {
        check[i] = 0;
        for (int j = 0; j < Ne;j++)
        {
            graph[i][j] = 0;
        }
    }

    for (int i = 0; i < Ne; i++)
    {
        scanf("%d %d",&V1,&V2); //可以再增加权重
        //InsertEdge
        graph[V1][V2] = 1;
        graph[V2][V1] = 1;
    }
}

int checkVisited()
{
    int i;
    for (i = 0; i < Nv;i++)
    {
        if (!check[i])
        {
            break;
        }
    }
    if (i==Nv) 
    {
        return -1;
    }
    return i;
}

void BFS()
{
    queue<int> Q;
    int i, j;
    i = checkVisited();
    if (i==-1)
    {
        return;
    }
    Q.push(i);
    check[i] = true;
    printf("{ %d ", i); //顶点从0开始编号 //控制流的输出方式
    while (!Q.empty())
    {
        int temp = Q.front();
        Q.pop();
        for (j = 0; j < Nv;j++)
        {
            if (graph[temp][j]==1&&!check[j])
            {
                check[j] = 1;
                printf("%d ", j);
                Q.push(j);
            }
        }
    }
    printf("}\n");
    return BFS();  //采用尾递归
}

void DFS(int V)
{
    check[V] = 1;
    printf("%d ",V);
    for (int i = 0; i < Nv; i++)
    {
        if (graph[V][i]==1&&!check[i])
        {
            DFS(i);
        }
    }
}

void DFSList()
{
    for (int i = 0; i < Nv;i++)
    {
        if (check[i]==0)
        {
            printf("{ ");
            DFS(i);
            printf("}\n");
        }
    }
}

int main()
{
    buildGraph();

    DFSList();
    
    //memset(check, 0, sizeof(int)*MaxSive);
    for (int i = 0; i < Nv; i++)
    {
        check[i] = 0;
    }

    BFS();

    return 0;
}
*/







猜你喜欢

转载自blog.csdn.net/qq_26565435/article/details/83720591