MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / mincut

Function mincut

networking_flow/minimum_cut.py:30–62  ·  view source on GitHub ↗

This array is filled by BFS and to store path >>> mincut(test_graph, source=0, sink=5) [(1, 3), (4, 3), (4, 5)]

(graph, source, sink)

Source from the content-addressed store, hash-verified

28
29
30def mincut(graph, source, sink):
31 """This array is filled by BFS and to store path
32 >>> mincut(test_graph, source=0, sink=5)
33 [(1, 3), (4, 3), (4, 5)]
34 """
35 parent = [-1] * (len(graph))
36 max_flow = 0
37 res = []
38 temp = [i[:] for i in graph] # Record original cut, copy.
39 while bfs(graph, source, sink, parent):
40 path_flow = float("Inf")
41 s = sink
42
43 while s != source:
44 # Find the minimum value in select path
45 path_flow = min(path_flow, graph[parent[s]][s])
46 s = parent[s]
47
48 max_flow += path_flow
49 v = sink
50
51 while v != source:
52 u = parent[v]
53 graph[u][v] -= path_flow
54 graph[v][u] += path_flow
55 v = parent[v]
56
57 for i in range(len(graph)):
58 for j in range(len(graph[0])):
59 if graph[i][j] == 0 and temp[i][j] > 0:
60 res.append((i, j))
61
62 return res
63
64
65if __name__ == "__main__":

Callers 1

minimum_cut.pyFile · 0.85

Calls 2

bfsFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected