C++实现无向图的邻接矩阵存储 及 深度优先遍历

若本文对你有帮助的话,记得点赞、关注我哟!

博客总领目录请看这篇,不看后悔

软件工程专业大学四年学什么_大学近代史学分是多少-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_41587612/article/details/104362661

复习累了可以点开视频,有惊喜哦!

1990年金曲《第一次的啾はじめてのチュウ初吻》出自哆啦A梦作者藤子不二雄的动画《齐天烈大百科》(キテレツ大百科)由实川俊晴谱写_哔哩哔哩_bilibili

1995年少女漫丰碑《天是红河岸/赤河恋影》广播剧歌曲合集,富有埃及和土耳其文化色彩,原创剪辑值得珍藏_哔哩哔哩_bilibili

【太阳之船Sol Bianca】高燃混剪,经典没人看系列_哔哩哔哩_bilibili

图

方一:依次输入所有顶点的关系

1

 2

 方二:手动两两输入相连的顶点

3

#include <iostream>
using namespace std;
#define MAX_VERTEX  20  //最多允许创建20个顶点的图
typedef char DataType;
typedef struct
{
	int vertexNum,edgeNum;
	DataType vertexArr[MAX_VERTEX];       //顶点元素数组	 
	int edgeArr[MAX_VERTEX][MAX_VERTEX]; //边矩阵二维数组 
	
}ArrayGraph;

int visited[MAX_VERTEX] = {0};
void ArrayGraph_init(ArrayGraph *pGraph);
void ArrayGraph_create(ArrayGraph *pGraph);
int locate_vertex(ArrayGraph *pGraph, DataType who);
void ArrayGraph_show(ArrayGraph *pGraph);
void DFTraverse(ArrayGraph *pGraph, int i);

int main()
{
	ArrayGraph g;
	ArrayGraph_init(&g);       //初始化图 
	ArrayGraph_create(&g);     //创建图 
	ArrayGraph_show(&g);       //打印图 
	cout<<"深度优先遍历序列是:";
	DFTraverse(&g, 0);
	cout << endl;
	system("pause");
	return 0;

}
//初始化为一个无圈图 ,也就是边矩阵中,主对角线元素都是0 
void ArrayGraph_init(ArrayGraph *pGraph)
{
	for (int i = 0; i < MAX_VERTEX; i++)
	{
		for(int j=0;j<MAX_VERTEX;j++)
			pGraph->edgeArr[i][j] = 0;
	}
}
//输入一个图 
void ArrayGraph_create(ArrayGraph *pGraph)
{
	cout<<"请输入图的顶点个数(不超过20):";
	cin>>pGraph->vertexNum;
	if (pGraph->vertexNum > MAX_VERTEX)
	{
		cout << "超过最大允许顶点数,请重新输入!"<<endl;
		return ArrayGraph_create(pGraph);
	}
	for (int i = 0; i < pGraph->vertexNum; ++i)    //填充顶点数组,也就是输入顶点元素 
	{
		cout<<"输入第"<<i + 1<<"个定点:";
		cin>>pGraph->vertexArr[i];
	}
	cout << endl;
	int choose;
	cout << "请选择建立边关系的方式(建议大图选1.输入所有边之间的关系,小图选2.输入相连的顶点):\t";
	cin >> choose;
	switch(choose){
	case 1:
		for (int j = 0; j <pGraph->vertexNum; ++j)   //填充边关系 
		{
			for (int i = j + 1; i < pGraph->vertexNum; ++i)
			{
				cout << "若元素" << pGraph->vertexArr[j] << "和" << pGraph->vertexArr[i] << "有边,则输入1,否则输入0:" << '\t';
				cin >> pGraph->edgeArr[j][i];
				pGraph->edgeArr[i][j] = pGraph->edgeArr[j][i];     //对称 
			}
		}break;
	case 2:
		cout<<"请输入边的数量:"<<" ";
		cin >> pGraph->edgeNum;
		while(pGraph->edgeNum > pGraph->vertexNum*(pGraph->vertexNum-1)/2) {
			cout << "边数为不可能值,请重新输入:";
			cin >> pGraph->edgeNum;
		}
		cout <<"\n请两两输入相连顶点:\n";
		for (int i = 0; i <pGraph->edgeNum; ++i)
		{
			DataType s1, s2;//边的两端的顶点
			cin >> s1 >> s2;
			int index_1 = locate_vertex(pGraph, s1);//找到这两个顶点所在的下标
			int index_2 = locate_vertex(pGraph, s2);
			pGraph->edgeArr[index_1][index_2] = pGraph->edgeArr[index_2][index_1] = 1;//矩阵是对称的
		}break;
	default:cout<<choose<<"是无效运算符!";
	}
	cout << endl;
}
int locate_vertex(ArrayGraph *pGraph, DataType who)//寻找某个顶点的位置
{
	for (int i = 0; i < pGraph->vertexNum; ++i)
		if (pGraph->vertexArr[i] == who)
			return i;    //找到
	return -1;//没找到
}
void ArrayGraph_show(ArrayGraph *pGraph)
{
	cout<<"\n顶点元素如下:\n";
	for (int i = 0; i < pGraph->vertexNum; ++i)
	{
		cout<< pGraph->vertexArr[i]<<" ";
	}
	cout<<"\n";
	cout<<"边矩阵如下:\n\n";
	cout << "   ";
	for (int i = 0; i<pGraph->vertexNum; ++i)
		cout<<pGraph->vertexArr[i]<<"  ";
	cout<<"\n";
	for (int j = 0; j <pGraph->vertexNum; ++j)
	{
		cout<<pGraph->vertexArr[j]<<"  ";
		for (int i = 0; i < pGraph->vertexNum; ++i)
		{
			cout<<pGraph->edgeArr[i][j]<<"  ";
		}
		cout<<"\n";
	}
	cout<<"\n";
}
void DFTraverse(ArrayGraph *pGraph, int i) 
{
	cout<<" "<<pGraph->vertexArr[i];
	visited[i] = 1;
	for (int j = 0; j < pGraph->vertexNum; j++) {
		if (pGraph->edgeArr[i][j] == 1 && visited[j] == 0)
			DFTraverse(pGraph, j);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值