Graph storage structure-adjacency matrix

Array representation of adjacency matrix

Insert picture description here

Adjacency matrix of undirected graph

Insert picture description here

Features of Adjacency Matrix of Undirected Graph

Insert picture description here

Degree of vertex i

Insert picture description here

Find all adjacent points of vertex i

Insert picture description here

Adjacency matrix of directed graph

Insert picture description here

Find the in-degree of vertex i

Insert picture description here

Find the out degree of vertex i

Insert picture description here

How to determine whether there is an edge from vertex i to vertex j

Insert picture description here

Adjacency matrix

Net graph definition: the graph with weight on each side is called net
Insert picture description here

Undirected graph class of adjacency matrix

Insert picture description here

The constructor of the graph in the adjacency matrixInsert picture description here

Insert picture description here
Insert picture description here

demonstration

Insert picture description here

#include<iostream>
using namespace std;
const int MAX = 10; //图的最大顶点个数为10
typedef char DataType;
class Graph 
{
    
    
private:
	//边的个数
	int arcNum;
	//顶点个数
	int vertexNum;
	//定义一个存放顶点的一位数组
	DataType vertex[MAX];
	//定义一个存放顶点间的边关系的二维数组
	int arc[MAX][MAX];
public:
	//v[]数组存放用户输入的一维数组的顶点数据,n表示顶点个数,e是边的个数
	Graph(DataType v[], int n, int e);
   //定义一个输出函数
	void display()
	{
    
    
		/*arc[1][3] = 1;
		arc[3][1] = 1;*/
		for (int i = 0; i <vertexNum; i++)
		{
    
    
			for (int j = 0; j < vertexNum; j++)
			{
    
    
				cout << arc[i][j]<<" ";
			}
			cout << endl;
		}
	}
};
//有参构造函数的实现
Graph::Graph(DataType v[], int n, int e)
{
    
    
	//初始化顶点个数
	vertexNum = n;
	//初始化边的个数
	arcNum = e;
	//初始化顶点数组
	for (int i = 0; i < MAX; i++)
		vertex[i] = v[i];
	//初始化边数组
	for (int i = 0; i < MAX; i++)
		for (int j = 0; j < MAX; j++)
			arc[i][j] = 0;
	//由用户来决定,哪两个顶点之间存在边
	int vi=0, vj=0;
	for (int i = 0; i < arcNum; i++)
	{
    
    
		cin >> vi >> vj;//输入边依附的两个顶点编号
		//这是无向图的边初始化标志
		arc[vi][vj] = 1;//有边的标志
		arc[vj][vi] = 1;
	}
}
//测试-------------------
void test()
{
    
    
	DataType v[5] = {
    
     'V0','V1','V2','V3' };
	Graph p(v, 4, 4);
	//输出测试
	p.display();
}
int main()
{
    
    
	
	test();
	system("pause");
	return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_53157173/article/details/114991730