{"id":26453,"date":"2017-12-20T21:11:27","date_gmt":"2017-12-20T15:41:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26453"},"modified":"2017-12-20T21:11:27","modified_gmt":"2017-12-20T15:41:27","slug":"cpp-algorithm-find-minimum-s-t-cut-flow-network","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-algorithm-find-minimum-s-t-cut-flow-network\/","title":{"rendered":"Cpp Algorithm &#8211; Find minimum s-t cut in a flow network"},"content":{"rendered":"<p>In a flow network, an s-t cut is a cut that requires the source \u2018s\u2019 and the sink \u2018t\u2019 to be in different subsets, and it consists of edges going from the source\u2019s side to the sink\u2019s side. <span id=\"more-120900\"><\/span>The capacity of an s-t cut is defined by the sum of capacity of each edge in the cut-set. (Source: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cut_(graph_theory)\" target=\"_blank\" rel=\"noopener noreferrer\">Wiki<\/a>)<br \/>\nThe problem discussed here is to find minimum capacity s-t cut of the given network. Expected output is all edges of the minimum cut.<\/p>\n<p>For example, in the following flow network, example s-t cuts are {{0 ,1}, {0, 2}}, {{0, 2}, {1, 2}, {1, 3}}, etc. The minimum s-t cut is {{1, 3}, {4, 3}, {4 5}} which has capacity as 12+7+4 = 23.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Minimum Cut and Maximum Flow<\/strong><br \/>\nLike <a href=\"http:\/\/www.geeksforgeeks.org\/maximum-bipartite-matching\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maximum Bipartite Matching<\/a>, this is another problem which can solved using <a href=\"http:\/\/www.geeksforgeeks.org\/ford-fulkerson-algorithm-for-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">Ford-Fulkerson Algorithm<\/a>. This is based on max-flow min-cut theorem.<\/p>\n<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Max-flow_min-cut_theorem\" target=\"_blank\" rel=\"noopener\">max-flow min-cut theorem<\/a> states that in a flow network, the amount of maximum flow is equal to capacity of the minimum cut. See <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-3\/p\/itmczynzhyhxv2gs?pid=9788120340077&amp;affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a> for proof of this theorem.<\/p>\n<p>From Ford-Fulkerson, we get capacity of minimum cut. How to print all edges that form the minimum cut? The idea is to use <a href=\"http:\/\/www.geeksforgeeks.org\/ford-fulkerson-algorithm-for-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">residual graph<\/a>.<\/p>\n<p>Following are steps to print all edges of minimum cut.<\/p>\n<p><strong>1)<\/strong> Run Ford-Fulkerson algorithm and consider the final <a href=\"http:\/\/www.geeksforgeeks.org\/ford-fulkerson-algorithm-for-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener noreferrer\">residual graph<\/a>.<\/p>\n<p><strong>2)<\/strong> Find the set of vertices that are reachable from source in the residual graph.<\/p>\n<p><strong>3)<\/strong> All edges which are from a reachable vertex to non-reachable vertex are minimum cut edges. Print all such edges.<\/p>\n<p>Following is C++ implementation of the above approach.<\/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 for finding minimum cut using Ford-Fulkerson<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 6<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\/>int 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\/>\/\/ A DFS based function to find all reachable vertices from s.  The function<br\/>\/\/ marks visited[i] as true if i is reachable from s.  The initial values in<br\/>\/\/ visited[] must be false. We can also use BFS to find reachable vertices<br\/>void dfs(int rGraph[V][V], int s, bool visited[])<br\/>{<br\/>    visited[s] = true;<br\/>    for (int i = 0; i &lt; V; i++)<br\/>       if (rGraph[s][i] &amp;&amp; !visited[i])<br\/>           dfs(rGraph, i, visited);<br\/>}<br\/> <br\/>\/\/ Prints the minimum s-t cut<br\/>void minCut(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]; \/\/ rGraph[i][j] indicates residual capacity of edge i-j<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\/>    \/\/ 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 edhes 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\/>        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\/> <br\/>    \/\/ Flow is maximum now, find vertices reachable from s<br\/>    bool visited[V];<br\/>    memset(visited, false, sizeof(visited));<br\/>    dfs(rGraph, s, visited);<br\/> <br\/>    \/\/ Print all edges that are from a reachable vertex to<br\/>    \/\/ non-reachable vertex in the original graph<br\/>    for (int i = 0; i &lt; V; i++)<br\/>      for (int j = 0; j &lt; V; j++)<br\/>         if (visited[i] &amp;&amp; !visited[j] &amp;&amp; graph[i][j])<br\/>              cout &lt;&lt; i &lt;&lt; &quot; - &quot; &lt;&lt; j &lt;&lt; endl;<br\/> <br\/>    return;<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, 16, 13, 0, 0, 0},<br\/>                        {0, 0, 10, 12, 0, 0},<br\/>                        {0, 4, 0, 0, 14, 0},<br\/>                        {0, 0, 9, 0, 0, 20},<br\/>                        {0, 0, 0, 7, 0, 4},<br\/>                        {0, 0, 0, 0, 0, 0}<br\/>                      };<br\/> <br\/>    minCut(graph, 0, 5);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>1 - 3\r\n4 - 3\r\n4 - 5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm &#8211; Find minimum s-t cut in a flow network &#8211;  Graph Algorithm &#8211; In a flow network, an s-t cut is a cut that requires the source \u2018s\u2019 <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82509,73906,78429],"tags":[78844,78840,78843,78837,78845,78841,78846,78847,78842,78838,78839,78848],"class_list":["post-26453","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree-2","category-graph-algorithms","category-maximum-flow","tag-explain-max-flow-min-cut-theorem-with-example","tag-ford-fulkerson-algorithm-for-maximum-flow-problem","tag-how-to-find-min-cut-of-a-graph","tag-max-flow-min-cut-algorithm","tag-max-flow-min-cut-geeksforgeeks","tag-max-flow-min-cut-theorem-in-graph-theory","tag-max-flow-min-cut-theorem-pdf","tag-max-flow-min-cut-theorem-ppt","tag-min-cut-algorithm-example","tag-minimum-cut-algorithm","tag-randomized-min-cut-algorithm","tag-s-t-cut"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26453","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=26453"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26453\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}