Data structure-the realization of the adjacency matrix of the graph

The adjacency matrix of the graph uses two arrays to represent the graph.
A one-dimensional array vexs[MAXVER]; used to store vertex elements;
a two-dimensional array arc[MAXVER][MAXVER]; used to store the edges or radians of the graph and prove its existence

The main diagonal elements of the two-dimensional array of the undirected graph do not exist in the two-dimensional array and present symmetrical distribution
because the edges are bidirectional.

The main diagonal elements of the two-dimensional array of the directed graph are also non-symmetrical.

When arc[i][j]=wi=j, arc is 0, the existing edge is w, when the edge does not exist, it is defined as infinity ∞ means that the value greater than the ownership value is a limit value

Definition of graph

#include<iostream>
#include <iomanip>
using namespace std;

typedef char vertextype;//顶点类型
typedef int edgetype;//边缘权值
typedef int Status;
#define MAXVER 25//最大顶点数
#define INF 65535//代表无穷
#define NULL 0

//定义一个图结构
typedef struct Graph
{
    
    
	vertextype vexs[MAXVER];//保存顶点元素
	edgetype arc[MAXVER][MAXVER];//保存边界的权值
	int numver, numedg;//顶点数目 边界数目
}MGhaph;

//输入图  其实就是给顶点表和边输入数据的过程

Graph structure

Status CreatGhaph(MGhaph &G)
{
    
    
	int i, j, k, w;
	cout << "Please enter the number of verticesof the graph : "<< endl;
	cin >> G.numver;
	cout << "Please enter the number of edges the graph : " << endl;
	cin >> G.numedg;

	for (i = 0; i < G.numver; i++){
    
    
		cout <<"Please enter the NO."<<i+1<<"%d name of vex : " << endl;
		cin >> G.vexs[i];
	}

	cout << "Diagonal infinity ..."<< endl;
	for (i = 0; i < G.numver; i++)
		for (j = 0; j < G.numver; j++)
		{
    
    
		G.arc[i][j] = INF;//简单图  不循环
		//cout << G.arc[i][j] << endl;//不理解为啥是1
		}
	cout << "...Diagonal infinity" << endl;

	for (int k = 0; k < G.numedg; k++){
    
    //因为具体哪条边存在不一定 所以选择性输入边
		cout << "Enter the subscripts and weights from vertex vi to vertex vj : " << endl;
		cin >> i>>j>>w;
		/*cout << "Please enter the subscript j of the edge : " << endl;
		cin >> j;
		cout << "Please enter the weight from vertex "<<i<<" to vertex "<<j<<" : " << endl;
		cin >> w;*/

		G.arc[i][j] = w;
		G.arc[j][i] = G.arc[i][j];//无向图  边的信息  是对称的  //有向图的话 无需设置
		
	}
	return 0;
}

Compared with the undirected graph, each weight of the directed graph must be defined separately

Output map

Status DispGraph(MGhaph &G)
{
    
    
	int i, j, k, w;
	//输出顶点元素
	for (i = 0; i < G.numver; i++){
    
    
		cout << i + 1 << " : " << G.vexs[i] << endl;
	}

	//输出边界权值
	for (i = 0; i < G.numver; i++){
    
    
		for (j = 0; j < G.numver; j++){
    
    
			if (G.arc[i][j] == INF){
    
    
				cout.flags(ios::left);//设置对齐方式
				cout << setw(6) << "∞";
			}
			else{
    
    
					cout.flags(ios::left);
					cout << setw(6) << G.arc[i][j];
				}
		}cout << endl;
	}
	return 0;
}

Code implementation
all parts

#include<iostream>
#include <iomanip>
using namespace std;

typedef char vertextype;//顶点类型
typedef int edgetype;//边缘权值
typedef int Status;
#define MAXVER 25//最大顶点数
#define INF 65535//代表无穷
#define NULL 0

//定义一个图结构
typedef struct Graph
{
    
    
	vertextype vexs[MAXVER];//保存顶点元素
	edgetype arc[MAXVER][MAXVER];//保存边界的权值
	int numver, numedg;//顶点数目 边界数目
}MGhaph;

//输入图  其实就是给顶点表和边输入数据的过程

//例子 无向图的构造
Status CreatGhaph(MGhaph &G)
{
    
    
	int i, j, k, w;
	cout << "Please enter the number of verticesof the graph : "<< endl;
	cin >> G.numver;
	cout << "Please enter the number of edges the graph : " << endl;
	cin >> G.numedg;

	for (i = 0; i < G.numver; i++){
    
    
		cout <<"Please enter the NO."<<i+1<<"%d name of vex : " << endl;
		cin >> G.vexs[i];
	}

	cout << "Diagonal infinity ..."<< endl;
	for (i = 0; i < G.numver; i++)
		for (j = 0; j < G.numver; j++)
		{
    
    
		G.arc[i][j] = INF;//简单图  不循环
		//cout << G.arc[i][j] << endl;//不理解为啥是1
		}
	cout << "...Diagonal infinity" << endl;

	for (int k = 0; k < G.numedg; k++){
    
    //因为具体哪条边存在不一定 所以选择性输入边
		cout << "Enter the subscripts and weights from vertex vi to vertex vj : " << endl;
		cin >> i>>j>>w;
		/*cout << "Please enter the subscript j of the edge : " << endl;
		cin >> j;
		cout << "Please enter the weight from vertex "<<i<<" to vertex "<<j<<" : " << endl;
		cin >> w;*/

		G.arc[i][j] = w;
		G.arc[j][i] = G.arc[i][j];//无向图  边的信息  是对称的  //有向图的话 无需设置
		
	}
	return 0;
}

//输出图
Status DispGraph(MGhaph &G)
{
    
    
	int i, j, k, w;
	//输出顶点元素
	for (i = 0; i < G.numver; i++){
    
    
		cout << i + 1 << " : " << G.vexs[i] << endl;
	}

	//输出边界权值
	for (i = 0; i < G.numver; i++){
    
    
		for (j = 0; j < G.numver; j++){
    
    
			if (G.arc[i][j] == INF){
    
    
				cout.flags(ios::left);
				cout << setw(6) << "∞";
			}
			else{
    
    
					cout.flags(ios::left);
					cout << setw(6) << G.arc[i][j];
				}
		}cout << endl;
	}
	return 0;
}
//若图存在顶点u  返回图的顶点u的位置
Status LocateVex(MGhaph &G)
{
    
    
	int i, j;
	vertextype vex;
	cout << "Please enter a vertex"<< endl;
	cin >> vex;
	for (i = 0; i < G.numver; i++)
	{
    
    
		if (G.vexs[i] == vex)
		{
    
    
			j = i;
		}
		else
		{
    
    
			continue;
		}
	}
	return j;
}
//将图的顶点替换  略

//销毁图
Status DelGraph(MGhaph &G)
{
    
    

	for (int i = 0; i < G.numver; i++)
	{
    
    
		free((void *)G.vexs[i]);
	}
	free(G.arc);

	for (int i = 0; i < G.numver; i++)
		for (int j = 0; j < G.numver; j++)
		{
    
    
		free((void *)G.arc[i][j]);
		}

	free(G.arc);
	return 0;
}

//返回顶点V的邻接顶点  若没有则返回为空
Status FirstVex(MGhaph &G)
{
    
    
	int i, j;
	vertextype vex,fis;
	cout << "Please enter a vertex" << endl;
	cin >> vex;
	for (i = 0; i < G.numver; i++){
    
    
		if (G.vexs[i] == vex){
    
    
			for (j = 0; j < G.numver; j++){
    
    
				if (G.arc[i][j]!=INF){
    
    //如果存在
					cout << G.vexs[j] << endl;}
				/*if (G.arc[j][i]!=INF){//有向图 加这一步
					cout << G.vexs[j]<<endl;
				}*/
			}
		}
	}
	return 0;
}

//新增顶点
Status ADDVex(MGhaph &G)
{
    
    
	vertextype vex;
	int k, w, edg_num, v;
	if (G.numver >= MAXVER)
	{
    
    
		//静态的  一旦确定不能扩大
		cout << "Graph memory is full and cannot be expanded ";
		return NULL;
	}
	else
	{
    
    
		cout << "Please enter a new vertex :" << endl;
		cin >> vex;
		G.numver++;
		G.vexs[G.numver - 1] = vex;

		cout << "Please enter the number of new edges" << endl;//新增边的数目
		cin >> edg_num;
		G.numedg = G.numedg + edg_num;

		for (int j = 0; j < G.numver; j++)
		{
    
    
			G.arc[j][G.numver-1]=G.arc[G.numver-1][j] = INF;//简单图  不循环
			//cout << G.arc[i][j] << endl;
		}

		for (k = 0; k < edg_num; k++)
		{
    
    
			cout << "Enter the weights : " << endl;
			cin >> w;
			cout << "Enter the vertex of the edge" << endl;
			cin >> v;
			G.arc[v][G.numver - 1] = w;
			G.arc[G.numver - 1][v] = G.arc[v][G.numver - 1];
		}
	}
	return 0;
}
//删除相关顶点和弧   偷个懒 

//增加弧

//删除弧
int main()
{
    
    
	MGhaph G;
	CreatGhaph(G);
	DispGraph(G);
	cout << LocateVex(G) << endl;
	FirstVex(G);
	ADDVex(G);
	DispGraph(G);
	DelGraph(G);
	DispGraph(G);
	return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/113134411