Keep finding a little more room.
Use integer-capacity residual routes to increase flow until no route remains.
01 Model the idea
Ford–Fulkerson is an augmenting-path method, not one fixed route-selection algorithm. This implementation uses an explicit DFS stack to find a positive-capacity residual route. Augment by the route bottleneck and repeat. With nonnegative integer capacities, each augmentation increases the flow value by at least one, so the process terminates; arbitrary irrational capacities do not share that simple termination guarantee.
Every augmentation preserves feasibility and increases the source-to-sink flow value.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
FIND any residual augmenting path
SEND its bottleneck
UPDATE forward and reverse capacities
REPEAT until no path remains03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
def residual_graph(n, edges):
graph = [{} for _ in range(n)]
for u, v, capacity in edges:
if type(capacity) is not int or capacity < 0: raise ValueError("Nonnegative integer capacities required")
graph[u][v] = graph[u].get(v, 0) + capacity
graph[v].setdefault(u, 0)
return graph
def maximum_flow(n, edges, source, sink):
if source == sink: raise ValueError("Distinct source and sink required")
residual, total = residual_graph(n, edges), 0
while True:
parent, stack = {source: None}, [source]
while stack and sink not in parent:
node = stack.pop()
for neighbour, available in residual[node].items():
if available > 0 and neighbour not in parent:
parent[neighbour] = node; stack.append(neighbour)
if sink not in parent: return total
amount, node = float("inf"), sink
while parent[node] is not None:
previous = parent[node]; amount = min(amount, residual[previous][node]); node = previous
node = sink
while parent[node] is not None:
previous = parent[node]
residual[previous][node] -= amount; residual[node][previous] += amount
node = previous
total += amount
edges = [(0, 1, 3), (0, 2, 2), (1, 2, 1), (1, 3, 2), (2, 3, 3)]
print(maximum_flow(4, edges, 0, 3))5
- 1
Each search records parents only on first discovery, preventing cycles in the recovered route. Reverse residual edges are available alongside original forward edges.
- 2
The two backward walks first measure the bottleneck, then update every residual edge pair consistently. The total grows by that bottleneck.
- 3
With adjacency lists and integer maximum-flow value F, this route-selection method has an O(EF) augmentation bound, plus setup. That depends on capacity magnitudes and is not a strongly polynomial bound.
04 Count the cost
O(EF + V + E) for integral flow value F with adjacency search
O(V + E)
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Test zero capacity and an isolated sink with the same implementation.
Reveal one possible solution +
def residual_graph(n, edges):
graph = [{} for _ in range(n)]
for u, v, capacity in edges:
if type(capacity) is not int or capacity < 0: raise ValueError("Nonnegative integer capacities required")
graph[u][v] = graph[u].get(v, 0) + capacity
graph[v].setdefault(u, 0)
return graph
def maximum_flow(n, edges, source, sink):
if source == sink: raise ValueError("Distinct source and sink required")
residual, total = residual_graph(n, edges), 0
while True:
parent, stack = {source: None}, [source]
while stack and sink not in parent:
node = stack.pop()
for neighbour, available in residual[node].items():
if available > 0 and neighbour not in parent:
parent[neighbour] = node; stack.append(neighbour)
if sink not in parent: return total
amount, node = float("inf"), sink
while parent[node] is not None:
previous = parent[node]; amount = min(amount, residual[previous][node]); node = previous
node = sink
while parent[node] is not None:
previous = parent[node]
residual[previous][node] -= amount; residual[node][previous] += amount
node = previous
total += amount
assert maximum_flow(2, [(0, 1, 0)], 0, 1) == 0
assert maximum_flow(3, [(0, 1, 5)], 0, 2) == 0
print("Disconnected and zero-capacity cases passed")06 Check your reasoning
Why does the integer-capacity version terminate?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.