TExcelTAD
Home › Graph Algorithms › Push-Relabel: An Overview

Push-Relabel: An Overview

Augmenting-path algorithms keep a valid flow at all times and improve it one whole path at a time. Push-relabel algorithms (Goldberg and Tarjan, 1988) work the other way round. They flood the network from the source at once, let vertices hold more flow than they pass on, and push the surplus downhill, vertex by vertex, toward the sink, or back to the source if the sink cannot take it. Only at the end is the result a flow. The method is local (each step looks at one vertex and its edges), which makes it fast in practice and easy to parallelize; the fastest practical max-flow codes are push-relabel variants. This lesson gives the ideas, the bounds and a working implementation, without the full proofs.

Preflows, excess and heights

A preflow satisfies the capacity constraint but relaxes conservation: every vertex other than ss may receive more than it sends. The difference is its excess,

e(u)=∑v∈Vf(v,u)−∑v∈Vf(u,v)≥0(u≠s)e(u) = \sum_{v \in V} f(v, u) - \sum_{v \in V} f(u, v) \ge 0 \qquad (u \ne s)

and a vertex u∉{s,t}u \notin \{s, t\} with e(u)>0e(u) > 0 is active. Each vertex also has an integer height h(u)h(u). Flow is only ever pushed downhill, by one level, along residual edges. The algorithm maintains the height invariant:

h(s)=V,h(t)=0,h(u)≤h(v)+1 for every residual edge (u,v)h(s) = V, \qquad h(t) = 0, \qquad h(u) \le h(v) + 1 \text{ for every residual edge } (u, v)

The invariant has a striking consequence: there is never a residual path from ss to tt. Such a path could have at most V−1V - 1 edges, and each edge lowers the height by at most 1, so it could descend at most V−1V - 1 from h(s)=Vh(s) = V, never reaching h(t)=0h(t) = 0. So when no vertex is active any more, the preflow is a flow with no augmenting path, a maximum flow by the max-flow min-cut theorem.

The two operations

text
INITIALIZE-PREFLOW(G, s)
1  h(u) = 0 and e(u) = 0 for every u;  f = 0;  h(s) = |V|
2  for each edge (s, v):  f(s, v) = c(s, v);  e(v) = c(s, v)     // saturate every edge out of s

PUSH(u, v)            // applies when u is active, c_f(u, v) > 0 and h(u) == h(v) + 1
1  delta = min(e(u), c_f(u, v))
2  send delta units along (u, v)    // forward: increase f(u, v); backward: decrease f(v, u)
3  e(u) = e(u) - delta;  e(v) = e(v) + delta

RELABEL(u)            // applies when u is active and h(u) <= h(v) for every residual edge (u, v)
1  h(u) = 1 + min { h(v) : (u, v) in E_f }

GENERIC-PUSH-RELABEL(G, s, t)
1  INITIALIZE-PREFLOW(G, s)
2  while some PUSH or RELABEL applies
3      perform one of them

An active vertex always has some residual edge (at least the backward edge along which its excess arrived), so one of the two operations always applies to it. RELABEL lifts uu just enough that it can push. Both operations preserve the height invariant.

The bounds

The analysis bounds the number of operations:

Quantity Bound Reason, in one line
height of any vertex 2V−12V - 1 an active vertex always has a residual path back to ss, of at most V−1V - 1 edges
relabels less than 2V22V^2 each of VV vertices rises at most 2V−12V - 1 times
saturating pushes less than 2VE2VE between two saturating pushes on one edge, its tail must rise by 2
non-saturating pushes less than 4V2(V+E)4V^2 (V + E) a potential-function argument on the heights of active vertices

With O(1)O(1) work per operation the generic algorithm runs in O(V2E)O(V^2 E). Choosing the operations in a good order improves it: processing vertices in FIFO order, or the relabel-to-front algorithm, gives O(V3)O(V^3); always choosing the active vertex of greatest height gives O(V2E)O(V^2 \sqrt{E}). Practical codes add two heuristics: a global relabel (recompute exact heights by BFS from tt now and then) and the gap heuristic (if no vertex has height kk, every vertex above kk can only send flow back to ss, so lift them all at once).

A FIFO implementation

The program implements the FIFO variant: active vertices wait in a queue, and the front vertex is discharged, pushing along admissible edges and relabelling when stuck, until its excess is 0. It prints every operation on the network of Residual Networks, whose maximum flow is 5.

python · try it yourself
from collections import deque

def push_relabel(V, cap, s, t, verbose=True):
    flow = {e: 0 for e in cap}
    nbrs = {u: sorted({b for (a, b) in cap if a == u} | {a for (a, b) in cap if b == u}) for u in V}
    def rc(u, v):
        return cap[(u, v)] - flow[(u, v)] if (u, v) in cap else flow.get((v, u), 0)
    def send(u, v, d):
        if (u, v) in cap: flow[(u, v)] += d
        else: flow[(v, u)] -= d
    h = {u: 0 for u in V}; h[s] = len(V)
    e = {u: 0 for u in V}
    active = deque()
    for v in nbrs[s]:
        if (s, v) in cap:
            send(s, v, cap[(s, v)]); e[v] += cap[(s, v)]; e[s] -= cap[(s, v)]
            if v != t: active.append(v)
    pushes = relabels = 0
    while active:
        u = active.popleft()
        while e[u] > 0:                                    # discharge u
            pushed = False
            for v in nbrs[u]:
                if rc(u, v) > 0 and h[u] == h[v] + 1:
                    d = min(e[u], rc(u, v))
                    send(u, v, d); e[u] -= d; e[v] += d; pushes += 1
                    if verbose: print(f"push {d} {u}->{v} (heights {h[u]} -> {h[v]})")
                    if v not in (s, t) and v not in active: active.append(v)
                    pushed = True
                    if e[u] == 0: break
            if e[u] > 0 and not pushed:
                h[u] = 1 + min(h[v] for v in nbrs[u] if rc(u, v) > 0)
                relabels += 1
                if verbose: print(f"relabel {u} to height {h[u]}")
    return e[t], pushes, relabels, flow

cap = {("S", "A"): 4, ("S", "C"): 3, ("A", "B"): 3, ("A", "D"): 6, ("C", "B"): 4, ("B", "T"): 2, ("D", "T"): 3}
value, pushes, relabels, flow = push_relabel("SABCDT", cap, "S", "T")
print(f"maximum flow {value} with {pushes} pushes and {relabels} relabels")
print("flow:", {f"{u}{v}": x for (u, v), x in flow.items()})
relabel A to height 1
push 3 A->B (heights 1 -> 0)
push 1 A->D (heights 1 -> 0)
relabel C to height 1
push 3 C->B (heights 1 -> 0)
relabel B to height 1
push 2 B->T (heights 1 -> 0)
relabel B to height 2
push 3 B->A (heights 2 -> 1)
push 1 B->C (heights 2 -> 1)
relabel D to height 1
push 1 D->T (heights 1 -> 0)
relabel A to height 2
push 3 A->D (heights 2 -> 1)
relabel C to height 3
push 1 C->B (heights 3 -> 2)
push 2 D->T (heights 1 -> 0)
relabel D to height 3
push 1 D->A (heights 3 -> 2)
relabel B to height 4
push 1 B->C (heights 4 -> 3)
relabel A to height 4
push 1 A->D (heights 4 -> 3)
relabel C to height 5
push 1 C->B (heights 5 -> 4)
relabel D to height 5
push 1 D->A (heights 5 -> 4)
relabel B to height 6
push 1 B->C (heights 6 -> 5)
relabel A to height 6
push 1 A->D (heights 6 -> 5)
relabel C to height 7
push 1 C->B (heights 7 -> 6)
relabel D to height 7
push 1 D->A (heights 7 -> 6)
relabel B to height 8
push 1 B->C (heights 8 -> 7)
relabel A to height 7
push 1 A->S (heights 7 -> 6)
push 1 C->S (heights 7 -> 6)
maximum flow 5 with 22 pushes and 18 relabels
flow: {'SA': 3, 'SC': 2, 'AB': 0, 'AD': 3, 'CB': 2, 'BT': 2, 'DT': 3}

Source SS floods 7 units into AA and CC, more than the sink can ever take (the minimum cut is 5). Vertices rise step by step; the 2 units that cannot reach TT are eventually pushed back up to SS once their vertices climb above height V=6V = 6. The long middle stretch, where single units bounce around the cycle B,C,D,AB, C, D, A while the heights climb one level at a time, is exactly the waste that the global-relabel and gap heuristics remove. The result is a maximum flow, though not the same one Edmonds-Karp finds (A→BA \to B ends empty here).

Checking against Edmonds-Karp

The program compares the FIFO push-relabel with Edmonds-Karp on random networks and reports the total work of each: pushes plus relabels for one, BFS edge looks for the other.

python · try it yourself
import random
from collections import deque

def push_relabel(n, cap, s, t):
    flow = dict.fromkeys(cap, 0)
    nb = [set() for _ in range(n)]
    for a, b in cap:
        nb[a].add(b); nb[b].add(a)
    nb = [sorted(x) for x in nb]
    rc = lambda u, v: cap[(u, v)] - flow[(u, v)] if (u, v) in cap else flow.get((v, u), 0)
    def send(u, v, d):
        if (u, v) in cap: flow[(u, v)] += d
        else: flow[(v, u)] -= d
    h, e = [0] * n, [0] * n
    h[s] = n
    q = deque()
    for v in nb[s]:
        if (s, v) in cap:
            send(s, v, cap[(s, v)]); e[v] += cap[(s, v)]
            if v != t: q.append(v)
    ops = 0
    while q:
        u = q.popleft()
        while e[u] > 0:
            moved = False
            for v in nb[u]:
                if e[u] and rc(u, v) > 0 and h[u] == h[v] + 1:
                    d = min(e[u], rc(u, v)); send(u, v, d); e[u] -= d; e[v] += d; ops += 1; moved = True
                    if v not in (s, t) and v not in q: q.append(v)
            if e[u] and not moved:
                h[u] = 1 + min(h[v] for v in nb[u] if rc(u, v) > 0); ops += 1
    return e[t], ops

def edmonds_karp(n, cap, s, t):
    flow = dict.fromkeys(cap, 0)
    out = [[] for _ in range(n)]
    for a, b in cap:
        out[a].append((b, (a, b), 1)); out[b].append((a, (a, b), -1))
    value = looks = 0
    while True:
        prev, q = {s: None}, deque([s])
        while q and t not in prev:
            u = q.popleft()
            for v, e, sign in out[u]:
                looks += 1
                r = cap[e] - flow[e] if sign == 1 else flow[e]
                if r > 0 and v not in prev:
                    prev[v] = (u, e, sign, r); q.append(v)
        if t not in prev:
            return value, looks
        steps, v = [], t
        while prev[v]:
            steps.append(prev[v]); v = prev[v][0]
        b = min(r for *_, r in steps)
        for u, e, sign, r in steps:
            flow[e] += sign * b
        value += b

rng = random.Random(55)
total_pr = total_ek = 0
for trial in range(150):
    n = rng.randint(4, 20)
    cap = {}
    for u in range(n):
        for v in range(n):
            if u != v and (v, u) not in cap and rng.random() < 0.25:
                cap[(u, v)] = rng.randint(1, 50)
    a, ops = push_relabel(n, cap, 0, n - 1)
    b, looks = edmonds_karp(n, cap, 0, n - 1)
    assert a == b
    total_pr += ops; total_ek += looks
print("push-relabel and Edmonds-Karp agree on 150 random networks")
print(f"push-relabel: {total_pr:,} pushes and relabels; Edmonds-Karp: {total_ek:,} residual edge looks")
push-relabel and Edmonds-Karp agree on 150 random networks
push-relabel: 15,563 pushes and relabels; Edmonds-Karp: 29,578 residual edge looks

The two counts measure different elementary steps, so the comparison is rough, but it shows the character of the method: many cheap local operations instead of repeated global searches. On large real networks, push-relabel with the global-relabel and gap heuristics is typically much faster than any augmenting-path algorithm.

In C++

The FIFO variant in C++, with capacities in a residual matrix, on the same network. It scans neighbours in index order (SS first) rather than alphabetically, so its operation counts differ slightly; the value is the same.

cpp · try it yourself
#include <algorithm>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>

int main() {
    enum { S, A, B, C, D, T, N };
    std::vector<std::vector<int>> r(N, std::vector<int>(N, 0));      // residual capacities
    r[S][A] = 4; r[S][C] = 3; r[A][B] = 3; r[A][D] = 6; r[C][B] = 4; r[B][T] = 2; r[D][T] = 3;
    std::vector<int> h(N, 0), e(N, 0);
    h[S] = N;
    std::queue<int> q;
    std::vector<bool> queued(N, false);
    for (int v = 0; v < N; ++v)
        if (r[S][v] > 0) {
            e[v] += r[S][v]; r[v][S] += r[S][v]; r[S][v] = 0;
            if (v != T) { q.push(v); queued[v] = true; }
        }
    int pushes = 0, relabels = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop(); queued[u] = false;
        while (e[u] > 0) {
            bool moved = false;
            for (int v = 0; v < N && e[u] > 0; ++v)
                if (r[u][v] > 0 && h[u] == h[v] + 1) {
                    int d = std::min(e[u], r[u][v]);
                    r[u][v] -= d; r[v][u] += d; e[u] -= d; e[v] += d; ++pushes; moved = true;
                    if (v != S && v != T && !queued[v]) { q.push(v); queued[v] = true; }
                }
            if (e[u] > 0 && !moved) {
                int m = INT_MAX;
                for (int v = 0; v < N; ++v) if (r[u][v] > 0) m = std::min(m, h[v]);
                h[u] = m + 1; ++relabels;
            }
        }
    }
    std::printf("maximum flow %d, %d pushes, %d relabels\n", e[T], pushes, relabels);
}
maximum flow 5, 20 pushes, 17 relabels

Test yourself

How does a preflow differ from a flow?

  1. It may exceed capacities
  2. Vertices other than the source may receive more flow than they send (non-negative excess)
  3. It has no source
  4. It is always integral

When may PUSH(u,v)(u, v) be applied?

  1. Whenever (u,v)(u, v) is an edge
  2. When uu is active, (u,v)(u, v) has residual capacity, and h(u)=h(v)+1h(u) = h(v) + 1
  3. When vv is active
  4. When h(u)<h(v)h(u) < h(v)

Why does the height invariant rule out a residual path from ss to tt?

  1. Because ss has no residual edges
  2. A residual path has at most V−1V - 1 edges, each dropping the height by at most 1, so it cannot get from h(s)=Vh(s) = V down to h(t)=0h(t) = 0
  3. Because heights only increase
  4. Because tt is never relabelled

What is the running time of the generic push-relabel algorithm?

  1. O(VE2)O(VE^2)
  2. O(V2E)O(V^2 E)
  3. O(V3)O(V^3)
  4. O(Elg⁡V)O(E \lg V)