Given an unweighted directed graph, can be cyclic or acyclic. Print the number of shortest paths from a given vertex to each of the vertices. For example consider the below graph. There is one shortest path vertex 0 to vertex 0 (from each vertex there is a single shortest path to itself), one shortest path between vertex 0 to vertex 2 (0->2), and there are 4 different shortest paths from vertex 0 to vertex 6:
The idea is to use BFS. We use two arrays called dist and paths, dist represents the shorest distances from source vertex, and paths represents the number of different shortest paths from the source vertex to each of the vertices. Initially all the elements in dist are infinity except source vertex which is equal to 0, since the distance to source vertex from itself is 0, and all the elements in paths are 0 except source vertex which is equal to 1, since each vertex has a single shortest path to itself. after that, we start traversing the graph using BFS manner.
Then, for every neighbor Y of each vertex X do:
1) if dist[Y] > dist[X]+1 decrease the dist[Y] to dist[X] +1 and assign the number of paths of vertex X to number of paths of vertex Y.
2) else if dist[Y] = dist[X] + 1, then add the number of paths of vertex X to the number of paths of vertex Y.
Let’s take a look at the below graph. The source vertex is 0. Suppose we traverse on vertex 2, we check all its neighbors, which is only 3.since vertex 3 was already visited when we were traversed vertex 1, dist = 2 and paths = 1. The second condition is true, so it means that addtional shortest paths have been found, so we add to the number of paths of vertex 3, the number of paths of vertex 2.
The equal condition happens when we traverse on vertex 5:
Numbers of shortest Paths are: 1 1 1 2 2 2 4
Time Complexity : O(V + E)
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.
- Multi Source Shortest Path in Unweighted Graph
- Shortest cycle in an undirected unweighted graph
- Shortest path in an unweighted graph
- Find the number of paths of length K in a directed graph
- 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 in Directed Acyclic Graph
- Shortest path in a directed graph by Dijkstra’s algorithm
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Print all shortest paths between given source and destination in an undirected graph
- Find any simple cycle in an undirected unweighted Graph
- Check if given path between two nodes of a graph represents a shortest paths
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Johnson's algorithm for All-pairs shortest paths
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Shortest paths from all vertices to a destination
- Sum of all pair shortest paths in a Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.