| advertise add site services publishers database health videos | ![]() | about toolbar stats live show health store more stuff JOIN/LOGIN |
Extra Depth Shoes | Deep Depth Shoes drewshoe.com | Wilderness Medical Associates - Wilderness EMT - Outdoor First Aid -... wildmed.com |
Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.
[edit] Formal definitionFormally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hasn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a stack for exploration. The time and space 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 O(|V| + |E|), linear in the size of the graph. In these applications it also uses space O(|V|) in the worst case to store the stack 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 for breadth 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 to search problems in artificial intelligence, however, the graph to be searched is often either too large to visit in its entirety or even infinite, and DFS may suffer from non-termination when the length of a path in the search tree is infinite. Therefore, the search is only performed to a limited depth, and due to limited memory availability one typically does not use data structures that keep track of the set of all previously visited vertices. In this case, the time is still linear in 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, 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 to heuristic methods of 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 a branching 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. For the following graph: a depth-first search starting at 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. Performing the same search without remembering previously visited nodes results in visiting 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 prevents this loop and will reach the following nodes on the following depths, assuming it proceeds left-to-right as above:
(Note that iterative deepening has now seen C, when a conventional depth-first search did not.)
(Note that it still sees C, but that it came later. Also note that it sees E via a different path, and loops back to F twice.)
For this graph, as more depth is added, the two cycles "ABFE" and "AEFB" will simply get longer before the algorithm gives up and tries another branch. [edit] Output of a depth-first searchThe most natural result of a depth first search of a graph (if it is considered as a function rather than a procedure) is a spanning 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, and cross edges, which do neither. Sometimes tree edges, edges which belong to the spanning tree itself, are classified separately from forward edges. It can be shown that if the graph is undirected then all of its edges are tree edges or back edges. [edit] Vertex orderingsIt is also possible to use the depth-first search to linearly order the vertices of the original graph (or tree). There are three common ways of doing this:
if (A) then { B } else { C } D
[edit] Implementation in PythonA recursive version of the algorithm: def dfs(v, visited = None, preorder_process = lambda x: None, postorder_process = lambda x: None): if visited is None: visited = set() visited.add(v) preorder_process(v) for neighbor in v.neighbors: if neighbor not in visited: dfs(neighbor, visited, preorder_process, postorder_process) postorder_process(v) Another version, without the recursion: def dfs(root, visited = None, preorder_process = lambda x: None): """ Given a starting vertex, root, do a depth-first search. """ from collections import deque to_visit = deque() if visited is None: visited = set() to_visit.append(root) # Start with root while len(to_visit) != 0: v = to_visit.pop() if v not in visited: visited.add(v) preorder_process(v) to_visit.extend(v.neighbors) [edit] ApplicationsAlgorithms where DFS is used:
[edit] References
[edit] External links
| |||||||||||||
| ↑ top of page ↑ | about thumbshots |