-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathedmond-karp-algorithm-max-flow-min-cut.py
96 lines (78 loc) · 2.52 KB
/
edmond-karp-algorithm-max-flow-min-cut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from collections import defaultdict
import sys
class Graph:
def __init__(self, graph):
super().__init__()
self.graph = graph
self.ROW = len(graph)
self.N = len(graph)
def BFS(self, s, t, parent):
visited = [False] * self.ROW
queue = []
queue.append(s)
visited[s] = True
while queue:
u = queue.pop(0)
for ind, val in enumerate(self.graph[u]):
if visited[ind] is False and val > 0:
if ind == t:
visited[t] = True
return True
queue.append(ind)
visited[ind] = True
parent[ind] = u
return False
def FordFulkerson(self, source, sink):
parent = [-1] * self.ROW
max_flow = 0
while not self.BFS(source, sink, parent):
path_flow = sys.maxsize
s = sink
while s != source:
print(s)
path_flow = min(path_flow, self.graph[parent[s]][s])
s = parent[s]
max_flow += path_flow
v = sink
while v != source:
u = parent[v]
self.graph[u][v] -= path_flow
self.graph[v][u] += path_flow
v = parent[v]
return max_flow
def maxflow(self, s, t):
F = [[0] * self.N] * self.N
while (path := self.maxflowBFS(F, s, t)) is not None:
flow = min(self.graph[u][v] - F[u][v] for u, v in path)
for u, v in path:
F[u][v] += flow
F[v][u] -= flow
return sum(F[s][i] for i in range(self.N))
def maxflowBFS(self, F, s, t):
queue = [s]
path = {s: []}
if s == t:
return path[s]
while queue:
u = queue.pop(0)
for v in range(self.N):
if self.graph[u][v] - F[u][v] > 0 and v not in path:
path[v] = path[u] + [(u, v)]
if v == t:
return path[v]
queue.append(v)
return None
if __name__ == "__main__":
graph = [
[0, 3, 3, 2, 11, 12], # s
[0, 0, 2, 3, 0, 0], # o
[0, 0, 0, 0, 2, 0], # p
[0, 0, 0, 0, 4, 2], # q
[0, 0, 0, 0, 0, 2], # r
[0, 0, 0, 0, 0, 3],
] # t
g = Graph(graph)
source = 0
sink = 5
print("The maximum possible flow is %d " % g.FordFulkerson(source, sink))
print(g.maxflow(0, 5))