Path Existence in Directed Graph

Given a directed and unweighted graph characterized by vertices 'V' and edges 'E', determine if a path exists from a specified 'source' vertex to a 'destination' vertex. The edges are represented in a 2-dimensional array 'Edges' where each entry Edges[i][0] to Edges[i][1] represents a direct connection.

Example:

Input:
Vertices: 4, Edges: 3
Edge List: [[0, 1], [1, 2], [2, 3]]
Source: 0, Destination: 2
Output:
true
Explanation:

There is a path from the source (0) to the destination (2) following 0 -> 1 -> 2.

Input Format:

The first line contains an integer ‘T’, representing the number of test cases.
The first line of each test case contains two integers 'V' and 'E'.
The subsequent 'E' lines contain two integers per line, describing the directed edge from 'Edges[i][0]' to 'Edges[i][1]'.
The last line of each test case specifies two integers, the 'source' and 'destination' vertices.

Output Format:

Output 'true' or 'false' for each test case regarding the existence of a path from the 'source' to 'destination', each on a new line.

Constraints:

  • 1 <= T <= 5
  • 1 <= V, E <= 10^5
  • 0 <= Edges[i][0], Edges[i][1] < V
  • 0 <= source, destination < V
  • Time Limit: 1 sec

Note:

Implement the function; there is no need to handle input/output operations directly, as they are managed elsewhere.

AnswerBot
1y

The task is to check if there exists a path from a given source vertex to a destination vertex in a directed and unweighted graph.

  • Read the number of test cases.

  • For each test case, read the number of v...read more

Help your peers!
Select
Add answer anonymously...
Front end 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