{"id":26452,"date":"2017-12-20T21:12:15","date_gmt":"2017-12-20T15:42:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26452"},"modified":"2018-10-30T16:32:55","modified_gmt":"2018-10-30T11:02:55","slug":"python-algorithm-find-minimum-s-t-cut-flow-network","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-find-minimum-s-t-cut-flow-network\/","title":{"rendered":"Find minimum s-t cut in a flow network"},"content":{"rendered":"<h3 id=\"find-minimum-s-t-cut-in-a-flow-network\"><span style=\"color: #993300;\">Find minimum s-t cut in a flow network:<\/span><\/h3>\n<p>In a flow network, an <strong>s-t cut<\/strong> is a cut that requires the <strong>source \u2018s<\/strong>\u2019 and the <strong>sink \u2018t\u2019<\/strong> 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 <strong>find minimum capacity s-t cut of the given network.<\/strong> Expected output is all edges of the minimum cut.<\/p>\n<p><strong>For example<\/strong>, 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=\u201dbanner\u201d]\n<h3 id=\"minimum-cut-and-maximum-flow\"><span style=\"color: #003366;\"><strong>Minimum Cut and Maximum Flow<\/strong><\/span><\/h3>\n<p>Like <a href=\"https:\/\/www.wikitechy.com\/technology\/26643-2\/\" target=\"_blank\" rel=\"noopener\">Maximum Bipartite Matching<\/a>, this is another problem which can solved using <a href=\"https:\/\/www.wikitechy.com\/technology\/python-algorithm-ford-fulkerson-algorithm-maximum-flow-problem\/\" target=\"_blank\" rel=\"noopener\">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\">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&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 residual graph.<\/p>\n<p><span style=\"color: #0000ff;\"><strong>Following are steps to print all edges of minimum cut:<\/strong><\/span><\/p>\n<p><strong>1)<\/strong> Run <strong>Ford-Fulkerson algorithm<\/strong> and consider the final residual <a href=\"https:\/\/www.wikitechy.com\/technology\/python-programming-eulerian-path-circuit-undirected-graph\/\" target=\"_blank\" rel=\"noopener\">graph<\/a>.<\/p>\n<p><strong>2)<\/strong> Find the <strong>set of vertices<\/strong> 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 <strong>minimum cut edges.<\/strong> Print all such edges.<\/p>\n<p>Following is <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/what-is-python\" target=\"_blank\" rel=\"noopener\">python<\/a> implementation of the above approach:<\/p>\n<h3 id=\"python-programming\"><span style=\"color: #ff9900;\"><strong>Python Programming:<\/strong><\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20for%20finding%20min-cut%20in%20the%20given%20graph%0A%23%20Complexity%20%3A%20(E*(V%5E3))%0A%23%20Total%20augmenting%20path%20%3D%20VE%20and%20BFS%20with%20adj%20matrix%20takes%20%3AV%5E2%20times%0A%20%0Afrom%20collections%20import%20defaultdict%0A%20%0A%23%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%20matrix%20representation%0Aclass%20Graph%3A%0A%20%0A%20%20%20%20def%20__init__(self%2Cgraph)%3A%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20graph%20%23%20residual%20graph%0A%20%20%20%20%20%20%20%20self.org_graph%20%3D%20%5Bi%5B%3A%5D%20for%20i%20in%20graph%5D%0A%20%20%20%20%20%20%20%20self.%20ROW%20%3D%20len(graph)%0A%20%20%20%20%20%20%20%20self.COL%20%3D%20len(graph%5B0%5D)%0A%20%0A%20%0A%20%20%20%20\u201d\u2019Returns%20true%20if%20there%20is%20a%20path%20from%20source%20\u2019s\u2019%20to%20sink%20\u2019t\u2019%20in%0A%20%20%20%20residual%20graph.%20Also%20fills%20parent%5B%5D%20to%20store%20the%20path%20\u201d\u2019%0A%20%20%20%20def%20BFS(self%2Cs%2C%20t%2C%20parent)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%5BFalse%5D*(self.ROW)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20%20%20%20%20queue%3D%5B%5D%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20source%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20queue.append(s)%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%20%3D%20True%0A%20%0A%20%20%20%20%20%20%20%20%20%23%20Standard%20BFS%20Loop%0A%20%20%20%20%20%20%20%20while%20queue%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23Dequeue%20a%20vertex%20from%20queue%20and%20print%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20u%20%3D%20queue.pop(0)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Get%20all%20adjacent%20vertices%20of%20the%20dequeued%20vertex%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20If%20a%20adjacent%20has%20not%20been%20visited%2C%20then%20mark%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20ind%2C%20val%20in%20enumerate(self.graph%5Bu%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bind%5D%20%3D%3D%20False%20and%20val%20%3E%200%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.append(ind)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bind%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20parent%5Bind%5D%20%3D%20u%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20we%20reached%20sink%20in%20BFS%20starting%20from%20source%2C%20then%20return%0A%20%20%20%20%20%20%20%20%23%20true%2C%20else%20false%0A%20%20%20%20%20%20%20%20return%20True%20if%20visited%5Bt%5D%20else%20False%0A%20%0A%20%0A%20%20%20%20%23%20Returns%20tne%20min-cut%20of%20the%20given%20graph%0A%20%20%20%20def%20minCut(self%2C%20source%2C%20sink)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20This%20array%20is%20filled%20by%20BFS%20and%20to%20store%20path%0A%20%20%20%20%20%20%20%20parent%20%3D%20%5B-1%5D*(self.ROW)%0A%20%0A%20%20%20%20%20%20%20%20max_flow%20%3D%200%20%23%20There%20is%20no%20flow%20initially%0A%20%0A%20%20%20%20%20%20%20%20%23%20Augment%20the%20flow%20while%20there%20is%20path%20from%20source%20to%20sink%0A%20%20%20%20%20%20%20%20while%20self.BFS(source%2C%20sink%2C%20parent)%20%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Find%20minimum%20residual%20capacity%20of%20the%20edges%20along%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20path%20filled%20by%20BFS.%20Or%20we%20can%20say%20find%20the%20maximum%20flow%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20through%20the%20path%20found.%0A%20%20%20%20%20%20%20%20%20%20%20%20path_flow%20%3D%20float(%22Inf%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20sink%0A%20%20%20%20%20%20%20%20%20%20%20%20while(s%20!%3D%20%20source)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20path_flow%20%3D%20min%20(path_flow%2C%20self.graph%5Bparent%5Bs%5D%5D%5Bs%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20parent%5Bs%5D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Add%20path%20flow%20to%20overall%20flow%0A%20%20%20%20%20%20%20%20%20%20%20%20max_flow%20%2B%3D%20%20path_flow%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20update%20residual%20capacities%20of%20the%20edges%20and%20reverse%20edges%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20along%20the%20path%0A%20%20%20%20%20%20%20%20%20%20%20%20v%20%3D%20sink%0A%20%20%20%20%20%20%20%20%20%20%20%20while(v%20!%3D%20%20source)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20u%20%3D%20parent%5Bv%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.graph%5Bu%5D%5Bv%5D%20-%3D%20path_flow%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.graph%5Bv%5D%5Bu%5D%20%2B%3D%20path_flow%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20v%20%3D%20parent%5Bv%5D%0A%20%0A%20%20%20%20%20%20%20%20%23%20print%20the%20edges%20which%20initially%20had%20weights%0A%20%20%20%20%20%20%20%20%23%20but%20now%20have%200%20weight%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(self.ROW)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(self.COL)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20self.graph%5Bi%5D%5Bj%5D%20%3D%3D%200%20and%20self.org_graph%5Bi%5D%5Bj%5D%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print%20str(i)%20%2B%20%22%20-%20%22%20%2B%20str(j)%0A%20%0A%20%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Agraph%20%3D%20%5B%5B0%2C%2016%2C%2013%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%200%2C%2010%2C%2012%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%204%2C%200%2C%200%2C%2014%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%200%2C%209%2C%200%2C%200%2C%2020%5D%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%207%2C%200%2C%204%5D%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%5D%0A%20%0Ag%20%3D%20Graph(graph)%0A%20%0Asource%20%3D%200%3B%20sink%20%3D%205%0A%20%0Ag.minCut(source%2C%20sink)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Neelam%20Yadav%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"output\"><span style=\"color: #339966;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre>1 - 3\r\n4 - 3\r\n4 - 5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python 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":31259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73906,78429],"tags":[78844,78840,78852,78843,78837,78845,78841,78849,78850,78842,78838,78839,78848,78851],"class_list":["post-26452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-graph-algorithms","category-maximum-flow","tag-explain-max-flow-min-cut-theorem-with-example","tag-ford-fulkerson-algorithm-for-maximum-flow-problem","tag-global-min-cut","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-proof","tag-maximum-flow-example","tag-min-cut-algorithm-example","tag-minimum-cut-algorithm","tag-randomized-min-cut-algorithm","tag-s-t-cut","tag-stoer-wagner-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26452","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=26452"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26452\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31259"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}