Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second.
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.
- The implementation below is using BFS.
- Create a queue and a visited array initially filled with 0, of size V where V is number of vertices.
- Insert the starting node in the queue, i.e. push u in the queue and mark u as visited.
- Run a loop until the queue is not empty.
- 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 unvisted vertices in the queue and mark them as visited.
- 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.
There is a path from 1 to 3 There is no path from 3 to 1
- Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.
- Space Compelxity: O(V).
There can be atmost V elements in the queue. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if there is a path between two vertices in a directed graph | Set 2
- Find if there is a path between two vertices in an undirected graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Longest Path in a Directed Acyclic Graph
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Path with minimum XOR sum of edges in a directed graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path in a directed graph by Dijkstra’s algorithm
- Shortest path with exactly k edges in a directed and weighted graph
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Longest path in a directed Acyclic graph | Dynamic Programming
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Find dependencies of each Vertex in a Directed Graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Find the number of paths of length K in a directed graph
- Find two disjoint good sets of vertices in a given graph
- Convert undirected connected graph to strongly connected directed graph
- Clone a Directed Acyclic Graph