27 图的邻接矩阵:广度优先遍历&& 深度广度遍历总结+视频

27 图的邻接矩阵:广度优先遍历

作者: 冯向阳时间限制: 1S章节: DS:图

截止日期: 2022-06-30 23:55:00

问题描述 :

目的:使用C++模板设计并逐步完善图的邻接矩阵抽象数据类型(ADT)。

内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接矩阵ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)

(2)设计并实现一个算法,使用队列,对一个已存在的图进行广度优先遍历(BFS),并输出遍历的顶点线性序列。遍历的起点通过输入指定。注意:遍历时,仅从该点出发遍历整个图,如果图不连通,则只遍历一个子图。图的存储结构采用邻接矩阵。将其加入到ADT中。

函数原型:

void BFS_Traverse(int u); //BFS遍历

辅助函数原型:

(1)int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1

(2)int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1

注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)

图的邻接矩阵模板类原型参考如下:

template <class TypeOfVer, class TypeOfEdge>

class adjmatrix_graph{

    private:

       int Vers;        //顶点数 

       int Edges;       //边数 

       TypeOfEdge **edge;  //存放邻接矩阵(TypeOfEdge表示顶点关系类型。对于无权图,用1或0,表示相邻否;对于带权图,则为权值类型) 

       TypeOfVer *ver;    //存放结点值 

       TypeOfEdge noEdge;  //邻接矩阵中的∞的表示值

       string GraphKind;   //图的种类标志 

        

       bool DFS(int u, int &num, int visited[]); //DFS遍历(递归部分)

    public:

       adjmatrix_graph( const string &kd, int vSize, const TypeOfVer d[], const TypeOfEdge noEdgeFlag); //构造函数构造一个只有结点没有边的图。4个参数的含义:图的类型、结点数、结点值和邻接矩阵中表示结点间没有边的标记(无权图:0,有权图:输入参数定) 

       adjmatrix_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e); //构造函数构造一个无权图。5个参数的含义:图的类型、结点数、边数、结点集和边集 

       adjmatrix_graph( const string &kd, int vSize, int eSize, const TypeOfEdge noEdgeFlag, const TypeOfVer d[], int **e, const TypeOfEdge w[]); //构造函数构造一个有权图。7个参数的含义:图的类型、结点数、边数、无边标记、结点集、边集、权集

       bool GraphisEmpty() { return Vers == 0; }  //判断图空否

       string GetGraphKind(){ return GraphKind; }

       bool GetVer(int u, TypeOfVer &data); //取得G中指定顶点的值 

       int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1 

       int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1

       bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值 

       bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点 

       int LocateVer(TypeOfVer data); //返回G中指定顶点的位置 

       bool PrintMatrix();  //输出邻接矩阵 

       int GetVerNum(){ return Vers;}    //取得当前顶点数 

       int GetEdgeNum(){ return Edges;}  //取得当前边数 

       bool Insert_Edge(int u, int v); //无权图插入一条边

       bool Insert_Edge(int u, int v, TypeOfEdge w); //有权图插入一条边

       bool DeleteVer(const TypeOfVer &data); //往G中删除一个顶点

       bool Delete_Edge(int u, int v); //无权图删除一条边 

       bool Delete_Edge(int u, int v, TypeOfEdge w); //有权图删除一条边 

       void DFS_Traverse(int u); //DFS遍历(外壳部分)

       void BFS_Traverse(int u); //BFS遍历

       ~adjmatrix_graph(); //析构函数 

};

输入说明 :

建图的输入数据格式参见建图的算法说明。

第一行:图的类型

第二行:结点数

第三行:结点集

第四行:边数

第五行:边集

第六行:起始顶点的位序

输出说明 :

第一行:顶点集

空行

第二行:邻接矩阵

空行

第三行:BFS遍历序列(结点之间用->分隔)

输入范例 :

UDG
8
V1 V2 V3 V4 V5 V6 V7 V8
8
0 1
0 2
1 3
1 4
2 5
2 6
3 7
4 7
0

输出范例 :

V1 V2 V3 V4 V5 V6 V7 V8

0 1 1 0 0 0 0 0 
1 0 0 1 1 0 0 0 
1 0 0 0 0 1 1 0 
0 1 0 0 0 0 0 1 
0 1 0 0 0 0 0 1 
0 0 1 0 0 0 0 0 
0 0 1 0 0 0 0 0 
0 0 0 1 1 0 0 0 

V1->V2->V3->V4->V5->V6->V7->V8

Donghua University Online Judge 1.0

Copyright © 2015-2016 . All Rights Reserved.

技术支持:Turbo Email:11249242@qq.com

-----------------------------------------------------------------------------------

struct student
{
    string element[100];
    int arr[100][100];
    int dian;
    int gather;
};

int  arr[100][100]={0};

这样写也可以 在visual studio里面要这样写 但在东华那个online judge上面 如果你这样子写 就会CE报错。

下面是OJ的AC代码

然后如果你还不会 广度优先搜索和深度优先搜索的话建议 你去B站看一下这个视频

对邻接矩阵表示的图进行广度优先和深度优先遍历。已知图的邻接矩阵如下图所示,从定点0出发,按照深度优先遍历的结果是()_哔哩哔哩_bilibili

深度优先遍历就是原来的先序遍历

广度优先遍历就是原来的层次遍历要用队列

#include<iostream>
#include<queue>
using namespace std;
int m = 0;
int kkkk = 0;
struct student
{
	string element[100];
	int arr[100][100];
	int dian;
	int gather;
};
int visit[100] = { 0 };

void BFS(student g,int v0)
{
	queue<int> q;
	
		q.push(v0);
		visit[v0] = 1;
		cout << g.element[v0] << "->";
		while (!q.empty())
		{
			int first = q.front();
			q.pop();
			for (int j = 0; j < g.dian; j++)
			{
				if (g.arr[first][j] == 1 && visit[j] == 0)
				{
					if (kkkk == 1)
					{
						cout << "->";
					}
					kkkk = 1;
					cout << g.element[j];
					visit[j] = 1;
					q.push(j);
				}
			}
		}
	
}


int main()
{

	string kind;
	cin >> kind;

	student m;
	cin >> m.dian;
	for (int i = 0; i < m.dian; i++)
	{
		cin >> m.element[i];
	}



	if (kind == "UDG")//无向图
	{

		cin >> m.gather;
		for (int u = 0; u < m.gather; u++)
		{
			int x;
			int y;
			cin >> x >> y;
			m.arr[x][y] = 1;
			m.arr[y][x] = 1;
		}
		int start;
		cin >> start;
		for (int j = 0; j < m.dian; j++)
		{
			cout << m.element[j];
			if (j != m.dian - 1)
				cout << " ";
		}
		cout << endl;
		cout << endl;
		for (int i = 0; i < m.dian; i++)
		{
			for (int e = 0; e < m.dian; e++)
				cout << m.arr[i][e] << " ";


			cout << endl;
		}
		cout << endl;

		BFS(m, start);

	}
	else if (kind == "DG")//有向图
	{

		cin >> m.gather;
		for (int u = 0; u < m.gather; u++)
		{
			int x;
			int y;
			cin >> x >> y;
			m.arr[x][y] = 1;

		}
		int start;
		cin >> start;

		for (int j = 0; j < m.dian; j++)
		{
			cout << m.element[j];
			if (j != m.dian - 1)
				cout << " ";
		}
		cout << endl;
		cout << endl;
		for (int i = 0; i < m.dian; i++)
		{
			for (int e = 0; e < m.dian; e++)
				cout << m.arr[i][e] << " ";

			cout << endl;
		}
		cout << endl;

		BFS(m, start);
	}

	else if (kind == "UDN")//
	{
		cin >> m.gather;
		for (int u = 0; u < m.gather; u++)
		{
			int x;
			int y;
			cin >> x >> y;
			m.arr[x][y] = 1;

		}
		int start;
		cin >> start;

		for (int j = 0; j < m.dian; j++)
		{
			cout << m.element[j];
			if (j != m.dian - 1)
				cout << " ";
		}
		cout << endl;
		cout << endl;
		for (int i = 0; i < m.dian; i++)
		{
			for (int e = 0; e < m.dian; e++)
				cout << m.arr[i][e] << " ";

			cout << endl;
		}
		cout << endl;
		BFS(m, start);

	}
	else if (kind == "DN")//
	{
		cin >> m.gather;
		for (int u = 0; u < m.gather; u++)
		{
			int x;
			int y;
			cin >> x >> y;
			m.arr[x][y] = 1;

		}
		int start;
		cin >> start;

		for (int j = 0; j < m.dian; j++)
		{
			cout << m.element[j];
			if (j != m.dian - 1)
				cout << " ";
		}
		cout << endl;
		cout << endl;
		for (int i = 0; i < m.dian; i++)
		{
			for (int e = 0; e < m.dian; e++)
				cout << m.arr[i][e] << " ";

			cout << endl;
		}
		cout << endl;

		BFS(m, start);
	}

	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨骅麟(Patrick Young)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值