TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Karger-Stein: Recursive Contraction

Karger-Stein: Recursive Contraction

Karger's contraction algorithm needs about n2ln⁡nn^2 \ln n independent runs to find a minimum cut with high probability, O(n4log⁡n)O(n^4 \log n) time in total. Look at where a run fails: the probability of contracting a minimum-cut edge at the step with rr vertices left is at most 2/r2/r, tiny at the start and large at the end. The early contractions are nearly safe, so repeating them in every run is wasteful. Karger and Stein (1996) share the safe early work between runs: contract to about n/2n/\sqrt 2 vertices, then branch into two independent recursive calls. The result runs in O(n2log⁡n)O(n^2 \log n) time per trial, succeeds with probability Ω(1/log⁡n)\Omega(1/\log n), and finds a minimum cut with high probability in O(n2log⁡3n)O(n^2 \log^3 n) time.

Surviving down to tt vertices

Running the contraction only until tt vertices remain keeps a fixed minimum cut CC with probability

∏r=t+1n(1−2r)=∏r=t+1nr−2r=t(t−1)n(n−1).\prod_{r = t+1}^{n} \left(1 - \frac{2}{r}\right) = \prod_{r=t+1}^{n} \frac{r - 2}{r} = \frac{t(t-1)}{n(n-1)} .

For t=⌈1+n/2⌉t = \lceil 1 + n/\sqrt{2} \rceil we get t−1≥n/2t - 1 \ge n/\sqrt 2 and t>n/2t > n/\sqrt 2, so t(t−1)≥n2/2≥n(n−1)/2t(t - 1) \ge n^2/2 \ge n(n-1)/2 and the survival probability is at least 1/21/2. Contracting a third of the vertices away is a coin flip at worst; the whole contraction down to 2 vertices is a 1/(n2)1/\binom{n}{2} lottery.

The algorithm

text
KARGER-STEIN(G)                     // G: multigraph on n vertices
0  if n <= 6
1      return a minimum cut of G by brute force
2  t = ceil(1 + n / sqrt(2))
3  G1 = CONTRACT-TO(G, t)           // independent random contractions
4  G2 = CONTRACT-TO(G, t)
5  return the smaller of KARGER-STEIN(G1) and KARGER-STEIN(G2)

CONTRACT-TO(G, t) performs random contractions until tt vertices remain. With an adjacency-matrix representation it costs O(n2)O(n^2).

Running time. T(n)=2T(⌈1+n/2⌉)+O(n2)T(n) = 2T(\lceil 1 + n/\sqrt 2 \rceil) + O(n^2). Here a=2a = 2 subproblems shrink by b=2b = \sqrt 2, and nlog⁡ba=n2n^{\log_b a} = n^2 matches the work per level: case 2 of the master theorem gives T(n)=O(n2log⁡n)T(n) = O(n^2 \log n). Each of the O(log⁡n)O(\log n) levels of the recursion tree does O(n2)O(n^2) work in total, because the number of subproblems doubles while n2n^2 halves.

Success probability. A call succeeds if, in at least one branch, CC survives the contraction and the recursive call succeeds. Let pkp_k be a lower bound on the success probability of a call at height kk above the leaves (leaves are exact, p0=1p_0 = 1). The two branches are independent, so

pk+1≥1−(1−12pk)2=pk−pk24.p_{k+1} \ge 1 - \left(1 - \tfrac12 p_k\right)^2 = p_k - \frac{p_k^2}{4} .

Claim: pk≥1k+1p_k \ge \frac{1}{k + 1}. By induction: the map p↦p−p2/4p \mapsto p - p^2/4 is increasing on [0,2][0, 2], so pk+1≥1k+1−14(k+1)2≥1k+2p_{k+1} \ge \frac{1}{k+1} - \frac{1}{4(k+1)^2} \ge \frac{1}{k+2}, where the last step is equivalent to 4(k+1)≥k+24(k + 1) \ge k + 2. The height is about log⁡2n=2lg⁡n\log_{\sqrt 2} n = 2 \lg n, so

Pr⁡[KARGER-STEIN finds C]≥12lg⁡n+O(1)=Ω ⁣(1log⁡n).\Pr[\text{KARGER-STEIN finds } C] \ge \frac{1}{2\lg n + O(1)} = \Omega\!\left(\frac{1}{\log n}\right).

Repetition. O(log⁡2n)O(\log^2 n) independent trials (say clg⁡n⋅(2lg⁡n+O(1))c \lg n \cdot (2 \lg n + O(1)) of them) push the failure probability below n−cn^{-c} (up to constants in the exponent), for a total of O(n2log⁡3n)O(n^2 \log^3 n) time.

Implementation and measurement

The program stores a multigraph as an edge list on vertices 0,…,r−10, \ldots, r - 1. contract_to shuffles the edges and merges with union-find until tt groups remain, then relabels the groups and keeps only edges between different groups. The test graphs are doubled cycles: a cycle on nn vertices in which every edge is doubled except two opposite ones. The two single edges form the unique minimum cut (size 2), and there are many cuts of size 3 and 4 to fall into. Work is counted as edges processed, the dominant cost.

python · try it yourself
import random
from itertools import product

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

def contract_to(n, edges, t, rng, work):
    order = edges[:]
    rng.shuffle(order)
    p = list(range(n))
    groups = n
    for u, v in order:
        work[0] += 1
        if groups == t:
            break
        a, b = find(p, u), find(p, v)
        if a != b:
            p[a] = b
            groups -= 1
    label = {}
    for x in range(n):
        label.setdefault(find(p, x), len(label))
    new = [(label[find(p, u)], label[find(p, v)]) for u, v in edges]
    work[0] += len(edges)
    return t, [(a, b) for a, b in new if a != b]

def brute_force(n, edges, work):
    best = None
    for bits in product([0, 1], repeat=n - 1):     # vertex n-1 is fixed on side 0
        side = bits + (0,)
        if any(side):
            c = sum(side[u] != side[v] for u, v in edges)
            work[0] += len(edges)
            best = c if best is None else min(best, c)
    return best

def karger_stein(n, edges, rng, work):
    if n <= 6:
        return brute_force(n, edges, work)
    t = -(-n * 1000 // 1414) + 1                   # ceil(1 + n / sqrt 2)
    return min(karger_stein(*contract_to(n, edges, t, rng, work), rng, work) for _ in range(2))

def karger(n, edges, rng, work):
    return len(contract_to(n, edges, 2, rng, work)[1])

def doubled_cycle(n):
    e = []
    for i in range(n):
        e.append((i, (i + 1) % n))
        if i not in (0, n // 2):
            e.append((i, (i + 1) % n))          # a parallel copy: every edge doubled but two
    return n, e

rng = random.Random(50)
print(" n   Karger: success  steps/run   Karger-Stein: success  steps/trial")
for size in [12, 18, 24]:
    n, edges = doubled_cycle(size)
    w1, w2 = [0], [0]
    k_ok = sum(karger(n, edges, rng, w1) == 2 for _ in range(600))
    ks_ok = sum(karger_stein(n, edges, rng, w2) == 2 for _ in range(6))
    print(f"{n:2}   {k_ok / 600:15.3f}  {w1[0] / 600:9.0f}   {ks_ok / 6:21.2f}  {w2[0] / 6:11.0f}")
 n   Karger: success  steps/run   Karger-Stein: success  steps/trial
12             0.098         36                    1.00        11424
18             0.040         58                    0.83        23973
24             0.038         80                    1.00        48308

One contraction succeeds less and less often as nn grows (on these graphs roughly like 1/n1/n; the bound 2/(n(n−1))2/(n(n-1)) is for the worst graphs), while a Karger-Stein trial succeeds most of the time. Per unit of work, though, plain repetition is still competitive at these small sizes: here a Karger-Stein trial costs thousands of edge steps, mostly in the many small subproblems near the leaves. The asymptotic advantage, O(n2log⁡3n)O(n^2 \log^3 n) against O(n4log⁡n)O(n^4 \log n) in the adjacency-matrix model, needs larger graphs to show; the mini project measures both on graphs of growing size.

The recursion tree itself shows where the work goes. The program counts the subproblems and their total size per level for n=200n = 200.

python · try it yourself
from math import ceil, sqrt

n = 200
level, sizes = 0, [n]
print("level   subproblems   size each   sum of size^2")
while sizes[0] > 6:
    print(f"{level:5}   {len(sizes):11}   {sizes[0]:9}   {sum(s * s for s in sizes):13}")
    t = ceil(1 + sizes[0] / sqrt(2))
    sizes = [t] * (2 * len(sizes))
    level += 1
print(f"{level:5}   {len(sizes):11}   {sizes[0]:9}   (brute-force leaves)")
level   subproblems   size each   sum of size^2
    0             1         200           40000
    1             2         143           40898
    2             4         103           42436
    3             8          74           43808
    4            16          54           46656
    5            32          40           51200
    6            64          30           57600
    7           128          23           67712
    8           256          18           82944
    9           512          14          100352
   10          1024          11          123904
   11          2048           9          165888
   12          4096           8          262144
   13          8192           7          401408
   14         16384           6   (brute-force leaves)

On the upper levels the total size squared stays near n2=40 000n^2 = 40\,000: the number of subproblems doubles while n2n^2 halves, which is the O(n2)O(n^2) work per level behind the O(n2log⁡n)O(n^2 \log n) bound. Near the leaves the "+1+1" and the ceiling in tt take over (11 vertices shrink only to 9, then 8, 7 and 6), so the last levels cost several times more. The analysis absorbs this into the constant, since sizes below a constant cost O(1)O(1) each and there are O(n2)O(n^2) leaves; implementations often stop the recursion at a larger size and finish with plain repeated contraction.

In C++

Karger-Stein in C++ with a fixed-seed std::mt19937 on the same doubled cycles, including a larger one. The random stream differs from Python's, so the success rates differ a little.

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

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

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

std::vector<Edge> contract_to(int n, std::vector<Edge> e, int t) {
    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 == t) break;
        int a = find(p, u), b = find(p, v);
        if (a != b) { p[a] = b; --groups; }
    }
    std::map<int, int> label;
    for (int x = 0; x < n; ++x) label.emplace(find(p, x), (int)label.size());
    std::vector<Edge> out;
    for (auto [u, v] : e) {
        int a = label[find(p, u)], b = label[find(p, v)];
        if (a != b) out.push_back({a, b});
    }
    return out;
}

int brute_force(int n, const std::vector<Edge>& e) {
    int best = 1 << 30;
    for (int mask = 1; mask < (1 << (n - 1)); ++mask) {      // vertex n-1 stays on side 0
        int c = 0;
        for (auto [u, v] : e) c += ((mask >> u) & 1) != ((mask >> v) & 1);
        best = std::min(best, c);
    }
    return best;
}

int karger_stein(int n, const std::vector<Edge>& e) {
    if (n <= 6) return brute_force(n, e);
    int t = (int)std::ceil(1 + n / std::sqrt(2.0));
    int a = karger_stein(t, contract_to(n, e, t));
    int b = karger_stein(t, contract_to(n, e, t));
    return std::min(a, b);
}

int main() {
    for (int n : {24, 48, 72}) {
        std::vector<Edge> e;
        for (int i = 0; i < n; ++i) {
            e.push_back({i, (i + 1) % n});
            if (i != 0 && i != n / 2) e.push_back({i, (i + 1) % n});
        }
        int trials = 20, ok = 0, one_run_ok = 0;
        for (int t = 0; t < trials; ++t) ok += karger_stein(n, e) == 2;
        for (int t = 0; t < 2000; ++t) one_run_ok += contract_to(n, e, 2).size() == 2;
        std::printf("n = %2d: Karger one run %.4f, Karger-Stein %2d/%d trials found the min cut 2\n",
                    n, one_run_ok / 2000.0, ok, trials);
    }
}
n = 24: Karger one run 0.0425, Karger-Stein 17/20 trials found the min cut 2
n = 48: Karger one run 0.0185, Karger-Stein 17/20 trials found the min cut 2
n = 72: Karger one run 0.0215, Karger-Stein 17/20 trials found the min cut 2

Test yourself

Why does Karger-Stein contract only down to about n/2n/\sqrt{2} vertices before branching?

  1. Because 2\sqrt 2 is irrational
  2. Because a fixed minimum cut survives contraction to ⌈1+n/2⌉\lceil 1 + n/\sqrt 2 \rceil vertices with probability at least 1/21/2
  3. To make the recursion tree binary
  4. Because smaller graphs have no minimum cuts

What is the running time of one Karger-Stein trial?

  1. O(n2)O(n^2)
  2. O(n2log⁡n)O(n^2 \log n)
  3. O(n4)O(n^4)
  4. O(n2log⁡3n)O(n^2 \log^3 n)

Which recurrence bounds the success probability, level by level from the leaves?

  1. pk+1=pk/2p_{k+1} = p_k/2
  2. pk+1=1−(1−pk/2)2p_{k+1} = 1 - (1 - p_k/2)^2
  3. pk+1=pk2p_{k+1} = p_k^2
  4. pk+1=2pkp_{k+1} = 2p_k

What is the success probability of one Karger-Stein trial, asymptotically?

  1. Ω(1/n2)\Omega(1/n^2)
  2. Ω(1/log⁡n)\Omega(1/\log n)
  3. Ω(1)\Omega(1)
  4. 1−1/n1 - 1/n