Traversal of Graphs-breadth-first Traversal and Depth-first Traversal

Keywords: C++

Traversal of graphs: Starting from any vertex of a graph, all vertices in a graph can be accessed once and only once in a certain order. Traversing frequently

Two methods are used: breadth-first traversal and depth-first traversal.

Width-first traversal: Similar to the hierarchical traversal of trees. Let's assume that starting from a vertex v in the graph, we access each unvisited vertex of V in turn after visiting v.

The adjacent points are accessed in turn from these adjacent points, and the "adjacent points of the first visited vertex" is preceded by "the next visited vertex".

The vertex's adjacent points are accessed until all the vertex's adjacent points in the graph are accessed. If there are still vertices in the graph that are not accessed at this time, select another graph.

A vertex that has not been accessed is used as the starting point and the process is repeated until all vertices in the graph are accessed.

Depth-first traversal: Similar to root traversal of trees, it is a generalization of root traversal of trees. Assuming that the initial state is that all vertices in the graph have not been accessed, the depth

Priority traversal starts from a vertex v in the graph, accesses the vertex, and then proceeds from the unaccounted adjacent point of V to the depth-first traversal graph until all the vertices in the graph are accessed.

The vertices of path-wise and v are accessed; if there are still vertices in the graph that have not been accessed at this time, another vertex in the graph that has not been accessed is selected as the starting point and repeated.

The above procedure lasts until all vertices in the graph are accessed.

The following two traversal methods are implemented in code:

#include <iostream>
#include <queue> using namespace std; #define SIZE 10 struct Edge { Edge(int v):destvalue(v),link(NULL){} int destvalue; Edge *link; }; struct Vertex { Vertex():list(NULL){} char data; Edge *list; }; class GraphLink { public: GraphLink() { MaxVertex = SIZE; NumVertex = NumEdge = 0; VertexTable = new Vertex[MaxVertex]; } void InsertVertex(char v) { if(NumVertex >= MaxVertex) return; VertexTable[NumVertex++].data = v; } int GetVertexI(char v) { for(int i = 0;i<NumVertex;i++) { if(VertexTable[i].data == v) return i; } return -1; } void InsertEdge(char v1,char v2) { int p1 = GetVertexI(v1); int p2 = GetVertexI(v2); if(p1 == -1 || p2 == -1) return; Edge *ed = new Edge(p2); ed->link = VertexTable[p1].list; VertexTable[p1].list = ed; ed = new Edge(p1); ed->link = VertexTable[p2].list; VertexTable[p2].list = ed; NumEdge++; } void Show() { Edge *ed = NULL; for(int i = 0;i<NumVertex;i++) { cout<<i<<":"<<VertexTable[i].data<<"->"; ed = VertexTable[i].list; while(ed) { cout<<ed->destvalue<<"->"; ed = ed->link; } cout<<"nul"<<endl; } } void BFS(char value) { queue<int> q; int v = GetVertexI(value); bool *visited = new bool[NumVertex]; Edge *ed = NULL; int w; for(int i = 0;i<NumVertex;i++) visited[i] = false; if(v == -1) return; cout<<value<<" "; visited[v] = true; q.push(v); while(!q.empty()) { v = q.front(); q.pop(); ed = VertexTable[v].list; while(ed) { w = ed->destvalue; if(!visited[w]) { cout<<VertexTable[w].data<<" "; q.push(w); visited[w] = true; } ed = ed->link; } } } void DFS(char v) { bool *visited = new bool[NumVertex]; for(int i = 0;i<NumVertex;i++) visited[i] = false; DFS(GetVertexI(v),visited); delete [] visited; visited = NULL; } char GetVertex(int v) { return VertexTable[v].data; } void DFS(int v,bool *visited) { cout<<GetVertex(v)<<" "; visited[v] = true; int w = GetFirstNeighbor(v); while(w != -1) { if(!visited[w]) { DFS(w,visited); } w = GetNextNeighbor(v,w); } } int GetFirstNeighbor(int v) //v First adjacent vertex { if(v == -1) return -1; Edge *p = VertexTable[v].list; if(p) return p->destvalue; return -1; } int GetNextNeighbor(int v,int w) //v The adjacent vertex of w Next adjacent vertex { if(v == -1 || w== -1) return -1; Edge *p = VertexTable[v].list; while(p && p->destvalue != w) p = p->link; if(p && p->link) return p->link->destvalue; return -1; } private: int MaxVertex; int NumVertex; int NumEdge; Vertex *VertexTable; };

In the process of code implementation, the queue in STL is used to store the code using the structure of adjacent table. First, the structure of edge is defined.

The value of the subscript of the vertex in the array and a linked list pointer to the next adjacent vertex are defined. Then the structure of the vertex is defined, which includes the data of the vertex and the data of the vertex.

A list of edges. Then the class of graph is defined, which includes the maximum number of vertices, the number of current vertices, the number of current edges and the type of vertices.

Arrays are implemented by (1) constructors: initialization of defined data. (2) Insert vertex function. (3) Get the vertex position information function.

(4) Functions that insert edges. (5) Functions that display information about constructed graphs. (6) breadth-first ergodic function. (7) Depth-first ergodic function. (8) get

Functions to vertex data. (9) The function of the first adjacent vertex of vertex v is obtained. (10) Get the function of the next adjacent vertex of vertex v.

Posted by webwired on Fri, 17 May 2019 19:12:57 -0700