Dinic's Algorithm
Imagine you're shipping packages and you've found every possible shortest route from the warehouse to the delivery hub. Edmonds-Karp sends packages along one route, then re-checks everything. Dinic's says: send packages along ALL shortest routes simultaneously, then re-evaluate. That's the entire idea.
This batch approach — called a blocking flow — makes Dinic's dramatically faster in practice and provably better in theory.
Phase 1: Build the Level Graph (BFS)
Start each phase with a BFS from the source s through the residual graph. Assign each vertex a level = its distance from s in number of edges.
The level graph keeps only edges (u→v) where level[v] = level[u] + 1 — edges that strictly move toward the sink. All s-t paths in the level graph have the same minimum length. If the sink t is unreachable, the algorithm stops (max flow found).
Phase 2: Find a Blocking Flow (DFS)
A blocking flow is a flow in the level graph where every s-t path has at least one saturated edge. After sending a blocking flow, no s-t path remains in the level graph — forcing the next BFS to find longer paths.
Finding the blocking flow uses DFS with a "dead end" optimization: when you reach a dead end (no forward edges left), delete that vertex from the level graph so you never try it again. When you reach t, send flow back along the path, saturating the bottleneck edge. Each edge is deleted at most once, so the whole blocking flow phase costs \(O(VE)\).
Why Path Lengths Increase Each Phase
After a blocking flow, every s-t path in the current level graph is broken. The next BFS must find a strictly longer shortest path. Since path lengths are at most V-1, there can be at most \(O(V)\) phases.
Each phase: one BFS = \(O(E)\), one blocking flow = \(O(VE)\). Total: \(O(V)\) × \(O(VE)\) = \(O(V^2E)\).
Unit Capacity Networks and Bipartite Matching
When all capacities are 1, blocking flows saturate edges very quickly and path lengths grow faster. The number of phases drops to \(O(√E)\) in general and \(O(√V)\) for bipartite graphs. Total complexity: \(O(E√V)\) — the same bound achieved by the Hopcroft-Karp matching algorithm (which is essentially Dinic's applied to bipartite graphs).