当前位置:网站首页>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
边栏推荐
- 省市区三级坐标边界数据csv转JSON
- Advanced learning of MySQL -- Fundamentals -- concurrency of transactions
- New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
- Provincial and urban level three coordinate boundary data CSV to JSON
- 城联优品入股浩柏国际进军国际资本市场,已完成第一步
- Leetcode (547) - number of provinces
- 建立自己的网站(17)
- Chapter II proxy and cookies of urllib Library
- . Bytecode structure of class file
- Value Function Approximation
猜你喜欢
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
Attention slam: a visual monocular slam that learns from human attention
Chapter II proxy and cookies of urllib Library
Configuring the stub area of OSPF for Huawei devices
第六篇,STM32脉冲宽度调制(PWM)编程
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
深度学习之数据处理
Mujoco second order simple pendulum modeling and control
省市区三级坐标边界数据csv转JSON
随机推荐
批量获取中国所有行政区域经边界纬度坐标(到县区级别)
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Policy Gradient Methods
from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc
Batch obtain the latitude coordinates of all administrative regions in China (to the county level)
Matlab learning notes
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
学习光线跟踪一样的自3D表征Ego3RT
【软件逆向-自动化】逆向工具大全
What kind of experience is it to realize real-time collaboration in jupyter
Equals() and hashcode()
【JokerのZYNQ7020】AXI_ EMC。
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
C9高校,博士生一作发Nature!
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
How to judge whether an element in an array contains all attribute values of an object
Let's talk about 15 data source websites I often use
在jupyter中实现实时协同是一种什么体验
Mujoco second order simple pendulum modeling and control