CS Glossary
Depth-First Search (DFS)
A graph/tree traversal that explores as deep as possible along each branch before backtracking, using recursion or a stack.
Overview
Depth-First Search (DFS) explores a graph or tree by going as deep as possible along each branch before backtracking. It is naturally implemented with recursion (the call stack acts as the implicit stack) or with an explicit stack data structure. DFS is essential for problems involving tree traversals, cycle detection, connected components, topological sorting, and path finding.
How It Works
- 1 Start at the source node and mark it as visited.
- 2 Explore one unvisited neighbor recursively (or push onto stack).
- 3 If no unvisited neighbors remain, backtrack to the previous node.
- 4 Repeat until all reachable nodes are visited.
- 5 For trees: pre-order (root, left, right), in-order (left, root, right), post-order (left, right, root).
Complexity Analysis
Each vertex and edge is visited once. The recursion depth equals the height of the tree/graph — O(log n) for balanced trees, O(n) for skewed trees. The iterative version using an explicit stack has the same complexity.
When to Use
- →Tree traversals (pre-order, in-order, post-order)
- →Detecting cycles in a graph
- →Finding connected components
- →Topological sorting (with post-order DFS)
- →Path existence problems ("is there a path from A to B?")
- →Solving puzzles with backtracking (N-Queens, Sudoku)
Common Mistakes
- ⚠Forgetting to mark nodes as visited before recursive calls (causes infinite loops)
- ⚠Not handling disconnected components (need to loop over all nodes)
- ⚠Confusing pre-order/in-order/post-order traversal order
- ⚠Stack overflow from deep recursion on large inputs (use iterative DFS)
- ⚠Modifying the graph while iterating (breaks adjacency lists)
Code Examples
List<Integer> dfs(List<List<Integer>> graph, int node, Set<Integer> visited) {
visited.add(node);
List<Integer> result = new ArrayList<>();
result.add(node);
for (int neighbor : graph.get(node)) {
if (!visited.contains(neighbor)) {
result.addAll(dfs(graph, neighbor, visited));
}
}
return result;
} Related Patterns
Practice Problems
| Number of Islands | Medium |
| Invert Binary Tree | Easy |
| Maximum Depth of Binary Tree | Easy |
| Clone Graph | Medium |
Frequently Asked Questions
What is DFS and how is it different from BFS?
When should I use DFS vs BFS in coding interviews?
What are pre-order, in-order, and post-order traversals?
Master this concept with practice
Browse all patterns and problems to build systematic interview intuition.