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.

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 Java code implementation, that use BFS for finding reachability of second vertex from first vertex.

directed-graph

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

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20check%20if%20there%20is%20exist%20a%20path%20between%20two%20vertices%0A%2F%2F%20of%20a%20graph.%0Aimport%20java.io.*%3B%0Aimport%20java.util.*%3B%0Aimport%20java.util.LinkedList%3B%0A%20%0A%0Aclass%20Graph%0A%7B%0A%20%20%20%20private%20int%20V%3B%20%20%20%0A%20%20%20%20private%20LinkedList%3CInteger%3E%20adj%5B%5D%3B%20%0A%20%0A%0A%20%20%20%20Graph(int%20v)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20V%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20adj%20%3D%20new%20LinkedList%5Bv%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cv%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20adj%5Bi%5D%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%7D%0A%20%0A%0A%20%20%20%20void%20addEdge(int%20v%2Cint%20w)%20%20%7B%20%20%20adj%5Bv%5D.add(w)%3B%20%20%20%7D%0A%20%0A%20%20%20%0A%20%20%20%20Boolean%20isReachable(int%20s%2C%20int%20d)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%3CInteger%3Etemp%3B%0A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20boolean%20visited%5B%5D%20%3D%20new%20boolean%5BV%5D%3B%0A%20%0A%20%20%20%0A%20%20%20%20%20%20%20%20LinkedList%3CInteger%3E%20queue%20%3D%20new%20LinkedList%3CInteger%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%3Dtrue%3B%0A%20%20%20%20%20%20%20%20queue.add(s)%3B%0A%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Iterator%3CInteger%3E%20i%3B%0A%20%20%20%20%20%20%20%20while%20(queue.size()!%3D0)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20queue.poll()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20n%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20adj%5Bs%5D.listIterator()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(i.hasNext())%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20i.next()%3B%0A%20%0A%20%20%20%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%20%20%20%20if%20(n%3D%3Dd)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%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%20%20%20%20if%20(!visited%5Bn%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bn%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.add(n)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Graph%20g%20%3D%20new%20Graph(4)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(0%2C%201)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(0%2C%202)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(1%2C%202)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(2%2C%200)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(2%2C%203)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(3%2C%203)%3B%0A%20%0A%20%20%20%20%20%20%20%20int%20u%20%3D%201%3B%0A%20%20%20%20%20%20%20%20int%20v%20%3D%203%3B%0A%20%20%20%20%20%20%20%20if%20(g.isReachable(u%2C%20v))%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22There%20is%20a%20path%20from%20%22%20%2B%20u%20%2B%22%20to%20%22%20%2B%20v)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22There%20is%20no%20path%20from%20%22%20%2B%20u%20%2B%22%20to%20%22%20%2B%20v)%3B%3B%0A%20%0A%20%20%20%20%20%20%20%20u%20%3D%203%3B%0A%20%20%20%20%20%20%20%20v%20%3D%201%3B%0A%20%20%20%20%20%20%20%20if%20(g.isReachable(u%2C%20v))%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22There%20is%20a%20path%20from%20%22%20%2B%20u%20%2B%22%20to%20%22%20%2B%20v)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22There%20is%20no%20path%20from%20%22%20%2B%20u%20%2B%22%20to%20%22%20%2B%20v)%3B%3B%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

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