Depth-First Search (DFS) Pattern
Depth-First Search (DFS) is a traversal algorithm that explores as far as possible along each branch before backtracking. It's used for exploring all paths or branches in graphs or trees.
When to Use This Pattern
- Exploring all paths in graphs or trees
- Finding connected components
- Topological sorting
- Finding cycles in graphs
- Path finding problems
- Tree/graph traversal
Key Concepts
DFS can be implemented using:
- Recursion: Natural for tree/graph traversal
- Stack: Iterative approach using explicit stack