当前位置:网站首页>BFS realizes breadth first traversal of adjacency matrix (with examples)
BFS realizes breadth first traversal of adjacency matrix (with examples)
2022-07-07 00:59:00 【Demo Dragon】
1. The establishment of adjacency matrix
#include<iostream>
using namespace std;
#include<queue>
#include<stdbool.h>
// Creation of adjacency matrix
typedef char vextype;// Vertex type customization
typedef int edgetype;// Custom weights on the edge
#define MAXVEX 100// Maximum number of vertices
#define MAXS 66666// Represents infinity
typedef struct
{
vextype vexs[MAXVEX];// Vertex table
edgetype arc[MAXVEX][MAXVEX];// Adjacency matrix
int numNodes;// Current number of vertices
int numEdges;// Current number of sides
}MGraph;
1. The adjacency matrix representation of undirected network graph is established by reference
//1. The adjacency matrix representation of undirected network graph is established by reference
void CreatGraph(MGraph& G)
{
cout << " Please enter the number of vertices and edges :";
cin >> G.numNodes >> G.numEdges;
cout << " Please enter all vertex information :" << endl;
for (int i = 0; i < G.numNodes; i++)
cin >> G.vexs[i];
for (int i = 0; i < G.numNodes; i++)
for (int j = 0; j < G.numNodes; j++)
G.arc[i][j] = 0;
cout << " Please enter the edges composed of vertices and their weights :" << endl;
for (int i = 0; i < G.numEdges; i++)
{
int w, m, n;
cin >> m >> n >> w;
G.arc[m][n] = w;
G.arc[n][m] = G.arc[m][n];
}
}
2. The pointer establishes the adjacency matrix representation of undirected network graph
//2. The pointer establishes the adjacency matrix representation of undirected network graph
void CreatGraph(MGraph* G)
{
cout << " Please enter the number of vertices and edges :";
cin >> G->numNodes >> G->numEdges;
cout << " Please enter all vertex information :" << endl;
for (int i = 0; i < G->numNodes; i++)
cin>>G->vexs[i];
for (int i = 0; i < G->numNodes; i++)
for (int j = 0; j < G->numNodes; j++)
G->arc[i][j] = 0;
cout << " Please enter the edges composed of vertices and their weights :" << endl;
for (int i = 0; i < G->numEdges; i++)
{
int w, m, n;
cin >> m >> n >> w;
G->arc[m][n] = w;
G->arc[n][m] = G->arc[m][n];
}
}
2. Print adjacency matrix
// Print adjacency matrix
void PrintGraph(MGraph G)
{
int len = G.numNodes;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
cout << G.arc[i][j] << " ";
}
cout << endl;
}
}
3. Depth first recursive algorithm of adjacency matrix
//3. Depth first recursive algorithm of adjacency matrix
#define MAXVEX 100
int visit[MAXVEX] = {
0 };
void DFS(MGraph G, int i)
{
cout << G.vexs[i] << " ";
visit[i] = 1;
for (int j = 0; j < G.numNodes; j++)
{
if (!visit[j] && G.arc[i][j] != 0)
{
DFS(G, j);
}
}
}
void DFST(MGraph G)
{
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 1; i < G.numNodes; i++)
{
if(!visit[i])
DFS(G, i);
}
}
4. Breadth first traversal algorithm of adjacency matrix
// Breadth first traversal algorithm of adjacency matrix
void BFS(MGraph G)
{
queue<vextype> Q;
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 0; i < G.numNodes; i++){
if (!visit[i]){
visit[i] = 1;
cout << G.vexs[i] << " ";
Q.push(i);
while (!Q.empty()){
Q.pop();
for (int j = 0; j < G.numNodes; j++)
if (G.arc[i][j] != 0 && visit[j] == 0){
visit[j] = 1;
cout << G.vexs[j] << " ";
Q.push(j);
}
}
}
}
}
5. Test case
#include<iostream>
using namespace std;
#include<queue>
#include<stdbool.h>
// Creation of adjacency matrix
typedef char vextype;// Vertex type customization
typedef int edgetype;// Custom weights on the edge
#define MAXVEX 100// Maximum number of vertices
#define MAXS 66666// Represents infinity
typedef struct
{
vextype vexs[MAXVEX];// Vertex table
edgetype arc[MAXVEX][MAXVEX];// Adjacency matrix
int numNodes;// Current number of vertices
int numEdges;// Current number of sides
}MGraph;
//1. The adjacency matrix representation of undirected network graph is established by reference
void CreatGraph(MGraph& G)
{
cout << " Please enter the number of vertices and edges :";
cin >> G.numNodes >> G.numEdges;
cout << " Please enter all vertex information :" << endl;
for (int i = 0; i < G.numNodes; i++)
cin >> G.vexs[i];
for (int i = 0; i < G.numNodes; i++)
for (int j = 0; j < G.numNodes; j++)
G.arc[i][j] = 0;
cout << " Please enter the edges composed of vertices and their weights :" << endl;
for (int i = 0; i < G.numEdges; i++)
{
int w, m, n;
cin >> m >> n >> w;
G.arc[m][n] = w;
G.arc[n][m] = G.arc[m][n];
}
}
//2. The pointer establishes the adjacency matrix representation of undirected network graph
/*void CreatGraph(MGraph* G) { cout << " Please enter the number of vertices and edges :"; cin >> G->numNodes >> G->numEdges; cout << " Please enter all vertex information :" << endl; for (int i = 0; i < G->numNodes; i++) cin>>G->vexs[i]; for (int i = 0; i < G->numNodes; i++) for (int j = 0; j < G->numNodes; j++) G->arc[i][j] = 0; cout << " Please enter the edges composed of vertices and their weights :" << endl; for (int i = 0; i < G->numEdges; i++) { int w, m, n; cin >> m >> n >> w; G->arc[m][n] = w; G->arc[n][m] = G->arc[m][n]; } }*/
//3. Depth first recursive algorithm of adjacency matrix
#define MAXVEX 100
int visit[MAXVEX] = {
0 };
void DFS(MGraph G, int i)
{
cout << G.vexs[i] << " ";
visit[i] = 1;
for (int j = 0; j < G.numNodes; j++)
{
if (!visit[j] && G.arc[i][j] != 0)
{
DFS(G, j);
}
}
}
void DFST(MGraph G)
{
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 1; i < G.numNodes; i++)
{
if(!visit[i])
DFS(G, i);
}
}
// Print adjacency matrix
void PrintGraph(MGraph G)
{
int len = G.numNodes;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
cout << G.arc[i][j] << " ";
}
cout << endl;
}
}
// Breadth first traversal algorithm of adjacency matrix
void BFS(MGraph G)
{
queue<vextype> Q;
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 0; i < G.numNodes; i++)
{
if (!visit[i])
{
visit[i] = 1;
cout << G.vexs[i] << " ";
Q.push(i);
while (!Q.empty()){
Q.pop();
for (int j = 0; j < G.numNodes; j++)
if (G.arc[i][j] != 0 && visit[j] == 0)
{
visit[j] = 1;
cout << G.vexs[j] << " ";
Q.push(j);
}
}
}
}
}
int main()
{
MGraph G;
CreatGraph(G);
cout << " Print adjacency matrix " << endl;
PrintGraph(G);
cout << " The depth first traversal result of the adjacency matrix is :" << endl;
DFST(G);
cout << "\n The breadth first traversal result of the adjacency matrix is :" << endl;
BFS(G);
}
test result 

边栏推荐
- .class文件的字节码结构
- Notes of training courses selected by Massey school
- 什么是时间
- dynamic programming
- stm32F407-------DAC数模转换
- 随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
- Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
- 第六篇,STM32脉冲宽度调制(PWM)编程
- 《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03
- 城联优品入股浩柏国际进军国际资本市场,已完成第一步
猜你喜欢

随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP

Deep learning environment configuration jupyter notebook

第四篇,STM32中断控制编程
![[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)](/img/1a/2b497a1baa04d84d28da715d097dfe.png)
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)

Stm32f407 ------- SPI communication

Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version

AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info

深度学习之环境配置 jupyter notebook

集合(泛型 & List & Set & 自定义排序)

Telerik UI 2022 R2 SP1 Retail-Not Crack
随机推荐
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
做微服务研发工程师的一年来的总结
Telerik UI 2022 R2 SP1 Retail-Not Crack
新手如何入门学习PostgreSQL?
View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
Advanced learning of MySQL -- basics -- multi table query -- self join
通过串口实现printf函数,中断实现串口数据接收
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
Deeply explore the compilation and pile insertion technology (IV. ASM exploration)
Advanced learning of MySQL -- basics -- multi table query -- subquery
第六篇,STM32脉冲宽度调制(PWM)编程
深入探索编译插桩技术(四、ASM 探秘)
Advanced learning of MySQL -- basics -- multi table query -- joint query
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
Mujoco Jacobi - inverse motion - sensor
Address information parsing in one line of code
深度学习简史(二)
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure