AdvancedAbout 25 min

    0-1 BFS (Deque Optimization)

    Read the full article on Substack

    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.

    0110110A0B∞C∞D∞E∞
    dequeA

    Source A. 0-weight edges go to the front of the deque, 1-weight edges to the back.

    1/11

    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

    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 here

    Back

    Drop items here
    5 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    Why 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.

    Keep going