TExcelTAD
Home › Online Algorithms & Competitive Analysis › RANKING and Its Primal-Dual Proof

RANKING and Its Primal-Dual Proof

RANDOM, which picks a fresh random neighbour for every arrival, is no better than greedy (Online Bipartite Matching and Greedy). Karp, Vazirani and Vazirani (1990) found the right way to randomize: draw one random ranking of the offline vertices before anything arrives, and match every online vertex to its best-ranked free neighbour. This algorithm, RANKING, obtains an expected 1−1e≈0.6321 - \frac{1}{e} \approx 0.632 fraction of the maximum matching, and no randomized algorithm does better against an oblivious adversary. The original proof was intricate (and a gap in it was later repaired by Goel and Mehta, 2008, and by Birnbaum and Mathieu, 2008); this lesson gives the short randomized primal-dual proof of Devanur, Jain and Kleinberg (2013), which also introduces the method of the next two lessons.

The algorithm

text
RANKING(U)
0  choose a uniformly random permutation pi of the offline vertices U
1  for each arriving online vertex v
2      if v has a free neighbour
3          match v to the free neighbour u with the smallest pi(u)

Equivalently, give every offline vertex uu an independent value yuy_u uniform on [0,1][0, 1] and use the order of the yuy_u as the ranking; the proof uses this continuous form. RANKING is greedy (it always matches when it can), so it is at least 12\frac{1}{2}-competitive in every run; the improvement is in expectation.

Upper-triangular graphs: the hard case

On the upper-triangular graph (vjv_j adjacent to uj,…,unu_j, \ldots, u_n, arriving in order j=1,…,nj = 1, \ldots, n, with offline vertices relabelled at random to hide the structure) RANKING's expected matching size tends to (1−1/e) n(1 - 1/e)\,n, and Karp, Vazirani and Vazirani showed with Yao's principle that this graph family defeats every randomized algorithm: no algorithm achieves more than (1−1/e) n+o(n)(1 - 1/e)\,n + o(n) in expectation. So 1−1/e1 - 1/e is exactly the right answer for online bipartite matching.

python · try it yourself
import random

def ranking(adj, n_offline, rng):
    rank = list(range(n_offline))
    rng.shuffle(rank)                                    # rank[u] = position of u in pi
    free = [True] * n_offline
    size = 0
    for nbrs in adj:
        cands = [u for u in nbrs if free[u]]
        if cands:
            free[min(cands, key=lambda u: rank[u])] = False
            size += 1
    return size

def random_greedy(adj, n_offline, rng):
    free = [True] * n_offline
    size = 0
    for nbrs in adj:
        cands = [u for u in nbrs if free[u]]
        if cands:
            free[rng.choice(cands)] = False
            size += 1
    return size

rng = random.Random(25)
print("   n   E[RANKING]/n   E[RANDOM]/n   1 - 1/e = 0.6321")
for n in [10, 50, 200]:
    adj = [list(range(j, n)) for j in range(n)]
    runs = 4000 // n
    r = sum(ranking(adj, n, rng) for _ in range(runs)) / runs
    g = sum(random_greedy(adj, n, rng) for _ in range(runs)) / runs
    print(f"{n:4}   {r / n:12.4f}   {g / n:11.4f}")
   n   E[RANKING]/n   E[RANDOM]/n   1 - 1/e = 0.6321
  10         0.6560        0.6575
  50         0.6388        0.6350
 200         0.6315        0.6352

On this family RANKING and RANDOM behave alike (both tend to 1−1/e1 - 1/e here); the instances that separate them, where RANDOM falls to 12\frac{1}{2}, are more elaborate. The point of this table is the upper bound: no algorithm, however clever, beats 1−1/e1 - 1/e on these graphs.

In C++

RANKING with a hand-written Fisher-Yates shuffle (so the permutation does not depend on the standard library's std::shuffle), on seeded random graphs where every online vertex has 3 neighbours, compared with the maximum matching.

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

std::mt19937 rng(26);

int ranking(const std::vector<std::vector<int>>& adj, int nu) {
    std::vector<int> rank(nu);
    for (int i = 0; i < nu; ++i) rank[i] = i;
    for (int i = nu - 1; i > 0; --i) std::swap(rank[i], rank[rng() % (i + 1)]);
    std::vector<char> free_(nu, 1);
    int size = 0;
    for (const auto& nbrs : adj) {
        int best = -1;
        for (int u : nbrs) if (free_[u] && (best < 0 || rank[u] < rank[best])) best = u;
        if (best >= 0) { free_[best] = 0; ++size; }
    }
    return size;
}

bool augment(int v, const std::vector<std::vector<int>>& adj, std::vector<int>& mu, std::vector<char>& seen) {
    for (int u : adj[v]) {
        if (seen[u]) continue;
        seen[u] = 1;
        if (mu[u] < 0 || augment(mu[u], adj, mu, seen)) { mu[u] = v; return true; }
    }
    return false;
}

int main() {
    const int n = 400, runs = 200;
    std::vector<std::vector<int>> adj(n);
    for (int v = 0; v < n; ++v)
        for (int j = 0; j < 3; ++j) adj[v].push_back(rng() % n);
    std::vector<int> mu(n, -1);
    int opt = 0;
    for (int v = 0; v < n; ++v) { std::vector<char> seen(n, 0); opt += augment(v, adj, mu, seen); }
    double total = 0;
    int worst = n;
    for (int r = 0; r < runs; ++r) { int s = ranking(adj, n); total += s; worst = std::min(worst, s); }
    std::printf("OPT %d, RANKING mean %.1f (%.3f of OPT), worst run %d (%.3f), guarantee 0.632\n",
                opt, total / runs, total / runs / opt, worst, double(worst) / opt);
}
OPT 374, RANKING mean 330.6 (0.884 of OPT), worst run 320 (0.856), guarantee 0.632

The primal-dual proof

The maximum matching LP and its dual, the fractional vertex cover, are

max⁡∑(u,v)∈Exuv  s.t. ∑vxuv≤1, ∑uxuv≤1, x≥0min⁡∑uαu+∑vβv  s.t. αu+βv≥1 ∀(u,v)∈E, α,β≥0.\max \sum_{(u,v) \in E} x_{uv} \ \ \text{s.t.} \ \sum_{v} x_{uv} \le 1, \ \sum_{u} x_{uv} \le 1, \ x \ge 0 \qquad \min \sum_{u} \alpha_u + \sum_{v} \beta_v \ \ \text{s.t.} \ \alpha_u + \beta_v \ge 1 \ \forall (u, v) \in E, \ \alpha, \beta \ge 0 .

By weak duality, any feasible dual solution is at least OPT\mathrm{OPT} (LP Duality treats duality in depth). The proof builds a random dual solution alongside RANKING whose total equals RANKING's matching size, and shows that it is feasible in expectation after scaling by 11−1/e\frac{1}{1 - 1/e}.

The dual assignment. Let g(y)=ey−1g(y) = e^{y - 1}. When RANKING matches vv to uu (which has value yuy_u), set

αu=g(yu),βv=1−g(yu).\alpha_u = g(y_u), \qquad \beta_v = 1 - g(y_u) .

Unmatched vertices get 0. Each matched edge contributes exactly 1 to ∑α+∑β\sum \alpha + \sum \beta, so this sum equals the size of RANKING's matching.

Theorem (Devanur, Jain and Kleinberg, 2013). For every edge (u,v)(u, v), E[αu+βv]≥1−1e\mathbb{E}[\alpha_u + \beta_v] \ge 1 - \frac{1}{e}. Consequently E[α]/(1−1/e)\mathbb{E}[\alpha] / (1 - 1/e) and E[β]/(1−1/e)\mathbb{E}[\beta] / (1 - 1/e) form a feasible dual solution, and

E[RANKING]=E[∑αu+∑βv]≥(1−1e)OPT.\mathbb{E}[\mathrm{RANKING}] = \mathbb{E}\Bigl[\sum \alpha_u + \sum \beta_v\Bigr] \ge \Bigl(1 - \frac{1}{e}\Bigr) \mathrm{OPT}.
Proof of the edge inequality

Fix the edge (u,v)(u, v) and the values yu′y_{u'} of all offline vertices other than uu. Run RANKING on the graph without uu; let ycy^c be the value of vv's partner in that run, or yc=1y^c = 1 if vv stays unmatched. Two facts hold for the real run, with uu present:

  1. If yu<ycy_u < y^c, then uu is matched. Suppose uu were unmatched. Then its presence changes nothing (an unmatched vertex never influences a choice), so the run is identical to the run without uu, and when vv arrives, uu is a free neighbour of rank better than vv's partner there (or vv has no partner). RANKING would match vv to uu: contradiction.
  2. βv≥1−g(yc)\beta_v \ge 1 - g(y^c) always. Adding a vertex to the offline side can only improve each online vertex's partner (a standard monotonicity property of RANKING, provable by induction over arrivals): vv's partner in the real run has value at most ycy^c, so βv=1−g(⋅)≥1−g(yc)\beta_v = 1 - g(\cdot) \ge 1 - g(y^c).

Averaging over yuy_u uniform on [0,1][0, 1], with everything else fixed:

E[αu+βv]≥∫0ycg(y) dy+1−g(yc)=(eyc−1−e−1)+1−eyc−1=1−1e.\mathbb{E}[\alpha_u + \beta_v] \ge \int_0^{y^c} g(y)\,dy + 1 - g(y^c) = \bigl(e^{y^c - 1} - e^{-1}\bigr) + 1 - e^{y^c - 1} = 1 - \frac{1}{e}.

The choice of gg is exactly what makes the bound independent of ycy^c: gg solves ∫0θg+1−g(θ)=const\int_0^\theta g + 1 - g(\theta) = \text{const}, i.e. g′=gg' = g.

The program estimates E[αu+βv]\mathbb{E}[\alpha_u + \beta_v] for every edge of a seeded random graph by Monte Carlo over the values yy, and reports the smallest one; it should not fall below 1−1/e1 - 1/e (up to sampling noise).

python · try it yourself
import random
from math import exp

def ranking_duals(adj, n_offline, y):
    owner = [None] * n_offline
    alpha, beta = [0.0] * n_offline, [0.0] * len(adj)
    for v, nbrs in enumerate(adj):
        cands = [u for u in nbrs if owner[u] is None]
        if cands:
            u = min(cands, key=lambda u: y[u])
            owner[u] = v
            alpha[u] = exp(y[u] - 1)
            beta[v] = 1 - alpha[u]
    return alpha, beta, sum(o is not None for o in owner)

rng = random.Random(27)
n_off, n_on = 12, 12
adj = [sorted(rng.sample(range(n_off), rng.randint(1, 4))) for _ in range(n_on)]
edges = [(u, v) for v, nbrs in enumerate(adj) for u in nbrs]
sums = {e: 0.0 for e in edges}
size_total, runs = 0, 4000
for _ in range(runs):
    y = [rng.random() for _ in range(n_off)]
    alpha, beta, size = ranking_duals(adj, n_off, y)
    size_total += size
    for u, v in edges:
        sums[(u, v)] += alpha[u] + beta[v]
worst = min(sums.values()) / runs
print(f"{len(edges)} edges; smallest E[alpha_u + beta_v] = {worst:.4f}, 1 - 1/e = {1 - exp(-1):.4f}")
print(f"E[matching size] = {size_total / runs:.3f} = E[sum alpha + sum beta]")
30 edges; smallest E[alpha_u + beta_v] = 0.7020, 1 - 1/e = 0.6321
E[matching size] = 10.652 = E[sum alpha + sum beta]

Every edge's dual sum stays above 1−1/e1 - 1/e, so the scaled expected duals are feasible, and the expected matching is at least (1−1/e) OPT(1 - 1/e)\,\mathrm{OPT} by weak duality.

Extensions

  • Vertex weights. If offline vertex uu has weight wuw_u and the goal is the total weight matched, ranking by the perturbed weights wu(1−g(yu))w_u (1 - g(y_u)) is (1−1/e)(1 - 1/e)-competitive (Aggarwal, Goel, Karande and Mehta, 2011); the primal-dual proof adapts directly.
  • Edge weights with free disposal, and budgets: see AdWords and Budgeted Allocation.
  • Random arrival order. If the online vertices arrive in random order, RANKING does better than 1−1/e1 - 1/e: at least about 0.6960.696 (Mahdian and Yan, 2011).

Test yourself

How does RANKING differ from RANDOM?

  1. RANKING is deterministic
  2. RANKING draws one random order of the offline vertices at the start and uses it for every arrival; RANDOM draws fresh randomness each time
  3. RANKING sees the future
  4. RANKING leaves some vertices unmatched on purpose

In the Devanur-Jain-Kleinberg proof, what are αu\alpha_u and βv\beta_v when vv is matched to uu?

  1. αu=1\alpha_u = 1, βv=0\beta_v = 0
  2. αu=eyu−1\alpha_u = e^{y_u - 1}, βv=1−eyu−1\beta_v = 1 - e^{y_u - 1}
  3. αu=yu\alpha_u = y_u, βv=1−yu\beta_v = 1 - y_u
  4. Both 12\frac{1}{2}

Why does E[αu+βv]≥1−1/e\mathbb{E}[\alpha_u + \beta_v] \ge 1 - 1/e for every edge imply the competitive ratio?

  1. Because the duals are integral
  2. Because the expected duals scaled by 1/(1−1/e)1/(1 - 1/e) are a feasible fractional vertex cover, whose value is at least OPT by weak duality, and the dual total equals the matching size
  3. Because RANKING is greedy
  4. Because OPT is at most nn

Can a randomized algorithm beat 1−1/e1 - 1/e for online bipartite matching against an oblivious adversary?

  1. Yes, RANDOM does
  2. No: upper-triangular graphs with randomly hidden labels limit every randomized algorithm to 1−1/e+o(1)1 - 1/e + o(1)
  3. Yes, with a better permutation
  4. Only deterministic algorithms can