Maximum flow with residual edges
Return maximum flow from source to distinct sink in a directed nonnegative integer capacity matrix. Do not mutate the matrix.
residual = [[0, 3], [0, 0]]
amount = 2
residual[0][1] -= amount
residual[1][0] += amount
print(residual)Reason about the state
Use breadth-first augmenting paths on residual capacities. Every augmentation adds reverse capacity so later paths can undo earlier choices. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return maximum flow from source to distinct sink in a directed nonnegative integer capacity matrix. Do not mutate the matrix.
The explanation opens here once every check passes.