Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

Max-Flow Min-Cut in Depth

Ford–Fulkerson, Edmonds–Karp, Dinic and the duality theorem.

Ford–Fulkerson's running time depends on the capacities: with integers it may need as many augmentations as the flow is large, and with irrational capacities it may never stop. Edmonds and Karp made the count O(nm)O(nm) by always taking a shortest path, Dinic batched shortest paths into blocking flows, Goldberg and Tarjan abandoned paths altogether with push-relabel, and in 2013 Orlin reached O(nm)O(nm), the bound that path-based methods had aimed at for decades.

What you’ll learn

  • Prove max-flow min-cut through linear-programming duality with threshold rounding, characterize when the minimum cut is unique, and prove the flow decomposition theorem.
  • Prove that Edmonds–Karp performs O(nm)O(nm) augmentations: residual distances never decrease, and each arc is critical O(n)O(n) times.
  • Analyze Dinic's algorithm: level graphs, blocking flows with current-arc pointers, O(n2m)O(n^2 m) in general and O(mn)O(m\sqrt n) on unit networks.
  • Explain push-relabel through its height invariant and bounds, and place the landmark algorithms from Ford–Fulkerson to Orlin's O(nm)O(nm).

Duality, decomposition and the set of minimum cuts

The Optimized lesson proved max-flow min-cut with augmenting paths. A second proof explains why such a theorem exists: maximum flow is a linear program, and minimum cut is its dual, with an integral optimum.

(P)max⁡ ∑vf(s,v)−∑vf(v,s)s.t. conservation at v≠s,t,  0≤f≤c(D)min⁡ ∑(u,v)∈Ec(u,v) y(u,v)s.t. y(u,v)≥π(u)−π(v),  y≥0,  π(s)=1, π(t)=0\begin{aligned} \textbf{(P)}\quad &\max\ \textstyle\sum_{v} f(s, v) - \sum_{v} f(v, s) \quad \text{s.t. conservation at } v \ne s, t,\ \ 0 \le f \le c \\ \textbf{(D)}\quad &\min\ \textstyle\sum_{(u,v) \in E} c(u, v)\, y(u, v) \quad \text{s.t. } y(u, v) \ge \pi(u) - \pi(v),\ \ y \ge 0,\ \ \pi(s) = 1,\ \pi(t) = 0 \end{aligned}
The dual assigns each vertex a potential π\pi and pays c(u,v)c(u, v) per unit of potential drop along an edge. A cut (S,T)(S, T) is the dual solution with π=1\pi = 1 on SS, π=0\pi = 0 on TT, and y=1y = 1 exactly on the edges from SS to TT; its objective is c(S,T)c(S, T). So the dual's optimum is at most the minimum cut.
DerivationThe dual has an integral optimum: threshold rounding
  1. Sθ={ v:π(v)≥θ }S_\theta = \{\, v : \pi(v) \ge \theta \,\}

    Take an optimal dual solution and clip every potential into [0,1][0, 1], which never increases a positive drop π(u)−π(v)\pi(u) - \pi(v). Pick a threshold θ\theta uniformly at random in (0,1](0, 1] and let Sθ={v:π(v)≥θ}S_\theta = \{v : \pi(v) \ge \theta\}: a cut, since π(s)=1≥θ>0=π(t)\pi(s) = 1 \ge \theta > 0 = \pi(t).

  2. Pr⁡[(u,v) crosses]=max⁡{0, π(u)−π(v)}≤y(u,v)\Pr[(u, v) \text{ crosses}] = \max\{0,\ \pi(u) - \pi(v)\} \le y(u, v)

    An edge (u,v)(u, v) crosses from SθS_\theta to its complement exactly when π(v)<θ≤π(u)\pi(v) < \theta \le \pi(u), which has probability at most the drop, and so at most y(u,v)y(u, v):

  3. min⁡Sc(S,T) ≤ E[c(Sθ,Tθ)] ≤ ∑c y=OPT(D)=OPT(P)\min_{S} c(S, T) \ \le\ \E\big[c(S_\theta, T_\theta)\big] \ \le\ \textstyle\sum c\,y = \mathrm{OPT}(D) = \mathrm{OPT}(P)

    By linearity of expectation, the average cut costs at most the dual optimum, so some cut does too. With LP strong duality (P = D), this is max-flow min-cut again:

Decomposition explains a barrier. There are networks where every maximum flow needs Θ(m)\Theta(m) paths of length Θ(n)\Theta(n), so any algorithm that builds the flow path by path, writing each path down, spends Ω(nm)\Omega(nm) time: the flow-decomposition barrier in the title of Goldberg and Rao's 1998 paper "Beyond the flow decomposition barrier". Orlin's O(nm)O(nm) meets it; going below requires algorithms that never list paths one at a time.

Check your understanding

After computing a maximum flow, let Smin⁡S_{\min} be the vertices reachable from ss in the residual graph, and Tmin⁡T_{\min} the vertices from which tt is reachable. When is the minimum cut unique?

  1. Always: a maximum flow determines its minimum cut
  2. Exactly when Smin⁡∪Tmin⁡=VS_{\min} \cup T_{\min} = V
  3. Exactly when every edge leaving Smin⁡S_{\min} is saturated
  4. Exactly when all capacities are distinct
Show the answer

B. Exactly when Smin⁡∪Tmin⁡=VS_{\min} \cup T_{\min} = V

Smin⁡S_{\min} is the source side of the minimum cut with the smallest source side, and V∖Tmin⁡V \setminus T_{\min} the source side of the one with the largest; every minimum cut's source side lies between them. They coincide iff no vertex is left over, iff Smin⁡∪Tmin⁡=VS_{\min} \cup T_{\min} = V. On the widget's ladder network, many vertices are reachable from neither side, and it has 15 minimum cuts.

Edmonds–Karp: O(nm) augmentations

DerivationProof
  1. δ′(v)<δ(v),δ′(u)=δ′(v)−1≥δ(u)\delta'(v) < \delta(v), \qquad \delta'(u) = \delta'(v) - 1 \ge \delta(u)

    (a) Suppose an augmentation changes ff into f′f' and some distances shrink; let vv be such a vertex with the smallest new distance δ′(v)\delta'(v), and uu its predecessor on a shortest path in Gf′G_{f'}. Then δ′(u)=δ′(v)−1\delta'(u) = \delta'(v) - 1, and by the choice of vv, δ′(u)≥δ(u)\delta'(u) \ge \delta(u).

  2. δ(v)=δ(u)−1≤δ′(u)−1=δ′(v)−2<δ′(v)\delta(v) = \delta(u) - 1 \le \delta'(u) - 1 = \delta'(v) - 2 < \delta'(v)

    If the arc (u,v)(u, v) was already in GfG_f, then δ(v)≤δ(u)+1≤δ′(v)\delta(v) \le \delta(u) + 1 \le \delta'(v), a contradiction. So (u,v)(u, v) is new: the augmentation pushed flow along (v,u)(v, u), which lay on a shortest path, so δ(u)=δ(v)+1\delta(u) = \delta(v) + 1. Then

  3. δ′′(u)=δ′′(v)+1 ≥ δ(v)+1=δ(u)+2\delta''(u) = \delta''(v) + 1 \ \ge\ \delta(v) + 1 = \delta(u) + 2

    which contradicts δ′(v)<δ(v)\delta'(v) < \delta(v). (b) When (u,v)(u, v) is critical it lies on a shortest path, so δ(v)=δ(u)+1\delta(v) = \delta(u) + 1, and it disappears. It can reappear only after some later augmentation, with flow f′′f'', pushes flow back along (v,u)(v, u) on a shortest path, and by (a):

  4. #augmentations≤2m⋅n2=nm,time O(nm⋅m)=O(nm2)\#\text{augmentations} \le 2m \cdot \tfrac n2 = nm, \qquad \text{time } O(nm \cdot m) = O(nm^2)

    So between two critical moments of (u,v)(u, v), the distance of uu grows by at least 2, and it stays below nn while uu is reachable. Each arc is critical at most n/2n/2 times; with at most 2m2m arcs and one breadth-first search of O(m)O(m) per augmentation:

Check your understanding

Which sequence of path lengths (numbers of arcs) can Edmonds–Karp not produce on any network?

  1. 3, 3, 4, 5
  2. 2, 2, 2, 2
  3. 3, 4, 3, 5
  4. 5, 7, 7
Show the answer

C. 3, 4, 3, 5

By part (a), the distance from ss to tt never decreases, and each augmenting path has exactly that length, so the lengths are non-decreasing. On the widget's classic network, Edmonds–Karp's lengths are 3, 3, 4, 5.

Other ways to tame the capacities: Edmonds and Karp also analyzed the fattest augmenting path, which needs O(mlog⁡U)O(m \log U) augmentations for integer capacities up to UU, and capacity scaling augments only along paths of residual capacity at least Δ\Delta, halving Δ\Delta from the largest power of 2 down to 1, for O(m2log⁡U)O(m^2 \log U) in all. These bounds are weakly polynomial: they depend on log⁡U\log U. Edmonds–Karp's O(nm2)O(nm^2) is strongly polynomial: it depends on nn and mm only.

Dinic: level graphs and blocking flows

DerivationProof sketch
  1. δf′(s,t)>δf(s,t)\delta_{f'}(s, t) > \delta_f(s, t)

    (a) After the blocking flow, the only new residual arcs are back edges, which go one level up. A path in the new residual graph of the old length d=δ(t)d = \delta(t) would have to use only level-increasing arcs of LfL_f, so it would be a path of LfL_f, and each of those contains a saturated arc. So the new distance exceeds dd.

  2. O(m+nm)=O(nm) per phase,O(n⋅nm)=O(n2m) in allO(m + nm) = O(nm) \text{ per phase}, \qquad O(n \cdot nm) = O(n^2 m) \text{ in all}

    (b) A depth-first search from ss advances along each vertex's current arc. If the search dead-ends at a vertex, the current arc into it is useless for the rest of the phase and the pointer moves on: at most mm such retreats per phase. Each search that reaches tt costs O(n)O(n) and saturates an arc, at most mm times:

  3. #phases≤2n⟹O(mn)\#\text{phases} \le 2\sqrt n \quad\Longrightarrow\quad O(m\sqrt n)

    (c) On a unit network each vertex carries at most one unit, so a residual flow decomposes into vertex-disjoint paths. After n\sqrt n phases every augmenting path has more than n\sqrt n vertices, so at most n/n=nn / \sqrt n = \sqrt n units remain, and each further phase adds at least one. A unit-capacity blocking flow takes O(m)O(m), since every arc is used once:

cpp
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
using namespace std;

struct Dinic {
    struct Edge { int to; long long cap; };
    vector<Edge> e;                          // edge i and its reverse i ^ 1
    vector<vector<int>> g;
    vector<int> level, it;
    Dinic(int n) : g(n), level(n), it(n) {}
    void add_edge(int u, int v, long long c) {
        g[u].push_back(e.size()); e.push_back({v, c});
        g[v].push_back(e.size()); e.push_back({u, 0});
    }
    bool bfs(int s, int t) {                 // levels in the residual graph
        fill(level.begin(), level.end(), -1);
        queue<int> q; q.push(s); level[s] = 0;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int id : g[u])
                if (e[id].cap > 0 && level[e[id].to] < 0) {
                    level[e[id].to] = level[u] + 1; q.push(e[id].to);
                }
        }
        return level[t] >= 0;
    }
    long long dfs(int u, int t, long long f) {   // one path of the level graph
        if (u == t) return f;
        for (int &i = it[u]; i < (int)g[u].size(); ++i) {   // current-arc pointer
            Edge &ed = e[g[u][i]];
            if (ed.cap > 0 && level[ed.to] == level[u] + 1) {
                long long d = dfs(ed.to, t, min(f, ed.cap));
                if (d > 0) { ed.cap -= d; e[g[u][i] ^ 1].cap += d; return d; }
            }
        }
        return 0;                            // dead end: the pointer stays past it
    }
    long long max_flow(int s, int t) {
        long long flow = 0;
        while (bfs(s, t)) {                  // one phase per BFS
            fill(it.begin(), it.end(), 0);
            while (long long f = dfs(s, t, LLONG_MAX)) flow += f;   // blocking flow
        }
        return flow;
    }
};

The contest standard. it[u] is the current-arc pointer: it is taken by reference, so arcs that lead nowhere are skipped for the rest of the phase. long long capacities avoid overflow when flows add up; on the classic network, max_flow returns 16.

Push-relabel, and seventy years of bounds

Push-relabel (Goldberg & Tarjan, 1988) drops the conservation constraint while it works. A preflow lets a vertex hold an excess e(v)≥0e(v) \ge 0 of inflow over outflow. Each vertex has a height h(v)h(v), with h(s)=nh(s) = n and h(t)=0h(t) = 0 fixed. An active vertex (one with excess) pushes along a residual arc that goes exactly one step down, h(u)=h(v)+1h(u) = h(v) + 1; if it has no such arc, it is relabeled to one more than its lowest residual neighbor. Start by saturating every edge out of ss, and stop when no vertex other than ss and tt has excess.

In practice, push-relabel with two heuristics, periodic global relabeling by a backward breadth-first search from tt and the gap heuristic, is among the fastest general max-flow codes (Cherkassky & Goldberg, 1997), while on the grid graphs of computer vision the Boykov–Kolmogorov augmenting-path algorithm is the usual choice.

YearAlgorithmTime
1956Ford–Fulkerson (integer capacities)O(m ∣f∗∣)O(m\,\abs{f^*})
1970Dinic: blocking flowsO(n2m)O(n^2 m)
1972Edmonds–Karp: shortest augmenting pathsO(nm2)O(nm^2)
1974Karzanov: preflowsO(n3)O(n^3)
1981–83Sleator–Tarjan: blocking flows with dynamic treesO(nmlog⁡n)O(nm \log n)
1986–88Goldberg–Tarjan: push-relabel with dynamic treesO(nmlog⁡(n2/m))O(nm \log(n^2/m))
1994King, Rao & TarjanO(nmlog⁡m/(nlog⁡n)n)O(nm \log_{m/(n \log n)} n)
1998Goldberg–Rao: binary blocking flows (capacities up to UU)O(mmin⁡{n2/3,m}log⁡(n2/m)log⁡U)O(m \min\{n^{2/3}, \sqrt m\} \log(n^2/m) \log U)
2013OrlinO(nm)O(nm), strongly polynomial
2022Chen, Kyng, Liu, Peng, Probst Gutenberg & Sachdevam1+o(1)m^{1+o(1)} (polynomially bounded integer capacities)
King, Rao and Tarjan's bound is already O(nm)O(nm) when m≥n1+εm \ge n^{1+\varepsilon}; Orlin's new algorithm handles the sparse graphs, and together they give O(nm)O(nm) for all mm. The last row, which escapes the flow-decomposition barrier with continuous optimization, is the subject of Maximum Flow in Almost-Linear Time.

Key takeaways

  • Maximum flow is a linear program whose dual is a fractional cut; threshold rounding of the dual potentials yields an integral cut, which is max-flow min-cut again. The minimum cut is unique iff every vertex is reachable from ss or reaches tt in the final residual graph.
  • Every flow decomposes into at most mm paths and cycles, which is why path-by-path algorithms face an Ω(nm)\Omega(nm) barrier.
  • Edmonds–Karp: residual distances never decrease and each arc is critical at most n/2n/2 times, so at most nmnm augmentations and O(nm2)O(nm^2) time, whatever the capacities.
  • Dinic: at most n−1n - 1 phases of O(nm)O(nm) blocking flows, O(n2m)O(n^2 m); on unit networks O(n)O(\sqrt n) phases and O(mn)O(m\sqrt n). Push-relabel keeps a height invariant that forbids augmenting paths: O(n2m)O(n^2 m), O(n3)O(n^3) with FIFO order.
  • The strongly polynomial record is Orlin's O(nm)O(nm) (2013); below it, only algorithms that do not build the flow path by path, such as the 2022 almost-linear-time algorithm, have broken through.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In the analysis of Edmonds–Karp, why can a residual arc (u,v)(u, v) be critical (the bottleneck that empties it) only O(n)O(n) times?

  1. Because its capacity halves each time it is critical
  2. Because between two times it is critical, the distance from ss to uu grows by at least 2, and distances are below nn
  3. Because each vertex can be on at most nn augmenting paths
  4. Because breadth-first search visits each arc once
Show the answer

B. Because between two times it is critical, the distance from ss to uu grows by at least 2, and distances are below nn

When (u,v)(u, v) is critical, δ(v)=δ(u)+1\delta(v) = \delta(u) + 1. It returns only after flow is pushed back along (v,u)(v, u), on a shortest path, when δ′(u)=δ′(v)+1≥δ(v)+1=δ(u)+2\delta'(u) = \delta'(v) + 1 \ge \delta(v) + 1 = \delta(u) + 2. Distances never decrease and stay below nn while uu is reachable, so this happens at most about n/2n/2 times.

Question 2

Why does Dinic's algorithm need at most n−1n - 1 phases?

  1. Each phase saturates at least one edge of the original network for good
  2. Each phase settles one vertex, as in Dijkstra's algorithm
  3. Each phase at least halves the remaining flow
  4. After a blocking flow, the distance from ss to tt in the residual graph strictly increases, and it is at most n−1n - 1
Show the answer

D. After a blocking flow, the distance from ss to tt in the residual graph strictly increases, and it is at most n−1n - 1

Every shortest path of the old level graph contains a saturated arc, and the only new arcs are back edges that go up a level, so no path of the old length survives. Distances are integers between 1 and n−1n - 1 while tt is reachable.

Question 3

Push-relabel keeps h(s)=nh(s) = n, h(t)=0h(t) = 0 and h(u)≤h(v)+1h(u) \le h(v) + 1 for every residual arc (u,v)(u, v). What does that invariant guarantee?

  1. At every moment, the residual graph has no path from ss to tt
  2. Every push moves flow closer to tt in distance
  3. Every vertex's excess is at most 1
  4. The heights equal the residual distances to tt
Show the answer

A. At every moment, the residual graph has no path from ss to tt

A residual path from height nn down to height 0 would need at least nn arcs, one per unit of height, but a simple path has at most n−1n - 1. So the preflow always has the property that defines a maximum flow; once the excesses are gone it is a flow, hence a maximum one. Push-relabel works from the opposite side of the theorem from augmenting paths.

Further reading

  • E. A. Dinic, "Algorithm for solution of a problem of maximum flow in a network with power estimation," Soviet Mathematics Doklady 11 (1970), 1277–1280.
  • J. Edmonds & R. M. Karp, "Theoretical improvements in algorithmic efficiency for network flow problems," Journal of the ACM 19(2) (1972), 248–264.
  • A. V. Goldberg & R. E. Tarjan, "A new approach to the maximum-flow problem," Journal of the ACM 35(4) (1988), 921–940.
  • J. B. Orlin, "Max flows in O(nm) time, or better," Proceedings of the 45th ACM Symposium on Theory of Computing (2013), 765–774.
  • R. K. Ahuja, T. L. Magnanti & J. B. Orlin, Network Flows: Theory, Algorithms, and Applications (Prentice Hall, 1993).