Leave room to change your mind.
Represent both additional capacity and the ability to cancel an earlier routing decision.
01 Model the idea
A flow network has directed capacities, a source, and a sink. Feasible flow respects capacities and conserves incoming/outgoing flow at intermediate vertices. A residual network describes adjustments still possible: a forward residual edge adds flow, while a reverse residual edge cancels previously sent flow. Reverse capacity is essential, not a decorative mirror of the original network.
Residual reverse capacity records how much prior flow can be cancelled.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
BUILD forward residual capacity and zero reverse capacity
FIND a source-to-sink route
SUBTRACT its bottleneck forward and add it backward| Edge | Before | After sending 3 |
|---|---|---|
| S → A | 4 | 1 |
| A → S | 0 | 3 |
| A → T | 3 | 0 |
| T → A | 0 | 3 |
03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
capacity = {"S": {"A": 4}, "A": {"T": 3}, "T": {}}
residual = {node: {} for node in capacity}
for u in capacity:
for v, value in capacity[u].items():
residual[u][v] = value
residual[v].setdefault(u, 0)
amount = min(residual["S"]["A"], residual["A"]["T"])
for u, v in [("S", "A"), ("A", "T")]:
residual[u][v] -= amount; residual[v][u] += amount
print(amount)
print(residual["S"]["A"], residual["A"]["S"])3 1 3
- 1
The augmenting route is S→A→T. Its bottleneck is three because the final edge cannot carry more.
- 2
Sending three leaves one forward unit from S to A and creates three reverse residual units from A to S. Those reverse units express a possible cancellation.
- 3
A final maximum flow has no remaining source-to-sink residual route. A reachable/unreachable cut in that residual graph provides a matching capacity certificate.
04 Count the cost
O(V + E) construction; O(path length) augmentation
O(V + E) residual adjacency storage
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
Cancel one unit along the reverse residual edge and show the forward capacity increases.
Reveal one possible solution +
forward, reverse = 1, 3
cancel = 1
forward += cancel; reverse -= cancel
print(forward, reverse)06 Check your reasoning
What does reverse residual capacity allow?
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.