Given a directed graph, a source vertex ‘s’ and a destination vertex ‘d’, print all paths from given ‘s’ to ‘d’.
Consider the following directed graph. Let the s be 2 and d be 3. There are 4 different paths from 2 to 3.
The idea is to do Depth First Traversal of given directed graph. Start the traversal from source. Keep storing the visited vertices in an array say ‘path’. If we reach the destination vertex, print contents of path. The important thing is to mark current vertices in path as visited also, so that the traversal doesn’t go in a cycle.
Following is implementation of above idea.
Following are all different paths from 2 to 3 2 0 1 3 2 0 3 2 1 3
This article is contributed by Shivam Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Print all paths from a given source to a destination using BFS
- Number of Walks from source to destination
- Count all possible walks from a source to a destination with exactly k edges
- Minimum edges to reverse to make path from a source to a destination
- Shortest paths from all vertices to a destination
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Minimum cost path from source node to destination node via an intermediate node
- Print all possible paths from top left to bottom right of a mXn matrix
- Number of decisions to reach destination
- Minimum steps to reach a destination
- Find the minimum cost to reach destination using a train
- Count number of ways to reach destination in a maze
- Find if there is a path of more than k length from a source
- Minimum distance to the corner of a grid from source
- Multi Source Shortest Path in Unweighted Graph