0-1 BFS (Deque Optimization)
6 min read.
Shortest path when edge weights are only 0 or 1. Uses deque instead of priority queue for O(V+E) instead of O(E log V). Critical for grid problems with obstacles.
Think of it this way: Imagine walking through a city where some roads are free (weight 0) and others cost a toll (weight 1). With 0-1 BFS, free roads get priority in line, so you always take the cheapest path first.
Source A. 0-weight edges go to the front of the deque, 1-weight edges to the back.
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Front or back of the deque?
Sort each case by where 0-1 BFS puts (or takes) the node.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Front
Drop items hereBack
Drop items hereQuiz
20 questionsWhy is 0-1 BFS faster than Dijkstra for 0-1 weight graphs?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.