Depth-first search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root 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 a stack, is needed to keep track of the nodes discovered so far along a specified branch which helps in backtracking of the graph.

Depth-first search
Order in which the nodes are visited
ClassSearch algorithm
Data structureGraph
Worst-case performance for explicit graphs traversed without repetition, for implicit graphs with branching factor b searched to depth d
Worst-case space complexity if entire graph is traversed without repetition, O(longest path length searched) = for implicit graphs without elimination of duplicate nodes
Optimalno (does not generally find shortest paths)

A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre Trémaux as a strategy for solving mazes.

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.