欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构与算法-15无向图广度遍历(邻接矩阵、邻接表)

程序员文章站 2023-12-23 19:51:39
...

Description
给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列。(同一个结点的同层邻接点,节点编号小的优先遍历)。

Input
输入第一行为整数n(0< n <100),表示数据的组数。 对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。 下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

Output
输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。

Sample Input
数据结构与算法-15无向图广度遍历(邻接矩阵、邻接表)
Sample Output
数据结构与算法-15无向图广度遍历(邻接矩阵、邻接表)
(一)基于邻接矩阵的无向图BFS
参考程序

#include <stdio.h>
#define LEN 100

int Map[LEN+5][LEN+5],VisitList[LEN+5],VTable[LEN+5];
//全局变量参数说明: Map的邻接矩阵; VisitList遍历序列; VTable访问标志数组 
void CreateMatrix(int PointAmount,int EdgeAmount)
{
	int a,b;
	while(EdgeAmount--)
	{
		scanf("%d%d",&a,&b);
		Map[a][b]=1;
		Map[b][a]=1;
	}
}

void BFS_Visit(int PointAmount,int EdgeAmount,int start)
{
	int cnt=0,j,Scan_pointer=0;
	VisitList[cnt++]=start;
	VTable[start]=1;
	while(1)
	{
		for(j=0;j<PointAmount;j++)
		{
			if(Map[ VisitList[Scan_pointer] ][j]==1&&VTable[j]==0)
			{
				VisitList[cnt++]=j;
				VTable[j]=1;
			}
		}
		if(Scan_pointer!=PointAmount-1)
		{
			Scan_pointer++;
		}
		else
		{
			break;
		}
	}
}

void OutputArray(int n,int a[])
{
	int i;
	for(i=0;i<=n-2;i++)
	{
		printf("%d ",a[i]);
	}
	printf("%d",a[i]);
}

int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int k,m,t;
		scanf("%d%d%d",&k,&m,&t);
		CreateMatrix(k,m);
		BFS_Visit(k,m,t);
		OutputArray(k,VisitList);
	}
	return 0;
}
/*For Test
1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5
*/

(二)基于邻接链表的无向图BFS
参考程序

#include <stdio.h>
#include <stdlib.h>
#define ROOM sizeof(struct MapNode)
#define LEN 100
 
struct MapNode
{
	int data;
	struct MapNode *next;
};

struct MapNode MapList[LEN];
int VisitList[LEN],VisitTable[LEN];
//全局变量参数说明:MapList结点数组,方便按号查找结点;VisitList遍历序列;VisitTable访问标志数组
void InsertNode(int a,int x)
{
	struct MapNode *p,*p1,*p2;
	p2=&MapList[a];
	p=(struct MapNode*)malloc(ROOM);
	p->data=x;
	p->next=NULL;
	if(p2->next==NULL)
	{
		p2->next=p;
	}
	else
	{
		p1=p2->next;
		while(p1)
		{
			if(x<p1->data)
			{	
				p->next=p2->next;
				p2->next=p;
				break;
			}
			else
			{
				p2=p1;
				p1=p1->next;
			}
		}
		if(p1==NULL)
		{
			p2->next=p;
		}
	}
}

void CreateLinkMap(int PointAmount,int EdgeAmount,int start)
{
	int a,b;
	while(EdgeAmount--)
	{
		scanf("%d%d",&a,&b);
		InsertNode(a,b);
		InsertNode(b,a);
	}
}

void BFSVisit(int PointAmount,int start)
{
	int ScanPointer,cnt=0;
	struct MapNode *p;
	VisitList[cnt++]=start;
	VisitTable[start]=1;
	ScanPointer=0;
	while(cnt!=PointAmount)
	{
		p=&MapList[ VisitList[ScanPointer] ];
		if(p->next)
		{
			p=p->next;
			while(p)
			{
				if(!VisitTable[p->data])
				{
					VisitList[cnt++]=p->data;
					VisitTable[p->data]=1;
				}
				p=p->next;
			}
		}
		ScanPointer++;
	}
}

void OutputArray(int PointAmount,int a[])
{
	int i;
	for(i=0;i<=PointAmount-2;i++)
	{
		printf("%d ",a[i]);
	}
	printf("%d\n",a[i]);
}

int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int k,m,t;
		scanf("%d%d%d",&k,&m,&t);
		CreateLinkMap(k,m,t);
		BFSVisit(k,t);
		OutputArray(k,VisitList);
	}
	return 0;
}

注意:
对于图的遍历都是建立在图的存储结构上的。对于无向图,建立邻接矩阵和邻接表都要注意输入的两个结点(一条边),尽管只有一条边,但要添加两个邻接关系,即a与b有边相连则b与a也有边相连。BFS是基于队列思想的,上面程序中,cnt控制向序列中加入结点,而ScanPointer指向“首段”,逐个取出结点,检验其邻接点是否被访问过。

相关标签: 数据结构与算法

上一篇:

下一篇: