Suppose there are n towns connected by m bidirectional roads. There are s towns among them with a police station. We want to find out the distance of each town from the nearest police station. If the town itself has one the distance is 0.
Input : Number of Vertices = 6 Number of Edges = 9 Towns with Police Station : 1, 5 Edges: 1 2 1 6 2 6 2 3 3 6 5 4 6 5 3 4 5 3 Output : 1 0 2 1 3 1 4 1 5 0 6 1
Naive Approach: We can loop through the vertices and from each vertex run a BFS to find the closest town with police station from that vertex. This will take O(V.E).
Naive approach implementation using BFS from each vertex:
1 0 2 1 3 1 4 1 5 0 6 1
Time Complexity: O(V.E)
Efficient Method A better method is to use the Djikstra’s algorithm in a modified way. Let’s consider one of the sources as the original source and the other sources to be vertices with 0 cost paths from the original source. Thus we push all the sources into the Djikstra Queue with distance = 0, and the rest of the vertices with distance = infinity. The minimum distance of each vertex from the original source now calculated using the Dijkstra’s Algorithm are now essentially the distances from the nearest source.
Explanation: The C++ implementation uses a set of pairs (distance from the source, vertex) sorted according to the distance from the source. Initially, the set contains the sources with distance = 0 and all the other vertices with distance = infinity.
On each step, we will go to the vertex with minimum distance(d) from source, i.e, the first element of the set (the source itself in the first step with distance = 0). We go through all it’s adjacent vertices and if the distance of any vertex is > d + 1 we replace its entry in the set with the new distance. Then we remove the current vertex from the set. We continue this until the set is empty.
The idea is there cannot be a shorter path to the vertex at the front of the set than the current one since any other path will be a sum of a longer path (>= it’s length) and a non-negative path length (unless we are considering negative edges).
Since all the sources have a distance = 0, in the beginning, the adjacent non-source vertices will get a distance = 1. All vertices will get distance = distance from their nearest source.
Implementation of Efficient Approach:
1 0 2 1 3 1 4 1 5 0 6 1
Time Complexity: O(E.logV)
- Shortest path in an unweighted graph
- Number of shortest paths in an unweighted and directed graph
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Multistage Graph (Shortest Path)
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest Path in Directed Acyclic Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Find if there is a path of more than k length from a source
- Source to destination in 2-D path with fixed sized jumps
- Minimum edges to reverse to make path from a source to a destination
- Shortest path on a Square
- Shortest Path using Meet In The Middle
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.