{"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=&#8221;banner&#8221;]\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\" 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 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for finding min-cut in the given graph<br\/># Complexity : (E*(V^3))<br\/># Total augmenting path = VE and BFS with adj matrix takes :V^2 times<br\/> <br\/>from collections import defaultdict<br\/> <br\/># This class represents a directed graph using adjacency matrix representation<br\/>class Graph:<br\/> <br\/>    def __init__(self,graph):<br\/>        self.graph = graph # residual graph<br\/>        self.org_graph = [i[:] for i in graph]<br\/>        self. ROW = len(graph)<br\/>        self.COL = len(graph[0])<br\/> <br\/> <br\/>    &#039;&#039;&#039;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 &#039;&#039;&#039;<br\/>    def BFS(self,s, t, parent):<br\/> <br\/>        # Mark all the vertices as not visited<br\/>        visited =[False]*(self.ROW)<br\/> <br\/>        # Create a queue for BFS<br\/>        queue=[]<br\/> <br\/>        # Mark the source node as visited and enqueue it<br\/>        queue.append(s)<br\/>        visited[s] = True<br\/> <br\/>         # Standard BFS Loop<br\/>        while queue:<br\/> <br\/>            #Dequeue a vertex from queue and print it<br\/>            u = queue.pop(0)<br\/> <br\/>            # Get all adjacent vertices of the dequeued vertex u<br\/>            # If a adjacent has not been visited, then mark it<br\/>            # visited and enqueue it<br\/>            for ind, val in enumerate(self.graph[u]):<br\/>                if visited[ind] == False and val &gt; 0 :<br\/>                    queue.append(ind)<br\/>                    visited[ind] = True<br\/>                    parent[ind] = u<br\/> <br\/>        # If we reached sink in BFS starting from source, then return<br\/>        # true, else false<br\/>        return True if visited[t] else False<br\/> <br\/> <br\/>    # Returns tne min-cut of the given graph<br\/>    def minCut(self, source, sink):<br\/> <br\/>        # This array is filled by BFS and to store path<br\/>        parent = [-1]*(self.ROW)<br\/> <br\/>        max_flow = 0 # There is no flow initially<br\/> <br\/>        # Augment the flow while there is path from source to sink<br\/>        while self.BFS(source, sink, 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\/>            path_flow = float(&quot;Inf&quot;)<br\/>            s = sink<br\/>            while(s !=  source):<br\/>                path_flow = min (path_flow, self.graph[parent[s]][s])<br\/>                s = parent[s]<br\/> <br\/>            # Add path flow to overall flow<br\/>            max_flow +=  path_flow<br\/> <br\/>            # update residual capacities of the edges and reverse edges<br\/>            # along the path<br\/>            v = sink<br\/>            while(v !=  source):<br\/>                u = parent[v]<br\/>                self.graph[u][v] -= path_flow<br\/>                self.graph[v][u] += path_flow<br\/>                v = parent[v]<br\/> <br\/>        # print the edges which initially had weights<br\/>        # but now have 0 weight<br\/>        for i in range(self.ROW):<br\/>            for j in range(self.COL):<br\/>                if self.graph[i][j] == 0 and self.org_graph[i][j] &gt; 0:<br\/>                    print str(i) + &quot; - &quot; + str(j)<br\/> <br\/> <br\/># Create a graph given in the above diagram<br\/>graph = [[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\/>g = Graph(graph)<br\/> <br\/>source = 0; sink = 5<br\/> <br\/>g.minCut(source, sink)<br\/> <br\/># This code is contributed by Neelam Yadav<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}