Problem Description
Given a graph with 'N' nodes and 'M' unidirectional edges, along with two integers 'S' and 'D' representing the source and destination respectively, your task is to find all possible paths from 'S' to 'D'.
Explanation:
An ordered sequence of nodes { S, u1, u2...un, D } is considered a valid path between 'S' and 'D' if all nodes in the path are unique.
Input:
The first line contains one positive integer 'T', denoting the number of test cases. For each test case:
- The first line contains two integers 'N' and 'M', indicating the number of nodes and the number of edges respectively.
- The next 'M' lines each contain two space-separated integers 'u' and 'v', denoting an edge from node 'u' to node 'v'.
- The last line contains two integers 'S' and 'D', the source and destination nodes respectively.
Output:
For each test case, output:
- An integer 'N' representing the number of valid paths from 'S' to 'D'.
- Each of the following 'N' lines should list the nodes in one of the paths from 'S' to 'D'.
If there are multiple paths, they should be printed in lexicographically sorted order.
Example:
Input:
T = 1, N = 4, M = 4, S = 0, D = 3
Edges:
0 1
1 2
2 3
2 0
Output:
2
0 1 2 3
0 2 3
In this example graph, there are two paths from 0 to 3.
Constraints:
- 1 <= T <= 5
- 1 <= N <= 5
- 1 <= M <= 10
- 0 <= u, v, S, D <= N - 1
Note:
You don't need to print anything; implement the function and return the answer.
AnswerBot
3d
Find all possible paths from a source node to a destination node in a graph with unique nodes.
Create a graph using the given nodes and edges
Implement a depth-first search (DFS) algorithm to find all p...read more
Help your peers!
Add answer anonymously...
Top Salesforce Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Salesforce Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app