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!
Add answer anonymously...
Top JUSPAY Front end Developer interview questions & answers
Popular interview questions of Front end 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