TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Karger's Contraction Algorithm for Minimum Cuts

Karger's Contraction Algorithm for Minimum Cuts

A cut of a connected undirected graph G=(V,E)G = (V, E) is a partition of VV into two nonempty sets SS and V∖SV \setminus S; its size is the number of edges crossing it. A minimum cut (global min cut) has the fewest crossing edges: it measures how robust a network is against link failures. Deterministically one can compute it with n−1n - 1 maximum-flow computations (The Max-Flow Min-Cut Theorem) or with the Stoer-Wagner algorithm. David Karger's contraction algorithm (1993) is much simpler: merge the endpoints of random edges until two vertices are left. It is a Monte Carlo algorithm, and its analysis is a gem of probabilistic reasoning.

Contraction

Contracting an edge {u,v}\{u, v\} merges uu and vv into one vertex: edges from uu or vv to other vertices now leave the merged vertex, edges between uu and vv disappear (they would be loops), and parallel edges are kept, so the graph becomes a multigraph. Every cut of the contracted graph is a cut of the original graph (with uu and vv on the same side), with the same size.

text
CONTRACT(G)                         // G: connected multigraph on n vertices
0  while G has more than 2 vertices
1      choose an edge {u, v} of G uniformly at random
2      contract it (merge u and v, delete the edges between them)
3  return the edges between the two remaining vertices

An efficient way to run it: shuffle the edge list once and process the edges in that order with a union-find structure, joining the endpoints of every edge whose endpoints are still in different sets, until two sets remain. Skipping edges inside a set is exactly "deleting loops", and the first remaining edge in a random order is a uniform random remaining edge, so this is the same random process. It is Kruskal's algorithm with random weights, stopped two steps early.

The success probability

Theorem (Karger, 1993). Let CC be any minimum cut of GG, with ∣C∣=k\lvert C \rvert = k. CONTRACT outputs exactly CC with probability at least

2n(n−1)=(n2)−1.\frac{2}{n(n-1)} = \binom{n}{2}^{-1} .

Proof. The output is CC if and only if no edge of CC is ever contracted. Every vertex has degree at least kk (its edges form a cut), so a multigraph on rr vertices whose minimum cut is still kk has at least rk/2rk/2 edges. Contractions never create smaller cuts, so as long as CC survives, the current graph on rr vertices has minimum cut exactly kk, and a random edge lies in CC with probability at most krk/2=2r\frac{k}{rk/2} = \frac{2}{r}. Multiplying the conditional survival probabilities for r=n,n−1,…,3r = n, n-1, \ldots, 3:

Pr⁡[C survives]≥∏r=3n(1−2r)=∏r=3nr−2r=(n−2)!n!/2=2n(n−1).□\Pr[C \text{ survives}] \ge \prod_{r=3}^{n} \left(1 - \frac{2}{r}\right) = \prod_{r=3}^{n} \frac{r - 2}{r} = \frac{(n-2)!}{n!/2} = \frac{2}{n(n-1)} . \qquad \square

Corollary. A graph has at most (n2)\binom{n}{2} minimum cuts. (The events "the output is CjC_j" are disjoint for distinct minimum cuts, and each has probability at least (n2)−1\binom{n}{2}^{-1}.) The cycle CnC_n shows this is tight: any 2 of its nn edges form a minimum cut.

Repeating

One run succeeds with small probability, so run it TT times independently and keep the smallest cut found. All runs miss a fixed minimum cut with probability at most

(1−(n2)−1)T≤e−T/(n2),\left(1 - \binom{n}{2}^{-1}\right)^T \le e^{-T/\binom{n}{2}},

so T=(n2)ln⁡nT = \binom{n}{2} \ln n runs give failure probability at most 1/n1/n. With an O(n2)O(n^2) implementation per run (an adjacency matrix with merged rows), the total is O(n4log⁡n)O(n^4 \log n), which is slow; the Karger-Stein algorithm reduces it to O(n2log⁡3n)O(n^2 \log^3 n).

The program implements CONTRACT with union-find and measures how often it finds a minimum cut on two graphs: two cliques joined by two edges (one minimum cut, of size 2), and a cycle, where every run finds some minimum cut but each particular one with probability exactly (n2)−1\binom{n}{2}^{-1}.

python · try it yourself
import random
from math import comb

def find(parent, x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]
        x = parent[x]
    return x

def contract(n, edges, rng):
    order = edges[:]
    rng.shuffle(order)
    parent = list(range(n))
    groups = n
    for u, v in order:
        if groups == 2:
            break
        ru, rv = find(parent, u), find(parent, v)
        if ru != rv:
            parent[ru] = rv
            groups -= 1
    side = [find(parent, x) == find(parent, 0) for x in range(n)]
    cut = [(u, v) for u, v in edges if side[u] != side[v]]
    return cut, frozenset(x for x in range(n) if side[x])

def two_cliques(h):
    edges = [(u, v) for u in range(h) for v in range(u + 1, h)]
    edges += [(u + h, v + h) for u, v in edges]
    return 2 * h, edges + [(0, h), (1, h + 1)]

rng = random.Random(40)
n, edges = two_cliques(6)
runs = 2000
sizes = [len(contract(n, edges, rng)[0]) for _ in range(runs)]
print(f"two 6-cliques + 2 bridge edges (n = {n}, m = {len(edges)}): min cut 2")
print(f"  success rate {sizes.count(2) / runs:.3f}  vs  bound 2/(n(n-1)) = {2 / (n * (n - 1)):.4f}")

n = 8
cycle = [(i, (i + 1) % n) for i in range(n)]
runs = 6000
outcomes = [contract(n, cycle, rng) for _ in range(runs)]
target = frozenset(range(1, 4))           # the side {1, 2, 3}: cut edges (0,1) and (3,4)
hits = sum(frozenset(range(n)) - s == target for _, s in outcomes)   # s is the side of vertex 0
print(f"cycle C_{n}: all {runs} runs gave a cut of size 2: {all(len(c) == 2 for c, _ in outcomes)}")
print(f"  one particular min cut found {hits / runs:.4f} of the time, 1/C(n,2) = {1 / comb(n, 2):.4f}")
two 6-cliques + 2 bridge edges (n = 12, m = 32): min cut 2
  success rate 0.306  vs  bound 2/(n(n-1)) = 0.0152
cycle C_8: all 6000 runs gave a cut of size 2: True
  one particular min cut found 0.0357 of the time, 1/C(n,2) = 0.0357

On the two cliques a random edge is far more likely to lie inside a clique than to be one of the two bridges, so the bridges usually survive; the success rate of about 0.3 is twenty times the worst-case bound. (A bridge is most at risk near the end, when few clique edges remain.) The cycle is the worst case, where the bound is exact.

In C++

The same algorithm on the two-clique graph, with a fixed-seed std::mt19937, a Fisher-Yates shuffle written out (so that the result does not depend on the library's std::shuffle), and the repetition count T=⌈(n2)ln⁡n⌉T = \lceil \binom{n}{2} \ln n \rceil from the analysis. The random stream differs from Python's, so the measured rate differs slightly.

cpp · try it yourself
#include <cmath>
#include <cstdio>
#include <numeric>
#include <random>
#include <utility>
#include <vector>

std::mt19937 rng(40);
using Edge = std::pair<int, int>;

int find(std::vector<int>& p, int x) {
    while (p[x] != x) { p[x] = p[p[x]]; x = p[x]; }
    return x;
}

int contract(int n, std::vector<Edge> e) {               // returns the size of the cut found
    for (int i = (int)e.size() - 1; i > 0; --i) std::swap(e[i], e[rng() % (i + 1)]);
    std::vector<int> p(n);
    std::iota(p.begin(), p.end(), 0);
    int groups = n;
    for (auto [u, v] : e) {
        if (groups == 2) break;
        int a = find(p, u), b = find(p, v);
        if (a != b) { p[a] = b; --groups; }
    }
    int cut = 0;
    for (auto [u, v] : e) cut += find(p, u) != find(p, v);
    return cut;
}

int main() {
    const int h = 6, n = 2 * h;
    std::vector<Edge> e;
    for (int u = 0; u < h; ++u)
        for (int v = u + 1; v < h; ++v) { e.push_back({u, v}); e.push_back({u + h, v + h}); }
    e.push_back({0, h});
    e.push_back({1, h + 1});
    int runs = 2000, ok = 0;
    for (int r = 0; r < runs; ++r) ok += contract(n, e) == 2;
    std::printf("n = %d, m = %zu: success rate of one run %.3f, bound %.4f\n", n, e.size(), double(ok) / runs, 2.0 / (n * (n - 1)));
    int T = (int)std::ceil(n * (n - 1) / 2.0 * std::log(n)), best = 1 << 30;
    for (int r = 0; r < T; ++r) best = std::min(best, contract(n, e));
    std::printf("best of T = %d runs: cut of size %d\n", T, best);
}
n = 12, m = 32: success rate of one run 0.302, bound 0.0152
best of T = 165 runs: cut of size 2

Finding all minimum cuts

The corollary has an algorithmic side: since each minimum cut is output with probability at least (n2)−1\binom{n}{2}^{-1}, O(n2log⁡n)O(n^2 \log n) runs find every minimum cut with high probability. On the cycle C6C_6 there are exactly (62)=15\binom{6}{2} = 15 minimum cuts, each found with probability exactly 1/151/15 per run. The program collects the distinct cuts found as the number of runs grows; the coupon-collector bound says about 15H15≈5015 H_{15} \approx 50 runs are needed on average to see all 15.

python · try it yourself
import random

def contract_cycle(n, rng):
    edges = [(i, (i + 1) % n) for i in range(n)]
    rng.shuffle(edges)
    parent = list(range(n))
    def find(x):
        while parent[x] != x:
            x = parent[x]
        return x
    groups = n
    for u, v in edges:
        if groups == 2:
            break
        if find(u) != find(v):
            parent[find(u)] = find(v)
            groups -= 1
    return frozenset((u, v) for u, v in edges if find(u) != find(v))   # the two cut edges

rng = random.Random(41)
n, seen = 6, set()
for runs in range(1, 201):
    seen.add(contract_cycle(n, rng))
    if runs in (5, 10, 20, 50, 100, 200):
        print(f"after {runs:3} runs: {len(seen):2} of 15 distinct minimum cuts found")
after   5 runs:  5 of 15 distinct minimum cuts found
after  10 runs: 10 of 15 distinct minimum cuts found
after  20 runs: 12 of 15 distinct minimum cuts found
after  50 runs: 15 of 15 distinct minimum cuts found
after 100 runs: 15 of 15 distinct minimum cuts found
after 200 runs: 15 of 15 distinct minimum cuts found

Where it stands

  • Monte Carlo, one-sided. The algorithm never reports a cut smaller than the minimum (every output is a real cut), so its only error is reporting a cut that is too large.
  • All minimum cuts at once. With O(n2log⁡n)O(n^2 \log n) runs, every one of the at most (n2)\binom{n}{2} minimum cuts is found with high probability, which deterministic flow-based methods do not give for free.
  • Faster versions. Karger-Stein (1996) runs in O(n2log⁡3n)O(n^2 \log^3 n). Karger (2000) gave a near-linear O(mlog⁡3n)O(m \log^3 n) algorithm using tree packings, and recent work has found deterministic near-linear algorithms for simple graphs (Kawarabayashi and Thorup, 2015) and weighted graphs (Henzinger, Li, Rao and Wang, 2024).

Test yourself

Why does a random edge of a multigraph on rr vertices with minimum cut kk lie in a fixed minimum cut with probability at most 2/r2/r?

  1. Because there are rr cuts
  2. Every vertex has degree at least kk, so there are at least rk/2rk/2 edges, of which kk are in the cut
  3. Because the graph is a tree
  4. Because contraction removes half of the edges

What is the lower bound on the probability that one run of the contraction algorithm returns a fixed minimum cut?

  1. 1/n1/n
  2. 2/(n(n−1))2/(n(n-1))
  3. 1/2n1/2^n
  4. 1/lg⁡n1/\lg n

What does the analysis imply about the number of minimum cuts in a graph on nn vertices?

  1. At most nn
  2. At most (n2)\binom{n}{2}
  3. At most 2n−12^{n-1}, and this is attained
  4. Exactly one

How many independent runs make the failure probability at most 1/n1/n?

  1. nn
  2. (n2)ln⁡n\binom{n}{2}\ln n
  3. lg⁡n\lg n
  4. n3n^3