Technique
0-1 BFS
Shortest paths when edges cost 0 or 1: push 0-edges to the deque front and 1-edges to the back for O(V + E).
0%
mastery
0/2 solved
0/2 solved
Recognise it
The signal
minimum number of changes/removals along a path
1Brute force
Dijkstra O(E log V).
2The observation
Heap overhead: with only two weights the deque keeps distances sorted.
3Why it works
The deque contains vertices with distance d or d+1 only, in order.
Reach for it when
- +some moves are free and others cost 1
Not when
- ×more than two distinct weights (dijkstra or dial's buckets)
Practice
Pitfalls
Misconceptions
- !marking visited on push (must allow later improvement as in dijkstra)
Go deeper1 combinations · resources
Combinations
0-1 BFS+BFS/Dijkstra on an Augmented State Graph
minimum modifications to make a path exist
- 0-1 BFS:
- 0-cost and 1-cost moves
- BFS/Dijkstra on an Augmented State Graph:
- grid cell plus direction/change state
Minimum Cost to Make at Least One Valid Path in a GridMinimum Obstacle Removal to Reach Corner