0-1 BFS — push 0 to the front, 1 to the back

free (0-edge) obstacle (1-edge) current settled front (bucket d) back (bucket d+1)

Controls

slow fast

Maze

Source is top-left (blue), target is bottom-right (purple). Entering a free cell costs 0, entering an obstacle costs 1. The number in each cell is its current best distance.

Why it works

relax(u -> v, w): if dist[u]+w < dist[v]: dist[v] = dist[u]+w if w==0: deque.push_front(v) else: deque.push_back(v) The deque holds at most TWO distance values: d (front) and d+1 (back). pop_front is always the minimum => O(V+E).

Grid

Deque (front → back)

← FRONT (pop_front here, push 0-edges here) BACK (push 1-edges here) →

Status

Press Step or Run to begin 0-1 BFS from the blue source.
steps: 0
settled: 0
pushes: 0
stale skips: 0
distinct dist in deque: 0