Depth-first traversal of graphs and common extension Algorithms

Source: Internet
Author: User
Depth traversal is a basic DFS framework,

Note that 1. The graph may not be connected, and the traversal is complete. 2. An infinite loop may exist in a graph. (Avoid repeated access even if there is no loop) So you need to mark the accessed points with visit to avoid repeated access. 1 Void Travel (Graph & G ){
2 For ( Int I =   0 ; I < G. vertnum (); I ++ )
3 G. MARK [I] = Unvisited;
4 For ( Int I =   0 ; I < G. vernum (); I ++ ){
5 If (G. MARK [I] = Unvisited)
6 DFS (G, I );
7 }
8 }
9  
10 Void DFS (Graph & G, Int V ){
11 G. MARK [v] = Visited;
12 Previsit (G, V );
13
14 For (Edge e = G. firstedge (V); G. isedge (E); e = G. nextedge (E )){
15 If (G. MARK [G. tovertices (e)] = Unvisited)
16 DFS (G, G. tovertices (e ));
17 }
18
19 Postvisit (G, V );
20 }
21 The core of some extensions of the depth-first travel is actually the difference above. Algorithm Marked, pre-access, and post-access. Pre-access is performed before all sub-nodes of a node are given priority in depth, previst is the post-Order Access to postvist after all the sub-nodes of a node are given priority in depth. these two statuses are very important. Generally, the node status is Unvisited After the access in the previous order, it indicates that the node is Visited The status of the node, and the node can be marked Pushed Indicates that the node has traveled all its lower-level subnodes. In this way, we can differentiate the following two different situations: loop and non-ring repeated access. See when traversing the left graph and repeating access to 1, you need to know that the ring appears. When traversing the right image and repeatedly accessing 3, you need to know that it is not a ring. How can we differentiate them? Use different pre-order and post-order. Init v. Mark = unvisitedpre_vist (v) v. Mark = visiteddfs (G, v) v. Mark = pushed The ring appears because in node V, V is encountered when its lower-layer subnode is accessed in depth first. That is, the status of the second V is visited. The right figure is not a ring because 1-> 2> 3 and then 3 the depth of the traversal of its subnodes ends. At this time, we think it is in the pushed status after the sequential traversal, later 1-> 3 at this time, 3 is not visited, but pushed. ! The difference above is very important. Here are two examples. 1. Topology Sorting. If a ring is found, an error is returned and the system exits. If (V. Mark = visited) has a ring, exit pre_vist (v) v. Mark = visited DFS (G, v) // if it is not in the pushed status, you can continue with the in-depth priority access. For(Edge e=G. firstedge (V); G. isedge (E); e=G. nextedge (E )){ If(G. MARK [G. tovertices (E)]!=Pushed) DFS ( V. mark = pushed if a vertex is not pushed, it can be accessed in depth first, and the post-order access order is the reverse order of the topological sorting sequence. When the accessed vertex is marked as visited, it indicates that a ring exists and exits. 2. Print all simple paths with K lengths from node u to V in the graph. Pre_vist (v) v. Mark = visiteddfs (G, v )// For(Edge e=G. firstedge (V); G. isedge (E); e=G. nextedge (E )){ If(G. MARK [G. tovertices (E)]!=Visited) DFS () V. mark = unvisited. The idea is to perform in-depth traversal first. When a node is accessed in the current order, it is marked as Visit to avoid loops and endless loops, when the subnode of a point has an in-depth priority access, the status of the subnode is set to unvisited, indicating that the point can continue to be accessed, this is to run the 1-> 2-> 3 path in the figure above, and also allow 1-> 3, (if 3 has subnodes and Recursion goes deep 1-> 2-> 3>... And 1-> 3-> .....)

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.