Print All Paths Problem Statement

In this problem, you are provided with a graph consisting of 'N' nodes and 'M' unidirectional edges. Additionally, two integers 'S' and 'D' are given, representing the source and destination respectively. Your task is to identify all unique paths from the source node 'S' to the destination node 'D'.

Note:

For a path to be considered valid, all nodes in the ordered set { S, u1, u2,..., un, D} must be unique.

Example:

Input:
N = 4,
M = 4,
S = 0,
D = 3
Explanation:

Consider the following paths:

  • The path 0 -> 1 -> 3 is valid as all nodes are unique.
  • The path 0 -> 1 -> 2 -> 1 -> 3 is invalid because node 1 is repeated.

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ N ≤ 5
  • 1 ≤ M ≤ 10
  • 0 ≤ u, v, S, D ≤ N-1
  • Time Limit: 1 sec.

Input:

The first line consists of a single integer 'T', indicating the number of test cases. Each test case follows the format:

The first line contains two integers 'N' and 'M', representing the number of nodes and edges, respectively.
The subsequent 'M' lines contain two integers 'u' and 'v', depicting a directed edge from 'u' to 'v'.
The last line contains two integers 'S' and 'D', denoting the source and destination nodes.

Output:

For each test case, output consists of:
An integer 'N', representing the total number of valid paths from 'S' to 'D'.
The following 'N' lines list all nodes from 'S' to 'D' in the 'i-th' valid path (1 ≤ i ≤ N).
If multiple paths exist, they should be sorted in lexicographical order.

Note:

You are not required to print anything. Implement the function as specified.
AnswerBot
4mo

The task is to find all unique paths from a source node to a destination node in a graph.

  • Identify all unique paths from source node to destination node in a graph

  • Ensure all nodes in the path are uniqu...read more

Help your peers!
Select
Add answer anonymously...

Nagarro Software Developer interview questions & answers

A Software Developer was asked 8mo agoQ. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ. Is the directory a file?
A Software Developer was asked 9mo agoQ. What is memory segmentation?

Popular interview questions of Software Developer

A Software Developer was asked 8mo agoQ1. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ2. Is the directory a file?
A Software Developer was asked 9mo agoQ3. What is memory segmentation?
Nagarro Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits