Deep Search (DFS) Algorithm

Source: Internet
Author: User

As with the algorithm name, the search policy followed by deep-first search is to search for graphs as deep as possible. In the deep-first search, if the last vertex is found, if it has an edge not detected starting from this point, it will continue to the Chinese path along this edge. When all the edges of node v have been explored, the search will go back to the Start Node where node V has the edge. This process continues until all nodes that can be reached from the source node are found. If there are still undiscovered nodes, select one of them as the source node and repeat the above process. The whole process continues until all nodes are discovered.

Similar to the breadth-first search, whenever scanning has found the node u adjacent table to find the new node v, depth-first search will set V's advanced domain π [v] for u. Different from the width-first search, the first subgraph of the former forms a tree, and the first subgraph generated by the latter can be composed of several trees, because the search may be repeated by multiple source vertices. Therefore, the definition of the source subgraph of deep priority search is slightly different from that of width Priority Search:

G π = (V, E π), E π = {(π [v], v) ε E: v ∈ V ∧ π [v] ≠ nil}

The advanced subgraph of deep priority search forms a depth priority forest composed of several depth priority trees. Edges in E π are called branches.

Similar to width-first search, depth-first also colors nodes during the search process to indicate the node status. Each vertex starts to be white. When it is found in the search, it is set to Gray. when it ends, it is set to Black (that is, when its adjacent table is completely searched ). This technique ensures that each vertex search ends on only one depth-first tree, so these trees are separated.

In addition to creating a deep priority forest, the deep priority search also stamps each node. Each node V has two timestamps: When node v is first discovered (and dimmed), it records the first timestamp d [v]. when the inspection is completed on the adjacent Table V (the parallel V is black), record the second time cut f [v]. Timestamps are used in many graph algorithms, which are helpful for estimating the depth-first search.

In the following process, DFS records when node u is found in variable d [u] And when node u is retrieved in variable F [u. These timestamps are integers between 1 and 2 | v |, because each node in V corresponds to a discovery event and a completion event. For each vertex u

D [u] <F [u] (1)

At the moment D [u], the node u is white. At the moment D [u] and f [u] are gray, and then black.

The pseudocode below is a basic deep priority search algorithm. The input graph G can be a directed graph or an undirected graph. The variable time is a global variable used to record the timestamp.

Procedure DFS (g); begin1 for each vertex uε V [g] Do begin2 color [u] ← white; 3 π [u] ← nil; end; 4 time limit 0; 5 for each vertex u ε v [g] do6 If color [u] = white7 then dfs_visit (G, U); end; Procedure dfs_visit (G, U ); begin1 color [u] ← gray; △white node u has been found 2 D [u] ← time + 1; 3 For each vertex v ε adj [u] Do △exploring edge (u, v) 4 If color [v] = white then begin5 π [v] ← U; 6 dfs_visit (G, V); end; 7 Color [u] finished black; Delta completed rear U is black 8 F [u] elapsed time + 1; end;

Figure 2 illustrates how DFS is executed on the graph shown in Figure 1. The edges explored by the algorithm are either shadow covered (if the edges are branches) or dotted lines (in other cases ). For non-branching edges, indicate B (or F) respectively to indicate the reverse side, cross side, or undirected side. We stamp the node with the timestamp in the form of Z completion time.

Figure 1 Directed Graph

Figure 2 execution process of deep Priority Search Algorithm DFS on Directed Graph 1

The procedure DFS is executed as follows. Line 1-3 sets all nodes to white, and all π fields are initialized to nil. Reset the global variable time in row 4th, and search the nodes in Column V in sequence in line 5-7. When a white knot is found, call dfs_visit to access the node. Each time you call dfs_visit through row 3, node u becomes the root of a new tree in the depth-first forest. When DFS returns, each node u corresponds to a time point D [u] and a time point F [u].

When dfs_visit (u) is called each time, the node u is white, the U is set to gray in row 1st, and the value-added global time variable 2nd is added to d [u, in this way, the detection time d [u] is recorded. Line 3-6 checks each vertex v adjacent to u. If V is a white node, recursive access is performed to the node v. When considering each node in the 3rd rows of statements, we can say that the side (u, v) is searched for in depth first. Finally, when all edges starting with u are explored, the 7-8 line statement sets u to black and records the completion time f [u].

What is the complexity of the algorithm DFS runtime? The cycle time of rows 1-2 and 5-7 in DFS is O (V), which does not include the time spent in executing the dfs_visit process statement. In fact, the dfs_visit process is called only once for each vertex v, because dfs_visit is only applicable to white nodes and the first step in the process is to set the nodes to gray, in dfs_visit (V) during execution, the loop in line 3-6 will be executed | adj [v] | times. Because Σ v ε v | adj [v] | = θ (E), the entire time occupied by the statements in rows 2-5 of dfs_visit during execution should be θ (e ). So the running time of DFS is θ (V + E ).

Nature of deep Priority Search

You can obtain a large amount of information about the structure of the graph based on the depth-first search. Perhaps the most basic feature of deep priority search is its parent subgraph g, forming a forest composed of trees, this is because the structure of the deep priority tree accurately reflects the structure of recursive calls in dfs_visit, that is, u = π [v] When and only when the dfs_visit (v) process is called in the process of searching the U's adjacent table ).

Figure 3 nature of deep Priority Search

Another important feature of deep priority search is that the discovery and completion time have a bracket structure. If we use the left bracket (u) To represent the discovery vertex u, And the right bracket (u) To complete the search) the discovery and completion records are a perfect expression when the brackets are correctly applied. For example, Figure 3 shows the nature of deep priority search. (A) Results of in-depth priority search for a directed graph. The timestamp and edge type of the node are displayed in the same way as Figure 2. (B) The parentheses in the figure indicate the intervals that correspond to the discovery time and completion time of each node. Each rectangle spans the time range set for finding and completing the corresponding node. The graph also shows the branches. If two intervals overlap, one interval must be nested in another interval, and the node corresponding to the cell is the descendant of the node corresponding to the larger range. (C) Re-Describe the graph in (a) so that all branches and forward edges in the depth tree are top-down, while all reverse edges direct from the descendant to the ancestor from the bottom-up. The Theorem below provides another way to mark the structure of brackets.

Theorem 1 parentheses Theorem

In any depth-first search of directed graph or undirected graph G = (V, E), for any two nodes U and V in the graph, one of the following three conditions is true:

  • The interval [d [u], F [u] and interval [d [v], F [v] are completely separated;
  • The range [d [u], F [u] is completely contained in the range [d [v], F [v] and in the depth priority tree, U is the descendant of V;
  • The range [d [v], F [v] is completely contained in the range [d [u], F [u] and in the depth priority tree, V is the descendant of U.

Proof:

First, we will discuss the situation of d [u] <D [v]. Based on whether d [v] is smaller than F [u], there are two possible situations: the first case, if d [v] <F [u] is found, the U node is still gray, which indicates that V is the descendant of U. Furthermore, because node v is found later than node u, all the edges starting from node v have been explored and completed before the node u is returned and completed, therefore, in this condition, the range [d [v], F [v] must be completely included in the range [d [u], F [u]. In the second case, if f [u] <D [v], then according to the inequality (1), interval [d [u], f [u] and [d [v], F [v] must be separated.

In the case of D [v] <D [u], it is similar to verifiable. You only need to check U and V in the above proof. (Certificate completed)

Inference 1 embedding the descendant Interval

In the depth-first forest with directed or undirected graph G, nodule V is the descendant of node u, and only if d [u] <D [v] <F [v] <F [u].

Proof:It is directly derived from Theorem 1. (Certificate completed)

If a node is the descendant of another node in the deep priority forest, the following theorem indicates another important feature.

Theorem 2 white path Theorem

In an undirected or undirected graph G = (V, E) depth-first forest, node V is the descendant of node u. If you only search for the moment when you find u, d [u], starting from node u, a path consisting of only white nodes can reach v.

Proof:

→: Assume that V is the descendant of U, and W is any node on the channel between U and V in the deep priority tree, W must be the descendant of U, from inference 1, we can see that D [u] <D [w]. Therefore, W should be white at moment D [u.

Direction: In the moment D [u], there is a path from u to V consisting of only white nodes, but in the depth priority tree V has not become the descendant of U. Without losing its universality, we assume that other vertices on the path are the descendant of U (otherwise, V is the closest node to u in the path and not the descendant of U ), set W to the ancestor of V on the link so that W is the descendant of U (in fact, W and u can be the same node ). According to the inference 1, F [w] is less than or equal to f [u]. Because V ε adj [w], the call to dfs_visit (W) is guaranteed to be completed before W, therefore, F [v] <F [w] ≤ f [u]. Because d [u] node v is white at the moment, d [u] <D [v] is available. From inference 1, we can see that V must be the descendant of U in the depth priority tree. (Certificate completed)

Edge Classification

Another interesting feature of Deep-priority search is that you can classify the edges of the input graph G = (V, E) by searching, this classification can find a lot of important information about the graph. For example, a directed graph has no loop. if and only when no reverse edge is found in the depth-first search ".

Based on the depth priority forest G produced by deep priority search on graph G, we can divide the edge of the graph into four types.

  1. Branches are the edges in the depth-first Forest g π. If node v is first discovered when exploring the edge (u, v), the side (u, v) is a branch.
  2. The reverse edge is the side connecting the node u to its ancestor V in the depth priority tree, and the ring is also considered as the reverse side.
  3. Forward edge refers to the non-branches edge connecting the vertex u to its descendant in the depth priority tree.
  4. Cross-edge refers to all other types of edges. They can be connected to two nodes in the same depth priority tree, as long as one node is not the ancestor of another node, you can also link the nodes that belong to the two deep priority trees.

In Figure 2 and Figure 3, the edge type is marked. Figure 3 (c) also illustrates how to redraw the graph in Figure 3 (a) so that the branches and forward edges in the depth-first tree are drawn downward, so that the reverse edges are drawn upward. Any image can be redrawn in this way.
You can modify the algorithm DFS to classify the algorithm when edges are encountered. The core idea of the algorithm is that the edge (u, v) can be mapped based on the color of the node v reached by the first edge to be searched) classification (but the forward and cross sides cannot be distinguished by color ).

  1. White indicates that it is a branch.
  2. Gray indicates that it is a reverse edge.
  3. Black indicates that it is a forward or cross edge.

The first case can be inferred by the algorithm. In the second case, we can find that the gray node always forms a descendant linear chain corresponding to the active dfs_visit call stack, the number of gray nodes equals to the depth of the recently discovered nodes plus 1 in the depth-first forest. The number of gray nodes always starts from the deepest depth, therefore, reaching the edge of another gray node must be its ancestor. The third scenario is possible. If D [u] <D [v], the side (u, v) is the forward side, if d [u]> d [v], (u, v) is the cross edge.

Therefore, we can draw the following conclusion: For an edge (u, v ),

  • If and only when d [u] <D [v] <F [v] <F [u], it is a branch edge or positive edge;
  • It is a reverse edge only when d [v] <D [u] <F [u] <F [v;
  • It is a cross edge only when d [v] <F [v] <D [u] <F [u;

This conclusion proves slightly.

In an undirected graph, because (u, v) and (V, u) are actually the same side, this classification of edges may produce ambiguity. In this case, the edges of the graph are classified as the first type in the classification table. Correspondingly, the first edge we encounter during Algorithm Execution is (u, v) or (v, u) to classify it.

Next, we will show that the positive side and cross edge will not appear in the deep-first search of undirected graphs.

Theorem 3

In the deep-first search of undirected graph G, each edge of G is either a reverse edge of the tree.

Proof:

Set (u, v) to any side of G without losing its universality. Assume that D [u] <D [v]. Because V is in the U's adjacent table, we must have discovered and completed V before completing U. If the edge (u, v) is first explored from u to V, then (u, v) must be a branch. If (u, v) is first explored in the direction from V to U, the U is still a gray node because the side is first explored, so (u, v) is a reverse edge. (Certificate completed)

 

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.