


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'.
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains two space-separated integers, ‘V’, and ‘E’, which denote the number of vertices and edges in the graph.
The next 'E' lines will denote the edges of the graph where every edge is defined by two space-separated integers 'Edges[i][0]’ and 'Edges[i][1]', which signifies an edge from vertex 'Edges[i][0]’ to vertex 'Edges[i][1]’.
The last line of each test case contains two integers, ‘source’ and ‘destination’.
For each test case, print 'true' if there exists a path from vertex 'source' to 'destination'. Otherwise, print 'false'.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= V, E <= 10 ^ 5
0 <= Edges[i][0], Edges[i][1] < V
0 <= source, destination < V
Time Limit: 1 sec
In this approach, we are going to use DFS(Depth-first search). We have to make a visited array isVisited which checks whether the particular node number is visited or not.
Initialize all entries of the isVisited array with 0. Now we have to use the recursive function here. The first call is made on the node source, and in the recursive function, mark this node as visited and checks whether it is the node destination or not. If yes, we will return true; otherwise, we will make recursive calls on all its adjacent unvisited nodes. If any of the calls return true then return we will return true, else return false.
In this approach, we are going to use BFS(Breadth-first search). We will take a queue that stores the integer corresponding to node number and take a visited array that checks whether that particular node is visited or not till now.
Initialize the isVisited array with all entries equal to 0. We will insert the source node number in the queue and mark it visited by making isVisited[source] as 1. We will iterate till the queue is not empty.
If we find the destination node, it means there exists some path, so we will return true otherwise, return false.
Algorithm: