Data Structure: Graph traversal-depth first, breadth first, and breadth
Graph traversal: depth first and breadth first
Traversal
Graph traversal refers to accessing each vertex in the graph based on a certain policy. Of course, each vertex can be accessed only once.
In graph traversal, depth first and breadth first are the two most commonly used traversal methods. These two traversal methods are applicable to undirected graphs and directed graphs, and all of them start to traverse from the specified vertex. First, let's look at the traversal rules of the two traversal methods:
Depth first
Depth-First traversal is also called Depth First Search ). Its traversal rules: Constantly traverse along the depth of the vertex. The depth direction of a vertex is its adjacent contact direction.
For details, if a graph G = <V, E> is given and visited [I] is used to indicate access to vertex I, all the visited [I] values are false in the initial condition. Assume that the traversal starts from vertex V0, then the vertex of the next traversal is V0.FirstNext, traverseFirstAdjacent contact Vj ,...... Until all vertices are accessed.
The so-calledFirstIt refers to the storage structure (the adjacent matrix and the adjacent table) Where all the adjacent points store the nearest position. It usually refers to the smallest subscript. In the traversal process, there are two situations that often occur.
Let's look at an example.
Traverse analysis from V0: V0 has two adjacent contacts V1 and V2, and V1 traversal with the smallest subscript is selected. Then we start in-depth traversal from V1. V1 only has adjacent nodes V3, which is not selected: traverse V3. Next, traverse from V3. V3 only has the adjacent V0, and V0 has been traversed. In this case, first, we start to backtrack V1, V1 without any adjacent contacts that are not traversed, and then backtrack V0. V0 has an untraversed adjacent contact V2, A new round of in-depth traversal starts with V2. V2 has no adjacent contacts and cannot be traced back. In this case, Case 2: Check visited [I], and only V4 is available. Deep traversal is complete. You can think of stack. The traversal sequence is V0-> V1-> V3-> V2-> V4. The depth-first traversal Sequence starting from other vertices is V1-> V3-> V0-> V2-> V4. V2-> V0-> V1-> V3-> V4.
V3> V0> V1> V2> V4.
V4-> V2-> V0-> V1-> V3.
The above results will be used to test the program later.
Combined with the implementation of the graph: the code in the adjacent matrix, we can see the depth traversal algorithm of the graph in the form of the adjacent matrix: Depth priority code
/* The depth-first search starts to traverse from vertex. visit is the function pointer to traverse vertices */void Graph: dfs (int vertex, void (* visit) (int )) {stack <int> s; // visited [I] indicates whether vertex I has been accessed by bool * visited = new bool [numV]; // count is used to count the number of vertices that have been traversed. int I, count; for (I = 0; I <numV; I ++) visited [I] = false; count = 0; while (count <numV) {visit (vertex); visited [vertex] = true; s. push (vertex); count ++; if (count = numV) break; while (visited [vertex]) {for (I = 0; I <numV & (visited [I] | matrix [vertex] [I] = 0 | matrix [vertex] [I] = MAXWEIGHT); I ++ ); if (I = numV) // All adjacent points of the current vertex have been accessed {if (! S. empty () {s. pop (); // at this time, vertex is the top of the stack. The first out of the stack if (! S. empty () {vertex = s. top (); s. pop ();} else // If the stack is empty, search for new and unaccessed vertices from the beginning {for (vertex = 0; vertex <numV & visited [vertex]; vertex ++) ;}} else // If the stack is empty, search for new and unaccessed vertices from the beginning {for (vertex = 0; vertex <numV & visited [vertex]; vertex ++ );}} else // to find a new vertex, update the currently accessed vertex vertexvertex = I ;}} delete [] visited ;}If you have already seen other code before, you will not be given it. The following figure shows the breadth traversal. Tests on Deep traversal and breadth traversal will be provided later.
Breadth-First-Search ). Its traversal rules: for specific points, a graph G = <V, E> is given, and visited [I] is used to indicate the access to vertex I, in the initial situation, all the visited [I] values are false. Assume that the traversal starts from vertex V0, and the adjacent vertex table of vertex V0 ranges from small to large Vi and Vj... Vk. Follow Rule 1 and traverse Vi, Vj, and Vk. Then, according to Rule 2, we should traverse all adjacent contacts of Vi, followed by all adjacent contacts of Vj,..., and finally all adjacent contacts of Vk. The next step is the recursive process... in the process of wide traversal, the graph is not connected. In this case, we need to test visited [I].... In the above process, we can see that the queue is used.
For example, the same image is used:
Traverse analysis from V0: V0 has two adjacent contacts V1 and V2, so it traverses V1 and V2 in order. V1 is accessed prior to V2, so the adjacent contact of V1 is accessed prior to that of V2, that is, access to V3. V2 has no adjacent contacts. You can only view the adjacent contacts of V3. V0 has been accessed. In this case, you need to check visited [I]. Only V4 is available. The breadth traversal is complete.
The traversal sequence is
V0-> V1-> V2-> V3-> V4. The extended traversal Sequence starting from other vertices is V1-> V3-> V0-> V2-> V4. V2-> V0-> V1-> V3-> V4.
V3> V0> V1> V2> V4.
V4-> V2-> V0-> V1-> V3.
The above results are also used for testing programs.
In the adjacent matrix, the image's breadth traversal algorithm's breadth-first code
/* The breadth-first search starts to traverse from vertex. visit is the function pointer to traverse vertices */void Graph: bfs (int vertex, void (* visit) (int )) {// use the queue <int> q; // visited [I] to mark whether vertex I has been accessed by bool * visited = new bool [numV]; // count is used to count the number of vertices that have been traversed. int I, count; for (I = 0; I <numV; I ++) visited [I] = false; q. push (vertex); visit (vertex); visited [vertex] = true; count = 1; while (count <numV) {if (! Q. empty () {vertex = q. front (); q. pop () ;}else {for (vertex = 0; vertex <numV & visited [vertex]; vertex ++); visit (vertex); visited [vertex] = true; count ++; if (count = numV) return; q. push (vertex) ;}// the code goes here. vertex is the accessed vertex for (int I = 0; I <numV; I ++) {if (! Visited [I] & matrix [vertex] [I]> 0 & matrix [vertex] [I] <MAXWEIGHT) {visit (I); visited [I] = true; count ++; if (count = numV) return; q. push (I) ;}} delete [] visited ;}
Combined with the two traversal codes, we test the same image. Its main function is
Void visit (int vertex) {cout <setw (4) <vertex;} int main () {cout <"****** graph traversal: depth-first and breadth-first *** by David *** "<endl; bool isDirected, isWeighted; int numV; cout <" "<endl; cout <"Number of input vertices"; cin> numV; cout <"Whether the edge has a weight value, 0 (without) or 1 ()"; cin> isWeighted; cout <"whether it is a directed Graph, 0 (undirected) or 1 (directed)"; cin> isDirected; graph Graph (numV, isWeighted, isDirected ); cout <"this is a"; isDirected? Cout <",": cout <","; isWeighted? Cout <" 图" <endl: cout <" 图" <endl; graph. createGraph (); cout <"Print adjacent matrix" <endl; graph. printAdjacentMatrix (); cout <endl; cout <"Deep traversal" <endl; for (int I = 0; I <numV; I ++) {graph. dfs (I, visit); cout <endl ;}cout <endl; cout <"span traversal" <endl; for (int I = 0; I <numV; I ++) {graph. bfs (I, visit); cout <endl;} system ("pause"); return 0 ;}
Run
Check the test results carefully and make sure that our code is correct.
Download complete code: Graph traversal: depth first, breadth first
Summary for an image, the sequence of depth-first traversal and breadth-first traversal is not unique, but when the storage structure of the image is determined, its traversal sequence is unique. Because when there are multiple candidate points, we always give priority to the smallest subscript.
Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/38323633
If this is helpful, try again!
Column Directory: Data Structure and algorithm directory
Data Structure: Image breadth-first traversal and depth-first Traversal
Depth traversal: 1-> 2-> 4-> 6-> 5-> 3
Traverse the image breadth first: 1-> 2-> 3-> 4-> 5-> 6
Someone else asked this question ....
Data Structure: depth-first traversal and breadth-first traversal of Graphs
Depth traversal: 1-> 2-> 4-> 6-> 5-> 3
Traverse the image breadth first: 1-> 2-> 3-> 4-> 5-> 6