Train of thought:
The non recursive BFS of graph is realized by queue. First push the traversal start vertex into the queue; then every time pop a vertex from the queue, traverse the vertex, and traverse the edge table to find the vertex connected with the vertex. If the connected vertex is not accessed, enter the queue. If the vertex has been accessed, continue until the queue is empty and the depth traversal ends.
Note: in depth traversal graph, the precondition for all vertices to be accessed is that the graph is connected.
Code:
#include <iostream>
#include<vector>
#include<queue>
using namespace std;
typedef int T; //Vertex value type int, char, double and so on
struct Node
{
T value; //Vertex value
int numbers; //Vertex number
bool operator==(const Node &other)
{
if((this->numbers==other.numbers)&&(this->value==other.value))
return true;
else
return false;
}
Node(int a1=0,int a2=0)
{
numbers=a1;
value=a2;
}
};
struct Edge
{
int num1; //Initial vertex
int num2; //Terminating vertices
int weight; //Weight corresponding to edge
};
class Graph
{
private:
int vertex_nums; //Vertex number
int edge_nums; //Edge number Ss
vector<Node> vertex; //Vertex table, forced to number from 0
vector<Edge> edge; //Side table
vector<vector<int> > edge_maze; //Adjacency matrix to store the connection relationship of each vertex
public:
Graph(int n1=10,int n2=10)
{
vertex_nums=n1;
edge_nums=n2;
for(int i=0;i<vertex_nums;i++)
{
Node a;
a.numbers=i;
a.value=0;
vertex.push_back(a);
}
for(int i=0;i<vertex_nums;i++) //Initialization of adjacency matrix
{
vector<int> temp(vertex_nums,0);
edge_maze.push_back(temp);
}
cout<<"please input the edges about the graph: vertex_number1 vertex_number2 weight"<<endl;
int x,y,z;
for(int i=0;i<edge_nums;i++)
{
cin>>x>>y>>z;
Edge temp_edge;
temp_edge.num1=x;
temp_edge.num2=y;
temp_edge.weight=z;
edge.push_back(temp_edge);
edge_maze[x][y]=1;
edge_maze[y][x]=1;
}
}
void print_edge_maze()
{
cout<<endl;
cout<<"Output adjacency matrix"<<endl;
cout<<" ";
for(int i=0;i<vertex_nums;i++)
cout<<"v"<<i<<" ";
cout<<endl;
for(int i=0;i<vertex_nums;i++)
{
cout<<"v"<<i<<" ";
for(int j=0;j<vertex_nums;j++)
{
cout<<edge_maze[i][j]<<" ";
}
cout<<endl;
}
}
void print_edge_vec()
{
cout<<endl;
cout<<"Output side table:"<<endl;
cout<<"Initial vertex-->End vertex edge weight"<<endl;
for(int i=0;i<edge_nums;i++)
{
cout<<" v"<<edge[i].num1<<" -->"<<" v"<<edge[i].num2<<" "<<edge[i].weight<<endl;
}
}
//Non recursive ergodic graph based on BFS
void BFS_Graph_nonrecursive(int start_vertex_num=0)
{
vector<int> if_output(vertex_nums,0); //Indicates whether the vertex is traversed
Node first(start_vertex_num);
queue<Node> qu;
qu.push(first);
if_output[start_vertex_num]=1; //To join a team means to be traversed
cout<<endl;
cout<<"Traverse the vertex path as follows"<<endl;
while(!qu.empty())
{
Node temp=qu.front();
qu.pop();
cout<<"v"<<temp.numbers<<"---->";
//Traverse all the edges, find the vertex connected with the queue head node, and add all the vertices connected with the queue head node to the queue
for(int i=0;i<edge_nums;i++)
{
if(edge[i].num1==temp.numbers)
{
if(if_output[edge[i].num2]==0) //Not traversed
{
Node temp_node(edge[i].num2); //Queue connection points
qu.push(temp_node);
if_output[edge[i].num2]=1; //To join a team means to be traversed
}
}
else if(edge[i].num2==temp.numbers)
{
if(if_output[edge[i].num1]==0) //Not traversed
{
Node temp_node(edge[i].num1); //Queue connection points
qu.push(temp_node);
if_output[edge[i].num1]=1;
}
}
else
continue;
}
}
cout<<"end"<<endl;
cout<<"Traversal end"<<endl;
}
};
int main()
{
Graph graph(8,10);
graph.print_edge_vec();
graph.print_edge_maze();
graph.BFS_Graph_nonrecursive();
return 0;
}
Output:
please input the edges about the graph: vertex_number1 vertex_number2 weight
0 1 0
1 2 1
2 3 2
3 4 3
4 5 4
5 6 5
6 7 6
7 3 7
7 4 8
6 4 9
Output side table:
Initial vertex-->End vertex Edge weight
v0 --> v1 0
v1 --> v2 1
v2 --> v3 2
v3 --> v4 3
v4 --> v5 4
v5 --> v6 5
v6 --> v7 6
v7 --> v3 7
v7 --> v4 8
v6 --> v4 9
Output adjacency matrix
v0 v1 v2 v3 v4 v5 v6 v7
v0 0 1 0 0 0 0 0 0
v1 1 0 1 0 0 0 0 0
v2 0 1 0 1 0 0 0 0
v3 0 0 1 0 1 0 0 1
v4 0 0 0 1 0 1 1 1
v5 0 0 0 0 1 0 1 0
v6 0 0 0 0 1 1 0 1
v7 0 0 0 1 1 0 1 0
Traverse the vertex path as follows
v0---->v1---->v2---->v3---->v4---->v7---->v5---->v6---->end
Traversal end