Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Maximum flow, one level graph at a time
Maximum flow in phases: BFS builds a level graph, DFS pushes a blocking flow through it
Select an algorithm and generate steps to begin visualization
Dinic's algorithm computes the maximum flow from a source to a sink in a network whose edges have capacities. Yefim Dinitz published it in 1970. Like Edmonds-Karp it only augments along shortest paths, but it sends flow along all shortest paths of the same length in one phase, which makes it much faster in practice and the standard choice in competitive programming.
Each phase starts with a breadth-first search from the source in the residual network, which gives every vertex a level, its distance from the source. Only arcs that go from one level to the next are kept: this is the level graph. Depth-first searches then push flow from the source to the sink through the level graph until every path is blocked by a full arc, a blocking flow. Arcs that lead to dead ends are dropped as soon as they are met, so no arc is tried twice in a phase. After each phase the distance from source to sink grows, so there are at most V - 1 phases and the running time is O(V²E), or O(E√V) when all capacities are 1. When the sink cannot be reached any more, the vertices the last search reached form a minimum cut.
Maximum flow models network throughput, traffic and pipeline capacity, project selection, image segmentation and many assignment problems. With unit capacities Dinic's algorithm solves bipartite matching as fast as Hopcroft-Karp, and it is the usual flow algorithm in programming contests because it is short to write and fast on real inputs. In algorithms courses it follows Ford-Fulkerson and Edmonds-Karp as the next step in the theory of flows.
Dinic computes a maximum flow in phases. Each phase builds a level graph with one BFS and then saturates it with a blocking flow found by repeated DFS, so a single BFS serves many augmenting paths.
Dinic(graph, s, t):
flow = 0
while BFS from s in the residual graph reaches t:
level[v] = BFS distance from s
next[v] = first residual edge of v // current arc
while (b = DFS(s, infinity)) > 0:
flow = flow + b
return flow
DFS(u, limit):
if u == t: return limit
while next[u] is an edge (u, v):
if level[v] == level[u] + 1 and residual(u, v) > 0:
b = DFS(v, min(limit, residual(u, v)))
if b > 0:
push b along (u, v), add b to (v, u)
return b
advance next[u] // dead edge
return 0Only edges that go exactly one level deeper are allowed, so every path found in a phase is a shortest augmenting path. A blocking flow saturates at least one edge on every such path, which means the next BFS must find a strictly longer one. The current arc pointer next[u] is the detail that makes a phase fast: an edge that leads to a dead end is skipped for the rest of the phase and never examined again.
Compute the maximum flow from A to F on the example this page loads, the flow network of CLRS figure 26.1, the same one the max-flow page uses.
Example graph: Directed capacities A-B 16, A-C 13, B-D 12, C-B 4, C-E 14, D-C 9, D-F 20, E-D 7 and E-F 4. The source is A and the sink is F.
The maximum flow from A to F is 23, found in two phases. The vertices still reachable at the end, A, B, C and E, form the source side of a minimum cut, and the edges leaving them, B-D (12), E-D (7) and E-F (4), add up to exactly 23, which certifies optimality by the max-flow min-cut theorem. With the same neighbour order, Edmonds-Karp finds the same three augmenting paths but runs a separate BFS for each of them.
Time: O(V^2 E) · Space: O(V + E)
The BFS distance from s to t increases by at least 1 in every phase and cannot exceed V - 1, so there are at most V - 1 phases. Within a phase, each successful DFS saturates at least one edge, so there are at most E augmentations, and each walks a path of at most V edges. Failed attempts advance the current arc pointer, so they cost O(E) in total for the whole phase. A phase therefore costs O(VE), and the algorithm O(V^2 E). On networks where every capacity is 1 the analysis tightens to O(E min(V^(2/3), E^(1/2))), and on the unit networks that come from bipartite matching it is O(E sqrt(V)), the same bound as Hopcroft-Karp.
Dinic is the usual choice for maximum flow in practice and in programming contests. The alternatives trade simplicity, bounds and constants differently.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Edmonds-Karp | You want the simplest correct implementation and the graph is small. One BFS per augmenting path. | O(VE^2) |
| Ford-Fulkerson with DFS | Capacities are small integers, so the number of augmentations, bounded by the flow value, stays small. | O(E f) |
| Push-relabel | Very large or dense networks, where its local operations and good heuristics often beat augmenting path methods. | O(V^3) with FIFO |
| Hopcroft-Karp | The problem is bipartite matching. It is Dinic specialised to unit capacities, with a smaller constant. | O(E sqrt(V)) |
| Min-cost flow | Edges also carry a cost per unit, and among maximum flows you want the cheapest. | pseudo-polynomial with SSP |
Read the full article: Max-Flow Min-Cut Theorem & Network Flow
Read the full article: Max-Flow & Min-Cut Interview Questions
Related algorithms: Maximum Flow, Minimum Cut, Hopcroft-Karp Matching, Breadth-First Search