Chapter 8: Network Flow

Algorithms
Flow networksResidual networks and augmenting pathsCutsMax-flow min-cut Ford-FulkersonEdmonds-KarpDinicPush–relabel
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Modeling

Model a problem as a flow network with a source, a sink and capacitated edges; write down the capacity and conservation constraints.

Residual networks and cuts

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.

Algorithms

Run Ford-Fulkerson, Edmonds-Karp, Dinic and push–relabel step by step on an example network.

Analysis

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.
4343266SABCDT

Edge labels are capacities. The maximum flow in this network is 7.

Water in a pipeline, traffic on roads and packets in a computer network are all flows. Each pipe (edge) carries at most its capacity; nothing accumulates at the junctions (vertices).

Flow Networks

Flows and Their Constraints

A flow is a function that assigns a number f(u, v) to every edge:

  1. Capacity constraint: 0 ≤ f(u, v) ≤ c(u, v).
  2. 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).

4/43/34/42/32/25/65/6SABCDT

Label: flow/capacity. 3 + 2 = 5 enters C and 5 leaves. |f| = 4 + 3 = 7.

An edge with f = c is saturated: no more flow can go through it.

Flow Networks

Several Sources and Sinks

S∞s₁∞s₂∞sₙt₁∞t₂∞tₘ∞T⋮⋮network
  • 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.
Flow network: f/c = 3/5
3/5uv
Residual network: 5 − 3 = 2 forward, 3 back
23uv

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.
4343266SABCDT

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).
4343266cutSTSABCDT

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):

  1. f is a maximum flow.
  2. The residual network Gf has no augmenting path.
  3. |f| = c(S, T) for some cut (S, T).
Consequence: maximum flow value = minimum cut capacity. A min cut is found by taking S to be the vertices reachable from s in the final residual network.
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.

AlgorithmApproachTimeNote
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 flowO(V² · E)faster than Edmonds-Karp; very efficient with unit capacities
Push–relabel (1988)preflow + heights, local operationsO(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.
1. build the residual network2. find an augmenting path3. send the bottleneckrepeatno path:stop

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

Residual network Gf
4343266SABCDT
Flow f (f/c)
0/40/30/40/30/20/60/6SABCDT|f| = 0

Start: all flows are 0, and the residual network is identical to the original.

Residual network: path p
4343266SABCDT
Flow: +2
2/40/32/40/32/20/60/6SABCDT|f| = 2

First augmenting path p = {S, A, B, T}: bottleneck 2 units. |f| = 2.

Residual network: path p
223223266SABCDT
Flow: +3
2/43/32/40/32/23/63/6SABCDT|f| = 5

Second augmenting path p = {S, C, D, T}: bottleneck 3 units. |f| = 5.

Residual network: path p
22322323333SABCDT
Flow: +2
4/43/34/42/32/25/65/6SABCDT|f| = 7

Third augmenting path p = {S, A, B, C, D, T}: bottleneck 2 units. |f| = 7.

Final residual network
4341221515SABCDT
Maximum flow
4/43/34/42/32/25/65/6SABCDT|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

Residual network Gf
1010428961010S1234T
Flow f (f/c)
0/100/100/40/20/80/90/60/100/10S1234T|f| = 0

Source S, sink T. All flows start at 0.

Residual network: path p (+8)
1010428961010S1234T
Flow f
8/100/100/40/28/80/90/60/108/10S1234T|f| = 8

p = S → 1 → 4 → T: bottleneck 1 → 4 (8). |f| = 8.

Residual network: path p (+2)
2810428961028S1234T
Flow f
8/102/100/40/28/82/90/60/1010/10S1234T|f| = 10

p = S → 3 → 4 → T: only 2 is left on 4 → T. |f| = 10.

Residual network: path p (+4)
28824287261010S1234T
Flow f
8/106/104/40/24/86/90/64/1010/10S1234T|f| = 14

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.

Residual network: path p (+2)
284642443666410S1234T
Flow f
10/106/104/40/26/86/92/66/1010/10S1234T|f| = 16

p = S → 1 → 4 → 2 → T: 2 units were left on S → 1. |f| = 16.

Residual network: path p (+3)
1046422636424610S1234T
Flow f
10/109/104/40/26/89/95/69/1010/10S1234T|f| = 19

p = S → 3 → 4 → 2 → T: bottleneck 3 → 4 (9 − 6 = 3 left). |f| = 19.

Final residual network
101942269151910S1234T
Maximum flow
10/109/104/40/26/89/95/69/1010/10S1234T|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?

Forward edges only (greedy)
  1. S → 1 → 4 → T: 8
  2. S → 3 → 4 → T: 2
  3. S → 1 → 2 → T: 2
  4. S → 3 → 4 → 2 → T: 6

Total 18. From S only 3 and 4 are reachable; 4's outgoing edges are full. Stuck!

Residual network (with reverse edges)

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.

A reverse edge does not really "send flow backward"; it cancels flow sent earlier: f(u, v) −= δ.

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 true once the sink is reached; parent[] gives the path backward.
  • Adding flow does res[u][v] −= d and res[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

4343113333SABCDT

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.

4343113333SABCDT

S → C has residual capacity 3 > 0 and C is unvisited: parent[C] = S, dfs(C).

4343113333SABCDT

C → B, a reverse edge (residual 3), B unvisited: parent[B] = C, dfs(B).

4343113333SABCDT

B → A, a reverse edge (residual 4), A unvisited: parent[A] = B, dfs(A).

4343113333SABCDT

A has no usable neighbor: a dead end. Return false, back up.

4343113333SABCDT

B → T has residual capacity 1 > 0 and T is unvisited: parent[T] = B, dfs(T).

4343113333SABCDT

u = T = sink: return true.

42141223333SABCDT

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.
IntegralityIf all capacities are integers, the flow stays integral and an integer-valued maximum flow is found. Matching problems use this property.
CautionO(E · |f*|) is not polynomial: |f*| depends on the magnitude of the numbers. With irrational capacities the method may not even terminate.

Ford-Fulkerson

A Bad Choice of Paths: 2000 Augmentations

10001000110001000sabt
  • 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.
sabctdBFS: s → c → t (2 edges)
Steps: (1) Find a shortest augmenting path from source to sink with BFS. (2) Send the bottleneck along it and update the reverse edges. (3) Repeat until BFS cannot reach the sink.

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.
The time no longer depends on |f*|: however large the capacities, the algorithm finishes in polynomial time.

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] = u stores 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)

1613104129147204s1234tqueuesred: BFS tree (parent)

Initial residual network (flows 0). s is marked and enqueued, parent[s] = −1; · = unassigned.

1613104129147204s1234tqueue12red: BFS tree (parent)

s dequeued: neighbors 1 (residual 16) and 2 (residual 13) enqueued, parent s.

1613104129147204s1234tqueue23red: BFS tree (parent)

1 is dequeued. Its unvisited neighbor 3 (residual 12) is enqueued with parent 1. 2 is already visited and is skipped.

1613104129147204s1234tqueue34red: BFS tree (parent)

2 is dequeued. Its unvisited neighbor 4 (residual 14) is enqueued with parent 2. 1 is already visited and is skipped.

1613104129147204s1234tqueue4tred: BFS tree (parent)

3 dequeued: t (residual 20) enqueued, parent 3; 2 skipped. t is reached, but the loop runs until the queue is empty.

1613104129147204s1234tqueuetred: BFS tree (parent)

4 is dequeued. 3 and t are already visited and are skipped.

1613104129147204s1234tqueuered: BFS tree (parent)

t is dequeued. It has no outgoing edge with positive residual capacity.

1613104129147204s1234tqueuered: BFS tree (parent)

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

1613104129147204s1324t

Start: residual network = capacities, maxFlow = 0.

1613104129147204s1324t

bfs found a shortest path: s → 1 → 3 → t (3 edges).

1613104129147204s1324t

Walking back from the target: flow = min(16, 12, 20) = 12.

412131041291478124s1324t

Subtract 12 from the forward edges on the path and add 12 to the reverse edges (dashed arrows).

412131041291478124s1324t

maxFlow = 0 + 12 = 12.

412131041291478124s1324t

bfs found a shortest path: s → 2 → 4 → t (3 edges).

412131041291478124s1324t

Walking back from the target: flow = min(13, 14, 4) = 4.

4129410412910478124s1324t

Subtract 4 from the forward edges on the path and add 4 to the reverse edges (dashed arrows).

4129410412910478124s1324t

maxFlow = 12 + 4 = 16.

4129410412910478124s1324t

bfs found a shortest path: s → 2 → 4 → 3 → t (4 edges).

4129410412910478124s1324t

Walking back from the target: flow = min(9, 10, 7, 8) = 7.

41221110412931171194s1324t

Subtract 7 from the forward edges on the path and add 7 to the reverse edges (dashed arrows).

41221110412931171194s1324t

maxFlow = 16 + 7 = 23.

41221110412931171194s1324t

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

Residual network
1613104129147204s1324t
Flow f
0/160/130/100/40/120/90/140/70/200/4s1324t|f| = 0

Take this network. Upper bound: |f*| ≤ 24 (capacity out of s is 16 + 13 = 29, into t 20 + 4 = 24; the smaller one).

Residual network: p1
1613104129147204s1324t
Flow f
12/160/130/100/412/120/90/140/712/200/4s1324t|f| = 12

Step 1: the augmenting path p1 = ⟨s, 1, 3, t⟩ is chosen, with cf(p1) = 12 because of c(1, 3). |f| = 12.

Residual network: p2
412131041291478124s1324t
Flow f
12/164/130/100/412/120/94/140/712/204/4s1324t|f| = 16

Step 2: the augmenting path p2 = ⟨s, 2, 4, t⟩ is chosen, with cf(p2) = 4 because of c(4, t). |f| = 16.

Residual network: p3
4129410412910478124s1324t
Flow f
12/1611/130/100/412/120/911/147/719/204/4s1324t|f| = 23

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 residual network
41221110412931171194s1324t
Final flow and min cut
12/1611/130/100/412/120/911/147/719/204/4s1324t|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

Residual network
33431226119ABCDEFG
Flow f
0/30/30/40/30/10/20/20/60/10/10/9ABCDEFG|f| = 0

Source A, sink G. All flows start at 0.

Residual network: path (+1)
33431226119ABCDEFG
Flow f
0/31/30/40/30/10/21/20/60/11/10/9ABCDEFG|f| = 1

p = {A, D, E, G}: bottleneck E → G (1). Flow = 1.

Residual network: path (+2)
3214312116119ABCDEFG
Flow f
0/33/30/40/30/10/21/22/60/11/12/9ABCDEFG|f| = 3

p = {A, D, F, G}: 2 was left on A → D. Flow = 1 + 2 = 3.

Residual network: path (+1)
33431211421172ABCDEFG
Flow f
1/33/31/40/31/10/21/23/60/11/13/9ABCDEFG|f| = 4

p = {A, B, C, D, F, G}: A → D is saturated, so BFS finds a 5-edge path; bottleneck C → D (1). Flow = 4.

Residual network: path (+1)
2133131211331163ABCDEFG
Flow f
2/33/32/40/31/11/20/24/60/11/14/9ABCDEFG|f| = 5

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.

Final residual network
1232231112241154ABCDEFG
Maximum flow
2/33/32/40/31/11/20/24/60/11/14/9ABCDEFG|f| = 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

Network (capacities)
424213113s1234t
Residual network
424213113s1234t

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.

Before: path
424213113s1234t
Then: +2
4242131112s1234t|f| = 2

Path s → 4 → t: bottleneck s → 4 (2). Push 2 units; s → 4 has 0 left, and the reverse edge 4 → s has 2.

Before: path
4242131112s1234t
Then: +3
132132131112s1234t|f| = 5

Path s → 1 → 2 → t: bottleneck 2 → t (3). Push 3 units.

Before: path
132132131112s1234t
Then: +1
421311131112s1234t|f| = 6

Path s → 1 → 3 → t: 1 was left on s → 1. Push 1 unit.

Final flow (f/c)
4/42/23/41/20/13/31/10/12/3s1234t|f| = 6
Final residual network
421311131112s1234t

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.
sabcdtlv 0lv 1lv 2lv 3
Edmonds-Karp uses one path after each BFS; Dinic saturates all shortest paths of the same level graph in one phase.

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

0/100/100/40/20/80/90/60/100/10s1324t|f| = 0

The network (flow/capacity). All flows start at 0; residual network = capacities.

1010428961010s1324t012123

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.

10/104/104/40/26/84/90/64/1010/10s1324t|f| = 14

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.

106442265466410s1324t|f| = 14

Residual network after phase 1 (Gf): saturated edges remain only in the reverse direction (dashed).

106442265466410s1324t033124

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.

10/109/104/40/26/89/95/69/1010/10s1324t|f| = 19

Phase 2 – blocking flow: {s, 2, 4, 3, t} 5 units (bottleneck 2 → 4) → |f| = 14 + 5 = 19.

101942269151910s1324t0∞∞1∞∞

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.

10/109/104/40/26/89/95/69/1010/10s1324t|f| = 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 next is reset and the levels are recomputed with BFS.

Dinic · Step through the code

Dinic: The Blocking Flow in Phase 1

0/100/100/40/20/80/90/60/100/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: —

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.

0/100/100/40/20/80/90/60/100/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞) → dfs(1, 10) → dfs(3, 4) → dfs(t, 4)

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.

4/100/104/40/20/80/90/64/100/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞)

As the flow returns along the path, 4 is added to each edge (and subtracted from the reverse edges). Back at s: |f| = 4.

4/100/104/40/20/80/90/64/100/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞) → dfs(1, 6) → dfs(4, 6) → dfs(t, 6)

s → 1 (residual 6), 1 → 4 (residual 8), 4 → t (residual 10). Each call caps the flow at the residual (min); 6 reaches t.

10/100/104/40/26/80/90/64/106/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞)

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.

10/100/104/40/26/80/90/64/106/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞) → dfs(2, 10) → dfs(4, 9) → dfs(t, 4)

s → 2 (residual 10), 2 → 4 (residual 9), 4 → t (residual 4). Each call caps the flow at the residual (min); 4 reaches t.

10/104/104/40/26/84/90/64/1010/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞)

As the flow returns along the path, 4 is added to each edge (and subtracted from the reverse edges). Back at s: |f| = 14.

10/104/104/40/26/84/90/64/1010/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞) → dfs(2, 6) → dfs(4, 5)

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.

10/104/104/40/26/84/90/64/1010/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞) → dfs(2, 6)

No flow came back over 2 → 4: next[2]++. No level path is left from 2: it returns 0.

10/104/104/40/26/84/90/64/1010/10slv 01lv 12lv 13lv 24lv 2tlv 3calls: dfs(s, ∞)

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).
Unit capacitiesIf every capacity is 1, O(E · √E); on unit networks such as bipartite matching, O(E · √V) (Hopcroft-Karp).
In practiceThe most popular maximum-flow algorithm in competitive programming: simple, and it usually runs far below its bound.

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.
h=0h=1h=2h=3h=4suvtfilled at startpushh(v) = h(t) → relabel

Push–Relabel

Preflow and Excess Flow

3724u₁u₂Vw₁w₂

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.
We choose h(s) = |V| because a residual s–t path would have length ≤ |V| − 1, which would force h(s) ≤ |V| − 1. So the residual network never contains an s–t path, and in the end the flow is maximum.

Push–Relabel

Push and Relabel

Push: push(u, v)

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.

Relabel: relabel(u)

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.

Excess that cannot reach the sink is pushed back to the source once its height exceeds |V|. Flow goes downhill; heights only increase.

Push–Relabel

Push–Relabel Example

0/130/100/30/60/70/100/5sABCDt
Vertexhe
s0−
A00
B00
C00
D00
t00

Take this network (flow/capacity). There are 6 vertices: |V| = 6.

13/1310/100/30/60/70/100/5sABCDt
Vertexhe
s6−
A013
B010
C00
D00
t00

Start: h(s) = 6; s → A and s → B are filled. e(A) = 13, e(B) = 10.

13/1310/100/30/60/70/100/5sABCDt
Vertexhe
s6−
A013
B110
C00
D00
t00

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.

13/1310/103/30/60/70/100/5sABCDt
Vertexhe
s6−
A016
B17
C00
D00
t00

B can now push its excess to A: δ = min(10, 3) = 3. e(A) = 16, e(B) = 7.

13/1310/103/30/60/70/100/5sABCDt
Vertexhe
s6−
A116
B17
C00
D00
t00

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.

13/1310/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A13
B17
C06
D00
t07

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.

13/1310/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A13
B17
C16
D00
t07

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

13/1310/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A23
B17
C16
D00
t07

A is relabeled: only B (h = 1) and s (h = 6) remain in the residual network → h(A) = 2.

13/1310/100/36/67/70/100/5sABCDt
Vertexhe
s6−
A20
B110
C16
D00
t07

A is higher than B: it pushes 3 units to B (cancelling flow on B → A). e(A) = 0, e(B) = 10.

13/1310/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A23
B37
C16
D00
t07

B has no other edge to use → relabel: h(B) = 3; it pushes 3 units back to A. e(A) = 3, e(B) = 7.

13/133/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A63
B70
C16
D00
t07

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.

10/133/103/36/67/70/100/5sABCDt
Vertexhe
s6−
A70
B70
C16
D00
t07

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.

10/133/103/36/67/76/100/5sABCDt
Vertexhe
s6−
A70
B70
C10
D06
t07

Take C: its 6 units of excess are pushed to D (h(C) = 1 > h(D) = 0).

10/133/103/36/67/76/105/5sABCDt
Vertexhe
s6−
A70
B70
C10
D11
t012

D is relabeled (h = 1) and pushes 5 units to the sink. D still has 1 unit of excess.

10/133/103/36/67/75/105/5sABCDt
Vertexhe
s6−
A70
B70
C11
D20
t012

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.

10/133/103/35/67/75/105/5sABCDt
Vertexhe
s6−
A71
B70
C80
D80
t012

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.

9/133/103/35/67/75/105/5sABCDt
Vertexhe
s6−
A70
B70
C80
D80
t012

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

OperationTotal countReason
RelabelO(V²)each height is ≤ 2|V| − 1 and only increases
Saturating pushO(V · E)to saturate an edge again, the heights at both ends must rise by ≥ 2
Nonsaturating pushO(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

AlgorithmBasic operationTimeOn the example network (|f*| = 19)
Ford-Fulkersonany augmenting path (DFS)O(E · |f*|)5 augmenting paths (order of Example 2)
Edmonds-Karpshortest augmenting path (BFS)O(V · E²)4 augmenting paths, 5 BFS runs
Diniclevel graph + blocking flowO(V² · E)2 phases, 3 BFS runs
Push–relabellocal push / relabelO(V² · E), FIFO O(V³)no path search
ChoosingSmall integer capacities: Ford-Fulkerson is enough. General purpose: Dinic. Very large, dense networks: push–relabel.
Common groundAll of them find the same |f*|; how the flow is spread over the edges may differ. The min cut is read off the final residual network.

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

Maximum flow 3 = maximum matching: a₁–b₂, a₂–b₁, a₃–b₃.

Summary

Summary

TopicKey points
Flow0 ≤ f ≤ c; flow in = flow out at internal vertices; |f| = net flow out of s
Residual networkforward c − f, reverse f; an augmenting path is an s → t path in Gf, bottleneck = smallest cf
Cutc(S, T) = capacities from S to T; maximum flow = min cut; S = what s reaches in the final Gf
Ford-Fulkersonany augmenting path; O(E · |f*|), terminates with integer capacities
Edmonds-Karpshortest paths via BFS; O(V · E²)
DinicBFS level graph + DFS blocking flow; ≤ V − 1 phases; O(V² · E)
Push–relabelh(s) = |V|, preflow; flow is pushed downhill, and a vertex that cannot push is relabeled; O(V² · E)

Summary

Common Mistakes

Forgetting reverse edgesSearching with forward edges only is a greedy algorithm and can miss the maximum (18 < 19 in the example).
Computing the bottleneck from capacitiesThe bottleneck is the minimum of the residual capacities, not the capacities: c − f (or f on a reverse edge).
Thinking a min cut counts edgesA min cut cuts the smallest total capacity, not the fewest edges.
Counting T → S edgesOnly edges from S to T contribute to the cut capacity; edges going back are not added.
Thinking Ford-Fulkerson is polynomialO(E · |f*|) depends on the capacity values; BFS (Edmonds-Karp) removes that dependence.
Pushing the wrong wayFlow goes downhill, only when h(u) = h(v) + 1; initially h(s) must be |V|.

Summary

Worked Problems

54236243sabcdt
  1. What upper bounds on |f*| do the source side and the sink side give?
  2. Which augmenting paths does Edmonds-Karp find, in order? (BFS tries neighbors alphabetically.)
  3. What is the maximum flow, and a minimum cut?
  4. What are cf(s, a) and cf(c, d) at the end?
  5. What are the h and e values at the start of push–relabel?
Show answers
  1. 5 + 4 = 9 leaves s, 4 + 3 = 7 enters t → |f*| ≤ 7.
  2. 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.
  3. |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).
  4. cf(s, a) = 5 − 3 = 2; cf(c, d) = f(d, c) = 1 (a reverse edge).
  5. h(s) = 6, the others 0; e(a) = 5, e(b) = 4, the others 0 (s → a and s → b are filled).

Summary

Exercises

  1. If one edge's capacity grows by 1, by how much can the maximum flow grow at most?
  2. If every capacity is multiplied by k, how do the maximum flow and the minimum cut change?
  3. How do you model the problem when vertices have capacities c(v)?
  4. Write a Java method that finds a minimum cut from the final residual network.
  5. On the "bad case" network, at most how many augmentations do DFS-based Ford-Fulkerson and Edmonds-Karp make?
  6. How is the number of edge-disjoint s–t paths found with maximum flow?
Answers / hints
  1. 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.
  2. The flow and every cut capacity are multiplied by k; the same cut stays minimum.
  3. Split each v into an edge vin → vout with capacity c(v); incoming edges attach to vin, outgoing ones to vout.
  4. Run BFS from s in the final residual network; the original edges (u, v) with u visited and v not visited form the cut.
  5. DFS-based Ford-Fulkerson takes 2000 augmentations in the worst case, Edmonds-Karp 2.
  6. 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.

The End

Chapter 8: Network Flow
Previous: Chapter 7: Minimum Spanning Trees · Next: Chapter 9: String Matching and Compression
1 / 1 Sercan KÜLCÜ. All rights reserved.