编程语言
首页 > 编程语言> > 图的邻接表表示法及深度优先、广度优先遍历算法

图的邻接表表示法及深度优先、广度优先遍历算法

作者:互联网

知识点:

1、图的邻接表表示法;

2、图的深度优先算法;

3、图的广度优先算法。

//图的邻接表表示法
//基于邻接表表示法的图的遍历 
#include "string.h" 
#include<stdlib.h>
#include<queue> 
#include<iostream>
using namespace std;
typedef struct ArcNode
{
	int adjvex;//邻接点
	double weight;//边的信息
	ArcNode *nextarc;//指向下一条边的指针
}ArcNode;
///头结点
typedef struct VexNode
{
	char data[5];//顶点信息
	ArcNode *firstarc;//指向第一条边的指针
}VexNode;

图
typedef struct
{
	VexNode vexs[100];//顶点的集合
	int vexnum,arcnum;//顶点的数目,边的数目
}ALGraph;
void InitGraph(ALGraph &mg)
{//图的初始化
	mg.arcnum=mg.vexnum=0;
	for(int i=1;i<100;i++)
	{
		strcpy(mg.vexs[i].data,"");
		mg.vexs[i].firstarc=0;
	}

}
int FindVex(ALGraph mg,char *vex)
{//查找顶点vex是否存在
	for(int i=1;i<=mg.vexnum;i++)
		if(strcmp(vex,mg.vexs[i].data )==0)
			return i;
	return 0;//不存在

}

int FindArc(ALGraph mg,char *v,char *w)
{//查找边<v,w>是否存在
	int v1=FindVex(mg,v);
	int w1=FindVex(mg,w);
	if(v1 *w1==0) return 0;//不存在

	ArcNode *p=mg.vexs[v1].firstarc;
	while(p)
	{
		if(p->adjvex==w1)return 1;//找到了
		p=p->nextarc;
	}

	return 0;//不存在
	
}
void InsertVex(ALGraph &mg,char *vex)
{//插入一个顶点vex
	int v=FindVex(mg,vex);
	if(v!=0)return;//该顶点已经存在
	mg.vexnum++;
	strcpy(mg.vexs[mg.vexnum ].data,vex);
	
}
void InsertArc(ALGraph &mg,char *v1,char *v2)
{//插入一条边<v1,v2>

	if(FindArc(mg,v1,v2)==0)//边不存在
	{//此时我们假定顶点已经存在
		ArcNode *p=new ArcNode;
		p->adjvex=FindVex(mg,v2);
		p->nextarc=0;
		p->weight=0;//生成一个新的结点

		int v=FindVex(mg,v1);

		p->nextarc=mg.vexs[v].firstarc;
		mg.vexs[v].firstarc=p;
	}

}
//
int FirstAdjVex(ALGraph mg,int v){
	//这里用顶点的下标代替字符串,如顶点“v1“对应下标为v,我们用v代表“v1“
	//在图mg中,求顶点v的第一个邻接点。

	//假定v存在
	if(mg.vexs[v].firstarc!=0)
	  return mg.vexs[v].firstarc->adjvex;
	else return 0;
	
}
int NextAdjVex(ALGraph mg,int v,int w)
{//在图mg中,求顶点v的相对于顶点w的下一个邻接点

	//先找到w在v链中的位置
	ArcNode *p;
	p=mg.vexs[v].firstarc;
	while(p)
	{
		if(p->adjvex==w)break;
		p=p->nextarc;
	}
	if(p==0) return 0;
	if(p->nextarc!=0)
	  return p->nextarc->adjvex;
	else return 0;
	
}
void print(ALGraph mg)
{
	int i;
	for(i=1;i<=mg.vexnum;i++)
		cout<<mg.vexs[i].data<<"  ";
	cout<<endl;

	for(i=1;i<=mg.vexnum;i++)
	{
		cout<<i<<":  ";
		ArcNode *p=mg.vexs[i].firstarc;
		while(p)
		{
			cout<<p->adjvex<<"  ";
			p=p->nextarc;
		}
		cout<<endl;
	}
}



bool visited[100]={false};
void DFS(ALGraph mg,int v)
{//深度优先遍历
	visited[v]=true;//以前未被访问,此处被访问
	               //改变对应的标志为已经访问
	cout<<mg.vexs[v].data<<"  "; //访问结点v
	for(int w=FirstAdjVex(mg,v);w>0;w=NextAdjVex(mg,v,w))
	{//对于v的每一个邻接点进行考察
		if(visited[w]==false)//当该结点未被访问时
			DFS(mg,w);//进行深度优先遍历
	}
}

//当图的存储结构为邻接表时,广度优先算法可以表示如下:
void BFS (ALGraph mg,int x)
{
	bool visited[100]={false};
	queue<int> q;
	cout<<mg.vexs[x].data <<"  ";visited[x]=true;q.push(x);
	while(q.empty()==false){
		int v=q.front();
		q.pop();
		for(int w=::FirstAdjVex(mg,v);w>0;w=::NextAdjVex(mg,v,w)){
			if(visited[w]==false){
				cout<<mg.vexs[w].data<<"  ";
				visited[w]=true;
				q.push(w);
			}
		}
	}
}



int main(int argc, char* argv[])
{
	ALGraph mg;
	InitGraph(mg);
	InsertVex(mg,"v1");
	InsertVex(mg,"v2");
	InsertVex(mg,"v3");
	InsertVex(mg,"v4");
	InsertVex(mg,"v5");
	InsertVex(mg,"v6");
	InsertVex(mg,"v7");
	InsertVex(mg,"v8");

	InsertArc(mg,"v1","v2");
	InsertArc(mg,"v1","v3");
	InsertArc(mg,"v2","v4");
	InsertArc(mg,"v2","v5");
	InsertArc(mg,"v3","v6");
	InsertArc(mg,"v3","v7");
	InsertArc(mg,"v4","v8");
	InsertArc(mg,"v5","v8");
	InsertArc(mg,"v6","v7");

	print(mg);
	
	cout<<"\n深度优先遍历结果\n";
	DFS(mg,1);//从图的第一个结点开始深度优先遍历
	cout<<"\n广度优先遍历结果\n";
	BFS(mg,1);//从图的第一个结点开始广度优先遍历
	return 0;
}

 

标签:优先,遍历,return,int,mg,表示法,ALGraph,ArcNode,顶点
来源: https://blog.csdn.net/weixin_43917370/article/details/110096850