Depth-first search and breadth-first search algorithm understanding

Source: Internet
Author: User

Depth-first search algorithm and breadth-first search algorithm are two interesting and useful algorithms in graph theory, so let's look at these two algorithms.

In strict book, the use of depth first search algorithm for graph traversal pseudo-code is as follows

1Boolean Visited[max];//Array of Flags2Status (*visitfunction) (intV);//Access Functions3  4 voidDfstraverse (Graph G, Status (*visit) (intv))5 {6Visitfunction =Visit;7      for(v =0; v < G.vexnum; ++V)//initializing an array of flags8VISITED[V] =false;9      for(v =0; v < G.vexnum; ++V)//call Dfs on a vertex that has not been visitedTen         if(!Visited[v]) One DFS (G, v); A } -   - voidDFS (Graph G,intV//Dfs from Vertex v the { -VISITED[V] =true; - visitfunction (v); -      for(w = Firstadjvex (G, v); W >=0; W =Nextadjvex (G, V, W)) +         if(!Visited[w]) -DFS (G, W);//recursive invocation of an inaccessible adjacency vertex w +}

A depth-first search and a breadth-first search require a record of vertex access, because the loops in the diagram cause trouble traversing the graph. To resolve this problem, you can use an visited array with an initial value of false, which is labeled as the corresponding subscript of the vertex in the corresponding storage structure, and when a vertex is accessed, the corresponding value in the visited array becomes true. In the algorithm given above, the 9th line of code is to solve the non-connected graph will appear in the depth of a vertex to search for incomplete. The depth-first search algorithm at the beginning of line 14 is based on recursion, and for the incoming graph G and a vertex v, when w = Firstadjvex (G, v) The resulting vertex W visited case is false, the Firstadjvex is always accessed, and when the visited[w] = The case of false indicates that the path from Vertex v starts each time the vertex formation from firstadjvex has been searched, and with the pop-up stack, the upper-level Nextadjvex begins to search for the next adjacency vertex, repeating the process, and the last V-connected graph can be traversed in depth first.

Corresponds to the above diagram, as it is connected graph, as long as the focus on the DFS algorithm section can. When the incoming parameter is G and V1, the visited condition that marks V1 first is true (hereafter referred to as V1 true,false the same), the v1,v1 of Access Firstadjvex is v2, so W = v2, because V2 is false, the DFS itself is called, Mark V2 to True, Access V2, next V4, V8, V5 the same, and V5 Firstadjvex for V2,v2 has been visited visited value for True,w to try to get V5 Nextadjvex, but V5 is not Nextadjvex, this call ends , return to the previous layer, the same as always return to V2,v2 has Nextadjvex V5, but it has been visited, and V2 no next Nextadjvex, continue to return to V1,v1 of Nextadjvex for V3, can access, V3 Firstadjvex V6 can access, In the same way always access to V7, at this time V7 and previous V5 status, so always return, V1 return after the end of the entire DFS function.

DFS Order v1 v2 v4 V8 V5 v3 V6 V7.

In strict book, the breadth-first traversal algorithm pseudo code is as follows:

1 voidBfstraverse (Graph G, Status (*visit) (intv))2 {3      for(v =0; v < G.vexnum; ++v)4VISITED[V] =false; 5 Initqueue (Q);6      for(v =0; v < G.vexnum; ++v)7         if(!Visited[v])8         {9VISITED[V] =true;Ten Visit (v); One EnQueue (Q, v); A              while(!Queueempty (Q)) -             { - DeQueue (Q, u); the                  for(w = Firstadjvex (G, u); W >=0; W =Nextadjvex (G, U, W)) -                     if(!Visited[w]) -                     { -VISITED[W] =true; + visit (w); - EnQueue (Q, W); +                     } A             } at         } -}

In order to achieve breadth-first search, using the data structure of the queue, the For loop on line 6th is to deal with the non-Unicom diagram, we take the top graph as an example, the loop will only go through once. Starting this loop with V1, first Mark v1 as true, Access V1, and then into the queue, at which point the queue element is <v1>,q not empty, out of the queue, and the queue element is assigned to u, the rear queue is <> The next for loop starts with the Firstadjvex of U, the V2, the V2, the V2, the V2 into the queue, the queue is <v2>, and the for loop of 15 rows, the Nextadjvex of U is assigned to W, the same, the tag v3, the Access v3, V3 into the queue, at this time the queue <v2, V3>, and then back to 15 lines, and v1 at this time there is no Nextadjvex, return 12 rows, the queue is not empty, v2 out of the queue assigned to U, at this time queue <v3>, as before, mark and access the queue V4, Mark and access the queue V5, return 12 rows, at this time the queue <v3, V4, v5>,v3 out the queue assigned to U, then mark and access the queue v6,v7, then go back to 12 lines, queue <v4, V5, V6, v7>,v4 out of the queue, mark and access the queue V8, Return 12 rows, at this time the queue <v5, V6, V7, v8>,v5 out of the queue assigned to U, because V5 Adjvex has been accessed, back to 12 lines, the same V8, V6, v7 out of the queue, the last queue is empty, function end.

BFS Sequence V1 v2 v3 v4 v5 V6 V7 V8

Depth-first search and breadth-first search algorithm understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.