DSA Notes
Master DFS traversal using recursion and stack, understand pre/post ordering, cycle detection, and connected component discovery.
The Intuition
Imagine exploring a maze. Instead of checking all nearby paths first (like BFS), you pick one direction and go as deep as possible until you hit a dead end. Then you backtrack to the last junction and try a different path. This "go deep, then backtrack" strategy is DFS.
DFS is like a curious explorer who always wants to see what is around the next corner before checking alternatives. It dives deep into one branch of the graph before exploring others.
How DFS Works
DFS uses a stack (explicitly or via recursion's call stack):
- Start at the source vertex, mark it visited
- For each unvisited neighbor, recursively visit it (go deeper)
- When all neighbors are explored, backtrack
The recursive nature makes DFS elegant and natural for problems involving paths, components, and cycles.
Dry Run Example
Graph
0 --- 1 --- 4
| |
2 --- 3
DFS from vertex 0 (choosing lowest-numbered neighbor first)
Visit 0 → go to neighbor 1
Visit 1 → go to neighbor 3 (0 already visited)
Visit 3 → go to neighbor 2 (1 already visited)
Visit 2 → all neighbors visited, backtrack
Back to 3 → backtrack
Back to 1 → go to neighbor 4
Visit 4 → all neighbors visited, backtrack
Back to 1 → backtrack
Back to 0 → all neighbors explored
DFS Order: 0, 1, 3, 2, 4
Python Implementation
Cycle Detection
One of DFS's most important applications is detecting cycles.
In Undirected Graphs
In Directed Graphs (Three-Color Method)
C++ Implementation
Time and Space Complexity
- Time: O(V + E) — visits every vertex and edge once
- Space: O(V) — for visited array + recursion stack (or explicit stack)
- Warning: For very deep graphs, recursive DFS can cause stack overflow. Use iterative version for graphs with depth > ~1000.
DFS vs BFS
| Feature | DFS | BFS |
|---|---|---|
| Data structure | Stack/Recursion | Queue |
| Exploration | Goes deep first | Goes wide first |
| Shortest path | No (for unweighted) | Yes (for unweighted) |
| Memory | O(depth) | O(width) |
| Cycle detection | Natural | Possible but less elegant |
| Topological sort | Natural | Via Kahn's algorithm |
| Best for | Paths, components, cycles | Shortest path, levels |
Applications of DFS
- Cycle detection in directed and undirected graphs
- Topological sorting of DAGs
- Finding connected components (undirected) and strongly connected components (directed)
- Path finding (find ANY path, not necessarily shortest)
- Maze generation (randomized DFS creates good mazes)
- Solving puzzles (backtracking is DFS with pruning)
- Detecting bridges and articulation points in networks
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Depth-First Search (DFS) Algorithm.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, dfs, algorithm
Related Data Structures & Algorithms Topics