Depth_first_search Depth_first_search

Depth first search - Definition

Related Words: Beat, Chase, Check, Comb, Dog, Dragnet, Examination, Exploration, Explore, Fan, Fathom

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. Intuitively, you start at the root (selecting some node as the root in the graph case) and explore as far as possible along each branch before backtracking.

Formally, 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 state is found, or it hits a node that has no children. Then the search backtracks and starts off on the next node.

All freshly expanded nodes are added to a LIFO queue (stack) for expansion.

  • DFS is complete: if the tree is finite, then a solution would be found if one exists.
  • DFS is not optimal, even with a finite tree and all non-negative path costs.

Space complexity of DFS is much lower compared to BFS (breadth-first search). It also lends itself much better to heuristic methods of choosing a likely-looking branch.

When searching large graphs that can not be fully contained in memory, DFS suffers from non-termination when the length of a path in the search tree is infinite. The simple solution of "remember which nodes I have already seen" doesn't work because there can be insufficient memory. This can be solved by maintaining an increasing limit on the depth of the tree, which is called iterative deepening depth-first search.

For the following graph:

Image:graph.traversal.example.png

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 result in the search visiting the nodes in the following order: A, B, D, F, E, C, G.

If the search did not remember previously visited nodes, but given the other conditions in the previous paragraph, the search would proceed as 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 right-to-left as above:

  • 0: A
  • 1: A (repeated), B, C, E

(Note that iterative deepening has now seen C, when a conventional depth-first search did not.)

  • 2: A, B, D, F, C, G, E, F

(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.)

  • 3: A, B, D, F, E, C, G, E, F, B

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.

Pseudocode

DFS(Start,Goal)

push(Stack,Start)
while(notEmpty(Stack))
  Node <- Pop(Stack)
  if (Node == Goal)
    return Node
  forall Child <- Expand(Node)
    push(Stack, Child)

External links

Example Usage of search

UFO_sighting: Google Blog search: Welcome to Mist Haley: Puzzle 026: UFO http://bit.ly/7rmd4g: Google Blog search: Welcome to.. http://bit.ly/5NM7sW
wichitalivenews: Police search For Missing Woman http://bit.ly/8DtM98
keralajobs12: seo expert needed by sonammag: I need my site to be in top ranking for Google search... I must have it guarante.. http://rly.cc/m9D5X
Copyright 2009 WordIQ.com - Privacy Policy  :: Terms of Use  :: Contact Us  :: About Us
This article is licensed under the GNU Free Documentation License. It uses material from the this Wikipedia article.