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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Longest Path in a Directed Acyclic Graph
- Path with minimum XOR sum of edges in a directed graph
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- 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 of Simple Path between two nodes in a Directed and Weighted 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