Given a directed graph where every edge has weight as either 1 or 2, find the shortest path from a given source vertex ‘s’ to a given destination vertex ‘t’. Expected time complexity is O(V+E).
A Simple Solution is to use Dijkstra’s shortest path algorithm, we can get a shortest path in O(E + VLogV) time.
How to do it in O(V+E) time? The idea is to use BFS. One important observation about BFS is, the path used in BFS always has least number of edges between any two vertices. So if all edges are of same weight, we can use BFS to find the shortest path. For this problem, we can modify the graph and split all edges of weight 2 into two edges of weight 1 each. In the modified graph, we can use BFS to find the shortest path.
How many new intermediate vertices are needed? We need to add a new intermediate vertex for every source vertex. The reason is simple, if we add a intermediate vertex x between u and v and if we add same vertex between y and z, then new paths u to z and y to v are added to graph which might have note been there in original graph. Therefore in a graph with V vertices, we need V extra vertices.
Below is C++ implementation of above idea. In the below implementation 2*V vertices are created in a graph and for every edge (u, v), we split it into two edges (u, u+V) and (u+V, w). This way we make sure that a different intermediate vertex is added for every source vertex.
Shortest Path between 0 and 3 is 0 1 3 Shortest Distance between 0 and 3 is 3
How is this approach O(V+E)? In worst case, all edges are of weight 2 and we need to do O(E) operations to split all edges and 2V vertices, so the time complexity becomes O(E) + O(V+E) which is O(V+E).
This article is contributed by Aditya Goel. 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 with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Maximize shortest path between given vertices by adding a single edge
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Product of minimum edge weight between all pairs of a Tree
- Remove all outgoing edges except edge with minimum weight
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Shortest Path in Directed Acyclic Graph
- Multistage Graph (Shortest Path)
- 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 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