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 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
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 an independent value uniform on and use the order of the as the ranking; the proof uses this continuous form. RANKING is greedy (it always matches when it can), so it is at least -competitive in every run; the improvement is in expectation.
Upper-triangular graphs: the hard case
On the upper-triangular graph ( adjacent to , arriving in order , with offline vertices relabelled at random to hide the structure) RANKING's expected matching size tends to , and Karp, Vazirani and Vazirani showed with Yao's principle that this graph family defeats every randomized algorithm: no algorithm achieves more than in expectation. So is exactly the right answer for online bipartite matching.
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 here); the instances that separate them, where RANDOM falls to , are more elaborate. The point of this table is the upper bound: no algorithm, however clever, beats 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.
#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
By weak duality, any feasible dual solution is at least (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 .
The dual assignment. Let . When RANKING matches to (which has value ), set
Unmatched vertices get 0. Each matched edge contributes exactly 1 to , so this sum equals the size of RANKING's matching.
Theorem (Devanur, Jain and Kleinberg, 2013). For every edge , . Consequently and form a feasible dual solution, and
Proof of the edge inequality
Fix the edge and the values of all offline vertices other than . Run RANKING on the graph without ; let be the value of 's partner in that run, or if stays unmatched. Two facts hold for the real run, with present:
- If , then is matched. Suppose were unmatched. Then its presence changes nothing (an unmatched vertex never influences a choice), so the run is identical to the run without , and when arrives, is a free neighbour of rank better than 's partner there (or has no partner). RANKING would match to : contradiction.
- 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): 's partner in the real run has value at most , so .
Averaging over uniform on , with everything else fixed:
The choice of is exactly what makes the bound independent of : solves , i.e. .
The program estimates for every edge of a seeded random graph by Monte Carlo over the values , and reports the smallest one; it should not fall below (up to sampling noise).
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 , so the scaled expected duals are feasible, and the expected matching is at least by weak duality.
Extensions
- Vertex weights. If offline vertex has weight and the goal is the total weight matched, ranking by the perturbed weights is -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 : at least about (Mahdian and Yan, 2011).
Test yourself
How does RANKING differ from RANDOM?
- RANKING is deterministic
- RANKING draws one random order of the offline vertices at the start and uses it for every arrival; RANDOM draws fresh randomness each time
- RANKING sees the future
- RANKING leaves some vertices unmatched on purpose
In the Devanur-Jain-Kleinberg proof, what are and when is matched to ?
- ,
- ,
- ,
- Both
Why does for every edge imply the competitive ratio?
- Because the duals are integral
- Because the expected duals scaled by are a feasible fractional vertex cover, whose value is at least OPT by weak duality, and the dual total equals the matching size
- Because RANKING is greedy
- Because OPT is at most
Can a randomized algorithm beat for online bipartite matching against an oblivious adversary?
- Yes, RANDOM does
- No: upper-triangular graphs with randomly hidden labels limit every randomized algorithm to
- Yes, with a better permutation
- Only deterministic algorithms can