In DFS the search tree is deepened as much as possible before going to the next sibling.
Depth-first search is easily implemented via a stack, including recursively (via the call stack).
Depth-first search visits every vertex once and checks every edge in the graph once. Therefore, DFS complexity is
It is mos space-efficient than breadth-first search because BFS keeps a priority queue of the entire frontier while DFS maintains a few pointers at each level.
Depth-first search is used in:
- Topological sorting.
- Scheduling problems.
- Cycle detection in graphs (e.g., spanning trees, mapping routes).
- Solving puzzles with only one solution (e.g., maze, sudoku).
Perform the following operations at each node:
- If the current node is empty then return.
- Execute the following three operations in a certain order:
- (N) Visit the current node.
- (L) Recursively traverse the current node's left subtree.
- (R) Recursively traverse the current node's right subtree.
To traverse arbitrary trees (not necessarily binary trees) with depth-first search, perform the following operations at each node:
- If the current node is empty then return.
- Visit the current node for pre-order traversal.
- For each i from 1 to the current node's number of subtrees - 1, or from the latter to the former for reversal traversal, do:
- Recursively traverse the current node's i-th subtree.
- Visit the current node for in-order traversal.
- Recursively traverse the current node's last subtree.
- Visit the current node for post-order traversal.
There are three common ways to traverse them in depth-first order: in-order, pre-order and post-order. Beyond these basic traversals, various more complex or hybrid schemes are possible, such as depth-limited searches like iterative deepening depth-first search.
Depth-first traversal (dotted path) of a binary tree:
- Pre-order (node visited at position red): F, B, A, D, C, E, G, I, H
- In-order (node visited at position green): A, B, C, D, E, F, G, H, I
- Post-order (node visited at position blue): A, C, E, D, B, H, I, G, F
- Visit current node
- Recursively traverse left subtree
- Recursively traverse right subtree
The pre-order traversal is a topologically sorted one, because a parent node is processed before any of its child nodes is done.
- Recursively traverse left subtree
- Recursively traverse right subtree
- Visit the current node
- Recursively traverse left subtree
- Visit the current node
- Recursively traverse right subtree
- Visit current node
- Recursively traverse right subtree
- Recursively traverse left subtree
- Recursively traverse right subtree
- Recursively traverse left subtree
- Visit the current node
- Recursively traverse right subtree
- Visit the current node
- Recursively traverse left subtree
In a binary search tree ordered such that in each node the key is greater than all keys in its left subtree and less than all keys in its right subtree, reverse in-order traversal retrieves the keys in descending sorted order.