数据结构:图的各种遍历算法实现

题目:

以0结点为起点实现上述图的深度优先和广度优先遍历算法;

思路:

(1) 邻接矩阵深度搜索:从起始点开始循环,对下一层第一个点开始往下搜索,穷尽后,再返回上一层的第n个点搜索。如果该点没被访问过,则输出;
(2) 邻接矩阵广度搜索:建立队列,把起始点放入队列,建立循环,把队首的所有边连接的结点放入队列,如果队首未被访问则输出并弹出,直到队空退出。
(3) 邻接表深度搜索:从起始点开始循环,对下一层第一个点开始往下搜索,穷尽后,再返回上一层的第n个点搜索。如果该点没被访问过,则输出;
(4) 邻接表广度搜索:建立队列,把起始点放入队列,建立循环,把队首的所有边连接的结点放入队列,如果队首未被访问则输出并弹出,直到队空退出。

代码块:

#include "pch.h"
#include <iostream>
#include<queue>
using namespace std;
#define MVNum 100
typedef char VerTexType;//顶点数据类型设置为字符
typedef int ArcType;//边的权值为整型
typedef int OtherInfo;
bool visited[MVNum] = { false };
/*********邻接矩阵***********/
struct AMGraph {
	VerTexType vexs[MVNum];//顶点表
	ArcType arcs[MVNum][MVNum];//邻接矩阵
	int vexnum;//顶点数量
	int	arcnum;//边数量
};
int Locatevex(AMGraph G, VerTexType v)//定位顶点下标
{
	for (int i = 0; i < G.vexnum; i++)//遍历寻找对应顶点下标
	{
		if (G.vexs[i] == v)
		{
			return i;
		}
	}
	cout << "顶点输入错误,请重新检查!" << endl;//顶点输入错误处理
	return -1;
}
void CreateUDN(AMGraph&G)
{
	cout << "请依次输入总顶点数和总边数:";
	cin >> G.vexnum >> G.arcnum;
	cout << "请输入各顶点信息:" << endl;
	for (int i = 0; i < G.vexnum; i++)//请输入各顶点信息
	{
		cin >> G.vexs[i];
	}
	for (int i = 0; i < G.vexnum; i++)//将邻接矩阵初始化为0
	{
		for (int j = 0; j < G.vexnum; j++)
		{
			G.arcs[i][j] = 0;
		}
	}
	for (int k = 0; k < G.arcnum; k++)
	{
		VerTexType v1, v2;
		int w;
		cout << "请依次输第入" << k + 1 << "条边的两个顶点及权值:";
		cin >> v1 >> v2 >> w;
		int i = Locatevex(G, v1);//找到顶点在图中边的下标
		int j = Locatevex(G, v2);
		if (i != -1 && j != -1)//如果能找到对应顶点,对边赋权值
		{
			G.arcs[i][j] = w;
			G.arcs[j][i] = w;
		}
	}
}
void DFS_AM(AMGraph G, int  v)//邻接矩阵的深度搜索
{
	cout << "->" << v;
	visited[v] = true;//访问过的点置true
	for (int j = 0; j < G.vexnum; j++)
	{//搜索具有以该点为顶点的边的另一个顶点
		if (G.arcs[v][j] && visited[j] == false)
		{
			DFS_AM(G, j);
		}
	}
	return;
}
void BFS_AM(AMGraph G, int v)//邻接矩阵的广度搜索
{
	for (int i = 0; i < G.vexnum; i++)
	{//将visited数组置false
		visited[i] = false;
	}
	queue<int>q;//建立队列
	q.push(v);
	visited[v] = true;//当前点放入队列并置true
	while (!q.empty())
	{//队列非空
		cout << "->" << q.front();
		v = q.front();//当前顶点置为队首
		for (int i = 0; i < G.vexnum; i++)
		{//搜索具有以该点为顶点的边的另一个顶点
			if (G.arcs[v][i] && visited[i] == false)
			{
				q.push(i);
				visited[i] = true;
			}
		}
		q.pop();
	}
}
/*********邻接表***********/
struct ArcNode {
	int adjvex;//连接的顶点下标
	struct ArcNode *nextarc;//下一节点
	OtherInfo info;//权值
};
struct VNode {
	VerTexType data;//顶点数据
	ArcNode *firstarc;//边的连接点
};
struct ALGraph {
	VNode vertices[MVNum];//顶点数组
	int vexnum;//顶点总数
	int arcnum;//边总数
};
int Locatevex(ALGraph G, VerTexType v)//定位顶点下标
{
	for (int i = 0; i < G.vexnum; i++)//遍历寻找对应顶点下标
	{
		if (G.vertices[i].data == v)
		{
			return i;
		}
	}
	cout << "顶点输入错误,请重新检查!" << endl;//顶点输入错误处理
	return -1;
}
void CreateUDG(ALGraph &G)//创建邻接表
{
	cout << "请依次输入总顶点数和总边数:";
	cin >> G.vexnum >> G.arcnum;
	cout << "请输入各顶点信息:" << endl;
	for (int i = 0; i < G.vexnum; i++)//请输入各顶点信息
	{
		cin >> G.vertices[i].data;
		G.vertices[i].firstarc = NULL;
	}
	for (int k = 0; k < G.arcnum; k++)
	{
		VerTexType v1, v2;
		int w;
		cout << "请依次输第入" << k << "条边的两个顶点及权值:";
		cin >> v1 >> v2 >> w;
		int i = Locatevex(G, v1);//找到顶点在图中边的下标
		int j = Locatevex(G, v2);
		ArcNode *p1 = new ArcNode;//边用前插法插入到顶点后方
		p1->info = w;
		p1->adjvex = i;
		p1->nextarc = G.vertices[j].firstarc;
		G.vertices[j].firstarc = p1;
		ArcNode *p2 = new ArcNode;
		p2->info = w;
		p2->adjvex = j;
		p2->nextarc = G.vertices[i].firstarc;
		G.vertices[i].firstarc = p2;
	}
}
void DFS_AL(ALGraph G, int v) {//邻接表的深度搜索
	cout << "->" << v;//输出格式
	visited[v] = true;
	ArcNode *p = G.vertices[v].firstarc;
	while (p)//从一个点开始穷尽搜索,搜索完再重新搜索下一个点
	{
		int w = p->adjvex;
		if (!visited[w]) DFS_AL(G, w);
		p = p->nextarc;
	}
}
void BFS_AL(ALGraph G, int v)
{//邻接表的广度搜索
	for (int i = 0; i < G.vexnum; i++)
	{//将visited数组置false
		visited[i] = false;
	}
	queue<int> q;//建立队列
	q.push(v);
	visited[v] = true;//起始点入队,并设置为已查找
	while (!q.empty())
	{//队不空则循环
		v = q.front();//获取队首
		ArcNode *p = G.vertices[v].firstarc;
		while (p)//将当前点连接的所有点入队
		{
			if (visited[p->adjvex]==false)
			{
				q.push(p->adjvex);
				visited[p->adjvex] = true;
			}
			p = p->nextarc;
		}
		cout<<"->" << q.front();//弹出队首并输出
		q.pop();
	}
}
int main()
{

	/*********邻接矩阵***********/
	cout << "/*********邻接矩阵***********/" << endl;
	AMGraph G;
	CreateUDN(G);
	for (int i = 0; i < G.vexnum; i++)
		cout << '\t' << "v" << i;
	cout << endl;
	for (int i = 0; i < G.vexnum; i++)//输出邻接矩阵
	{
		cout << "v" << i << '\t';
		for (int j = 0; j < G.vexnum; j++)
		{
			cout << G.arcs[i][j] << '\t';
		}
		cout << endl;
	}
	cout << "邻接矩阵深度搜索结果:";
	cout << endl << "开始" ;
	DFS_AM(G, 0);
	cout <<endl<< "邻接矩阵广度搜索结果:";
	cout << endl << "开始";
	BFS_AM(G, 0);
	/*********邻接表***********/
	cout <<endl<< "/*********邻接表***********/" << endl;
	ALGraph g;
	CreateUDG(g);
	for (int i = 0; i < g.vexnum; i++)//输出邻接表
	{
		cout << "第" << i + 1 << "个顶点:" << g.vertices[i].data << endl;
		int k = 1;
		ArcNode *p = g.vertices[i].firstarc;
		while (p)
		{
			cout << "第" << i + 1 << "个顶点的" << "第" << k << "条边顶点:" << g.vertices[p->adjvex].data << '\t' << "权重:" << p->info << endl;
			p = p->nextarc;
			k++;
		}
	}
	cout << "邻接表深度搜索结果:";
	cout << endl << "开始";
	DFS_AL(g, 0);
	cout <<endl<< "邻接表广度搜索结果:";
	cout << endl << "开始";
	BFS_AL(g, 0);
}

效果图:
图一为邻接矩阵的深度和广度搜索,图二三邻接表的深度和广度搜索
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值