Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Flow through a network
EXPERIMENT 47 / 66

Leave room to change your mind.

Represent both additional capacity and the ability to cancel an earlier routing decision.

BY NAVAN SAMI◷ 8 MIN READSTRETCHCONCEPT

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.

THE THING THAT MUST STAY TRUE

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.

the_strategy.txt
BUILD forward residual capacity and zero reverse capacity
FIND a source-to-sink route
SUBTRACT its bottleneck forward and add it backward
A worked trace for this experiment
EdgeBeforeAfter sending 3
S → A41
A → S03
A → T30
T → A03

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

residual_networks.py
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"])
↳ EXPECTED OUTPUT
3
1 3
  1. 1

    The augmenting route is S→A→T. Its bottleneck is three because the final edge cannot carry more.

  2. 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. 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

TIME

O(V + E) construction; O(path length) augmentation

SPACE

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

A SMALL MISSION

Cancel one unit along the reverse residual edge and show the forward capacity increases.

Reveal one possible solution +
one_possible_solution.py
forward, reverse = 1, 3
cancel = 1
forward += cancel; reverse -= cancel
print(forward, reverse)

06 Check your reasoning

QUICK SIGNAL CHECK

What does reverse residual capacity allow?

ANOTHER PATTERN IN YOUR TOOLKIT.

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.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.