This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Depth-first search" – news ·newspapers ·books ·scholar ·JSTOR(July 2010) (Learn how and when to remove this message) |
![]() A tree labeled by the order in which DFS expands its nodes | |
Class | Search algorithm |
---|---|
Data structure | Graph |
Worst-caseperformance | for explicit graphs traversed without repetition, for implicit graphs with branching factorb searched to depthd |
Worst-casespace complexity | if entire graph is traversed without repetition, O(longest path length searched) =for implicit graphs without elimination of duplicate nodes |
Optimal | no (does not generally find shortest paths) |
Depth-first search (DFS) is analgorithm for traversing or searchingtree orgraph data structures. The algorithm starts at theroot node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. Extra memory, usually astack, is needed to keep track of the nodes discovered so far along a specified branch which helps in backtracking of the graph.
A version of depth-first search was investigated in the 19th century by French mathematicianCharles Pierre Trémaux[1] as a strategy forsolving mazes.[2][3]
Thetime andspace analysis of DFS differs according to its application area. In theoretical computer science, DFS is typically used to traverse an entire graph, and takes time,[4] where is the number ofvertices and the number ofedges. This is linear in the size of the graph. In these applications it also uses space in the worst case to store thestack of vertices on the current search path as well as the set of already-visited vertices. Thus, in this setting, the time and space bounds are the same as forbreadth-first search and the choice of which of these two algorithms to use depends less on their complexity and more on the different properties of the vertex orderings the two algorithms produce.
For applications of DFS in relation to specific domains, such as searching for solutions inartificial intelligence or web-crawling, the graph to be traversed is often either too large to visit in its entirety or infinite (DFS may suffer fromnon-termination). In such cases, search is only performed to alimited depth; due to limited resources, such as memory or disk space, one typically does not use data structures to keep track of the set of all previously visited vertices. When search is performed to a limited depth, the time is still linear in terms of the number of expanded vertices and edges (although this number is not the same as the size of the entire graph because some vertices may be searched more than once and others not at all) but the space complexity of this variant of DFS is only proportional to the depth limit, and as a result, is much smaller than the space needed for searching to the same depth using breadth-first search. For such applications, DFS also lends itself much better toheuristic methods for choosing a likely-looking branch. When an appropriate depth limit is not known a priori,iterative deepening depth-first search applies DFS repeatedly with a sequence of increasing limits. In the artificial intelligence mode of analysis, with abranching factor greater than one, iterative deepening increases the running time by only a constant factor over the case in which the correct depth limit is known due to the geometric growth of the number of nodes per level.
DFS may also be used to collect asample of graph nodes. However, incomplete DFS, similarly to incompleteBFS, isbiased towards nodes of highdegree.
For the following graph:
a depth-first search starting at the node A, assuming that the left edges in the shown graph are chosen before right edges, and assuming the search remembers previously visited nodes and will not repeat them (since this is a small graph), will visit the nodes in the following order: A, B, D, F, E, C, G. The edges traversed in this search form aTrémaux tree, a structure with important applications ingraph theory.Performing the same search without remembering previously visited nodes results in visiting the nodes in the order A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G.
Iterative deepening is one technique to avoid this infinite loop and would reach all nodes.
The result of a depth-first search of a graph can be conveniently described in terms of aspanning tree of the vertices reached during the search. Based on this spanning tree, the edges of the original graph can be divided into three classes:forward edges, which point from a node of the tree to one of its descendants,back edges, which point from a node to one of its ancestors, andcross edges, which do neither. Sometimestree edges, edges which belong to the spanning tree itself, are classified separately from forward edges. If the original graph is undirected then all of its edges are tree edges or back edges.
It is also possible to use depth-first search to linearly order the vertices of a graph or tree. There are four possible ways of doing this:
Forbinary trees there is additionallyin-ordering andreverse in-ordering.
For example, when searching the directed graph below beginning at node A, the sequence of traversals is either A B D B A C A or A C D C A B A (choosing to first visit B or C from A is up to the algorithm). Note that repeat visits in the form of backtracking to a node, to check if it has still unvisited neighbors, are included here (even if it is found to have none). Thus the possible preorderings are A B D C and A C D B, while the possible postorderings are D B C A and D C B A, and the possible reverse postorderings are A C B D and A B C D.
Reverse postordering produces atopological sorting of anydirected acyclic graph. This ordering is also useful incontrol-flow analysis as it often represents a natural linearization of the control flows. The graph above might represent the flow of control in the code fragment below, and it is natural to consider this code in the order A B C D or A C B D but not natural to use the order A B D C or A C D B.
if (A) then {B} else {C}D
A recursive implementation of DFS:[5]
procedure DFS(G,v)is labelv as discoveredfor all directed edges fromv tow that areinG.adjacentEdges(v)doif vertexw is not labeled as discoveredthen recursively call DFS(G,w)
A non-recursive implementation of DFS with worst-case space complexity, with the possibility of duplicate vertices on the stack:[6]
procedure DFS_iterative(G,v)is letS be a stackS.push(v)whileS is not emptydov =S.pop()ifv is not labeled as discoveredthen labelv as discoveredfor all edges fromv towinG.adjacentEdges(v)doS.push(w)
These two variations of DFS visit the neighbors of each vertex in the opposite order from each other: the first neighbor ofv visited by the recursive variation is the first one in the list of adjacent edges, while in the iterative variation the first visited neighbor is the last one in the list of adjacent edges. The recursive implementation will visit the nodes from the example graph in the following order: A, B, D, F, E, C, G. The non-recursive implementation will visit the nodes as: A, E, F, B, D, C, G.
The non-recursive implementation is similar tobreadth-first search but differs from it in two ways:
IfG is atree, replacing the queue of the breadth-first search algorithm with a stack will yield a depth-first search algorithm. For general graphs, replacing the stack of the iterative depth-first search implementation with a queue would also produce a breadth-first search algorithm, although a somewhat nonstandard one.[7]
Another possible implementation of iterative depth-first search uses a stack ofiterators of the list of neighbors of a node, instead of a stack of nodes. This yields the same traversal as recursive DFS.[8]
procedure DFS_iterative(G,v)is letS be a stack labelv as discoveredS.push(iterator ofG.adjacentEdges(v))whileS is not emptydoifS.peek().hasNext()thenw =S.peek().next()ifw is not labeled as discoveredthen labelw as discoveredS.push(iterator ofG.adjacentEdges(w))elseS.pop()
Algorithms that use depth-first search as a building block include:
Thecomputational complexity of DFS was investigated byJohn Reif. More precisely, given a graph, let be the ordering computed by the standard recursive DFS algorithm. This ordering is called the lexicographic depth-first search ordering. John Reif considered the complexity of computing the lexicographic depth-first search ordering, given a graph and a source. Adecision version of the problem (testing whether some vertexu occurs before some vertexv in this order) isP-complete,[12] meaning that it is "a nightmare forparallel processing".[13]: 189
A depth-first search ordering (not necessarily the lexicographic one), can be computed by a randomized parallel algorithm in the complexity classRNC.[14] As of 1997, it remained unknown whether a depth-first traversal could be constructed by a deterministic parallel algorithm, in the complexity classNC.[15]