-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfordfulkerson.py
67 lines (61 loc) · 2.07 KB
/
fordfulkerson.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
#ford fulkerson method Edomonds Karp algorithm for finding max flow
# java code https://github.com/mission-peace/interview/blob/master/src/com/interview/graph/FordFulkerson.java
from queue import Queue
import sys
def max_flow(capacity, source, sink):
residual_capacity = [x[:] for x in capacity]
augmented_paths = []
max_flow = 0
while True:
found_augmented_path, parent = bfs(residual_capacity, source, sink)
if not found_augmented_path:
break
augmented_path = []
v = sink
flow = sys.maxsize
while not v == source:
augmented_path.append(v)
u = parent[v]
if flow > residual_capacity[u][v]:
flow = residual_capacity[u][v]
v = u
augmented_path.append(source)
augmented_paths.append(augmented_path[::-1])
max_flow += flow
v = sink
while not v == source:
u = parent[v]
residual_capacity[u][v] -= flow
residual_capacity[v][u] += flow
v = u
print("Augmented path")
print(augmented_paths)
return max_flow
def bfs(residual_capacity, source, sink):
visited = set()
queue = Queue()
parent = {}
queue.put(source)
visited.add(source)
found_augmented_path = False
while not queue.empty():
u = queue.get()
for v in range(len(residual_capacity)):
if v not in visited and residual_capacity[u][v] > 0:
parent[v] = u
visited.add(v)
queue.put(v)
if v == sink:
found_augmented_path = True
break;
return found_augmented_path, parent
if __name__ == '__main__':
capacity = [[0, 3, 0, 3, 0, 0, 0],
[0, 0, 4, 0, 0, 0, 0],
[3, 0, 0, 1, 2, 0, 0],
[0, 0, 0, 0, 2, 6, 0],
[0, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 9],
[0, 0, 0, 0, 0, 0, 0]]
max_val = max_flow(capacity, 0, 6)
print(max_val)