programming-examples/python/Algorithms/maxflow_fordfulkerson_simple.py

29 lines
713 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
# https://en.wikipedia.org/wiki/FordFulkerson_algorithm in O(V^2 * flow)
def max_flow(cap, s, t):
def augment_path(cap, visited, i, t):
if i == t:
return True
visited[i] = True
for j in range(len(cap)):
if not visited[j] and cap[i][j] > 0 and augment_path(cap, visited, j, t):
cap[i][j] -= 1
cap[j][i] += 1
return True
return False
flow = 0
while True:
if not augment_path(cap, [False] * len(cap), s, t):
return flow
flow += 1
def test():
capacity = [[0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 0]]
assert max_flow(capacity, 0, 3) == 2
test()