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
- Finding in and out degrees of all vertices in a graph
- Longest path between any pair of vertices
- Articulation Points (or Cut Vertices) in a Graph
- Construct a graph from given degrees of all vertices
- Maximum and minimum isolated vertices in a graph
- Minimum number of edges between two vertices of a Graph
- Minimum Operations to make value of all vertices of the tree Zero
- Minimum number of edges between two vertices of a graph using DFS
- Number of Simple Graph with N Vertices and M Edges
- Find if there is a path between two vertices in a directed graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Check whether given degrees of vertices represent a Graph or Tree
- Minimum initial vertices to traverse whole matrix with given conditions
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.