Count the total number of ways or paths that exist between two vertices in a directed graph. These paths doesn’t contain a cycle, the simple enough reason is that a cylce contain infinite number of paths and hence they create problem.
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
The problem can be solved using backtracking, that is we take a path and start walking it, if it leads us to the destination vertex then we count the path and backtrack to take another path. If the path doesn’t leads us to the destination vertex, we discard the path.
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 gives us four paths between source(A) and destination(E) vertex.
Problem Associated with this: Now if we add just one more edge between C and B, it would make a cycle (B -> D -> C -> B). And hence we could loop the cycles any number of times to get a new path, and there would be infinitely many paths because of the cycle.
- Find maximum number of edge disjoint paths between two vertices
- Construct a graph from given degrees of all vertices
- Longest path between any pair of vertices
- Articulation Points (or Cut Vertices) in a Graph
- Minimum number of edges between two vertices of a Graph
- Maximum and minimum isolated vertices in a graph
- Find if there is a path between two vertices in a directed graph
- Minimum initial vertices to traverse whole matrix with given conditions
- Largest subset of Graph vertices with edges of 2 or more colors
- Check whether given degrees of vertices represent a Graph or Tree
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Number of Unicolored Paths between two nodes
- Print all paths from a given source to a destination
- Print all paths from a given source to a destination using BFS
- Maximum product of two non-intersecting 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.