{"id":26343,"date":"2017-10-26T21:09:54","date_gmt":"2017-10-26T15:39:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26343"},"modified":"2018-10-29T12:11:01","modified_gmt":"2018-10-29T06:41:01","slug":"java-programming-find-path-two-vertices-directed-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-find-path-two-vertices-directed-graph\/","title":{"rendered":"Find if there is a path between two vertices in a directed graph"},"content":{"rendered":"<p>Given a <a href=\"https:\/\/www.wikitechy.com\/technology\/detect-cycle-directed-graph-using-colors\/\" target=\"_blank\" rel=\"noopener\">Directed Graph<\/a> and two vertices in it, check whether there is a path from the first given vertex to second. <span id=\"more-18750\"><\/span>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.<\/p>\n<p>We can either use <a href=\"https:\/\/www.wikitechy.com\/technology\/java-algorithm-breadth-first-traversal-bfs-graph\/\" target=\"_blank\" rel=\"noopener\">Breadth First Search<\/a> (BFS) or <a href=\"https:\/\/www.wikitechy.com\/technology\/python-algorithm-depth-first-traversal-dfs-graph\/\" target=\"_blank\" rel=\"noopener\">Depth First Search<\/a> (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.<\/p>\n<p>Following is <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-programming\" target=\"_blank\" rel=\"noopener\">Java code implementation<\/a>, that use BFS for finding reachability of second <a href=\"https:\/\/www.wikitechy.com\/technology\/java-program-vertex-cover-problem-introduction-approximate-algorithm\/\" target=\"_blank\" rel=\"noopener\">vertex<\/a> from first vertex.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-31627\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/10\/directed-graph.png\" alt=\"directed-graph\" width=\"360\" height=\"208\" \/><\/p>\n<h3 id=\"java-programming-to-find-if-there-is-a-path-between-two-vertices-in-a-directed-graph\"><span style=\"color: #003366;\">Java\u00a0Programming to Find if there is a path between two vertices in a directed graph:<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to check if there is exist a path between two vertices<br\/>\/\/ of a graph.<br\/>import java.io.*;<br\/>import java.util.*;<br\/>import java.util.LinkedList;<br\/> <br\/><br\/>class Graph<br\/>{<br\/>    private int V;   <br\/>    private LinkedList&lt;Integer&gt; adj[]; <br\/> <br\/><br\/>    Graph(int v)<br\/>    {<br\/>        V = v;<br\/>        adj = new LinkedList[v];<br\/>        for (int i=0; i&lt;v; ++i)<br\/>            adj[i] = new LinkedList();<br\/>    }<br\/> <br\/><br\/>    void addEdge(int v,int w)  {   adj[v].add(w);   }<br\/> <br\/>   <br\/>    Boolean isReachable(int s, int d)<br\/>    {<br\/>        LinkedList&lt;Integer&gt;temp;<br\/> <br\/>       <br\/>        boolean visited[] = new boolean[V];<br\/> <br\/>   <br\/>        LinkedList&lt;Integer&gt; queue = new LinkedList&lt;Integer&gt;();<br\/> <br\/>        <br\/>        visited[s]=true;<br\/>        queue.add(s);<br\/> <br\/>      <br\/>        Iterator&lt;Integer&gt; i;<br\/>        while (queue.size()!=0)<br\/>        {<br\/>           <br\/>            s = queue.poll();<br\/> <br\/>            int n;<br\/>            i = adj[s].listIterator();<br\/> <br\/>            <br\/>            while (i.hasNext())<br\/>            {<br\/>                n = i.next();<br\/> <br\/>                <br\/>                if (n==d)<br\/>                    return true;<br\/> <br\/>               <br\/>                if (!visited[n])<br\/>                {<br\/>                    visited[n] = true;<br\/>                    queue.add(n);<br\/>                }<br\/>            }<br\/>        }<br\/> <br\/>    <br\/>        return false;<br\/>    }<br\/> <br\/>   <br\/>    public static void main(String args[])<br\/>    {<br\/>        <br\/>        Graph g = new Graph(4);<br\/>        g.addEdge(0, 1);<br\/>        g.addEdge(0, 2);<br\/>        g.addEdge(1, 2);<br\/>        g.addEdge(2, 0);<br\/>        g.addEdge(2, 3);<br\/>        g.addEdge(3, 3);<br\/> <br\/>        int u = 1;<br\/>        int v = 3;<br\/>        if (g.isReachable(u, v))<br\/>            System.out.println(&quot;There is a path from &quot; + u +&quot; to &quot; + v);<br\/>        else<br\/>            System.out.println(&quot;There is no path from &quot; + u +&quot; to &quot; + v);;<br\/> <br\/>        u = 3;<br\/>        v = 1;<br\/>        if (g.isReachable(u, v))<br\/>            System.out.println(&quot;There is a path from &quot; + u +&quot; to &quot; + v);<br\/>        else<br\/>            System.out.println(&quot;There is no path from &quot; + u +&quot; to &quot; + v);;<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<h3 id=\"output\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre> There is a path from 1 to 3\r\n There is no path from 3 to 1<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Find if there is a path between two vertices in a directed graph &#8211; check whether there is a path from the first given vertex to second. <\/p>\n","protected":false},"author":1,"featured_media":31628,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,78385,73906,2139],"tags":[78426,78421,78420,78419,78424,78423,78417,78425],"class_list":["post-26343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","category-coding","category-connectivity","category-graph-algorithms","category-java","tag-bfs-find-all-paths","tag-find-all-paths-between-two-nodes-in-a-graph-c","tag-find-all-paths-between-two-nodes-in-a-graph-java","tag-find-all-paths-from-source-to-destination","tag-find-all-paths-from-source-to-destination-java","tag-find-all-paths-in-a-directed-graph","tag-find-all-possible-paths-between-two-vertices-on-a-graph","tag-find-path-between-two-nodes-in-a-tree"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26343"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26343\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31628"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}