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)
More Efficient Approach : An even better method is to use the Multisource BFS which is a modification of BFS.We will put the all source vertices to the queue at first rather than a single vertex which was in case of standard BFS.This way Multisource BFS will first visit all the source vertices. After that it will visit the vertices which are at a distance of 1 from all source vertices, then at a distance of 2 from all source vertices and so on and so forth.
Below is the implementation of the above approach:
1 0 2 1 3 1 4 1 5 0 6 1
Time Complexity: O(V+E)
- Shortest path in an unweighted graph
- Number of shortest paths in an unweighted and directed graph
- Shortest cycle in an undirected unweighted graph
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- D'Esopo-Pape Algorithm : Single Source Shortest Path
- Shortest Path with even number of Edges from Source to Destination
- Single source shortest path between two cities
- Print all shortest paths between given source and destination in an undirected graph
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Find any simple cycle in an undirected unweighted 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
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Multistage Graph (Shortest Path)
- Check if given path between two nodes of a graph represents a shortest paths
- 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
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.
Improved By : radhesh