06-图1 列出连通集(lists the connectivity set)

题目描述:
Given one hasN vertices andFor the undirected graph of the E side, please list all connected sets with DFS and BFS respectively. Suppose the vertices from 0 toN − 1 number. When searching, let’s assume that we always start from the lowest numbered vertex and access the neighbors in increasing order of numbers.
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

Input format:
输入格式:

Enter line 1 to give 2 integersN (0 < N ≤ 1 0 ) andE is the number of vertices and the number of sides of the graph. SubsequentlyLine E , each line gives two endpoints of an edge. The numbers in each line are separated by 1 space.
输入第1行给出2个整数N(0

Output format:
输出格式:

according to"{ v1, v​2… v​k}"“format, each line of output set of a communication. DFS first output, and then outputs the result of the BFS.
按照”{ v1, v​2… v​k}"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。

Input example:
输入样例:

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
#include
#define Maxsize 100
struct ENode{
	int v1,v2;
};//边 
typedef struct ENode *Edge;
struct Gnode{
	int topnum;
	int edgenum;
	int G[Maxsize][Maxsize];
	int Data[Maxsize];
};//图 
int queue[Maxsize];//队列 
int front = -1;
int rear = -1;
typedef struct Gnode *Graph;
void InsertEdge(Graph graphptr,Edge E); //插入 
void DFS(Graph graphptr,int v,int *);//深度遍历 
void BFS(Graph graphptr,int v,int *);//广度遍历 
int main()
{
	Graph graphptr = (Graph)malloc(sizeof(struct Gnode));
	scanf("%d",&graphptr->topnum);
	scanf("%d",&graphptr->edgenum);
	int v,w,flag = 0,visited[graphptr->topnum] = {0};//初始化 
	Edge E ;
	for(v = 0; v <= graphptr->topnum - 1;v ++)
		for(w = 0;w <= graphptr->topnum - 1;w ++)
			graphptr->G[v][w] = 0; //初始化 
	if(graphptr->edgenum != 0)
	{
		E = (Edge)malloc(sizeof(struct ENode));
		for(v = 0; v <= graphptr->edgenum - 1;v++)
		{
			scanf("%d %d",&E->v1,&E->v2);//边的数据读入 
			InsertEdge(graphptr,E); 
		} 
	}
	for(v = 0;v <= graphptr->topnum - 1;v++)
	{
		if( !visited[v] )//查找 
		{
			printf("{ ");
			DFS(graphptr,v,visited);
			printf("}\n"); 
		}
	}
	for(v = 0;v <=graphptr->topnum - 1;v ++)//初始化 
		visited[v] = 0;
	for(v = 0;v <= graphptr->topnum - 1;v++)
	{
		if( !visited[v] )//查找,为零,表明未被查找过 
		{
			printf("{ ");
			BFS(graphptr,v,visited);
			printf("}\n"); 
		}
	}
	return 0;
}
void InsertEdge(Graph graphptr,Edge E)
{
	graphptr->G[E->v1][E->v2] = 1;//无向图 
	graphptr->G[E->v2][E->v1] = 1; 
} 
void DFS(Graph graphptr,int v,int visited[])
{
	visited[v] = 1;
	printf("%d ",v);//先打印自身,自身也是单独连通图 
	for(int w = 0;w <= graphptr->topnum - 1;w ++)
	{
		if( graphptr->G[v][w] && !visited[w])// 为零,表明未被查找过 
		DFS( graphptr,w , visited );
	}
	return ; 
}
void BFS(Graph graphptr,int v,int visited[])
{
	int w = 0,i = 0;
	visited[v] = 1;
	queue[++rear] = v;//入队 
	while(front != rear)
	{
		w = queue[++front];//出队 
		printf("%d ",w);
		for(i = 0;i <=graphptr->topnum - 1;i++)
		{
			if( !visited[i] && graphptr->G[w][i])// 为零,表明未被查找过 
			{
				visited[i] = 1;//改变状态 
				queue[++rear] = i;//入队 
			}
		}
	} 
}

若有不清楚这两种方法的,点击了解图的遍历+深度优先+广度优先(Graph)和图的概念

你可能感兴趣的:(数据结构,#,exercise)