0-1 BFS

Last Updated : 3 Feb, 2026

0-1 BFS Algorithm is a variation of simple BFS and is used to calculate Single Source Shortest Path to all the nodes in a weighted graphs provided the edges have weights 0 and 1.

Why to use 0-1 BFS Algorithm?

  1. We can find the minimum distance from a source node to all other nodes in O(E) time provided the graph is an unweighted graph or all the edges have the same weight.
  2. When edge weights are limited to 0 or 1, using Dijkstra's (O(E x log(V))), Bellman-Ford (O(V x E)) algorithm is inefficient. The 0-1 BFS Algorithm provides a solution with O(E) time complexity for finding the minimum distance from a source node.

Intuition for the 0-1 BFS Algorithm

The 0-1 BFS algorithm optimizes Dijkstra’s Algorithm for graphs with edge weights of 0 and 1. Standard Dijkstra uses a priority queue (O(E x log(V))), but the restricted edge weights allow for a deque (O(V + E)) due to the limited distance discovery range.

Logical explanation for the 0-1 BFS Algorithm:

The core logic:

  • If node X at distance D is removed, the remaining nodes have distances of D or D+1. A node at distance D can only relax a neighbor to a max distance of D+1 due to the max edge weight of 1.
  • No node in the queue can have a distance less than D, as the algorithm processes nodes in non-decreasing order and they would have already been processed.

To maintain this sorted order without a heap, we use a deque to manage the two active distance layers:

  • When an edge with weight 0 is relaxed, the neighbor stays within the current distance layer D, so we push it to the front of the deque to be processed next.
  • When an edge with weight 1 is relaxed, the neighbor moves to the next layer D+1, so we push it to the back.

This simple "front for 0, back for 1" rule ensures that we always process every node at distance D before any node at distance D+1, effectively simulating a priority queue at a fraction of the computational cost.

Example:

Input: V = 6, E = 7, src = 0, edges[][] = [[0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 4, 1], [4, 5, 0], [5, 0, 0], [1, 4, 1]]

Output: [0, 1, 2, 1, 0, 0]
source

Implementation of 0-1 BFS Algorithm

To familiarize yourself with the 0-1 BFS algorithm, you can refer here.

How to identify the problem based on 0-1 BFS Algorithm?

The problems based on 0-1 BFS Algorithm have one of the following patterns:

  • All the edges in the graph have weights either 0 or w, where w is not equal to 0
  • We have choices to make, where some choices have cost equal to 0, whereas the other choices have cost w, where w is not equal to 0, and we have to minimize the cost while performing these choices.

The following example would help us understand the concept of identification with the help of some problems:

Problem 1: Given a directed graph and a source node and destination node, we need to find how many edges we need to reverse in order to make at least 1 path from the source node to the destination node.

Solution:

In the above problem, we can assign a weight of zero to all the original edges and for each of the original edges, we create a reversed edge with a weight of one. Now, the problem is reduced to find the path with smallest distance to a node where all the edges have weights either 0 or 1.

Problem 2: Given an N x M matrix with numbers written on each cell. One move to adjacent cells with same number costs 0, with different numbers costs W. Find the minimum no. of coins to go from (0, 0) to (N-1, M-1).

Solution:

In the above problem, we can start a bfs from (0,0) and start checking the neighboring cells. If the neighboring cell has same value, then push it at the front of the deque else push it at the back. We also need to keep a cost[][] array to store the minimum cost to reach a cell.

Problem 3: Given an N x M matrix with empty and blocked cells. Moving in the same direction costs 0, while a 90-degree rotation costs C. Starting at (0, 0), we can initially move in any direction (Up, Down, Left, Right) without cost. Find the minimum cost to reach (N-1, M-1), choosing to move in the same direction (if the next cell is empty) or rotate 90 degrees.

Solution:

We can consider all directions as starting directions. At any cell, we have these options: Move in the same direction (cost = 0), turn left (cost = C), or turn right (cost = C). We avoid turning in the opposite direction (cost = 2C). If we move in the same direction, add the new cell to the front of the deque; otherwise, to the back. Use a `cost[][]` array to store the minimum cost to reach each cell.

Application of 0-1 BFS Algorithm

1. Minimum edges to reverse to make path from a source to a destination:

Given an unweighted directed graph, source, and destination, find the minimum edges to reverse to create a path. Create "fake" reverse edges, assign original edges weight 0 and fake edges weight 1. Use 0-1 BFS to find the minimum distance. Refer to this article for implementation.

2. Calculating minimum cost to move between two cells with move costs limited to 0 or W (W > 0):

In this problem, we are given a matrix, and we need to move from one node start to another node end, with some allowed moves. Some of these moves cost us nothing whereas others cost us some fixed coins and we need to find the minimum amount to coins needed to reach the end node from the start node.

3. Dial's Algorithm:

In dial's algorithm, we extend 0-1 BFS Algorithm for a graph having multiple weights until all the edge weights are less than W, where W is a small integer. We can maintain K buckets in the queue and start popping from the front of the queue. Thus, we start moving from smaller weight buckets to larger ones.

Practice Problems

Comment