Challenge: Check if a Path Exists Between Two Vertices
Given a directed graph and two vertices, can you write a code to check if a path exists between the two vertices?
We'll cover the following
Problem statement
You have to implement the check_path()
function. It takes a source vertex and a destination vertex and tells us whether or not a path exists between the two.
Input
A directed graph, a source value, and a destination value.
Output
Returns True
if a path exists from the source to the destination.
Sample input
graph = {
0 -> 2
0 -> 5
2 -> 3
2 -> 4
5 -> 3
5 -> 6
3 -> 6
6 -> 7
6 -> 8
6 -> 4
7 -> 8
}
source = 0
destination = 7
Sample output
True
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.