The main idea uses DFS to explore all paths from the source to the destination in a directed graph. It recursively builds paths, stores them when the destination is reached, and backtracks to explore alternative routes.
Output
2 0 3
2 0 1 3
2 1 3
Time Complexity: O(2V ), where V is number of vertices. There can be at most 2V paths in the graph Auxiliary Space: O(V + E)
[Approach 2] Using BFS
The main idea is to use Breadth-First Search (BFS) to find all paths from a source to a destination in a directed graph. It uses a queue to explore all possible paths level by level. Each path is stored and extended by adding adjacent nodes, and when the destination is reached, the current path is added to the result.
Output
2 0 3
2 1 3
2 0 1 3
Time Complexity: O(2V), where V is number of vertices. There can be at most 2V paths in the graph Auxiliary Space: O(V + E)