当前位置:网站首页>Depth first DFS and breadth first BFS -- traversing adjacency tables
Depth first DFS and breadth first BFS -- traversing adjacency tables
2022-07-05 22:14:00 【Demo Dragon】
1. Creation of adjacency table
#include<iostream>
using namespace std;
#include<queue>
#define MAXVEX 100
// Creation of adjacency table
typedef char vextype;// Custom vertex type
typedef int edgetype;// Customize the weight on the edge
// Side table node
typedef struct EdgeNode
{
int index;// Save the subscript corresponding to the vertex
edgetype val;// Store weights
struct EdgeNode* next;// The chain domain that points to the next critical point
}EdgeNode;
// Vertex table node
typedef struct VexNode
{
vextype data;// Store vertex information
EdgeNode* firstNode;// Side header pointer
}VexNode,AList[MAXVEX];
// The graph structure
typedef struct
{
AList Datas;// Equivalent to an array of structures ,
int numNodes;// Number of vertices in the graph
int numEdges;// The number of edges in a graph
}MGraph;
1. Pointer to establish the adjacency table structure in the graph
//1. Pointer to establish the adjacency table structure in the graph
void CreatGraph(MGraph* G)
{
EdgeNode* p;
cout << " Please enter the number of vertices and edges :";
cin >> G->numNodes >> G->numEdges;
cout << " Please enter the data of all vertices :" << endl;
for (int i = 0; i < G->numNodes; i++)// Read in vertex information , Create side tables
{
cin >> G->Datas[i].data;
G->Datas[i].firstNode = NULL;
}
cout << " Please enter all edges (v1,v2) The sequence number of the top vertex :" << endl;
for (int k = 0; k < G->numEdges; k++)
{
int m, n;
cin >> m >> n;
// The degree of
p = new EdgeNode;
p->index = n;
p->next = G->Datas[m].firstNode;
G->Datas[m].firstNode = p;
// The degree of
p = new EdgeNode;
p->index = m;
p->next = G->Datas[n].firstNode;
G->Datas[n].firstNode = p;
}
}
2. Reference to establish the adjacency table structure in the graph
//2. Reference to establish the adjacency table structure in the graph
void CreatGraph(MGraph &G)
{
EdgeNode* p;
cout << " Please enter the number of vertices and edges :";
cin >> G.numNodes >> G.numEdges;
cout << " Please enter the data of all vertices :" << endl;
for (int i = 0; i < G.numNodes; i++)// Read in vertex information , Create side tables
{
cin >> G.Datas[i].data;
G.Datas[i].firstNode = NULL;
}
cout << " Please enter all edges (v1,v2) The sequence number of the top vertex :" << endl;
for (int k = 0; k < G.numEdges; k++)
{
int m, n;
cin >> m >> n;
// The degree of
p = new EdgeNode;
p->index = n;
p->next = G.Datas[m].firstNode;
G.Datas[m].firstNode = p;
// The degree of
p = new EdgeNode;
p->index = m;
p->next = G.Datas[n].firstNode;
G.Datas[n].firstNode = p;
}
}
2. Depth first traversal algorithm of adjacency table
//3. Depth first traversal algorithm of adjacency table
int visit[MAXVEX] = {
0 };
void DFS(MGraph G,int i)
{
EdgeNode* p;
visit[i] = 1;
cout << G.Datas[i].data << " ";
p = G.Datas[i].firstNode;
while (p)
{
if (!visit[p->index])
DFS(G, p->index);
p = p->next;
}
}
void DFST(MGraph G)
{
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 0; i < G.numNodes; i++)
if (!visit[i])
DFS(G, i);
}
3. Breadth first traversal algorithm of adjacency table
// 4. Breadth first traversal algorithm of adjacency table
void BFS(MGraph G)
{
queue<vextype>Q;
EdgeNode* p;
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.Datas[i].data<<" ";// Print vertex
Q.push(i);
while (!Q.empty())
{
Q.pop();
p = G.Datas[i].firstNode;// Find the header pointer of the edge linked list of the current vertex
while (p)
{
if (!visit[p->index])// If this vertex is not accessed
{
visit[p->index] = 1;
cout << G.Datas[p->index].data<<" ";
Q.push(p->index);
}
p = p->next;
}
}
}
}
}
4. Test cases
#include<iostream>
using namespace std;
#include<queue>
#define MAXVEX 100
// Creation of adjacency table
typedef char vextype;// Custom vertex type
typedef int edgetype;// Customize the weight on the edge
// Side table node
typedef struct EdgeNode
{
int index;// Save the subscript corresponding to the vertex
edgetype val;// Store weights
struct EdgeNode* next;// The chain domain that points to the next critical point
}EdgeNode;
// Vertex table node
typedef struct VexNode
{
vextype data;// Store vertex information
EdgeNode* firstNode;// Side header pointer
}VexNode,AList[MAXVEX];
// The graph structure
typedef struct
{
AList Datas;// Equivalent to an array of structures ,
int numNodes;// Number of vertices in the graph
int numEdges;// The number of edges in a graph
}MGraph;
//1. Pointer to establish the adjacency table structure in the graph
/*void CreatGraph(MGraph* G) { EdgeNode* p; cout << " Please enter the number of vertices and edges :"; cin >> G->numNodes >> G->numEdges; cout << " Please enter the data of all vertices :" << endl; for (int i = 0; i < G->numNodes; i++)// Read in vertex information , Create side tables { cin >> G->Datas[i].data; G->Datas[i].firstNode = NULL; } cout << " Please enter all edges (v1,v2) The sequence number of the top vertex :" << endl; for (int k = 0; k < G->numEdges; k++) { int m, n; cin >> m >> n; // The degree of p = new EdgeNode; p->index = n; p->next = G->Datas[m].firstNode; G->Datas[m].firstNode = p; // The degree of p = new EdgeNode; p->index = m; p->next = G->Datas[n].firstNode; G->Datas[n].firstNode = p; } }*/
//2. Reference to establish the adjacency table structure in the graph
void CreatGraph(MGraph &G)
{
EdgeNode* p;
cout << " Please enter the number of vertices and edges :";
cin >> G.numNodes >> G.numEdges;
cout << " Please enter the data of all vertices :" << endl;
for (int i = 0; i < G.numNodes; i++)// Read in vertex information , Create side tables
{
cin >> G.Datas[i].data;
G.Datas[i].firstNode = NULL;
}
cout << " Please enter all edges (v1,v2) The sequence number of the top vertex :" << endl;
for (int k = 0; k < G.numEdges; k++)
{
int m, n;
cin >> m >> n;
// The degree of
p = new EdgeNode;
p->index = n;
p->next = G.Datas[m].firstNode;
G.Datas[m].firstNode = p;
// The degree of
p = new EdgeNode;
p->index = m;
p->next = G.Datas[n].firstNode;
G.Datas[n].firstNode = p;
}
}
//3. Depth first traversal algorithm of adjacency table
int visit[MAXVEX] = {
0 };
void DFS(MGraph G,int i)
{
EdgeNode* p;
visit[i] = 1;
cout << G.Datas[i].data << " ";
p = G.Datas[i].firstNode;
while (p)
{
if (!visit[p->index])
DFS(G, p->index);
p = p->next;
}
}
void DFST(MGraph G)
{
for (int i = 0; i < G.numNodes; i++)
visit[i] = 0;
for (int i = 0; i < G.numNodes; i++)
if (!visit[i])
DFS(G, i);
}
// 4. Breadth first traversal algorithm of adjacency table
void BFS(MGraph G)
{
queue<vextype>Q;
EdgeNode* p;
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.Datas[i].data<<" ";// Print vertex
Q.push(i);
while (!Q.empty())
{
Q.pop();
p = G.Datas[i].firstNode;// Find the header pointer of the edge linked list of the current vertex
while (p)
{
if (!visit[p->index])// If this vertex is not accessed
{
visit[p->index] = 1;
cout << G.Datas[p->index].data<<" ";
Q.push(p->index);
}
p = p->next;
}
}
}
}
}
int main()
{
MGraph G;
CreatGraph(G);
cout << " The depth first traversal result of the adjacency table :" << endl;
DFST(G);
cout << "\n The breadth first traversal result of the adjacency table :" << endl;
BFS(G);
}
test result
边栏推荐
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- Index optimization of performance tuning methodology
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- 854. String BFS with similarity K
- HDU 4391 paint the wall segment tree (water
- The simple problem of leetcode is to split a string into several groups of length K
- Leetcode simple question: check whether each row and column contain all integers
- 微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- Server optimization of performance tuning methodology
猜你喜欢
Oracle hint understanding
What changes has Web3 brought to the Internet?
元宇宙中的三大“派系”
Installation of VMware Workstation
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Shell script, awk condition judgment and logic comparison &||
Stored procedures and stored functions
每日刷题记录 (十四)
Cobaltstrike builds an intranet tunnel
Summary of concurrency control
随机推荐
Blocking protocol for concurrency control
Three "factions" in the metauniverse
The new content of the text component can be added through the tag_ Config set foreground and background colors
Daily question brushing record (XIV)
Poj3414 extensive search
Overview of concurrency control
Oracle views the data size of a table
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Database tuning solution
EBS Oracle 11g cloning steps (single node)
FBO and RBO disappeared in webgpu
A long's perception
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
PyGame practical project: write Snake games with 300 lines of code
C language knowledge points link
ESP32 hosted
How to develop and introduce applet plug-ins
Platform bus
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Oracle checkpoint queue - Analysis of the principle of instance crash recovery