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 source vertex to every other vertex.
Input : Source Vertex = 0 and below graph 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 bool array to mark visited nodes but at each step we will check for the optimal distance condition. We use double ended queue to store the node. While performing BFS if a edge having weight = 0 is found node is pushed at front of double ended queue and if a edge having weight = 1 is found, it is pushed at back of double ended queue.
The approach is similar to Dijkstra that the if the shortest distance to node is relaxed by the previous node then only it will be pushed in the queue.
The above idea works in all cases, when pop a vertex (like Dijkstra), it is the minimum weight vertex among remaining vertices. If there is a 0 weight vertex adjacent to it, then this adjacent has same distance. If there is a 1 weight adjacent, then this adjacent has maximum distance among all vertices in dequeue (because all other vertices are either adjacent of currently popped vertex or adjacent of previously popped vertices).
Below is the implementation of the above idea.
0 0 1 1 2 1 2 1 2
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).
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest Path in Directed Acyclic Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Multistage Graph (Shortest Path)
- Shortest path in an unweighted graph
- Multi Source Shortest Path in Unweighted Graph
- Check if given path between two nodes of a graph represents a shortest paths
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path in a directed graph by Dijkstra’s algorithm
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Shortest path in a complement graph
- Create a Graph by connecting divisors from N to M and find shortest path
- Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
- Finding the path from one vertex to rest using BFS
- Breadth First Search or BFS for a Graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- BFS for Disconnected Graph