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

Dinic, Level Graphs & Scaling

Faster Flow 32 minBuild a level graph with one BFS, then saturate several augmenting paths at once with a blocking flow, instead of one BFS per path

dsa / Dinic, Level Graphs & Scaling

Explore each scene at your own pace.

01 · Same question, a wider network

Same question, a wider network

Dinic, Level Graphs & Scaling

Definition
Dinic's algorithm speeds up maximum flow by labelling nodes with their distance from the source (a level graph) and then finding a blocking flow, saturating every shortest path in one depth-first pass, before relabelling. It runs in O(V²E) time.
In plain words
Instead of finding one route, sending water and starting over, you map the network once by distance, then fill every shortest route in a single sweep. Only when those are all full do you redraw the map.

In short: Edmonds-Karp finds one shortest augmenting path, pushes it, and throws its BFS away. Dinic's algorithm keeps the BFS: it labels every node with its distance from the source (a level graph), then runs a single DFS that saturates every shortest route the level graph allows — a blocking flow — before rebuilding. Each phase strictly grows the sink's level, so there are at most V phases, and a phase with E edges costs O(V·E) to block, for a total of O(V²·E). On a network where several shortest routes don't share a node, one phase does the work Edmonds-Karp needed several separate searches for.

The wide network redrawn as three horizontal bands by distance from depot: depot alone in level 0, dock, mill and port in level 1, harbour and relay in level 2, with only arrows that step down exactly one band kept, and a note that dock's and mill's routes share no node so one blocking flow saturates both

Same question, a wider network

Meridian's depot network from the last two lessons has only one route of each length, so Edmonds-Karp and Dinic take the same number of rounds on it. Widen it — add a second depot-fed branch through mill, and a longer one through port and relay — and the difference shows up immediately:

watch it move · graph

dock's and mill's routes are both length 2 and share no node. port's route is length 3. One phase settles the two short routes together.

BFS labels every node with its distance from depot. harbour is distance 2 — reachable via dock or mill — and relay, at distance 2, is still one hop short of harbour. 6 nodes, 7 edges, directed. depot: 0, dock: 1, mill: 1, port: 1, harbour: 2.

1 / 5
Scene 1 of 7
Project context and further resources
You're building a piece ofMeridian — the dispatcher
This piece — push_volume_fast(): Answers the same capacity question, but saturates several routes per pass instead of one.
Scenario The nightly capacity check on Meridian's full depot graph can afford Edmonds-Karp. A live dashboard query against the same graph, run on demand, needs an answer that doesn't visibly lag — and that means fewer, bigger rounds.
Additional resources
push_volume_fast.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
push_volume_fast(pipes, source, sink) → intThe max flow, found by level graphs and blocking flow instead of one augmenting path at a time.
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.