{"id":26333,"date":"2017-10-26T21:06:37","date_gmt":"2017-10-26T15:36:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26333"},"modified":"2017-10-26T21:06:37","modified_gmt":"2017-10-26T15:36:37","slug":"find-path-two-vertices-directed-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-path-two-vertices-directed-graph\/","title":{"rendered":"C Programming &#8211; Find if there is a path between two vertices in a directed graph"},"content":{"rendered":"<p>Given a Directed Graph 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 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.<\/p>\n<p>Following are C++,Java and Python codes that use BFS for finding reachability of second vertex from first vertex.<\/p>\n<p>C++ Programming<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to check if there is exist a path between two vertices<br\/>\/\/ of a graph.<br\/>#include&lt;iostream&gt;<br\/>#include &lt;list&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ This class represents a directed graph using adjacency list <br\/>\/\/ representation<br\/>class Graph<br\/>{<br\/>    int V;    \/\/ No. of vertices<br\/>    list&lt;int&gt; *adj;    \/\/ Pointer to an array containing adjacency lists<br\/>public:<br\/>    Graph(int V);  \/\/ Constructor<br\/>    void addEdge(int v, int w); \/\/ function to add an edge to graph<br\/>    bool isReachable(int s, int d);  <br\/>};<br\/> <br\/>Graph::Graph(int V)<br\/>{<br\/>    this-&gt;V = V;<br\/>    adj = new list&lt;int&gt;[V];<br\/>}<br\/> <br\/>void Graph::addEdge(int v, int w)<br\/>{<br\/>    adj[v].push_back(w); \/\/ Add w to v\u2019s list.<br\/>}<br\/> <br\/>\/\/ A BFS based function to check whether d is reachable from s.<br\/>bool Graph::isReachable(int s, int d)<br\/>{<br\/>    \/\/ Base case<br\/>    if (s == d)<br\/>      return true;<br\/> <br\/>    \/\/ Mark all the vertices as not visited<br\/>    bool *visited = new bool[V];<br\/>    for (int i = 0; i &lt; V; i++)<br\/>        visited[i] = false;<br\/> <br\/>    \/\/ Create a queue for BFS<br\/>    list&lt;int&gt; queue;<br\/> <br\/>    \/\/ Mark the current node as visited and enqueue it<br\/>    visited[s] = true;<br\/>    queue.push_back(s);<br\/> <br\/>    \/\/ it will be used to get all adjacent vertices of a vertex<br\/>    list&lt;int&gt;::iterator i;<br\/> <br\/>    while (!queue.empty())<br\/>    {<br\/>        \/\/ Dequeue a vertex from queue and print it<br\/>        s = queue.front();<br\/>        queue.pop_front();<br\/> <br\/>        \/\/ Get all adjacent vertices of the dequeued vertex s<br\/>        \/\/ If a adjacent has not been visited, then mark it visited<br\/>        \/\/ and enqueue it<br\/>        for (i = adj[s].begin(); i != adj[s].end(); ++i)<br\/>        {<br\/>            \/\/ If this adjacent node is the destination node, then <br\/>            \/\/ return true<br\/>            if (*i == d)<br\/>                return true;<br\/> <br\/>            \/\/ Else, continue to do BFS<br\/>            if (!visited[*i])<br\/>            {<br\/>                visited[*i] = true;<br\/>                queue.push_back(*i);<br\/>            }<br\/>        }<br\/>    }<br\/>     <br\/>    \/\/ If BFS is complete without visiting d<br\/>    return false;<br\/>}<br\/> <br\/>\/\/ Driver program to test methods of graph class<br\/>int main()<br\/>{<br\/>    \/\/ Create a graph given in the above diagram<br\/>    Graph g(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, v = 3;<br\/>    if(g.isReachable(u, v))<br\/>        cout&lt;&lt; &quot;\\n There is a path from &quot; &lt;&lt; u &lt;&lt; &quot; to &quot; &lt;&lt; v;<br\/>    else<br\/>        cout&lt;&lt; &quot;\\n There is no path from &quot; &lt;&lt; u &lt;&lt; &quot; to &quot; &lt;&lt; v;<br\/> <br\/>    u = 3, v = 1;<br\/>    if(g.isReachable(u, v))<br\/>        cout&lt;&lt; &quot;\\n There is a path from &quot; &lt;&lt; u &lt;&lt; &quot; to &quot; &lt;&lt; v;<br\/>    else<br\/>        cout&lt;&lt; &quot;\\n There is no path from &quot; &lt;&lt; u &lt;&lt; &quot; to &quot; &lt;&lt; v;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\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>C 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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,82927,1,78385,73906],"tags":[78422,78421,78420,78419,78424,78423,78417,78418],"class_list":["post-26333","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-2","category-coding","category-connectivity","category-graph-algorithms","tag-check-if-path-exists-between-two-nodes","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-graph"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26333","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=26333"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26333\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}