forked from tarun620/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edmund_karp.py
54 lines (48 loc) · 1.35 KB
/
edmund_karp.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
import decimal
def EdmondsKarp(E, C, s, t):
"""
:param E: Adjacency Matrix
:param C: Capacity Matrix
:param s: Source
:param t: Sink
"""
n = len(C)
flow = 0
F = [[0 for y in range(n)] for x in range(n)]
while True:
P = [-1 for x in range(n)]
P[s] = -2
M = [0 for x in range(n)]
M[s] = decimal.Decimal('Infinity')
BFSq = []
BFSq.append(s)
pathFlow, P = BFSEK(E, C, s, t, F, P, M, BFSq)
if pathFlow == 0:
break
flow = flow + pathFlow
v = t
while v != s:
u = P[v]
F[u][v] = F[u][v] + pathFlow
F[v][u] = F[v][u] - pathFlow
v = u
return flow
def BFSEK(E, C, s, t, F, P, M, BFSq):
while (len(BFSq) > 0):
u = BFSq.pop(0)
for v in E[u]:
if C[u][v] - F[u][v] > 0 and P[v] == -1:
P[v] = u
M[v] = min(M[u], C[u][v] - F[u][v])
if v != t:
BFSq.append(v)
else:
return M[t], P
return 0, P
if __name__ == "__main__":
E = [[1, 2], [2, 3], [3], []] # Adjacency Matrix
C = [[0, 1000000, 1000000, 0], [0, 0, 1, 1000000], [0, 0, 0, 1000000],
[0, 0, 0, 0]] # Capacity Matrix
s = 0 # Source
t = 3 # Sink
print(EdmondsKarp(E, C, s, t))