Skip to the workspace
Pebellslearn by building
Flow & Matching  ›  Lesson

Maximum Flow

Maximum Flow 34 minPush volume along augmenting paths in a residual graph, undoing a bad commitment through a backward edge when you must

dsa / Maximum Flow

Explore each scene at your own pace.

01 · From matching to flow

From matching to flow

Maximum Flow

Definition
Maximum flow is the largest amount that can be sent from a source to a sink through a network whose edges have capacities. The Edmonds-Karp algorithm finds it by repeatedly pushing along a shortest augmenting path in the residual graph, which records spare capacity and allows earlier pushes to be undone.
In plain words
Picture water through a system of pipes of different widths. You keep finding any route with room left and sending more along it, and you may redirect water you sent earlier if that makes space for more overall.

In short: A flow network is a set of pipes, each with a capacity, and the question is how much can move from a source to a sink at once. Edmonds-Karp answers it by repeatedly finding a shortest route with spare capacity (an augmenting path) and pushing the most it can along it. Every push opens a same-sized pipe running the other way — a residual edge — so a later path can undo part of an earlier commitment if that turns out to free up more overall. The algorithm stops when no augmenting path is left, after at most O(V·E) pushes, each costing an O(E) search: O(V·E²).

A dock to axle pipe with 3 of its 5 capacity already used by green flow, axle to harbour fully used, and a dashed red arrow going backward from axle to dock labelled residual 3, undo, with a note explaining that sending a unit along it frees dock to send it on to berth instead

From matching to flow

Section 17's driver-and-job dispatch board was already a flow network, drawn differently. Give every driver one pipe of capacity 1 from a source, every licence a pipe of capacity 1, and every job one pipe of capacity 1 to a sink, and "the largest matching" and "the maximum flow" are the exact same number. The augmenting-path flip that moved dee into the van and pushed ana to the truck was already using a backward edge — the pipe that undid dee's job going to someone else's chain. This lesson writes that machinery out in full, for pipes that carry more than one unit at a time.

Scene 1 of 12
Project context and further resources
You're building a piece ofMeridian — the dispatcher
This piece — push_volume(): Finds how much the depot network can carry from the depot to the harbour at once.
Scenario Meridian's depot network has pipes of fixed capacity, and dispatch wants to know the single number that matters before anything else: how much can move from the depot to the harbour at once, right now.
push_volume.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
push_volume(pipes, source, sink) → intThe most volume per run that can move from source to sink at once.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.