Traversal
Visiting every vertex reachable from a start, systematically. BFS walks
outward in rings using a queue; DFS commits to one branch and plunges using a
stack (explicit, or the call stack via recursion). Both are the same loop with
two knobs - the neighbour function, and how you track visited.
The mechanics, the correctness argument for why BFS's first arrival is always
shortest, and the traps that come from marking visited at the wrong moment
all live on the learn page: Traversal. The
problems below are that engine with one knob turned.
1971. Find if Path Exists in Graph
Easy·
4 Approaches · 2 patterns
DFS - Recursive
O(V + E)O(V + E)
DFS - Iterative
O(V + E)O(V + E)
BFS - Iterative
O(2V + 2E)O(V + E)
DSU↗ DSU on Graph
O(E * α(n))O(3n)
FIG. FIND IF PATH EXISTS IN GRAPH● INTERACTIVE
visualization loads as you reach it
133. Clone Graph
Medium·
133Clone Graph
4 Approaches
Recursive DFS
O(V + E)O(V)
Iterative DFS (Stack)
O(V + E)O(V)
Iterative BFS (Queue)
O(V + E)O(V)
Iterative BFS (Parent-Linked)
O(V + E)O(V + E)
FIG. 133 CLONE GRAPH RECURSIVE● INTERACTIVE
visualization loads as you reach it
2101. Detonate the Maximum Bombs
Medium·
3 Approaches
DFS - Recursive
O(n^2 + n^3)O(n^2)
DFS - Iterative
O(n^2 + n^3)O(n^2)
BFS - Iterative
O(n^2 + n^3)O(n^2)
FIG. 2101 DETONATE THE MAXIMUM BOMBS● INTERACTIVE
visualization loads as you reach it