Given a graph where every edge has weight as either 0 or 1. A source vertex is also given in the graph. Find the shortest path from the source vertex to every other vertex.
For Example:
Input : Source Vertex = 0 and below graph Having vertices like(u,v,w):
Edges: [[0,1,0], [0, 7, 1], [1,2,1], [1, 7, 1], [2, 3, 0], [2, 5, 0], [2, 8, 1], [3, 4, 1],[3, 5, 1],[4, 5, 1],[5, 6, 1],[6, 7, 1],[7, 8, 1]]
Output : Shortest distances from given source
0 0 1 1 2 1 2 1 2
Explanation :
Shortest distance from 0 to 0 is 0
Shortest distance from 0 to 1 is 0
Shortest distance from 0 to 2 is 1
..................
In normal BFS of a graph, all edges have equal weight but in 0-1 BFS some edges may have 0 weight and some may have 1 weight. In this, we will not use a bool array to mark visited nodes but at each step, we will check for the optimal distance condition. We use a double-ended queue to store the node. While performing BFS if an edge having weight = 0 is found node is pushed at front of the double-ended queue and if an edge having weight = 1 is found, it is pushed to the back of the double-ended queue.
The approach is similar to Dijkstra that if the shortest distance to the node is relaxed by the previous node then only it will be pushed into the queue.
The above idea works in all cases, when pop a vertex (like Dijkstra), it is the minimum weight vertex among the remaining vertices. If there is a 0-weight vertex adjacent to it, then this adjacent has the same distance. If there is a 1 weight adjacent, then this adjacent has maximum distance among all vertices in the dequeue (because all other vertices are either adjacent to the currently popped vertex or adjacent to previously popped vertices).
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
#define V 9
struct node
{
int to, weight;
};
vector <node> edges[V];
void zeroOneBFS(int src)
{
int dist[V];
for (int i=0; i<V; i++)
dist[i] = INT_MAX;
deque <int> Q;
dist[src] = 0;
Q.push_back(src);
while (!Q.empty())
{
int v = Q.front();
Q.pop_front();
for (int i=0; i<edges[v].size(); i++)
{
if (dist[edges[v][i].to] > dist[v] + edges[v][i].weight)
{
dist[edges[v][i].to] = dist[v] + edges[v][i].weight;
if (edges[v][i].weight == 0)
Q.push_front(edges[v][i].to);
else
Q.push_back(edges[v][i].to);
}
}
}
for (int i=0; i<V; i++)
cout << dist[i] << " ";
}
void addEdge(int u, int v, int wt)
{
edges[u].push_back({v, wt});
edges[v].push_back({u, wt});
}
int main()
{
addEdge(0, 1, 0);
addEdge(0, 7, 1);
addEdge(1, 7, 1);
addEdge(1, 2, 1);
addEdge(2, 3, 0);
addEdge(2, 5, 0);
addEdge(2, 8, 1);
addEdge(3, 4, 1);
addEdge(3, 5, 1);
addEdge(4, 5, 1);
addEdge(5, 6, 1);
addEdge(6, 7, 1);
addEdge(7, 8, 1);
int src = 0;
zeroOneBFS(src);
return 0;
}
|
Java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
public class ZeroOneBFS {
private static class Node {
int to;
int weight;
public Node(int to, int wt) {
this.to = to;
this.weight = wt;
}
}
private static final int numVertex = 9;
private ArrayList<Node>[] edges = new ArrayList[numVertex];
public ZeroOneBFS() {
for (int i = 0; i < edges.length; i++) {
edges[i] = new ArrayList<Node>();
}
}
public void addEdge(int u, int v, int wt) {
edges[u].add(edges[u].size(), new Node(v, wt));
edges[v].add(edges[v].size(), new Node(u, wt));
}
public void zeroOneBFS(int src) {
int[] dist = new int[numVertex];
for (int i = 0; i < numVertex; i++) {
dist[i] = Integer.MAX_VALUE;
}
Deque<Integer> queue = new ArrayDeque<Integer>();
dist[src] = 0;
queue.addLast(src);
while (!queue.isEmpty()) {
int v = queue.removeFirst();
for (int i = 0; i < edges[v].size(); i++) {
if (dist[edges[v].get(i).to] >
dist[v] + edges[v].get(i).weight) {
dist[edges[v].get(i).to] =
dist[v] + edges[v].get(i).weight;
if (edges[v].get(i).weight == 0) {
queue.addFirst(edges[v].get(i).to);
} else {
queue.addLast(edges[v].get(i).to);
}
}
}
}
for (int i = 0; i < dist.length; i++) {
System.out.print(dist[i] + " ");
}
}
public static void main(String[] args) {
ZeroOneBFS graph = new ZeroOneBFS();
graph.addEdge(0, 1, 0);
graph.addEdge(0, 7, 1);
graph.addEdge(1, 7, 1);
graph.addEdge(1, 2, 1);
graph.addEdge(2, 3, 0);
graph.addEdge(2, 5, 0);
graph.addEdge(2, 8, 1);
graph.addEdge(3, 4, 1);
graph.addEdge(3, 5, 1);
graph.addEdge(4, 5, 1);
graph.addEdge(5, 6, 1);
graph.addEdge(6, 7, 1);
graph.addEdge(7, 8, 1);
int src = 0;
graph.zeroOneBFS(src);
return;
}
}
|
Python3
from sys import maxsize as INT_MAX
from collections import deque
V = 9
class node:
def __init__(self, to, weight):
self.to = to
self.weight = weight
edges = [0] * V
for i in range(V):
edges[i] = []
def zeroOneBFS(src: int):
dist = [0] * V
for i in range(V):
dist[i] = INT_MAX
Q = deque()
dist[src] = 0
Q.append(src)
while Q:
v = Q[0]
Q.popleft()
for i in range(len(edges[v])):
if (dist[edges[v][i].to] >
dist[v] + edges[v][i].weight):
dist[edges[v][i].to] = dist[v] + edges[v][i].weight
if edges[v][i].weight == 0:
Q.appendleft(edges[v][i].to)
else:
Q.append(edges[v][i].to)
for i in range(V):
print(dist[i], end = " ")
print()
def addEdge(u: int, v: int, wt: int):
edges[u].append(node(v, wt))
edges[u].append(node(v, wt))
if __name__ == "__main__":
addEdge(0, 1, 0)
addEdge(0, 7, 1)
addEdge(1, 7, 1)
addEdge(1, 2, 1)
addEdge(2, 3, 0)
addEdge(2, 5, 0)
addEdge(2, 8, 1)
addEdge(3, 4, 1)
addEdge(3, 5, 1)
addEdge(4, 5, 1)
addEdge(5, 6, 1)
addEdge(6, 7, 1)
addEdge(7, 8, 1)
src = 0
zeroOneBFS(src)
|
C#
using System;
using System.Collections.Generic;
class ZeroOneBFS {
private class Node {
public int to;
public int weight;
public Node(int to, int wt) {
this.to = to;
this.weight = wt;
}
}
private const int numVertex = 9;
private List<Node>[] edges = new List<Node>[numVertex];
public ZeroOneBFS() {
for (int i = 0; i < edges.Length; i++) {
edges[i] = new List<Node>();
}
}
public void addEdge(int u, int v, int wt) {
edges[u].Add(new Node(v, wt));
edges[v].Add(new Node(u, wt));
}
public void zeroOneBFS(int src) {
int[] dist = new int[numVertex];
for (int i = 0; i < numVertex; i++) {
dist[i] = int.MaxValue;
}
Queue<int> queue = new Queue<int>();
dist[src] = 0;
queue.Enqueue(src);
while (queue.Count > 0) {
int v = queue.Dequeue();
for (int i = 0; i < edges[v].Count; i++) {
if (dist[edges[v][i].to] >
dist[v] + edges[v][i].weight) {
dist[edges[v][i].to] =
dist[v] + edges[v][i].weight;
if (edges[v][i].weight == 0) {
queue.Enqueue(edges[v][i].to);
} else {
queue.Enqueue(edges[v][i].to);
}
}
}
}
for (int i = 0; i < dist.Length; i++) {
Console.Write(dist[i] + " ");
}
}
static void Main(string[] args) {
ZeroOneBFS graph = new ZeroOneBFS();
graph.addEdge(0, 1, 0);
graph.addEdge(0, 7, 1);
graph.addEdge(1, 7, 1);
graph.addEdge(1, 2, 1);
graph.addEdge(2, 3, 0);
graph.addEdge(2, 5, 0);
graph.addEdge(2, 8, 1);
graph.addEdge(3, 4, 1);
graph.addEdge(3, 5, 1);
graph.addEdge(4, 5, 1);
graph.addEdge(5, 6, 1);
graph.addEdge(6, 7, 1);
graph.addEdge(7, 8, 1);
int src = 0;
graph.zeroOneBFS(src);
return;
}
}
|
Javascript
<script>
class Node
{
constructor(to,wt)
{
this.to = to;
this.weight = wt;
}
}
let numVertex = 9;
let edges = new Array(numVertex);
function _ZeroOneBFS()
{
for (let i = 0; i < edges.length; i++) {
edges[i] = [];
}
}
function addEdge(u,v,wt)
{
edges[u].push(edges[u].length, new Node(v, wt));
edges[v].push(edges[v].length, new Node(u, wt));
}
function zeroOneBFS(src)
{
let dist = new Array(numVertex);
for (let i = 0; i < numVertex; i++) {
dist[i] = Number.MAX_VALUE;
}
let queue = [];
dist[src] = 0;
queue.push(src);
while (queue.length!=0) {
let v = queue.shift();
for (let i = 0; i < edges[v].length; i++) {
if (dist[edges[v][i].to] >
dist[v] + edges[v][i].weight) {
dist[edges[v][i].to] =
dist[v] + edges[v][i].weight;
if (edges[v][i].weight == 0) {
queue.unshift(edges[v][i].to);
} else {
queue.push(edges[v][i].to);
}
}
}
}
for (let i = 0; i < dist.length; i++) {
document.write(dist[i] + " ");
}
}
_ZeroOneBFS();
addEdge(0, 1, 0);
addEdge(0, 7, 1);
addEdge(1, 7, 1);
addEdge(1, 2, 1);
addEdge(2, 3, 0);
addEdge(2, 5, 0);
addEdge(2, 8, 1);
addEdge(3, 4, 1);
addEdge(3, 5, 1);
addEdge(4, 5, 1);
addEdge(5, 6, 1);
addEdge(6, 7, 1);
addEdge(7, 8, 1);
let src = 0;
zeroOneBFS(src);
</script>
|
This problem can also be solved by Dijkstra but the time complexity will be O(E + V Log V) whereas by BFS it will be O(V+E).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...