当前位置:网站首页>Adjacency matrix undirected graph (I) - basic concepts and C language
Adjacency matrix undirected graph (I) - basic concepts and C language
2022-07-01 11:31:00 【Life needs depth】
This chapter introduces the adjacency matrix undirected graph . stay " The theoretical basis of graph " The theory of graph has been introduced in , The concept of graph is not repeated here . As usual , This article will first give C The realization of language ; We will give you the following C++ and Java Implementation of version . The language of implementation is different , But the principle is the same , Choose one of them to learn . If there are mistakes or deficiencies in the article , Please point out !
Catalog
1. Introduction of adjacency matrix undirected graph
2. Adjacency matrix undirected graph code description
3. Adjacency matrix undirected graph complete source codeReprint please indicate the source : If the sky doesn't die - Blog Garden
Introduction of adjacency matrix undirected graph
Adjacency matrix undirected graph refers to the undirected graph represented by adjacency matrix .
The picture above G1 Contains "A,B,C,D,E,F,G" common 7 vertices , And it includes "(A,C),(A,D),(A,F),(B,C),(C,D),(E,G),(F,G)" common 7 side . Because this is an undirected graph , So side (A,C) He Bian (C,A) It's the same side ; Here are the side times , It is listed in alphabetical order .
The matrix on the right of the above figure is G1 Diagram of adjacency matrix in memory .A[i][j]=1 It means the first one i Vertices and the j Vertices are adjacency points ,A[i][j]=0 They are not adjacency points ; and A[i][j] It means No i Xing di j The value of the column ; for example ,A[1,2]=1, It means the first one 1 vertices ( Vertex B) And the 2 vertices (C) It's the adjacency point .
Adjacency matrix undirected graph code description
1. The basic definition
// Adjacency matrix
typedef struct _graph
{
char vexs[MAX]; // Vertex set
int vexnum; // Number of vertices
int edgnum; // Number of edges
int matrix[MAX][MAX]; // Adjacency matrix
}Graph, *PGraph;
Graph Is the structure corresponding to the adjacency matrix .
vexs Lets you save vertices ,vexnum It's the top point ,edgnum It's the number of sides ;matrix Is a two-dimensional array used to store matrix information . for example ,matrix[i][j]=1, said " The vertices i( namely vexs[i])" and " The vertices j( namely vexs[j])" It's the adjacency point ;matrix[i][j]=0, They are not adjacency points .
2. Create a matrix
Here are two ways to create a matrix . One is Using known data , Another rule The user needs to input data manually .
2.1 Create diagrams ( Use the provided matrix )
/*
* Create diagrams ( Use the provided matrix )
*/
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;
// Input " Number of vertices " and " Number of edges "
if ((pG=(Graph*)malloc(sizeof(Graph))) == NULL )
return NULL;
memset(pG, 0, sizeof(Graph));
// initialization " Number of vertices " and " Number of edges "
pG->vexnum = vlen;
pG->edgnum = elen;
// initialization " The vertices "
for (i = 0; i < pG->vexnum; i++)
{
pG->vexs[i] = vexs[i];
}
// initialization " edge "
for (i = 0; i < pG->edgnum; i++)
{
// Read the start and end vertices of the edge
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;
}
Fold
createexamplegraph The function of yes is to create an undirected graph of adjacency matrix .
Be careful : Undirected graph created by this method , It's the picture above G1.
2.2 Create diagrams ( Enter your own )
/*
* Create diagrams ( Use the provided matrix )
*/
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;
// Input " Number of vertices " and " Number of edges "
if ((pG=(Graph*)malloc(sizeof(Graph))) == NULL )
return NULL;
memset(pG, 0, sizeof(Graph));
// initialization " Number of vertices " and " Number of edges "
pG->vexnum = vlen;
pG->edgnum = elen;
// initialization " The vertices "
for (i = 0; i < pG->vexnum; i++)
{
pG->vexs[i] = vexs[i];
}
// initialization " edge "
for (i = 0; i < pG->edgnum; i++)
{
// Read the start and end vertices of the edge
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;
}
Fold
create_graph() Is to read the user's input , Convert the input data into the corresponding undirected graph .
边栏推荐
- redis中value/list
- Learning summary on June 30, 2022
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Harbor webhook from principle to construction
- How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
- Harbor webhook从原理到构建
- 8 best practices to protect your IAC security!
- Exposure: a white box photo post processing framework reading notes
- 【MAUI】为 Label、Image 等控件添加点击事件
- 超详细黑苹果安装图文教程送EFI配置合集及系统
猜你喜欢
Learning summary on June 28, 2022
Brief analysis of edgedb architecture
田溯宁投的天润云上市:市值22亿港元 年利润下降75%
Unittest 框架介绍及第一个demo
索引失效的几种情况
TEMPEST HDMI泄漏接收 3
Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%
Compile and debug net6 source code
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
达梦数据冲刺科创板:拟募资24亿 冯裕才曾为华科教授
随机推荐
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
ES6 Promise用法小结
Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
TEMPEST HDMI泄漏接收 4
Is it safe for Huatai Securities to open an account online?
How to set decimal places in CAD
CPI tutorial - asynchronous interface creation and use
redis中value/hush
Wechat applet development - user authorization to log in to "suggestions collection"
IPlImage的width和widthStep
用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义
邻接矩阵无向图(一) - 基本概念与C语言
Several cases of index failure
How to realize the four isolation levels of MySQL (brief)
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%
MySQL in and not in() empty list error
Learning summary on June 29, 2022
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
Web foundation of network security note 02