-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxflow.py
57 lines (47 loc) · 1.45 KB
/
maxflow.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
"""Module implementing a maxflow algorithm
https://en.wikipedia.org/wiki/Maximum_flow_problem"""
import math
def find_next_node(graph, source, sink, forbidden):
for i in range(len(graph[source])):
if graph[source][i] > 0 and i not in forbidden:
path = find_path(graph, i, sink, forbidden)
if path is not None:
return i, path
return None, None
def find_path(graph, source, sink, forbidden):
if graph[source][sink] > 0:
return [source, sink]
else:
next_node, path = find_next_node(graph, source, sink, forbidden + [source])
if next_node:
return [source] + path
else:
return None
def min_flow(graph, path):
flow = math.inf
for i in range(len(path)-1):
flow = min(flow, graph[path[i]][path[i+1]])
return flow
def add_path_flow(graph, path, flow):
for i in range(len(path)-1):
graph[path[i]][path[i+1]] -= flow
graph[path[i+1]][path[i]] += flow
def maxflow(graph,source,sink):
path = find_path(graph,source,sink,[])
total_flow = 0
while path is not None:
flow = min_flow(graph, path)
"""
print("Path:")
print(path)
print("Min flow:")
print(flow)
"""
add_path_flow(graph, path, flow)
total_flow += flow
path = find_path(graph,source,sink,[])
"""
print("Total flow:")
print(total_flow)
"""
return total_flow