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

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

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

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

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

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

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

#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);
	}
}

 

 

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/qq_41587612/article/details/84595475
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞