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
4mo
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...
Salesforce Software Developer interview questions & answers
A Software Developer was asked Q. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer was asked Q. What is the difference between Early Binding and Late Binding in C++?
A Software Developer was asked Q. What is meant by multitasking and multithreading in operating systems?
Popular interview questions of Software Developer
A Software Developer was asked Q1. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer was asked Q2. What is the difference between Early Binding and Late Binding in C++?
A Software Developer was asked Q3. What is meant by multitasking and multithreading in operating systems?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

