# C + + demonstrates the breadth traversal and depth traversal of undirected graphs

Posted by siobhan on Sun, 02 Jan 2022 03:22:16 +0100

A little white ~ ~ ~ directly on the report and code

[problem description]

Write a program to demonstrate the traversal operation of undirected graph.

[basic requirements]

1. Taking adjacency table as storage structure, the depth first and breadth first traversal of connected undirected graph are realized by recursive algorithm.
2. With the help of stack, depth first traversal is realized by non recursive algorithm.

[design idea]

My design idea is roughly divided into three steps:

1. Step 1: generate adjacency table:
• For the adjacency table, I define a simple Node node, which contains its own value, data of type int, and link of type Node * linking the next Node.
• Store the direct connection of the input points in the adjacency table. I use the function bool create (int line, int point) to generate this adjacency table. The reason for using bool type function is to judge whether the graph is empty or does not meet the requirements (such as no connection).
• Generation process of adjacency table: first, generate a dynamic Array array of point+1 size according to the number of input points. It is of Node * * type. The pointer of Node * type is stored in the Array. Set its size to point+1 because Array[i] represents the ith Node. After setting the size, initialize the first to point positions of the Array. The pointer p - > link of each position is NULL, and P - > data is its value. For example, Array[i] - > data = I. Next, start to input each edge, that is, two points, and the nodes behind the two points in the Array should be updated and inserted.

The following two figures show how the adjacency table I defined stores nodes.

1. Step 2: depth first traversal:
• Firstly, there are duplicate nodes on the adjacency table. Therefore, a function bool Judge(int x[], int point, int y) is defined, which can judge whether y exists in array X. if yes, it returns false, and if not, it is true. I defined an array of judge with the size of point+1. It is of type int, and its initial state is 0. If a point I is traversed, there will be judge[i]=i.
• Because we don't allow recursion, we have to delete the concise and clear code that has been written and use the stack method. In order to determine whether all the points around the stack vertex x have been traversed, I defined the bool Nempty(int x, int point) function, which can Judge whether the value of the node following Array[x] has been traversed (Judge is used) ()) if the function has been traversed, the point x will be popped out of the stack, and then the next point y in the stack will be determined. If there are nodes following y that have not been traversed, such as z, z will be output and z will be pushed into the stack.
1. Step 3: breadth first traversal:
• That is, the data of Array[i] and the data of subsequent link nodes are output successively from the source node I. In order to record the following nodes for breadth traversal, I use the queue. Whenever a node is output, I put the node into the queue, and then repeat the above steps. Of course, there are redundant nodes in the adjacency table, so you also need to use the function Judge().
• The breadth traversal still uses recursion. The teacher doesn't ask not to use it. At the end of the breadth traversal function, a judgment should be added. If the queue is empty, it means that the recursion can be ended after traversal.

[test data]

[output result]

[summary]

In fact, this program has many problems, such as multiple global variables, which limits the limitations of the function. Secondly, there are many requirements for point input, which must start from 1, such as 1, 2, 3, 4,... You can't input every other point, such as 1, 3, 7,... Another problem is that you can't judge whether it is two or more areas, such as a path between 1, 2, 3 and 4, a path between 5, 6, 7 and 8, etc. in short, it needs a lot of optimization, Or there are many places not considered~~~~

Source code:

```#include<iostream>
#include <queue>
#include<stack>
using namespace std;
//Definition of nodes
struct Node {
int data = 0;
};
Node** Array=NULL;
//Two judgment array definitions
int* judge = NULL;
int* judges = NULL;
queue<int>Q;
bool Creat(int line, int point) {
if (point == 0 && line == 0) { cout << "The graph is empty." << endl; return false; }
if (point == 0 && line != 0 || point != 0 && line == 0) { cout << "The diagram is wrong." << endl; return false; }
Array = new Node * [point + 1];
for (int i = 1; i <= point; i++) {
Node* example = new Node;
example->data = i;
Array[i] = example;
}
int step = 1;
while (line != 0) {
int head = 0; int tail = 0;
cout << "Please enter page" << step << "The head and tail of the strip." << endl;
{
}
{
}
line--; step++;
}
return true;
}
//Whether the point has been traversed by the function
bool Judge(int x[], int point, int y) {
for (int i = 0; i <=point; i++) {
if (x[i] == y)return true;
}
return false;
}
//Whether the node followed by the node has been traversed by the function
bool Nempty(int x, int point) {
Node* w = Array[x];
while (w != NULL) {
x = w->data;
if (!Judge(judge, point, x))return true;
else {
}
}
return false;
}
//Depth first traversal function
void DFS(int i,int point) {
stack<int> S;
S.push(i); cout << i << "  "; judge[i] = i;
int m; Node* w = new Node;
while (!S.empty()) {
m = S.top();
w = Array[m];
if (Nempty(m, point)) {
if (!Judge(judge, point, w->data)) {
cout << w->data << "  ";
S.push(w->data);
judge[w->data] = w->data;
break;
}
}
}
else { S.pop(); }
}
}
//Depth first traversal preparation function
void DFSTraverse(int point) {
judge = new int[point + 1];
cout << "The order of depth traversal is as follows:" << endl;
for (int i = 0; i <= point; i++)
judge[i] = 0;
if (!Judge(judge, point, 1))DFS(1, point);
cout << endl;
}
void BFS(int point,int i){

Node* w = Array[i];
for (;; w = w->link) {
if (w != NULL) {
i = w->data;
if (!Judge(judges, point, i)) {
cout << i << "  "; judges[i] = i; Q.push(i);
}
}
else break;
}
Q.pop();
if(!Q.empty()){
i = Q.front();
BFS(point, i);
}
}
void BFSTraverse(int point) {
cout << "The order of breadth traversal is as follows:" << endl;
judges = new int[point + 1];
for (int x = 0;x <= point; x++)
judges[x] = 0;
BFS(point, 1);
}
//Main function
int main() {
int point, line;
cout << "Please enter the number of edges and points of the graph:" << endl;
cin >> line >> point;
Creat(line, point);
cout << "The default traversal order starts at point 1." << endl;
DFSTraverse(point);
BFSTraverse(point);
int pause;
cin >> pause;
}```

Topics: C++ Algorithm data structure