The Online Primal-Dual Framework
RANKING and MSVV were analysed by building a dual solution alongside the algorithm. Buchbinder and Naor turned this into a design method: write the online problem as a linear program whose constraints arrive one by one, raise primal and dual variables together with a multiplicative update, and read the competitive ratio off the ratio between primal and dual growth. Their survey (The Design of Competitive Online Algorithms via a Primal-Dual Approach, 2009) derives from one template the optimal randomized ski-rental algorithm, fractional set cover, weighted paging, routing and much more. This lesson presents the template, applies it to ski rental and to online set cover, and checks the guarantees numerically.
The template
An online covering LP has nonnegative variables with costs and covering constraints that arrive one at a time; the algorithm may only increase variables, and must satisfy each constraint on arrival:
Its dual is a packing LP with one variable per constraint, subject to for every . The generic algorithm:
ONLINE-COVER(d) // d = largest |S_i|
0 x_j = 0 for every j
1 when constraint i arrives
2 while sum of x_j over j in S_i < 1
3 for each j in S_i: x_j = x_j * (1 + 1/c_j) + 1 / (|S_i| * c_j)
4 y_i = y_i + 1Theorem (Buchbinder and Naor, 2009). ONLINE-COVER keeps the primal feasible, and its cost is times the optimal fractional cost; the dual , scaled down by , is feasible.
Proof
Primal grows at most twice as fast as the dual. One iteration of the loop raises by 1 and the primal cost by , because the loop runs only while .
The dual is nearly feasible. Fix and let , the number of iterations that raised . Each such iteration multiplies by at least (check: ). So . But is never raised once it reaches 1, so for costs , and therefore
So is dual feasible, and by weak duality . The primal cost is at most .
The constant and the base of the logarithm depend on the update rule; the exact form above is one of several in the survey. What matters is the shape of the argument: (1) bound the primal increase by a constant times the dual increase, (2) show the dual violates its constraints by at most a factor ; then the algorithm is -competitive.
Ski rental, again
Write ski rental as a covering LP. Variable is "bought" (cost ) and is "rented on day " (cost 1); every day of the season brings the constraint . The dual has per day and . The primal-dual algorithm for this LP, with a constant tuned to the problem, is:
SKI-PRIMAL-DUAL(B) // C = (1 + 1/B)^B - 1
0 x = 0
1 on each day t of the season
2 if x < 1
3 z_t = 1 - x ; y_t = 1
4 x = x * (1 + 1/B) + 1 / (C * B)Each day with raises the dual by 1 and the primal by . After days, exactly, so the algorithm stops paying, and holds: the dual is feasible. Hence the fractional solution costs at most , and
The fractional after day is read as the probability of having bought by day , which turns it into a randomized strategy with the same expected cost: the algorithm of Randomized Ski Rental, derived this time without guessing a distribution.
def ski_primal_dual(B, season):
C = (1 + 1 / B) ** B - 1
x, primal, dual = 0.0, 0.0, 0
for t in range(season):
if x < 1 - 1e-12:
z = 1 - x
new_x = x * (1 + 1 / B) + 1 / (C * B)
primal += B * (new_x - x) + z
dual += 1
x = new_x
return primal, dual, x
for B in [4, 10, 50]:
C = (1 + 1 / B) ** B - 1
worst = max(ski_primal_dual(B, D)[0] / min(D, B) for D in range(1, 3 * B))
p, d, x = ski_primal_dual(B, 3 * B)
print(f"B = {B:2}: x after B days = {x:.6f}, primal/dual = {p / d:.4f}, "
f"worst ratio over seasons = {worst:.4f}, 1 + 1/C = {1 + 1 / C:.4f}")
print(f"e/(e - 1) = {2.718281828459045 / 1.718281828459045:.4f}")B = 4: x after B days = 1.000000, primal/dual = 1.6938, worst ratio over seasons = 1.6938, 1 + 1/C = 1.6938
B = 10: x after B days = 1.000000, primal/dual = 1.6275, worst ratio over seasons = 1.6275, 1 + 1/C = 1.6275
B = 50: x after B days = 1.000000, primal/dual = 1.5912, worst ratio over seasons = 1.5912, 1 + 1/C = 1.5912
e/(e - 1) = 1.5820
The worst ratio over all season lengths equals exactly, and for large it approaches from above. (The equalizing distribution of the randomized ski-rental lesson is slightly better for each finite ; both have the same limit.)
Online set cover
In online set cover, a family of sets with costs is known in advance; elements arrive one by one, and each arriving element must be covered by some chosen set containing it. The fractional version is exactly the covering template with one constraint per arriving element , so ONLINE-COVER is -competitive for fractional set cover, where is the largest number of sets containing one element. Alon, Awerbuch, Azar, Buchbinder and Naor (2003; journal version 2009) round the fractional solution online with randomization and obtain an integral -competitive algorithm, being the number of elements.
The program runs ONLINE-COVER on a seeded instance and compares its cost with the offline fractional optimum (computed by an LP solver after the fact) and with the dual it built.
import random
import numpy as np
from scipy.optimize import linprog
rng = random.Random(30)
m, universe = 12, 40
cost = [rng.randint(1, 5) for _ in range(m)]
sets = [set(rng.sample(range(universe), rng.randint(3, 9))) for _ in range(m)]
arrivals = [e for e in range(universe) if any(e in S for S in sets)]
rng.shuffle(arrivals)
arrivals = arrivals[:30]
x, y = [0.0] * m, {}
for e in arrivals:
S_e = [s for s in range(m) if e in sets[s]]
y[e] = 0
while sum(x[s] for s in S_e) < 1:
for s in S_e:
x[s] = x[s] * (1 + 1 / cost[s]) + 1 / (len(S_e) * cost[s])
y[e] += 1
online = sum(c * xs for c, xs in zip(cost, x))
A = np.array([[-1.0 if e in sets[s] else 0.0 for s in range(m)] for e in arrivals])
lp = linprog(cost, A_ub=A, b_ub=-np.ones(len(arrivals)), bounds=[(0, None)] * m, method="highs")
d = max(sum(e in S for S in sets) for e in arrivals)
overload = max(sum(y[e] for e in arrivals if e in sets[s]) / cost[s] for s in range(m))
print(f"{len(arrivals)} elements, {m} sets, d = {d}")
print(f"online fractional cost {online:.3f}, offline fractional OPT {lp.fun:.3f}, ratio {online / lp.fun:.3f}")
print(f"dual total {sum(y.values())}, worst dual overload factor {overload:.2f} (O(log d))")30 elements, 12 sets, d = 6
online fractional cost 36.213, offline fractional OPT 24.000, ratio 1.509
dual total 25, worst dual overload factor 3.00 (O(log d))
The online cost is within a small factor of the offline fractional optimum, and the dual it built is feasible after dividing by its overload factor, which certifies the ratio without the LP solver.
In C++
ONLINE-COVER with unit costs (the update becomes ) on a larger seeded instance, reporting the online cost, the dual total and the overload factor. By weak duality , so the printed certified ratio is an upper bound on the true competitive ratio of this run.
#include <algorithm>
#include <cstdio>
#include <random>
#include <vector>
int main() {
const int m = 200, universe = 1000, arrivals = 600;
std::mt19937 rng(31);
std::vector<std::vector<int>> sets_of(universe);
for (int s = 0; s < m; ++s) {
int size = 5 + rng() % 30;
for (int t = 0; t < size; ++t) sets_of[rng() % universe].push_back(s);
}
std::vector<double> x(m, 0.0), load(m, 0.0);
double dual = 0;
int d = 0, served = 0;
for (int t = 0; t < arrivals; ++t) {
int e = rng() % universe;
auto& S = sets_of[e];
std::sort(S.begin(), S.end());
S.erase(std::unique(S.begin(), S.end()), S.end());
if (S.empty()) continue; // no set contains e: skip it
++served;
d = std::max(d, (int)S.size());
auto covered = [&]() { double c = 0; for (int s : S) c += x[s]; return c; };
while (covered() < 1) {
for (int s : S) { x[s] = 2 * x[s] + 1.0 / S.size(); load[s] += 1; }
dual += 1;
}
}
double cost = 0, overload = 0;
for (int s = 0; s < m; ++s) { cost += x[s]; overload = std::max(overload, load[s]); }
std::printf("%d elements served, d = %d: online cost %.2f, dual %.0f, overload %.0f\n",
served, d, cost, dual, overload);
std::printf("certified: cost / (dual / overload) = %.2f\n", cost / (dual / overload));
}582 elements served, d = 11: online cost 153.34, dual 107, overload 3
certified: cost / (dual / overload) = 4.30
The certified ratio is pessimistic (the overload bound is worst case), but it is a proof for this run: no fractional cover is cheaper than dual/overload.
Where the method goes
The same template, with problem-specific constants, gives for randomized weighted paging (Bansal, Buchbinder and Naor, 2007), for routing and load balancing problems, the MSVV bound for AdWords (AdWords and Budgeted Allocation), and the fractional engines inside the polylogarithmic -server algorithms (Randomized k-Server). Its modern descendant is online mirror descent: the multiplicative update is a gradient step with an entropy regularizer, which is how the -server bound on trees and the bound for metrical task systems were obtained.
Test yourself
In the online covering template, which variables may the algorithm change?
- Any variable, up or down
- It may only increase the primal variables, and it raises one dual variable per arriving constraint
- Only the dual
- Only variables of the arriving constraint, up or down
Why is the primal increase per iteration at most 2 in ONLINE-COVER?
- Because costs are at most 2
- Because it equals , and the loop runs only while
- Because
- Because
For primal-dual ski rental with , what is the competitive ratio?
- , which tends to
What does the online primal-dual method give for integral online set cover?
- An fractional solution, rounded online to an integral -competitive algorithm
- Nothing, rounding is impossible online
- An exact algorithm