当前位置:网站首页>邻接矩阵无向图(一) - 基本概念与C语言
邻接矩阵无向图(一) - 基本概念与C语言
2022-07-01 11:26:00 【生活需要深度】
本章介绍邻接矩阵无向图。在"图的理论基础"中已经对图进行了理论介绍,这里就不再对图的概念进行重复说明了。和以往一样,本文会先给出C语言的实现;后续再分别给出C++和Java版本的实现。实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可。若文章有错误或不足的地方,请不吝指出!
目录
1. 邻接矩阵无向图的介绍
2. 邻接矩阵无向图的代码说明
3. 邻接矩阵无向图的完整源码转载请注明出处:如果天空不死 - 博客园
更多内容:数据结构与算法系列 目录
邻接矩阵无向图的介绍
邻接矩阵无向图是指通过邻接矩阵表示的无向图。

上面的图G1包含了"A,B,C,D,E,F,G"共7个顶点,而且包含了"(A,C),(A,D),(A,F),(B,C),(C,D),(E,G),(F,G)"共7条边。由于这是无向图,所以边(A,C)和边(C,A)是同一条边;这里列举边时,是按照字母先后顺序列举的。
上图右边的矩阵是G1在内存中的邻接矩阵示意图。A[i][j]=1表示第i个顶点与第j个顶点是邻接点,A[i][j]=0则表示它们不是邻接点;而A[i][j]表示的是第i行第j列的值;例如,A[1,2]=1,表示第1个顶点(即顶点B)和第2个顶点(C)是邻接点。
邻接矩阵无向图的代码说明
1. 基本定义

// 邻接矩阵
typedef struct _graph
{
char vexs[MAX]; // 顶点集合
int vexnum; // 顶点数
int edgnum; // 边数
int matrix[MAX][MAX]; // 邻接矩阵
}Graph, *PGraph;

Graph是邻接矩阵对应的结构体。
vexs用于保存顶点,vexnum是顶点数,edgnum是边数;matrix则是用于保存矩阵信息的二维数组。例如,matrix[i][j]=1,则表示"顶点i(即vexs[i])"和"顶点j(即vexs[j])"是邻接点;matrix[i][j]=0,则表示它们不是邻接点。
2. 创建矩阵
这里介绍提供了两个创建矩阵的方法。一个是用已知数据,另一个则需要用户手动输入数据。
2.1 创建图(用已提供的矩阵)

/*
* 创建图(用已提供的矩阵)
*/
Graph* create_example_graph()
{
char vexs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char edges[][2] = {
{'A', 'C'},
{'A', 'D'},
{'A', 'F'},
{'B', 'C'},
{'C', 'D'},
{'E', 'G'},
{'F', 'G'}};
int vlen = LENGTH(vexs);
int elen = LENGTH(edges);
int i, p1, p2;
Graph* pG;
// 输入"顶点数"和"边数"
if ((pG=(Graph*)malloc(sizeof(Graph))) == NULL )
return NULL;
memset(pG, 0, sizeof(Graph));
// 初始化"顶点数"和"边数"
pG->vexnum = vlen;
pG->edgnum = elen;
// 初始化"顶点"
for (i = 0; i < pG->vexnum; i++)
{
pG->vexs[i] = vexs[i];
}
// 初始化"边"
for (i = 0; i < pG->edgnum; i++)
{
// 读取边的起始顶点和结束顶点
p1 = get_position(*pG, edges[i][0]);
p2 = get_position(*pG, edges[i][1]);
pG->matrix[p1][p2] = 1;
pG->matrix[p2][p1] = 1;
}
return pG;
}
折叠

createexamplegraph是的作用是创建一个邻接矩阵无向图。
注意:该方法创建的无向图,就是上面图G1。
2.2 创建图(自己输入)

/*
* 创建图(用已提供的矩阵)
*/
Graph* create_example_graph()
{
char vexs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char edges[][2] = {
{'A', 'C'},
{'A', 'D'},
{'A', 'F'},
{'B', 'C'},
{'C', 'D'},
{'E', 'G'},
{'F', 'G'}};
int vlen = LENGTH(vexs);
int elen = LENGTH(edges);
int i, p1, p2;
Graph* pG;
// 输入"顶点数"和"边数"
if ((pG=(Graph*)malloc(sizeof(Graph))) == NULL )
return NULL;
memset(pG, 0, sizeof(Graph));
// 初始化"顶点数"和"边数"
pG->vexnum = vlen;
pG->edgnum = elen;
// 初始化"顶点"
for (i = 0; i < pG->vexnum; i++)
{
pG->vexs[i] = vexs[i];
}
// 初始化"边"
for (i = 0; i < pG->edgnum; i++)
{
// 读取边的起始顶点和结束顶点
p1 = get_position(*pG, edges[i][0]);
p2 = get_position(*pG, edges[i][1]);
pG->matrix[p1][p2] = 1;
pG->matrix[p2][p1] = 1;
}
return pG;
}
折叠

create_graph()是读取用户的输入,将输入的数据转换成对应的无向图。
边栏推荐
- Intel Labs annonce de nouveaux progrès en photonique intégrée
- The idea runs with an error command line is too long Shorten command line for...
- Global filter (processing time format)
- tmux使用
- 优雅地翻转数组
- Question: what professional qualities should test engineers have?
- Value/list in redis
- S7-1500PLC仿真
- Impressive bug summary (continuously updated)
- Flip the array gracefully
猜你喜欢

Technology sharing | introduction to linkis parameters

node版本管理器nvm安装及切换

编译调试Net6源码

Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%

The idea runs with an error command line is too long Shorten command line for...

Wonderful! MarkBERT

redis配置环境变量

2022年6月编程语言排行,第一名居然是它?!

Redis configuration environment variables

Huawei equipment is configured with large network WLAN basic services
随机推荐
Flip the array gracefully
Node version manager NVM installation and switching
Paxos 入门
妙啊!MarkBERT
In June 2022, it was the first programming language?!
Redis configuration environment variables
Learning summary on June 28, 2022
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
开发说,“ 这个不用测,回归正常流程就行 “,测试人员怎么办?
[Maui] add click events for label, image and other controls
Value/sortedset in redis
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
树莓派4B安装tensorflow2.0[通俗易懂]
Can solo be accessed through IPv6?
Give up high paying jobs in Shenzhen and go back home
超详细黑苹果安装图文教程送EFI配置合集及系统
Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%
Can I open a securities account anywhere? Is it safe to open an account
ABBIRB120工业机器人机械零点位置