You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
713 B
Python

5 years ago
# 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()