Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Dijkstra without a heap, when edges cost 0 or 1
Shortest paths when every edge weighs 0 or 1: a deque replaces the priority queue, 0-edges go to the front and 1-edges to the back
Select an algorithm and generate steps to begin visualization
0-1 BFS computes shortest paths from one source when every edge weighs either 0 or 1. Plain BFS counts edges and would treat a free edge like a paid one, while Dijkstra handles the weights but pays for a priority queue. 0-1 BFS keeps a double-ended queue instead: a vertex reached over a 0-edge goes to the front, because its distance equals the current one, and a vertex reached over a 1-edge goes to the back. The deque then always holds at most two distances, d and d + 1, in sorted order, so taking the front is exactly what Dijkstra would do, in O(V + E) time with no heap at all. The method is folklore in competitive programming, where 0-1 weights turn up constantly.
Start with the source at distance 0 in the deque. Pop the front vertex, and if its recorded distance is smaller than the popped one, skip it: a better copy was taken already. Otherwise relax each edge: when the neighbour gets closer, push it at the front for a 0-edge or at the back for a 1-edge. A vertex can be pushed twice, first at the back and later at the front, which is why stale copies are skipped. The visualizer shows the deque in the panel, marks 0-edges, writes each distance in a badge and draws the shortest-path tree in green. In the example, A reaches C for free and C reaches B for free, so B, first pushed at distance 1 directly from A, is taken at distance 0, and its old copy is skipped later. All 9 vertices are reached, the farthest at distance 2, and the deque never holds more than 3 vertices.
0-1 weights model a choice between a free move and a costly one. On grids it gives the fewest walls to break to reach a cell, or the fewest direction changes on a route when moving straight is free and turning costs 1. In a directed graph, adding each edge reversed with weight 1 lets 0-1 BFS find the fewest edges to reverse so that a path exists. It counts the fewest line changes on a transit map, where riding a line is free and switching costs one, and the fewest flips in many puzzle and state-space problems. It is a staple of competitive programming because it is shorter and faster than Dijkstra, and it generalises to small integer weights with Dial's bucket queue.
A deque replaces the priority queue: 0-edges push to the front, 1-edges to the back.
ZeroOneBFS(G, s):
dist(v) = infinity for every v, dist(s) = 0
D = a deque holding (s, 0)
while D is not empty:
(u, d) = pop the front of D
if d > dist(u): continue // a better copy was taken
for each edge (u, v) with weight w in {0, 1}:
if dist(u) + w < dist(v):
dist(v) = dist(u) + w
if w = 0: push (v, dist(v)) at the front of D
else: push (v, dist(v)) at the back of D
return distInvariant: the deque holds keys d, …, d, d + 1, …, d + 1 in that order, where d is the key at the front. A 0-edge pushes key d at the front and a 1-edge pushes key d + 1 at the back, so the order survives. Taking the front is therefore the same choice Dijkstra makes with a heap.
The nine-vertex example loaded on this page, from A.
Example graph: Vertices A to I and 13 edges. Five are free: A–C, C–B, D–E, D–G and F–H. The other eight weigh 1.
All 9 vertices reached: A, B, C at 0, D, E, G at 1, F, H, I at 2. The deque never held more than 3 vertices.
Time: O(V + E) · Space: O(V)
Each vertex is processed once, when its first copy with the final distance leaves the deque, and later copies are skipped in O(1). Processing a vertex looks at each of its edges once, so edges are examined O(E) times in all, and every push comes from one such examination. Deque operations are O(1), so the total is O(V + E), against O((V + E) log V) for Dijkstra with a binary heap. Dial's algorithm extends the idea to integer weights up to C with C + 1 buckets, in O(V·C + E).
Shortest path methods by the weights they allow.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS | All edges weigh the same. A plain queue is enough. | O(V + E) |
| Dijkstra | Weights are arbitrary non-negative numbers. | O((V + E) log V) |
| Dial's algorithm | Weights are small integers from 0 to C. One bucket per distance, scanned in order. | O(V·C + E) |
Related algorithms: Breadth-First Search, Dijkstra's Algorithm, Δ-Stepping (Parallel Shortest Paths), Uniform Cost Search (UCS), Bidirectional BFS
Built by Mohammed Islam Hadjoudj · Work with me