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

Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second. For example, in the following graph, there is a path from vertex 1 to 3. As another example, there is no path from 3 to 0.

directed-graph

We can either use Breadth First Search (BFS) or Depth First Search (DFS) to find path between two vertices. Take the first vertex as source in BFS (or DFS), follow the standard BFS (or DFS). If we see the second vertex in our traversal, then return true. Else return false.

Following is Python code that use BFS for finding reachability of second vertex from first vertex.

Python Programming Find if there is a path between two vertices in a directed graph:

[pastacode lang=”python” manual=”%23%20program%20to%20check%20if%20there%20is%20exist%20a%20path%20between%20two%20vertices%0A%23%20of%20a%20graph%0A%20%0Afrom%20collections%20import%20defaultdict%0A%20%20%0A%23This%20class%20represents%20a%20directed%20graph%20using%20adjacency%20list%20representation%0Aclass%20Graph%3A%0A%20%20%0A%20%20%20%20def%20__init__(self%2Cvertices)%3A%0A%20%20%20%20%20%20%20%20self.V%3D%20vertices%20%23No.%20of%20vertices%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20defaultdict(list)%20%23%20default%20dictionary%20to%20store%20graph%0A%20%20%0A%20%20%20%20%23%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20def%20addEdge(self%2Cu%2Cv)%3A%0A%20%20%20%20%20%20%20%20self.graph%5Bu%5D.append(v)%0A%20%20%20%20%20%0A%20%20%20%20%23%20Use%20BFS%20to%20check%20path%20between%20s%20and%20d%0A%20%20%20%20def%20isReachable(self%2C%20s%2C%20d)%3A%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%5BFalse%5D*(self.V)%0A%20%20%0A%20%20%20%20%20%20%20%20%23%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20%20%20%20%20queue%3D%5B%5D%0A%20%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20source%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20queue.append(s)%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%20%3D%20True%0A%20%20%0A%20%20%20%20%20%20%20%20while%20queue%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23Dequeue%20a%20vertex%20from%20queue%20%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20queue.pop(0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20If%20this%20adjacent%20node%20is%20the%20destination%20node%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20then%20return%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20n%20%3D%3D%20d%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20True%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%20Else%2C%20continue%20to%20do%20BFS%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20self.graph%5Bn%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.append(i)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%23%20If%20BFS%20is%20complete%20without%20visited%20d%0A%20%20%20%20%20%20%20%20return%20False%0A%20%20%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Ag%20%3D%20Graph(4)%0Ag.addEdge(0%2C%201)%0Ag.addEdge(0%2C%202)%0Ag.addEdge(1%2C%202)%0Ag.addEdge(2%2C%200)%0Ag.addEdge(2%2C%203)%0Ag.addEdge(3%2C%203)%0A%20%0Au%20%3D1%3B%20v%20%3D%203%0A%20%0Aif%20g.isReachable(u%2C%20v)%3A%0A%20%20%20%20print(%22There%20is%20a%20path%20from%20%25d%20to%20%25d%22%20%25%20(u%2Cv))%0Aelse%20%3A%0A%20%20%20%20print(%22There%20is%20no%20path%20from%20%25d%20to%20%25d%22%20%25%20(u%2Cv))%0A%20%0Au%20%3D%203%3B%20v%20%3D%201%0Aif%20g.isReachable(u%2C%20v)%20%3A%0A%20%20%20%20print(%22There%20is%20a%20path%20from%20%25d%20to%20%25d%22%20%25%20(u%2Cv))%0Aelse%20%3A%0A%20%20%20%20print(%22There%20is%20no%20path%20from%20%25d%20to%20%25d%22%20%25%20(u%2Cv))” message=”” highlight=”” provider=”manual”/]

Output:

 There is a path from 1 to 3
 There is no path from 3 to 1
[ad type=”banner”]