0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Step 1 / 6
Edges
Deque state
Distances
node 0
0
node 1
inf
node 2
inf
node 3
inf
node 4
inf
node 5
inf
Deque instead of heap
Start from node 0 with distance 0.
Family
Graphs
Nodes, edges, traversal, connectivity, and shortest paths.
Builds on
3 topics
Read these first if you want the surrounding context.
Unlocks
0 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
2
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You need shortest paths from one source, but edge weights are restricted to only 0 or 1.
A normal BFS fails because weights are not uniform. Dijkstra works, but a heap is unnecessary overhead because there are only two possible distance increments.
That special structure is exactly what 0-1 BFS exploits.
Key insight
When you relax an edge:
- weight
0means the neighbor should be processed as soon as possible - weight
1means the neighbor belongs one layer later
So the frontier can be maintained with a deque:
- push to the front for a 0-edge
- push to the back for a 1-edge
That keeps nodes in nondecreasing distance order without a priority queue.
Algorithm
dist[source] <- 0
deque <- [source]
while deque not empty:
u <- pop_front()
for (v, w) in adj[u]:
if dist[u] + w < dist[v]:
dist[v] <- dist[u] + w
if w == 0:
push_front(v)
else:
push_back(v)
This looks like Dijkstra, but the priority structure collapses into a deque because weights are so constrained.
Why it works
At any moment, nodes in the deque differ in tentative distance by at most 1.
- front side = current best distance bucket
- back side = next bucket
That is why the deque is enough. You never need a full heap ordering.
Complexity
| Operation | Time |
|---|---|
| Full shortest paths |
Each edge is relaxed in the same linear style as BFS.
When to use it
Use 0-1 BFS when all edge weights are binary:
- teleport or normal move
- free or paid transition
- same-color edge vs color-change edge
- reverse-edge constructions where one direction costs 0 and the other costs 1
If weights can be larger than 1, switch to Dijkstra or another shortest-path method.
Key takeaways
- 0-1 BFS sits between BFS and Dijkstra
- It is really Dijkstra with a deque because the key space is only
{0, 1} - Deques are not just linear containers; they unlock this shortest-path trick directly
- This is niche, but when the constraint fits, it is both elegant and fast
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Minimum Cost to Make at Least One Valid Path in a Grid | 🔴 Hard | Direction-following moves cost 0, changes cost 1 |
| Shortest Path in Binary Matrix variants | varies | Binary-weight transitions often reduce to 0-1 BFS |
| Chef and Reversing | 🟡 Medium | Original direction 0, reversed edge 1 |
Relation to other topics
- BFS handles unweighted shortest paths
- Dijkstra handles general nonnegative weights
- Deque is the enabling data structure that makes this optimization work
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
Graph Fundamentals
Learn the core language of nodes, edges, paths, and cycles. Graphs are the broad container that trees, BFS, DFS, Dijkstra, and MST all live inside.
Deque (Double-Ended Queue)
Push and pop from both ends in O(1). A deque can behave like a queue, a stack, or a monotonic window depending on how you use it.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Dijkstra's Algorithm
Find the shortest path in weighted graphs with non-negative edges. Dijkstra is BFS with a priority queue — it processes nodes in order of cumulative distance.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
More from Graphs
Stay in the same family when you want parallel variations of the same mental model.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
Graph Fundamentals
Learn the core language of nodes, edges, paths, and cycles. Graphs are the broad container that trees, BFS, DFS, Dijkstra, and MST all live inside.
Dijkstra's Algorithm
Find the shortest path in weighted graphs with non-negative edges. Dijkstra is BFS with a priority queue — it processes nodes in order of cumulative distance.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Graph toolkit
Start with graph shape, then layer traversal, connectivity structure, and both single-source and all-pairs path algorithms.
Weighted shortest paths
See the spectrum from plain BFS to deque-based 0-1 BFS to priority-queue shortest paths and dense all-pairs DP.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.