Count the total number of ways or paths that exist between two vertices in a directed graph. These paths don’t contain a cycle, the simple enough reason is that a cycle contains an infinite number of paths and hence they create a problem.
For the following Graph: Input: Count paths between A and E Output : Total paths between A and E are 4 Explanation: The 4 paths between A and E are: A -> E A -> B -> E A -> C -> E A -> B -> D -> C -> E Input : Count paths between A and C Output : Total paths between A and C are 2 Explanation: The 2 paths between A and C are: A -> C A -> B -> D -> C
The problem can be solved using backtracking, that says take a path and start walking on it and check if it leads us to the destination vertex then count the path and backtrack to take another path. If the path doesn’t lead to the destination vertex, discard the path.
This type of graph traversal is called Backtracking.
Backtracking for above graph can be shown like this:
The red color vertex is the source vertex and the light-blue color vertex is destination, rest are either intermediate or discarded paths.
This give four paths between source(A) and destination(E) vertex.
Why this solution will not work for a graph which contains cycles?
The Problem Associated with this is that now if one more edge is added between C and B, it would make a cycle (B -> D -> C -> B). And hence after every cycle through the loop, the length path will increase and that will be considered as a different path, and there would be infinitely many paths because of the cycle.
- Create a recursive function that takes index of node of a graph and the destination index. Keep a global or a static variable count to store the count.
- If the current nodes is the destination increase the count.
- Else for all the adjacent nodes, i.e. nodes that are accessible from the current node, call the recursive function with the index of adjacent node and the destination.
- Print the Count.
- Time Complexity: O(N!).
If the graph is complete then there can be around N! recursive calls, so the time Complexity is O(N!)
- Space Complexity: O(1).
Since no extra space is required.
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
- Shortest paths from all vertices to a destination
- Find maximum number of edge disjoint paths between two vertices
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Count even paths in Binary Tree
- Count of exponential paths in a Binary Tree
- Number of trees whose sum of degrees of all the vertices is L
- Longest path between any pair of vertices
- Construct a graph from given degrees of all vertices
- Articulation Points (or Cut Vertices) in a Graph
- Finding in and out degrees of all vertices in a graph
- Find if there is a path between two vertices in a directed graph
- Minimum Operations to make value of all vertices of the tree Zero
- Maximum and minimum isolated vertices in a graph
- Minimum number of edges between two vertices of a Graph
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.