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.
- Shortest paths from all vertices to a destination
- Find maximum number of edge disjoint paths between two vertices
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Articulation Points (or Cut Vertices) in a Graph
- Finding in and out degrees of all vertices in a graph
- Longest path between any pair of vertices
- Construct a graph from given degrees of all vertices
- Number of trees whose sum of degrees of all the vertices is L
- Minimum number of edges between two vertices of a graph using DFS
- Minimum Operations to make value of all vertices of the tree Zero
- Maximum and minimum isolated vertices in a graph
- Find if there is a path between two vertices in a directed graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a Graph
- Minimum initial vertices to traverse whole matrix with given conditions
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.