You are given a graph with ‘N’ nodes and ‘M’ unidirectional edges. Also you are given two integers ‘S’ and ‘D’ denoting the source and destination. Your task is to find all the paths from ‘S’ to ‘D’.
Note: An ordered set of nodes { S, u1, u2...un, D} is a valid path between ‘S’ and ‘D’, if all nodes along the path are unique.
For example:
For given N = 4, M = 4, S = 0 and D =3.

In the above example, the path 0 -> 1 - > 3 is a valid path as all nodes along the path are unique and the path 0 -> 1 -> 2 -> 1 -> 3 is not a valid path because node 1 is visited twice.
The first line contains one positive integer ‘T’, denoting the number of test cases, then ‘T’ test cases follows.
The first line of each test case contains two integers ‘N’ and ‘M’, denoting the number of nodes and the number of edges.
The next ‘M’ lines of each test case contains two space-separated integers ’u’ and ‘v’, denoting the edge between ‘u’ and ‘v’.
The last line of each test case contains two integers ‘S’ and ‘D’ denoting the source and destination.
Output Format:
The first line of each test case contains an integer ‘N’ , denoting the number of valid paths from ‘S’ to ‘D’.
The next ‘N’ line of each test case contains the all nodes from ‘S’ to ‘D’ along the ‘ith’ path where 1 <= i <= N.
If there are more than one path then the ith path should be lexicographically smaller than (i + 1) path.
Output of each test case will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 5
1 <= M <= 10
0 <= u, v , S, D <= N - 1
Time Limit: 1 sec.
2
4 4
0 1
1 2
1 3
2 3
0 3
5 3
0 1
1 2
3 4
0 3
2
0 1 2 3
0 1 3
0
In the first test case, the graph like this:

As we can observe, there are two valid path
P1 : 0 -> 1 -> 2 -> 3
P2 : 0 -> 1 -> 3
Note : Path P2: 0 -> 1 -> 3 is lexicographically larger than P1: 0 -> 1 -> 2 -> 3, so path P1 is printed before path P2.
In the second test case, the graph looks like this:

As we can observe, there are two disconnected graphs - {0, 1, 2} and {3, 4}. There is no path between 0 and 3.
2
5 4
0 0
0 1
1 3
4 2
3 0
2 2
0 0
1 1
0 0
1
3 1 0
1
0
Can you use backtracking and try out all possible paths?
The idea is very simple: we will perform dfs from a given source node and try out all possible paths using the backtracking concept.
The steps are as follows:
Let ‘allAllPaths(n, m, edges, src, des)’ be the function that returns a 2D array that contains all the possible paths.
O(N ^ N), where ‘N’ is the number of vertices.
The time complexity is exponential. From each vertex there are ‘N’ vertices that can be visited from the current vertex.
O(N ^ N), where ‘N’ is the number of vertices.
To store the paths ( N ^ N) space is needed. Hence the space complexity is O( N ^ N).