{"id":26415,"date":"2017-10-26T22:08:36","date_gmt":"2017-10-26T16:38:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26415"},"modified":"2017-10-26T22:08:36","modified_gmt":"2017-10-26T16:38:36","slug":"c-algorithm-find-maximum-number-edge-disjoint-paths-two-vertices-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-find-maximum-number-edge-disjoint-paths-two-vertices-2\/","title":{"rendered":"Cpp Algorithm &#8211; Find maximum number of edge disjoint paths between two vertices"},"content":{"rendered":"<p>Given a directed graph and two vertices in it, source \u2018s\u2019 and destination \u2018t\u2019, find out the maximum number of edge disjoint paths from s to t.<span id=\"more-123305\"><\/span> Two paths are said edge disjoint if they don\u2019t share any edge.<\/p>\n<p>&nbsp;<\/p>\n<p>There can be maximum two edge disjoint paths from source 0 to destination 7 in the above graph. Two edge disjoint paths are highlighted below in red and blue colors are 0-2-6-7 and 0-3-6-5-7.<\/p>\n<p>&nbsp;<\/p>\n<p>Note that the paths may be different, but the maximum number is same. For example, in the above diagram, another possible set of paths is 0-1-2-6-7 and 0-3-6-5-7 respectively.<\/p>\n<p>This problem can be solved by reducing it to <a href=\"http:\/\/www.geeksforgeeks.org\/ford-fulkerson-algorithm-for-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">maximum flow problem<\/a>. Following are steps.<br \/>\n<strong>1)<\/strong> Consider the given source and destination as source and sink in flow network. Assign unit capacity to each edge.<br \/>\n<strong>2)<\/strong> Run Ford-Fulkerson algorithm to find the maximum flow from source to sink.<br \/>\n<strong>3)<\/strong> The maximum flow is equal to the maximum number of edge-disjoint paths.<\/p>\n<p>When we run Ford-Fulkerson, we reduce the capacity by a unit. Therefore, the edge can not be used again. So the maximum flow is equal to the maximum number of edge-disjoint paths.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of the above algorithm. Most of the code is taken from <a href=\"http:\/\/www.geeksforgeeks.org\/ford-fulkerson-algorithm-for-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p><strong>C++ Programming:<\/strong><\/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 find maximum number of edge disjoint paths<br\/>#include &lt;iostream&gt;<br\/>#include &lt;limits.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;queue&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Number of vertices in given graph<br\/>#define V 8<br\/> <br\/>\/* Returns true if there is a path from source &#039;s&#039; to sink &#039;t&#039; in<br\/>  residual graph. Also fills parent[] to store the path *\/<br\/>bool bfs(int rGraph[V][V], int s, int t, int parent[])<br\/>{<br\/>    \/\/ Create a visited array and mark all vertices as not visited<br\/>    bool visited[V];<br\/>    memset(visited, 0, sizeof(visited));<br\/> <br\/>    \/\/ Create a queue, enqueue source vertex and mark source vertex<br\/>    \/\/ as visited<br\/>    queue &lt;int&gt; q;<br\/>    q.push(s);<br\/>    visited[s] = true;<br\/>    parent[s] = -1;<br\/> <br\/>    \/\/ Standard BFS Loop<br\/>    while (!q.empty())<br\/>    {<br\/>        int u = q.front();<br\/>        q.pop();<br\/> <br\/>        for (int v=0; v&lt;V; v++)<br\/>        {<br\/>            if (visited[v]==false &amp;&amp; rGraph[u][v] &gt; 0)<br\/>            {<br\/>                q.push(v);<br\/>                parent[v] = u;<br\/>                visited[v] = true;<br\/>            }<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ If we reached sink in BFS starting from source, then return<br\/>    \/\/ true, else false<br\/>    return (visited[t] == true);<br\/>}<br\/> <br\/>\/\/ Returns tne maximum number of edge-disjoint paths from s to t.<br\/>\/\/ This function is copy of forFulkerson() discussed at http:\/\/goo.gl\/wtQ4Ks<br\/>int findDisjointPaths(int graph[V][V], int s, int t)<br\/>{<br\/>    int u, v;<br\/> <br\/>    \/\/ Create a residual graph and fill the residual graph with<br\/>    \/\/ given capacities in the original graph as residual capacities<br\/>    \/\/ in residual graph<br\/>    int rGraph[V][V]; \/\/ Residual graph where rGraph[i][j] indicates<br\/>                     \/\/ residual capacity of edge from i to j (if there<br\/>                     \/\/ is an edge. If rGraph[i][j] is 0, then there is not)<br\/>    for (u = 0; u &lt; V; u++)<br\/>        for (v = 0; v &lt; V; v++)<br\/>             rGraph[u][v] = graph[u][v];<br\/> <br\/>    int parent[V];  \/\/ This array is filled by BFS and to store path<br\/> <br\/>    int max_flow = 0;  \/\/ There is no flow initially<br\/> <br\/>    \/\/ Augment the flow while tere is path from source to sink<br\/>    while (bfs(rGraph, s, t, parent))<br\/>    {<br\/>        \/\/ Find minimum residual capacity of the edges along the<br\/>        \/\/ path filled by BFS. Or we can say find the maximum flow<br\/>        \/\/ through the path found.<br\/>        int path_flow = INT_MAX;<br\/> <br\/>        for (v=t; v!=s; v=parent[v])<br\/>        {<br\/>            u = parent[v];<br\/>            path_flow = min(path_flow, rGraph[u][v]);<br\/>        }<br\/> <br\/>        \/\/ update residual capacities of the edges and reverse edges<br\/>        \/\/ along the path<br\/>        for (v=t; v != s; v=parent[v])<br\/>        {<br\/>            u = parent[v];<br\/>            rGraph[u][v] -= path_flow;<br\/>            rGraph[v][u] += path_flow;<br\/>        }<br\/> <br\/>        \/\/ Add path flow to overall flow<br\/>        max_flow += path_flow;<br\/>    }<br\/> <br\/>    \/\/ Return the overall flow (max_flow is equal to maximum<br\/>    \/\/ number of edge-disjoint paths)<br\/>    return max_flow;<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    \/\/ Let us create a graph shown in the above example<br\/>    int graph[V][V] = { {0, 1, 1, 1, 0, 0, 0, 0},<br\/>                        {0, 0, 1, 0, 0, 0, 0, 0},<br\/>                        {0, 0, 0, 1, 0, 0, 1, 0},<br\/>                        {0, 0, 0, 0, 0, 0, 1, 0},<br\/>                        {0, 0, 1, 0, 0, 0, 0, 1},<br\/>                        {0, 1, 0, 0, 0, 0, 0, 1},<br\/>                        {0, 0, 0, 0, 0, 1, 0, 1},<br\/>                        {0, 0, 0, 0, 0, 0, 0, 0}<br\/>                      };<br\/> <br\/>    int s = 0;<br\/>    int t = 7;<br\/>    cout &lt;&lt; &quot;There can be maximum &quot; &lt;&lt; findDisjointPaths(graph, s, t)<br\/>         &lt;&lt; &quot; edge-disjoint paths from &quot; &lt;&lt; s &lt;&lt;&quot; to &quot;&lt;&lt; t ;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>There can be maximum 2 edge-disjoint paths from 0 to 7<\/pre>\n<p><strong>Time Complexity: <\/strong>Same as time complexity of Edmonds-Karp implementation of Ford-Fulkerson<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Algorithm &#8211; Find maximum number of edge disjoint paths between two vertices &#8211; Graph Algorithm &#8211; Given a directed graph and two vertices in it, source<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,73906,78429],"tags":[78743,78742,78744,78749,78737,78741,78738,78745,78747,78736,78740,78739,78748],"class_list":["post-26415","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-graph-algorithms","category-maximum-flow","tag-disjoint-paths-in-a-network","tag-edge-disjoint-definition","tag-edge-disjoint-meaning","tag-edge-disjoint-path-algorithm","tag-edge-disjoint-path-problem","tag-edge-disjoint-paths-example","tag-edge-disjoint-paths-undirected-graph","tag-edge-disjoint-subgraph","tag-node-disjoint-path","tag-vertex-disjoint-path","tag-vertex-disjoint-path-problem","tag-vertex-disjoint-paths-max-flow","tag-what-is-edge-disjoint-path"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26415","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=26415"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26415\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}