How Dinic blocks a level graph

Short answer: Dinic’s algorithm repeats two steps. Breadth-first search assigns a level, the residual distance from the source, and discards every edge that does not advance exactly one level. Depth-first search then pushes a blocking flow: every residual source-sink path in that level graph is saturated. The level of the sink increases each phase. There are O(V) phases and a phase costs O(VE), for O(V²E) time in general. Unit-capacity networks are faster.


What the problem actually asks

Edmonds-Karp throws away the breadth-first layering after a single path. Many disjoint shortest paths may exist at the same distance. A blocking flow sends as much as possible on the whole layering before the next expensive rebuild. An edge that jumps more than one level, or that goes backward in level, cannot belong to a shortest residual path, so the level graph omits it.

The depth-first search uses a pointer into each adjacency list so that a dead edge is not rescanned in the same phase. That current-arc optimization is what makes a phase O(VE) rather than a fresh search per path.


What the algorithm guarantees

Each phase increases the residual distance of the sink by at least 1, so there are at most V−1 phases. A phase builds levels in O(E) and pushes a blocking flow in O(VE) with current arcs, for O(V²E) overall. Extra memory is the level array, the residual graph, and the arc pointers. The flow is maximum when a phase cannot reach the sink.

On unit-capacity networks the bound improves to O(min(V^{2/3}, E^{1/2}) · E). On the unit network that represents bipartite matching it becomes O(E √V), matching Hopcroft-Karp. Those improvements are properties of the capacities, not of a different code path, although a matching implementation can be specialized further.


The working steps

  1. Maintain residual capacities in both directions, initially equal to the capacities forward and zero backward.
  2. Breadth-first search residual edges from the source and record levels. If the sink has no level, stop.
  3. Keep only residual edges u→v with level[v] = level[u]+1. Depth-first search from the source to the sink in this graph, pushing the bottleneck and updating residuals, including backward edges.
  4. A vertex that cannot extend deletes its current arc and retreats. The search continues until no residual path remains in the level graph. That is a blocking flow.
  5. Discard the levels and repeat. Advance the current-arc pointers only inside a phase, and reset them when levels are rebuilt.

The O(V²E) bound is the general one. Quoting the matching bound O(E √V) on an arbitrary capacity graph is false. The level graph must be rebuilt after a blocking flow, because residual backward edges change distances. Push-relabel is the alternative that never searches for a path.


Approaches that look simpler and fail

Approach Why it fails when a phase does not block the level graph
Send one shortest path and recompute levels That is Edmonds-Karp. It is correct and pays for a new BFS per path instead of per phase.
Keep edges that skip a level Those edges are not on a shortest residual path. Using them breaks the proof that the sink’s distance increases.
Forget the backward residual when the DFS augments Later paths in the same phase, and every later phase, need the ability to undo flow.
Apply the bipartite O(E √V) bound to arbitrary capacities That bound uses unit capacities in a matching network. General capacities stay at O(V²E) for this algorithm.

Fewest-edge augmentations one at a time are Edmonds-Karp. Maximum bipartite matching can call this algorithm on the split network or call Hopcroft-Karp directly. Preflows belong to push-relabel.


A working example

This Python fragment builds a level graph with breadth-first search and sends blocking flow along it. On the same network used for Edmonds–Karp, source edges 3 and 2 into a sink that can take 2 and 3, the value is 5.

from collections import deque
cap = {(0, 1): 3, (0, 2): 2, (1, 3): 2, (2, 3): 3, (1, 2): 1}
res = dict(cap)

def bfs():
    level = [-1] * 4
    level[0] = 0
    q = deque([0])
    while q:
        u = q.popleft()
        for v in range(4):
            if res.get((u, v), 0) and level[v] < 0:
                level[v] = level[u] + 1
                q.append(v)
    return level

def dfs(u, limit, level, it):
    if u == 3:
        return limit
    while it[u] < 4:
        v = it[u]
        if res.get((u, v), 0) and level[v] == level[u] + 1:
            pushed = dfs(v, min(limit, res[(u, v)]), level, it)
            if pushed:
                res[(u, v)] -= pushed
                res[(v, u)] = res.get((v, u), 0) + pushed
                return pushed
        it[u] += 1
    return 0

flow = 0
while True:
    level = bfs()
    if level[3] < 0:
        break
    it = [0] * 4
    while (pushed := dfs(0, 10**9, level, it)):
        flow += pushed
assert flow == 5
print(flow)

Where it is used

Dinic’s blocking-flow algorithm is a maximum-flow implementation used for network capacity and for bipartite matching reduced to a flow. Each phase stays inside a level graph built by breadth-first search. Edmonds-Karp is the simpler bound; push-relabel is the other production family.


When you can drop this algorithm

Drop Dinic only when a simpler Edmonds-Karp implementation is fast enough for the instance, or when a specialized matching or unit-capacity code is already in use. Keep it as the general maximum-flow algorithm with a blocking flow per level graph.


Why this stays published

We publish this so a search for Dinic returns the blocking flow on the level graph and the O(V²E) general bound. Dinic’s algorithm computes a maximum flow in O(V²E) time by sending a blocking flow through each breadth-first level graph.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *