Chapter 8: Network Flow
Introduction
Learning Outcomes
Model a problem as a flow network with a source, a sink and capacitated edges; write down the capacity and conservation constraints.
Compute the residual network, an augmenting path and its bottleneck; find the capacity of an s–t cut and apply the max-flow min-cut theorem.
Run Ford-Fulkerson, Edmonds-Karp, Dinic and push–relabel step by step on an example network.
Explain where the bounds O(E·|f*|), O(V·E²) and O(V²·E) come from; compare the algorithms and pick the right one.
Flow Networks
The Maximum-Flow Problem
- A directed graph has a source vertex s and a sink vertex t.
- Each edge has a capacity c(u, v) ≥ 0: the most that can pass through it per unit time.
- Goal: find the maximum flow that can be sent from source to sink without exceeding any capacity.
Edge labels are capacities. The maximum flow in this network is 7.
Flow Networks
Flows and Their Constraints
A flow is a function that assigns a number f(u, v) to every edge:
- Capacity constraint: 0 ≤ f(u, v) ≤ c(u, v).
- Flow conservation: for every v other than s and t,
flow in = flow out.
Value of the flow: |f| = Σ f(s, v) − Σ f(v, s)
(net flow out of the source = net flow into the sink).
Label: flow/capacity. 3 + 2 = 5 enters C and 5 leaves. |f| = 4 + 3 = 7.
Flow Networks
Several Sources and Sinks
- With several sources (s₁ … sₙ) and sinks (t₁ … tₘ), add a new super source S and super sink T.
- Give the edges S → sᵢ and tⱼ → T unlimited (∞) capacity; the problem becomes a single-source, single-sink maximum flow.
Residual Networks and Augmenting Paths
The Residual Network
- The residual network Gf shows how much flow can still be sent under the current flow f.
- Forward edge: cf(u, v) = c(u, v) − f(u, v): capacity minus flow; if the flow is below capacity, the leftover is positive.
- Reverse edge: cf(v, u) = f(u, v): flow already sent can be taken back (cancelled).
- Only edges with cf > 0 belong to Gf; augmenting paths are searched for in this network.
Solid arrow: forward (remaining) capacity · dashed arrow: reverse edge. Without reverse edges the algorithm cannot undo its first bad choice.
Residual Networks and Augmenting Paths
Augmenting Paths
- A directed path from source to sink in the residual network; it carries extra flow from source to sink.
- Every edge on the path has positive residual capacity: it carries less flow than its capacity (or is a reverse edge).
- Bottleneck: cf(p) = min cf(u, v), (u, v) ∈ p. That much flow is added along the path.
- Usually found with DFS or BFS; when no augmenting path is left, the flow is maximum.
p = S → A → B → T: min(4, 4, 2) = 2.
Cuts
Cuts
- An s–t cut (S, T) splits the vertices in two with s ∈ S and t ∈ T; removing the cut edges disconnects the graph.
- Cut capacity: c(S, T) = Σ c(u, v), u ∈ S, v ∈ T: only edges going from S to T count.
- For any flow, the net flow across a cut equals |f|; hence |f| ≤ c(S, T).
- Minimum cut (min-cut): a cut of minimum capacity (the sum of capacities, not the number of edges).
S = {S, A}: c(S, T) = 4 + 3 = 7 (minimum).
Cuts
The Max-Flow Min-Cut Theorem
For a flow f, the following are equivalent (Ford and Fulkerson, 1956):
- f is a maximum flow.
- The residual network Gf has no augmenting path.
- |f| = c(S, T) for some cut (S, T).
| S side (example network) | c(S, T) |
|---|---|
| {S} | 7 |
| {S, A} | 7 |
| {S, A, B} | 8 |
| {S, A, B, C, D} | 8 |
| {S, C} | 10 |
| {S, B, D} | 18 |
The smallest of the 16 cuts is 7; since the flow found also has value 7, it is a maximum flow.
Cuts
Network-Flow Algorithms
Goal: send as much flow as possible from source to sink under the constraints.
| Algorithm | Approach | Time | Note |
|---|---|---|---|
| Ford-Fulkerson (1956) | any augmenting path (usually DFS) | O(E · |f*|) | simple; time depends on the flow value |
| Edmonds-Karp (1972) | shortest augmenting path (BFS) | O(V · E²) | the polynomial-time form of Ford-Fulkerson |
| Dinic (1970) | level graph + blocking flow | O(V² · E) | faster than Edmonds-Karp; very efficient with unit capacities |
| Push–relabel (1988) | preflow + heights, local operations | O(V² · E) | no path search; FIFO version O(V³) |
|f*|: the value of a maximum flow. The Ford-Fulkerson bound holds for integer capacities.
Ford-Fulkerson
The Ford-Fulkerson Method
- Developed by L. R. Ford Jr. and D. R. Fulkerson (1956).
- Finds the maximum flow between two vertices of a weighted (capacitated) directed graph: a graph problem that models the most water that can be sent from source to sink through pipes of given capacity.
- Principles: the network is a directed graph; each edge has a capacity; all flows start at 0; augmenting paths are found until the flow is maximum.
- It is a method, since it does not say how to find the augmenting path: Ford-Fulkerson usually uses DFS, Edmonds-Karp uses BFS.
Ford-Fulkerson
Ford-Fulkerson: The Steps
0. Set f(u, v) = 0 on every edge; the residual network = the original network.
The flow starts at zero, so each edge's residual capacity is its own capacity.
1. Find an augmenting path p from source to sink in the residual network (edges with cf > 0).
The path is found with DFS (Ford-Fulkerson) or BFS (Edmonds-Karp).
2. Compute the bottleneck: δ = min cf(u, v), (u, v) ∈ p.
The narrowest edge limits the path's flow.
3. Send δ units along the path: f(u, v) += δ, and for the reverse edge f(v, u) −= δ; |f| += δ.
Reverse edges are updated; at least one edge on the path becomes saturated.
4. Repeat 1–3 until no augmenting path is left; |f| is maximum.
The loop ends when the sink is unreachable (no augmenting path is left); the max-flow min-cut theorem guarantees the result is optimal.
Ford-Fulkerson
Ford-Fulkerson Example 1
Start: all flows are 0, and the residual network is identical to the original.
First augmenting path p = {S, A, B, T}: bottleneck 2 units. |f| = 2.
Second augmenting path p = {S, C, D, T}: bottleneck 3 units. |f| = 5.
Third augmenting path p = {S, A, B, C, D, T}: bottleneck 2 units. |f| = 7.
Both edges out of S are saturated: in the residual network S cannot reach anything. Maximum flow 7; min cut ({S}, the rest) = 4 + 3.
Ford-Fulkerson
Ford-Fulkerson Example 2: Using a Reverse Edge
Source S, sink T. All flows start at 0.
p = S → 1 → 4 → T: bottleneck 1 → 4 (8). |f| = 8.
p = S → 3 → 4 → T: only 2 is left on 4 → T. |f| = 10.
p = S → 3 → 4 → 1 → 2 → T: the reverse edge 4 → 1 is used; the flow on 1 → 4 drops from 8 to 4, and the 4 units leaving 1 are rerouted to 1 → 2. |f| = 14.
p = S → 1 → 4 → 2 → T: 2 units were left on S → 1. |f| = 16.
p = S → 3 → 4 → 2 → T: bottleneck 3 → 4 (9 − 6 = 3 left). |f| = 19.
In the residual network S reaches only 3 → min cut ({S, 3}, the rest) = 10 + 9 = 19 = |f*|.
Ford-Fulkerson
Why Are Reverse Edges Needed?
- S → 1 → 4 → T: 8
- S → 3 → 4 → T: 2
- S → 1 → 2 → T: 2
- S → 3 → 4 → 2 → T: 6
Total 18. From S only 3 and 4 are reachable; 4's outgoing edges are full. Stuck!
The first choice sent 8 units on 1 → 4. The reverse edge 4 → 1 lets us undo part of that decision: flow from 1 goes to 2, and flow from 3 goes to 4.
Result 19 = min cut. Whatever order the paths are chosen in, the residual network reaches the maximum.
Ford-Fulkerson
Ford-Fulkerson: Pseudocode
FORD_FULKERSON(G, source, sink): flow[u, v] = 0, for each (u, v) in E // start at 0 maxFlow = 0 while an augmenting path exists do: path = DFS(G, source, sink, flow) // edges with c − flow > 0 delta = ∞ // smallest residual on path v = sink while v ≠ source do: u = path.parent[v] delta = min(delta, capacity[u, v] − flow[u, v]) v = u v = sink // update flows on the path while v ≠ source do: u = path.parent[v] flow[u, v] += delta flow[v, u] −= delta // reverse edge v = u maxFlow += delta return maxFlow, flow
Ford-Fulkerson
Java: Augmenting Paths with DFS
boolean dfs(int u, int sink, boolean[] visited, int[] parent) { visited[u] = true; if (u == sink) return true; for (int v = 0; v < V; v++) { if (!visited[v] && res[u][v] > 0) { parent[v] = u; if (dfs(v, sink, visited, parent)) return true; } } return false; // no path to sink from here }
res[u][v]: the residual capacity matrix. It starts as the capacities; reverse edges start at 0.- Returns
trueonce the sink is reached;parent[]gives the path backward. - Adding flow does
res[u][v] −= dandres[v][u] += d: the reverse edge appears automatically. - One DFS takes O(V²) on an adjacency matrix, O(V + E) on an adjacency list.
Ford-Fulkerson · Step through the code
DFS Paths: Dead Ends, Reverse Edges
Residual network after 2 augmentations (|f| = 4); dashed: reverse edges. dfs(S, T, ...) seeks path 3, trying S, A, B, C, D, T in order.
S → C has residual capacity 3 > 0 and C is unvisited: parent[C] = S, dfs(C).
C → B, a reverse edge (residual 3), B unvisited: parent[B] = C, dfs(B).
B → A, a reverse edge (residual 4), A unvisited: parent[A] = B, dfs(A).
A has no usable neighbor: a dead end. Return false, back up.
B → T has residual capacity 1 > 0 and T is unvisited: parent[T] = B, dfs(T).
u = T = sink: return true.
true passes up every call. Path S → C → B → T, bottleneck 1; now |f| = 5 (updated residuals shown).
boolean dfs(int u, int sink, boolean[] visited, int[] parent) { visited[u] = true; if (u == sink) return true; for (int v = 0; v < V; v++) { if (!visited[v] && res[u][v] > 0) { parent[v] = u; if (dfs(v, sink, visited, parent)) return true; } } return false; // no path to sink from here }
Ford-Fulkerson
Ford-Fulkerson: Complexity Analysis
- The running time depends on the network's structure and its capacities.
- Finding one augmenting path (DFS/BFS) and updating the flow: O(E).
- With integer capacities each augmenting path adds at least 1 unit → at most |f*| iterations.
- Total: O(E · |f*|), where E is the number of edges and |f*| the maximum flow value.
Ford-Fulkerson
A Bad Choice of Paths: 2000 Augmentations
- Maximum flow 2000 (s → a → t and s → b → t).
- A bad choice uses the middle edge every time: s → a → b → t, then via the reverse edge s → b → a → t … Each path adds only 1 unit → 2000 augmentations.
- Edmonds-Karp (BFS) picks shortest paths: 2 augmentations suffice.
- With capacity 10⁹ the DFS version could take billions of steps; BFS removes this dependence.
Edmonds-Karp
The Edmonds-Karp Algorithm
- Finds the maximum flow between two vertices of a weighted directed graph; a variant of the Ford-Fulkerson method.
- Developed by Jack Edmonds and Richard Karp (1972; Dinic independently in 1970).
- Finds augmenting paths with BFS (breadth-first search): each time it picks the augmenting path with the fewest edges.
- Edmonds-Karp uses BFS to find an augmenting path; Dinic uses BFS to check whether more flow is possible and to build a level graph.
Edmonds-Karp
Edmonds-Karp: Complexity O(V · E²)
- Each iteration is one BFS plus one path update: O(E).
- Because BFS picks shortest paths, the residual distance from s to every vertex never decreases.
- Each augmentation makes at least one edge critical (saturated). For the same edge to become critical again its distance must grow by at least 2; since distances are ≤ V, each edge is critical at most ~V/2 times.
- Number of augmentations O(V · E) → total O(V · E) · O(E) = O(V · E²), with E edges and V vertices.
Edmonds-Karp
Java: Augmenting Paths with BFS
boolean bfs(int src, int sink, int[] parent) { boolean[] visited = new boolean[V]; Queue<Integer> queue = new LinkedList<>(); queue.add(src); visited[src] = true; parent[src] = -1; while (!queue.isEmpty()) { int u = queue.poll(); for (int v = 0; v < V; v++) if (!visited[v] && res[u][v] > 0) { queue.add(v); parent[v] = u; visited[v] = true; } } return visited[sink]; }
- The queue starts at the source; every unvisited neighbor with positive residual capacity is enqueued.
parent[v] = ustores the BFS tree; if the sink is visited, the path found has the fewest edges.- This function is the only difference from Ford-Fulkerson; the main loop is the same.
Edmonds-Karp · Step through the code
Fewest-Edge Path: bfs(s, t, parent)
Initial residual network (flows 0). s is marked and enqueued, parent[s] = −1; · = unassigned.
s dequeued: neighbors 1 (residual 16) and 2 (residual 13) enqueued, parent s.
1 is dequeued. Its unvisited neighbor 3 (residual 12) is enqueued with parent 1. 2 is already visited and is skipped.
2 is dequeued. Its unvisited neighbor 4 (residual 14) is enqueued with parent 2. 1 is already visited and is skipped.
3 dequeued: t (residual 20) enqueued, parent 3; 2 skipped. t is reached, but the loop runs until the queue is empty.
4 is dequeued. 3 and t are already visited and are skipped.
t is dequeued. It has no outgoing edge with positive residual capacity.
Return true. Via parent[]: s → 1 → 3 → t, the fewest edges (3); bottleneck 12.
boolean bfs(int src, int sink, int[] parent) { boolean[] visited = new boolean[V]; Queue<Integer> queue = new LinkedList<>(); queue.add(src); visited[src] = true; parent[src] = -1; while (!queue.isEmpty()) { int u = queue.poll(); for (int v = 0; v < V; v++) if (!visited[v] && res[u][v] > 0) { queue.add(v); parent[v] = u; visited[v] = true; } } return visited[sink]; }
Edmonds-Karp · Step through the code
edmondsKarp(s, t): Step Through the Code
Start: residual network = capacities, maxFlow = 0.
bfs found a shortest path: s → 1 → 3 → t (3 edges).
Walking back from the target: flow = min(16, 12, 20) = 12.
Subtract 12 from the forward edges on the path and add 12 to the reverse edges (dashed arrows).
maxFlow = 0 + 12 = 12.
bfs found a shortest path: s → 2 → 4 → t (3 edges).
Walking back from the target: flow = min(13, 14, 4) = 4.
Subtract 4 from the forward edges on the path and add 4 to the reverse edges (dashed arrows).
maxFlow = 12 + 4 = 16.
bfs found a shortest path: s → 2 → 4 → 3 → t (4 edges).
Walking back from the target: flow = min(9, 10, 7, 8) = 7.
Subtract 7 from the forward edges on the path and add 7 to the reverse edges (dashed arrows).
maxFlow = 16 + 7 = 23.
BFS cannot reach t (from s only s, 1, 2, 4 are reachable). return 23.
int edmondsKarp(int s, int t) { int maxFlow = 0; int[] parent = new int[V]; while (bfs(s, t, parent)) { int flow = Integer.MAX_VALUE; for (int v = t; v != s; v = parent[v]) flow = Math.min(flow, res[parent[v]][v]); for (int v = t; v != s; v = parent[v]) { int u = parent[v]; res[u][v] -= flow; // forward res[v][u] += flow; // reverse } maxFlow += flow; } return maxFlow; }
Edmonds-Karp
Edmonds-Karp Example
Take this network. Upper bound: |f*| ≤ 24 (capacity out of s is 16 + 13 = 29, into t 20 + 4 = 24; the smaller one).
Step 1: the augmenting path p1 = ⟨s, 1, 3, t⟩ is chosen, with cf(p1) = 12 because of c(1, 3). |f| = 12.
Step 2: the augmenting path p2 = ⟨s, 2, 4, t⟩ is chosen, with cf(p2) = 4 because of c(4, t). |f| = 16.
Step 3: the augmenting path p3 = ⟨s, 2, 4, 3, t⟩ is chosen, with cf(p3) = 7 because of c(4, 3). |f| = 23.
Final state: in the residual network s reaches only 1, 2, 4. The cut ({s, 1, 2, 4}, {3, t}) (red: T side): 12 + 7 + 4 = 23 = |f*| (flow into t: 19 + 4).
Edmonds-Karp
Edmonds-Karp Example 2
Source A, sink G. All flows start at 0.
p = {A, D, E, G}: bottleneck E → G (1). Flow = 1.
p = {A, D, F, G}: 2 was left on A → D. Flow = 1 + 2 = 3.
p = {A, B, C, D, F, G}: A → D is saturated, so BFS finds a 5-edge path; bottleneck C → D (1). Flow = 4.
p = {A, B, C, E, D, F, G}: the reverse edge E → D lowers the flow on D → E from 1 to 0. Flow = 5.
BFS cannot reach G; from A, the vertices A, B, C, E are reachable. Cut capacity A→D 3 + C→D 1 + E→G 1 = 5 = |f*|.
Edmonds-Karp
Edmonds-Karp Example 3: Working on the Residual Network
From s to t; initially the residual network = the capacities. BFS first finds the 2-edge path s→4→t, then the 3-edge paths s→1→2→t and s→1→3→t.
Path s → 4 → t: bottleneck s → 4 (2). Push 2 units; s → 4 has 0 left, and the reverse edge 4 → s has 2.
Path s → 1 → 2 → t: bottleneck 2 → t (3). Push 3 units.
Path s → 1 → 3 → t: 1 was left on s → 1. Push 1 unit.
The edges out of s are saturated → no path. |f*| = 2 + 3 + 1 = 6: 3 units along s→1→2→t, 1 unit along s→1→3→t, 2 units along s→4→t.
Dinic
Dinic's Algorithm
- Finds a maximum flow in a graph (Y. Dinitz, 1970); works on a capacitated (weighted) graph.
- Level graph: BFS on the residual network gives each vertex a level (its distance from the source); only edges with level(v) = level(u) + 1 are kept.
- Blocking flow: a flow in the level graph that leaves at least one saturated edge on every s–t path; it is found with DFS.
- The flow is updated with reverse edges; phases repeat until the sink is unreachable in the level graph.
Dinic
Dinic: The Steps
1. Set the flow on every edge to 0.
The residual network is identical to the original.
2. Run BFS from the source to find each vertex's level (distance from the source). If the sink has no level, stop.
BFS uses edges with positive residual capacity; if the sink is unreachable, the flow is maximum.
3. Use DFS to find augmenting paths that go one level up at each step.
Only edges with level(v) = level(u) + 1 are followed; dead-end edges are never tried again.
4. Add each path's minimum capacity to the total flow; update the capacities of the edges and reverse edges.
When no path is left, this phase's blocking flow is found.
5. Go back to step 2 and repeat.
The s–t distance grows by at least 1 in each phase, so there are at most V − 1 phases.
Dinic
Dinic Example: Phases
The network (flow/capacity). All flows start at 0; residual network = capacities.
Phase 1 – BFS: levels s = 0; 1, 2 = 1; 3, 4 = 2; t = 3. The faded edges (1 → 2, 4 → 3) do not go up a level and are not used.
Phase 1 – blocking flow (DFS): {s, 1, 3, t} 4 units, {s, 1, 4, t} 6 units, {s, 2, 4, t} 4 units → |f| = 14.
Residual network after phase 1 (Gf): saturated edges remain only in the reverse direction (dashed).
Phase 2 – BFS: s = 0; 2 = 1; 4 = 2; 1, 3 = 3; t = 4. The s–t distance went from 3 to 4. The edge 4 → 1 is a dead end.
Phase 2 – blocking flow: {s, 2, 4, 3, t} 5 units (bottleneck 2 → 4) → |f| = 14 + 5 = 19.
Phase 3 – BFS: only s (0) and 2 (1) are reachable; there is no path to t → the algorithm ends. Min cut ({s, 2}, the rest) = 10 + 9 = 19.
Result: maximum flow 19; just 2 phases (2 + 1 BFS runs) were enough. On the same network Ford-Fulkerson used 5 augmenting paths.
Dinic
Dinic: Pseudocode
DINIC(G, source, sink): flow = [ ] // 0 for every edge maxFlow = 0 while BFS(G, source, sink, flow) do: // level graph is built while a blocking path exists do: delta = DFS(G, source, sink, flow, level, ∞) maxFlow += delta return maxFlow BFS(G, source, sink, flow): level[v] = −1, for every v level[source] = 0 queue.add(source) while queue is not empty do: u = queue.remove() for each (u, v) in G[u]: if level[v] < 0 and capacity[u,v] − flow[u,v] > 0: level[v] = level[u] + 1 queue.add(v) return level[sink] ≥ 0
DFS(G, u, sink, flow, level, lim): if u == sink: return lim for each (u, v) in G[u]: left = capacity[u,v] − flow[u,v] if level[v] == level[u] + 1 and left > 0: delta = DFS(G, v, sink, flow, level, min(lim, left)) if delta > 0: flow[u, v] += delta flow[v, u] −= delta // reverse return delta return 0
Dinic
Java: DFS on Levels with a Current-Arc Array
int dfs(int u, int t, int flow, int[] next) { if (u == t) return flow; for (; next[u] < adj[u].size(); next[u]++) { Edge e = adj[u].get(next[u]); if (level[e.to] == level[u] + 1 && e.flow < e.cap) { int d = dfs(e.to, t, Math.min(flow, e.cap - e.flow), next); if (d > 0) { e.flow += d; // forward edge adj[e.to].get(e.rev).flow -= d; // reverse edge return d; } } } return 0; // no level path from u to t left }
- Edges are kept in adjacency lists; each edge knows the index of its reverse edge (
rev). next[u]: the next edge to try at u (the current arc). A dead-end edge is never retried; all DFS calls in one phase take O(V · E) in total.- At the start of each phase
nextis reset and the levels are recomputed with BFS.
Dinic · Step through the code
Dinic: The Blocking Flow in Phase 1
Phase 1, with the BFS levels next to the vertices (lv). Only edges that go up one level are used; next[u] is the next edge to try at u.
Descend one level at a time: s → 1 (residual 10), 1 → 3 (residual 4), 3 → t (residual 10). Each call caps the flow at the residual (min); 4 reaches t.
As the flow returns along the path, 4 is added to each edge (and subtracted from the reverse edges). Back at s: |f| = 4.
s → 1 (residual 6), 1 → 4 (residual 8), 4 → t (residual 10). Each call caps the flow at the residual (min); 6 reaches t.
As the flow returns along the path, 6 is added to each edge (and subtracted from the reverse edges). Back at s: |f| = 10. s → 1 is saturated.
s → 2 (residual 10), 2 → 4 (residual 9), 4 → t (residual 4). Each call caps the flow at the residual (min); 4 reaches t.
As the flow returns along the path, 4 is added to each edge (and subtracted from the reverse edges). Back at s: |f| = 14.
Descended via s → 2 (residual 6), 2 → 4 (residual 5). No level path is left from 4 (4 → 3 stays on the same level, 4 → t is saturated): it returns 0.
No flow came back over 2 → 4: next[2]++. No level path is left from 2: it returns 0.
No flow came back over s → 2: next[s]++. No level path is left from s: it returns 0. The blocking flow is complete; the phase ends with |f| = 14.
int dfs(int u, int t, int flow, int[] next) { if (u == t) return flow; for (; next[u] < adj[u].size(); next[u]++) { Edge e = adj[u].get(next[u]); if (level[e.to] == level[u] + 1 && e.flow < e.cap) { int d = dfs(e.to, t, Math.min(flow, e.cap - e.flow), next); if (d > 0) { e.flow += d; adj[e.to].get(e.rev).flow -= d; return d; } } } return 0; // no level path left }
Dinic
Dinic: Complexity O(V² · E)
- Each phase needs one BFS: O(V + E).
- After each phase the s–t distance strictly increases, and since it is ≤ V − 1 there are at most V − 1 phases.
- Blocking flow in one phase: each augmenting path has length O(V) and at most E paths are found; with the current-arc array that is O(V · E) per phase.
- Total: O(V) · O(V · E) = O(V² · E).
Push–Relabel
Push–Relabel
- A. Goldberg and R. Tarjan (1988). It does not search for augmenting paths; it works through local operations on vertices.
- It starts by filling every edge out of the source, creating excess flow; the flow is a preflow that temporarily violates conservation.
- Each vertex carries a height label; flow is pushed only downhill, from higher to lower, like water.
- If a vertex cannot push its excess, its height is raised (relabel).
- When no vertex has excess left, the preflow is a true flow; the excess at the sink is the maximum flow.
Push–Relabel
Preflow and Excess Flow
e(V) = (3 + 7) − (2 + 4) = 4
- Excess: e(v) = total flow in − total flow out.
- Preflow: obeys the capacity constraint, but instead of conservation requires e(v) ≥ 0 (except at s).
- A vertex (other than s and t) with e(v) > 0 is active; the algorithm ends when no active vertex is left.
Push–Relabel
The Height Function and Initialization
Initialization:
- Every edge has flow 0.
- h(s) = |V| (the number of vertices); every other vertex has h = 0.
- Every edge out of s is filled: f(s, v) = c(s, v), e(v) = c(s, v).
Invariants:
- h(s) = |V| and h(t) = 0 never change.
- For every edge (u, v) of the residual network, h(u) ≤ h(v) + 1.
- So h(u) is a lower bound on the residual distance from u to t.
Push–Relabel
Push and Relabel
Condition: e(u) > 0, cf(u, v) > 0 (the edge's flow is below its capacity) and h(u) = h(v) + 1.
Push δ = min(e(u), cf(u, v)): e(u) −= δ, e(v) += δ. If the edge becomes saturated it is a saturating push, otherwise a nonsaturating one.
Condition: e(u) > 0 but h(u) ≤ h(v) for every residual edge (u, v); that is, it cannot push to any neighbor.
h(u) = 1 + min h(v) over residual edges (u, v). This makes at least one push possible.
Push–Relabel
Push–Relabel Example
| Vertex | h | e |
|---|---|---|
| s | 0 | − |
| A | 0 | 0 |
| B | 0 | 0 |
| C | 0 | 0 |
| D | 0 | 0 |
| t | 0 | 0 |
Take this network (flow/capacity). There are 6 vertices: |V| = 6.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 0 | 13 |
| B | 0 | 10 |
| C | 0 | 0 |
| D | 0 | 0 |
| t | 0 | 0 |
Start: h(s) = 6; s → A and s → B are filled. e(A) = 13, e(B) = 10.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 0 | 13 |
| B | 1 | 10 |
| C | 0 | 0 |
| D | 0 | 0 |
| t | 0 | 0 |
Take B. It is at the same height as A (0), so it cannot send its excess to A → relabel: h(B) = 1 + min(h(A) = 0, h(s) = 6) = 1.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 0 | 16 |
| B | 1 | 7 |
| C | 0 | 0 |
| D | 0 | 0 |
| t | 0 | 0 |
B can now push its excess to A: δ = min(10, 3) = 3. e(A) = 16, e(B) = 7.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 1 | 16 |
| B | 1 | 7 |
| C | 0 | 0 |
| D | 0 | 0 |
| t | 0 | 0 |
Take A. It has no lower neighbor to push to → relabel: h(A) = 1 + min(h(C) = 0, h(t) = 0, h(B) = 1, h(s) = 6) = 1.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 1 | 3 |
| B | 1 | 7 |
| C | 0 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
A now pushes 6 units to C and 7 to the sink (both edges saturate). e(A) = 3, e(C) = 6, 7 at the sink.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 1 | 3 |
| B | 1 | 7 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
C is relabeled: h(C) = 1 + h(D) = 1. (C's 6 units of excess wait for now; active vertices may be chosen in any order.)
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 2 | 3 |
| B | 1 | 7 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
A is relabeled: only B (h = 1) and s (h = 6) remain in the residual network → h(A) = 2.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 2 | 0 |
| B | 1 | 10 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
A is higher than B: it pushes 3 units to B (cancelling flow on B → A). e(A) = 0, e(B) = 10.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 2 | 3 |
| B | 3 | 7 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
B has no other edge to use → relabel: h(B) = 3; it pushes 3 units back to A. e(A) = 3, e(B) = 7.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 6 | 3 |
| B | 7 | 0 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
Relabels and pushes alternate between A and B (h(A) = 4, h(B) = 5, h(A) = 6, h(B) = 7). Once B rises above the source, it pushes 3 units to A and 7 units to the source.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 0 |
| B | 7 | 0 |
| C | 1 | 6 |
| D | 0 | 0 |
| t | 0 | 7 |
A is relabeled: h(A) = 1 + min(h(s) = 6, h(B) = 7) = 7; it pushes 3 units to the source. A and B have no excess left.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 0 |
| B | 7 | 0 |
| C | 1 | 0 |
| D | 0 | 6 |
| t | 0 | 7 |
Take C: its 6 units of excess are pushed to D (h(C) = 1 > h(D) = 0).
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 0 |
| B | 7 | 0 |
| C | 1 | 0 |
| D | 1 | 1 |
| t | 0 | 12 |
D is relabeled (h = 1) and pushes 5 units to the sink. D still has 1 unit of excess.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 0 |
| B | 7 | 0 |
| C | 1 | 1 |
| D | 2 | 0 |
| t | 0 | 12 |
D is relabeled: the edge to the sink is full and its only neighbor is C (h = 1) → h(D) = 2. It pushes 1 unit to C.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 1 |
| B | 7 | 0 |
| C | 8 | 0 |
| D | 8 | 0 |
| t | 0 | 12 |
Relabels and pushes alternate between C and D until C rises above A (7) (C: 3, 5, 7, 8; D: 4, 6, 8). C pushes its 1 unit of excess to A.
| Vertex | h | e |
|---|---|---|
| s | 6 | − |
| A | 7 | 0 |
| B | 7 | 0 |
| C | 8 | 0 |
| D | 8 | 0 |
| t | 0 | 12 |
A pushes its 1 unit of excess to the source. No active vertex is left: maximum flow = flow into the sink = 7 + 5 = 12 = 9 + 3 out of the source.
Push–Relabel
Push–Relabel: Pseudocode
PUSH_RELABEL(G, source, sink): height[source] = |V| height[v] = 0, v ≠ source excess[v] = 0, for every v flow[e] = 0, for every edge e // preflow: fill the source edges for each (source, v) ∈ E: c = capacity[source, v] flow[source, v] = c flow[v, source] = −c excess[v] = c excess[source] −= c
while some u has excess[u] > 0: // u ≠ source, u ≠ sink if residual(u,v) > 0 and height[u] == height[v]+1: // Push delta = min(excess[u], residual(u,v)) flow[u,v] += delta flow[v,u] −= delta excess[u] −= delta excess[v] += delta else: // Relabel m = ∞ for each (u, v) in G[u]: if flow[u,v] < capacity[u,v]: m = min(m, height[v]) height[u] = m + 1 return excess[sink] // residual = capacity − flow
Push–Relabel
Java: Push and Relabel
boolean push(Node n) { for (Edge e : n.edges) { if (n.height > e.to.height && e.flow != e.cap) { int flow = Math.min( e.cap - e.flow, n.excess); n.excess -= flow; e.to.excess += flow; e.flow += flow; updateReverse(e, flow); return true; // pushed } } return false; }
void relabel(Node n) { int minH = Integer.MAX_VALUE; for (Edge e : n.edges) { // residual edges only if (e.flow != e.cap && e.to.height < minH) { minH = e.to.height; n.height = minH + 1; } } }
Main loop: while an active vertex exists, if (!push(n)) relabel(n);. The answer is the sink's excess. Reverse edges hold negative flow (capacity 0).
Push–Relabel
Push–Relabel: Complexity
| Operation | Total count | Reason |
|---|---|---|
| Relabel | O(V²) | each height is ≤ 2|V| − 1 and only increases |
| Saturating push | O(V · E) | to saturate an edge again, the heights at both ends must rise by ≥ 2 |
| Nonsaturating push | O(V² · E) | via the potential function Σ h(v) (active v) |
- The generic algorithm is O(V² · E) in the worst case; a relabel scans at most degree-many edges (O(V · E) in total).
- O(V³) with FIFO selection, O(V² · √E) with highest-label selection.
- With the gap and global relabeling heuristics it is among the fastest general-purpose maximum-flow methods in practice.
Comparison and Applications
Comparing the Algorithms
| Algorithm | Basic operation | Time | On the example network (|f*| = 19) |
|---|---|---|---|
| Ford-Fulkerson | any augmenting path (DFS) | O(E · |f*|) | 5 augmenting paths (order of Example 2) |
| Edmonds-Karp | shortest augmenting path (BFS) | O(V · E²) | 4 augmenting paths, 5 BFS runs |
| Dinic | level graph + blocking flow | O(V² · E) | 2 phases, 3 BFS runs |
| Push–relabel | local push / relabel | O(V² · E), FIFO O(V³) | no path search |
Comparison and Applications
Applications
- Bipartite matching: worker–job and student–project assignment (unit capacities).
- Edge-disjoint paths (Menger): maximum flow with unit capacities.
- Image segmentation: separating foreground from background with a minimum cut.
- Network reliability: the smallest total capacity that must be cut to split the network.
- Transportation and logistics: pipelines, traffic, airline scheduling, project selection.
Maximum flow 3 = maximum matching: a₁–b₂, a₂–b₁, a₃–b₃.
Summary
Summary
| Topic | Key points |
|---|---|
| Flow | 0 ≤ f ≤ c; flow in = flow out at internal vertices; |f| = net flow out of s |
| Residual network | forward c − f, reverse f; an augmenting path is an s → t path in Gf, bottleneck = smallest cf |
| Cut | c(S, T) = capacities from S to T; maximum flow = min cut; S = what s reaches in the final Gf |
| Ford-Fulkerson | any augmenting path; O(E · |f*|), terminates with integer capacities |
| Edmonds-Karp | shortest paths via BFS; O(V · E²) |
| Dinic | BFS level graph + DFS blocking flow; ≤ V − 1 phases; O(V² · E) |
| Push–relabel | h(s) = |V|, preflow; flow is pushed downhill, and a vertex that cannot push is relabeled; O(V² · E) |
Summary
Common Mistakes
Summary
Worked Problems
- What upper bounds on |f*| do the source side and the sink side give?
- Which augmenting paths does Edmonds-Karp find, in order? (BFS tries neighbors alphabetically.)
- What is the maximum flow, and a minimum cut?
- What are cf(s, a) and cf(c, d) at the end?
- What are the h and e values at the start of push–relabel?
Show answers
- 5 + 4 = 9 leaves s, 4 + 3 = 7 enters t → |f*| ≤ 7.
- s→a→c→t (3), s→b→d→t (3), s→b→d→c→t (1). The first two have 3 edges, the last one 4.
- |f*| = 7; S = {s, a, b, c, d}: c→t 4 + d→t 3 = 7 (in the final residual network every vertex except t is reachable).
- cf(s, a) = 5 − 3 = 2; cf(c, d) = f(d, c) = 1 (a reverse edge).
- h(s) = 6, the others 0; e(a) = 5, e(b) = 4, the others 0 (s → a and s → b are filled).
Summary
Exercises
- If one edge's capacity grows by 1, by how much can the maximum flow grow at most?
- If every capacity is multiplied by k, how do the maximum flow and the minimum cut change?
- How do you model the problem when vertices have capacities c(v)?
- Write a Java method that finds a minimum cut from the final residual network.
- On the "bad case" network, at most how many augmentations do DFS-based Ford-Fulkerson and Edmonds-Karp make?
- How is the number of edge-disjoint s–t paths found with maximum flow?
Answers / hints
- At most 1 (each cut's capacity grows by at most 1); if the edge is in no min cut, it may not grow at all.
- The flow and every cut capacity are multiplied by k; the same cut stays minimum.
- Split each v into an edge vin → vout with capacity c(v); incoming edges attach to vin, outgoing ones to vout.
- Run BFS from s in the final residual network; the original edges (u, v) with u visited and v not visited form the cut.
- DFS-based Ford-Fulkerson takes 2000 augmentations in the worst case, Edmonds-Karp 2.
- Give every edge capacity 1; maximum flow = the number of edge-disjoint paths (Menger).
Summary
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022 (Chapter 24: Maximum Flow).
- Kleinberg and Tardos, Algorithm Design, Pearson, 2006 (Chapter 7: Network Flow).
- Sedgewick and Wayne, Algorithms, 4th ed., 2011 (Section 6.4: Maximum Flow).
- L. R. Ford and D. R. Fulkerson, "Maximal Flow Through a Network", Canadian Journal of Mathematics 8, 1956.
- J. Edmonds and R. M. Karp, "Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems", Journal of the ACM 19(2), 1972.
- E. A. Dinic, "Algorithm for Solution of a Problem of Maximum Flow in Networks with Power Estimation", Soviet Math. Doklady 11, 1970.
- A. V. Goldberg and R. E. Tarjan, "A New Approach to the Maximum-Flow Problem", Journal of the ACM 35(4), 1988.