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.
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.
- Find maximum number of edge disjoint paths between two vertices
- Shortest paths from all vertices to a destination
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count number of times each Edge appears in all possible paths of a given Tree
- Print all possible paths from top left to bottom right of a mXn matrix
- Check whether the cost of going from any node to any other node via all possible paths is same
- Print all possible paths from the first row to the last row in a 2D array
- Print all possible paths in a DAG from vertex whose indegree is 0
- Find if there is a path between two vertices in a directed graph
- Minimum number of edges between two vertices of a Graph
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Find if there is a path between two vertices in a directed graph | Set 2
- Number of pairs such that path between pairs has the two vertices A and B
- Find if there is a path between two vertices in an undirected graph
- Calculate number of nodes between two vertices in an acyclic Graph by DFS method
- Minimum number of edges between two vertices of a graph using DFS
- Number of Unicolored Paths between two nodes
- Check if given path between two nodes of a graph represents a shortest paths
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.