# Find if there is a path between two vertices in a directed graph

• Difficulty Level : Easy
• Last Updated : 29 Mar, 2022

Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second.
Example:

Consider the following Graph:

Input : (u, v) = (1, 3)
Output: Yes
Explanation: There is a path from 1 to 3, 1 -> 2 -> 3

Input : (u, v) = (3, 6)
Output: No
Explanation: There is no path from 3 to 6

Approach: Either Breadth First Search (BFS) or Depth First Search (DFS) can be used to find path between two vertices. Take the first vertex as source in BFS (or DFS), follow the standard BFS (or DFS). If the second vertex is found in our traversal, then return true else return false.

BFS Algorithm:

1. The implementation below is using BFS.
2. Create a queue and a visited array initially filled with 0, of size V where V is number of vertices.
3. Insert the starting node in the queue, i.e. push u in the queue and mark u as visited.
4. Run a loop until the queue is not empty.
5. Dequeue the front element of the queue. Iterate all its adjacent elements. If any of the adjacent element is the destination return true. Push all the adjacent and unvisited vertices in the queue and mark them as visited.
6. Return false as the destination is not reached in BFS.

Implementation: C++, Java and Python codes that use BFS for finding reachability of the second vertex from the first vertex.

## Javascript


Output
There is a path from 1 to 3
There is no path from 3 to 1

Complexity Analysis:

• Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.
• Space Complexity: O(V).
There can be atmost V elements in the queue. So the space needed is O(V).

DFS Algorithm:

1. if start==end return 1 since we have to reached our destination.

2. Mark start as visited.

3. Traverse directly connected vertices of start and recur the function dfs for every such unexplored vertex.

4. return 0 if we do not reach our destination.

Implementation:

## C++14

 #include using namespace std;typedef long long ll; vector adj[100000];bool visited[100000]; bool dfs(int start, int end){    if (start == end)        return true;    visited[start] = 1;    for (auto x : adj[start]) {        if (!visited[x])            if (dfs(x, end))                return true;    }    return false;} int main(){     int V = 4;    vector members = { 2, 5, 7, 9 };     int E = 4;    vector > connections        = { { 2, 9 }, { 7, 2 }, { 7, 9 }, { 9, 5 } };     for (int i = 0; i < E; i++)        adj[connections[i].first].push_back(            connections[i].second);     int sender = 7, receiver = 9;     if (dfs(sender, receiver))        cout << "1";    else        cout << "0";     return 0;}// this code is contributed by prophet1999
Output
1

Complexity Analysis:

Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.
Space Complexity: O(V).
There can be atmost V elements in the stack. So the space needed is O(V).

Trade-offs between BFS and DFS: Breadth-First search can be useful to find the shortest path between nodes, and depth-first search may traverse one adjacent node very deeply before ever going into immediate neighbours.
As an exercise, try an extended version of the problem where the complete path between two vertices is also needed.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up