Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second.
Input : (u, v) = (1, 3)
There is a path from 1 to 3, 1 -> 2 -> 3
Input : (u, v) = (3, 6)
There is no path from 3 to 6
- Create a boolean 2D matrix mat where mat[i][j] will be true if there is a path from vertex i to j.
- For every starting vertex i and ending vertex j iterate over all intermediate vertex k and do check if there is a path for i to j through k then mark mat[i][j] as true.
- Finally check if mat[u][v] is true then return true else return false.
Below is the implementation of the above approach :
Time Complexity : O ( V 3)
Auxiliary Space : O ( V 2)
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
- 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
- Path with minimum XOR sum of edges in a directed graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph
- Shortest path in a directed graph by Dijkstra’s algorithm
- 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 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
- Hierholzer's Algorithm for directed graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : princi singh