Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Check If Path Exists
EASY
10 mins
6 upvotes
Graph
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Graph
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Check If Path Exists

Contributed by
Waris
Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 90 %
Share
6 upvotes

Problem Statement

You are given a directed and unweighted graph of 'V' vertices and 'E' edges. All edges are given in a 2-dimensional array ‘Edges’ in which ‘Edges[i][0]’ and ‘Edges[i][1]’ contain an edge. Your task is to check if there exists a path from the vertex 'source' to 'destination'.

For Example:
Consider the number of vertices is 4 and number of edges is 3, and the array of edges is:
[ [0, 1]
  [1, 2] 
  [2, 3] ]
there exists one path between 0 and 2, which is 0 -> 1 -> 2. Hence, the answer is 'true'.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
3 3
0 1
1 2
1 0
0 2
4 2
1 2
0 3
0 2
Sample Output 1:
true 
false   
Explanation:
In test case 1:
In this, there are 3 vertices and 3 edges, and there is a path between 0 and 2 which is 0 -> 1 -> 2. Hence, the answer is true.

In test case 2:
In this, there are 4 vertices and 2 edges, and there is no path between 0 and 2. Hence, the answer is false.
Sample Input 2:
1
4 5
0 1
0 2
1 2
2 0
2 3
3 1
Sample Output 2:
false 
Explanation:
There are 4 vertices and 5 edges, and there is no path between 3 and 1, so our answer is false.
Reset Code
Full screen
Auto
copy-code
Console