learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

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

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Dinic's Algorithm Calculator

Maximum flow, one level graph at a time

Maximum flow in phases: BFS builds a level graph, DFS pushes a blocking flow through it

Time: O(V²E)
Space: O(V + E)
Use Case: Network throughput, bipartite matching, programming contests
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Dinic's Algorithm

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.

How it works

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.

Applications

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.

Pseudocode

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 0

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

Worked example, step by step

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.

  1. Phase 1 levels. BFS from A gives level 0 to A, level 1 to B and C, level 2 to D and E, and level 3 to F. The edges C-B and E-D join vertices on the same level, and D-C goes back up, so none of them belongs to this level graph.
  2. Phase 1 blocking flow. DFS finds A-B-D-F and pushes 12, saturating B-D. It then finds A-C-E-F and pushes 4, saturating E-F. B and E now lead nowhere in the level graph, then C does too, and the phase ends with three dead ends dropped and a flow of 16.
  3. Phase 2 levels. In the residual graph A still reaches B and C, C reaches E through its 10 units of spare capacity, E reaches D through E-D, and D reaches F through the 8 units left on D-F. The sink is now 4 levels down, one deeper than before, exactly as the theory predicts.
  4. Phase 2 blocking flow. The only path in the new level graph is A-C-E-D-F, with bottleneck E-D of 7. Pushing 7 brings the flow to 23.
  5. Termination. The third BFS reaches B, C and E but cannot leave them: B-D, E-D and E-F are all saturated. F is unreachable, so the flow is maximum.

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.

Complexity, and where it comes from

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.

When to use Dinic's Algorithm, and when not to

Dinic is the usual choice for maximum flow in practice and in programming contests. The alternatives trade simplicity, bounds and constants differently.

AlternativePrefer it whenCost
Edmonds-KarpYou want the simplest correct implementation and the graph is small. One BFS per augmenting path.O(VE^2)
Ford-Fulkerson with DFSCapacities are small integers, so the number of augmentations, bounded by the flow value, stays small.O(E f)
Push-relabelVery large or dense networks, where its local operations and good heuristics often beat augmenting path methods.O(V^3) with FIFO
Hopcroft-KarpThe problem is bipartite matching. It is Dinic specialised to unit capacities, with a smaller constant.O(E sqrt(V))
Min-cost flowEdges also carry a cost per unit, and among maximum flows you want the cheapest.pseudo-polynomial with SSP

Common pitfalls

  • Leaving out the current arc pointer. Without it, every DFS rescans edges already known to lead to dead ends, and a phase can cost far more than O(VE). This is the most common reason an implementation of Dinic times out.
  • Allowing edges that do not go one level deeper. Using any residual edge, instead of only those from level k to level k + 1, breaks the shortest path property, and with it the bound on the number of phases.
  • Forgetting reverse edges. Pushing flow along u to v must add the same amount of residual capacity on v to u. Without reverse edges the algorithm cannot reroute flow and will often stop below the maximum.
  • Recursing too deeply. On long chains the DFS recursion can be as deep as V. In languages with small stacks, write the DFS iteratively or raise the stack limit.

Frequently asked questions

What is Dinic's algorithm?
It is a maximum flow algorithm, published by Yefim Dinitz in 1970, that works in phases. Each phase computes BFS levels from the source, keeps only edges that go one level deeper, and pushes a blocking flow through this level graph before the levels are recomputed.
What is a level graph?
The subgraph of the residual network that contains only edges from a vertex at BFS distance k to a vertex at distance k + 1 from the source. Every path from source to sink in it is a shortest augmenting path.
What is a blocking flow?
A flow in the level graph such that every path from source to sink contains at least one saturated edge. It need not be a maximum flow of the level graph, only one that cannot be extended without leaving it.
What is the time complexity of Dinic's algorithm?
O(V^2 E) in general. There are at most V - 1 phases and each costs O(VE). On unit capacity networks it is much faster, O(E sqrt(V)) for bipartite matching.
What is the difference between Dinic and Edmonds-Karp?
Both augment along shortest paths. Edmonds-Karp runs a new BFS for every single augmenting path, while Dinic runs one BFS per phase and then finds all the shortest augmenting paths of that length with DFS. That lowers the bound from O(VE^2) to O(V^2 E).

Related algorithms: Maximum Flow, Minimum Cut, Hopcroft-Karp Matching, Breadth-First Search

Interactive Controls

Zoom Controls

39%
Nodes: 6
Edges: 9