Given a Directed Acyclic Graph with n vertices and m edges. The task is to find the number of different paths that exist from a source vertex to destination vertex.
Input: source = 0, destination = 4
0 -> 2 -> 3 -> 4
0 -> 3 -> 4
0 -> 4
Input: source = 0, destination = 1
Explanation: There exists only one path 0->1
Approach : Let f(u) be the number of ways one can travel from node u to destination vertex. Hence, f(source) is required answer. As f(destination) = 1 here so there is just one path from destination to itself. One can observe, f(u) depends on nothing other than the f values of all the nodes which are possible to travel from u. It makes sense because the number of different paths from u to the destination is the sum of all different paths from v1, v2, v3… v-n to destination vertex where v1 to v-n are all the vertices that have a direct path from vertex u. This approach, however, is too slow to be useful. Each function call branches out into further calls, and that branches into further calls, until each and every path is explored once.
The problem with this approach is the calculation of f(u) again and again each time the function is called with argument u. Since this problem exhibits both overlapping subproblems and optimal substructure, dynamic programming is applicable here. In order to evaluate f(u) for each u just once, evaluate f(v) for all v that can be visited from u before evaluating f(u). This condition is satisfied by reverse topological sorted order of the nodes of the graph.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Longest Path in a Directed Acyclic Graph | Set 2
- Longest Path in a Directed Acyclic Graph
- Shortest Path in Directed Acyclic Graph
- All Topological Sorts of a Directed Acyclic Graph
- Assign directions to edges so that the directed graph remains acyclic
- Longest path in a directed Acyclic graph | Dynamic Programming
- Count all possible walks from a source to a destination with exactly k edges
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Convert undirected connected graph to strongly connected directed graph
- Detect Cycle in a Directed Graph using BFS
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path with exactly k edges in a directed and weighted graph
- Algorithms | Graph Shortest Paths | Question 10
- Algorithms | Graph Shortest Paths | Question 11
- Algorithms | Graph Shortest Paths | Question 15
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.