![]() |
VOOZH | about |
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.
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.
The core logic:
To maintain this sorted order without a heap, we use a deque to manage the two active distance layers:
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
To familiarize yourself with the 0-1 BFS algorithm, you can refer here.
The problems based on 0-1 BFS Algorithm have one of the following patterns:
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.
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.