Depth-First Search (DFS)
Prerequisites
- State Space Representation — Frontier, expanding nodes
- BFS — Comparison with level-by-level strategy
🎯 Learning Objectives
- Implement DFS using a LIFO stack or recursion
- Trace DFS execution step-by-step on example graphs
- Understand DFS is not complete (in infinite/cyclic spaces) and not optimal
- Analyze time O(bm) and the key advantage: space O(bm)
- Describe the backtracking search variant
1. Intuition
DFS dives as deep as possible along each branch before backtracking. Think of exploring a maze by always taking the leftmost path until you hit a dead end, then going back to the last junction and trying the next path.
DFS uses a LIFO stack (Last-In, First-Out) for the frontier. The most recently generated node is expanded first, driving the search deeper into the tree.
2. DFS Algorithm
Iterative Version (Stack)
function DFS(problem):
frontier = Stack(Node(problem.INITIAL-STATE))
explored = {}
loop:
if frontier is empty: return FAILURE
node = frontier.POP()
if problem.GOAL-TEST(node.STATE): return SOLUTION(node)
add node.STATE to explored
for each child in EXPAND(problem, node):
if child.STATE not in explored:
frontier.PUSH(child)
Recursive Version
function RECURSIVE-DFS(node, problem, explored):
if problem.GOAL-TEST(node.STATE): return SOLUTION(node)
add node.STATE to explored
for each child in EXPAND(problem, node):
if child.STATE not in explored:
result = RECURSIVE-DFS(child, problem, explored)
if result ≠ FAILURE: return result
return FAILURE
The recursive version uses the call stack as the implicit frontier. Both approaches are equivalent in behavior. The recursive version is more elegant; the iterative version avoids stack overflow for very deep searches.
3. Step-by-Step Trace
Same graph as BFS example:
S
/ \
A B
/ \ \
C D E
|
G (goal)
Children generated left-to-right: A before B, C before D.
| Step | Pop (Expand) | Stack (top → bottom) | Explored |
|---|---|---|---|
| 0 | — | [S] | {} |
| 1 | S | [B, A] | {S} |
| 2 | A | [B, D, C] | {S, A} |
| 3 | C | [B, D] (C has no children) | {S, A, C} |
| 4 | D | [B, G] | {S, A, C, D} |
| 5 | G | GOAL TEST → SUCCESS! | {S, A, C, D, G} |
Solution: S → A → D → G (cost = 3). In this case, DFS found the same solution as BFS. But this is not guaranteed in general!
4. Properties of DFS
| Property | Value | Explanation |
|---|---|---|
| Complete? | No (tree search) | DFS can get stuck in infinite branches or cycles. Graph search DFS is complete for finite state spaces. |
| Optimal? | No | DFS may find a deep solution before a shallow one. It returns the first goal found, not the shallowest or cheapest. |
| Time | O(bm) | m = maximum depth of the search tree. DFS may explore the entire tree. Terrible if m ≫ d. |
| Space | O(bm) | Only stores the current path + siblings at each level. This is the key advantage! Linear in depth, not exponential. |
At each depth level, DFS stores at most b sibling nodes (the unexpanded children of the current path). Along a path of length m, this gives b × m = O(bm) nodes. Compare with BFS’s O(bd)—for b=10, m=12, DFS uses ~120 nodes vs. BFS’s 1012!
5. Backtracking Search
Backtracking search is a memory-optimized variant of DFS that generates only one successor at a time rather than all successors. When it needs to backtrack, it generates the next sibling.
Space complexity: O(m)—only the current path is stored. This is even better than standard DFS’s O(bm). Used in constraint satisfaction and game-tree search.
6. When DFS is Preferred
- Memory-constrained problems: When the state space is too large for BFS to store the frontier.
- Many solutions exist: If any solution is acceptable (not just the optimal one), DFS often finds one quickly.
- Tree-structured problems: No cycles, so DFS is complete and only explores each branch once.
- Deep solutions: If solutions tend to be deep, DFS reaches them faster than BFS.
7. More Worked Examples
Example 2: DFS Gets Stuck in an Infinite Branch
Tree: S has children A, B. A has children A1, A2, A3, … (infinite branch). B has child G (goal).
DFS (left-to-right): Expands S, pushes B and A (A on top). Pops A, pushes A3, A2, A1. Pops A1, pushes its children… DFS goes down the infinite left branch forever and never reaches G via B.
Example 3: DFS Returns a Non-Optimal Solution
Tree: S→A→B→G (depth 3, cost 3) and S→G (depth 1, cost 1). DFS with left-to-right ordering: expands S, pushes G and A (A on top). Pops A, eventually finds G at depth 3 instead of the depth-1 goal.
Summary
- DFS uses a LIFO stack and goes as deep as possible before backtracking.
- Not complete (infinite loops possible) and not optimal.
- Time: O(bm) — can be much worse than BFS if m ≫ d.
- Space: O(bm) — the big advantage! Linear in depth.
- Backtracking variant uses only O(m) space.
- Preferred when memory is limited and solutions are deep.
References
- Russell & Norvig, AIMA, Chapter 3.4.3.