Table of contents
Undirected graph construction:
Undirected network, directed network, directed graph:
Preconditions:
#include<iostream>
using namespace std;
typedef int Status;
#define MaxInt 999999 //表示无穷大
#define MVNum 100 //最大顶点数
//MAX Vertex Number
typedef char VertexType; //设顶点类型:字符型
typedef int ArcType; //设边的权值类型:int型
struct AMG
{
VertexType vex[MVNum]; //顶点表
ArcType arc[MVNum][MVNum]; //邻接矩阵
int VexNum, ArcNum; //(图)当前:顶点数和边数
}; //Adjacency Matrix Graph
Adjacency:
Connecting (adjacent) near; adjoining; adjacency; adjacency (object); adjacent distance;
Undirected graph construction:
Create an undirected graph:
Function body:
Status CreateUDN(AMG& G)
//Un Directed Net
{
cout << "input num" << endl;
cin >> G.VexNum >> G.ArcNum;
cout << "input vexs" << endl;
for (int i = 0; i <= G.VexNum; i++)
cin >> G.vex[i];
//数组不是从零开始吗,那不是多了一个位置
for (int j = 0; j <= G.ArcNum; j++)
for (int k = 0; k <= G.ArcNum; k++)
G.arc[j][k] = MaxInt;
//构建邻接矩阵
for (int k = 0; k < G.ArcNum; k++)
{
int weight;
VertexType v1, v2;
cin >> v1 >> v2 >> weight; //输入一条边依附的顶点和边的权值
int i = LocateVex(G, v1); //确定v1在G中的位置
int j = LocateVex(G, v2); //确定v2在G中的位置
G.arc[i][j] = weight; //邻接矩阵赋值
G.arc[j][i] = G.arc[i][j]; //无向图,反过来值相同
}
return true;
}
Written by myself: ([Build an adjacency matrix, input the weight of each edge into the matrix] module)
for (int m = 0; m <= G.ArcNum; m++)
//逐个输入每一条边
{
int weight;
VertexType v1, v2;
cin >> v1 >> v2 >> weight;
int a, b;
a = LocateVexNumber(G, v1);
b = LocateVexNumber(G, v2);
G.arc[a][b] =G.arc[b][a]= weight;
}
LocateVex(G, v) function:
Status LocateVex(AMG &G, VertexType& v)
{
for (int i = 0; i < G.VexNum; ++i)
{
if (G.vex[i] == v)
return i;
}
return -1;
}
Notice:
don't forget to write
return -1;
!!!
question:
//Doesn't the array start from zero? Isn't that one more position
Undirected network, directed network, directed graph:
In short, it is to modify the procedure of [constructing an adjacency matrix]:
Undirected net:
//构造邻接矩阵
for (int k = 0; k < G.ArcNum; k++)
{
//int weight;
VertexType v1, v2;
cin >> v1 >> v2;//>> weight; //输入一条边依附的顶点和边的权值
int i = LocateVex(G, v1); //确定v1在G中的位置
int j = LocateVex(G, v2); //确定v2在G中的位置
G.arc[i][j] = 1;// weight; //邻接矩阵赋值
G.arc[j][i] = G.arc[i][j]; //无向图,反过来值相同
}
Directed net:
//构建邻接矩阵
for (int k = 0; k < G.ArcNum; k++)
{
int weight;
VertexType v1, v2;
cin >> v1 >> v2 >> weight; //输入一条边依附的顶点和边的权值
int i = LocateVex(G, v1); //确定v1在G中的位置
int j = LocateVex(G, v2); //确定v2在G中的位置
G.arc[i][j] = weight; //邻接矩阵赋值
//G.arc[j][i] = G.arc[i][j]; //无向图,反过来值相同
}
Directed graph:
//构造邻接矩阵
for (int k = 0; k < G.ArcNum; k++)
{
//int weight;
VertexType v1, v2;
cin >> v1 >> v2;//>> weight; //输入一条边依附的顶点和边的权值
int i = LocateVex(G, v1); //确定v1在G中的位置
int j = LocateVex(G, v2); //确定v2在G中的位置
G.arc[i][j] = 1;// weight; //邻接矩阵赋值
//G.arc[j][i] = G.arc[i][j]; //无向图,反过来值相同
}
Additional:
Claw determinant: Except for the first row and the first column, there are also diagonal lines, and the rest of the elements are zero