TExcelTAD
Home › Online Algorithms & Competitive Analysis › The Online Primal-Dual Framework

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, O(log⁡d)O(\log d) 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 x1,…,xnx_1, \ldots, x_n with costs cj>0c_j > 0 and covering constraints that arrive one at a time; the algorithm may only increase variables, and must satisfy each constraint on arrival:

min⁡∑jcjxjs.t.∑j∈Sixj≥1  for each arriving i,x≥0.\min \sum_j c_j x_j \quad \text{s.t.} \quad \sum_{j \in S_i} x_j \ge 1 \ \ \text{for each arriving } i, \qquad x \ge 0 .

Its dual is a packing LP with one variable yiy_i per constraint, max⁡∑iyi\max \sum_i y_i subject to ∑i:j∈Siyi≤cj\sum_{i : j \in S_i} y_i \le c_j for every jj. The generic algorithm:

text
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 + 1

Theorem (Buchbinder and Naor, 2009). ONLINE-COVER keeps the primal feasible, and its cost is O(log⁡d)O(\log d) times the optimal fractional cost; the dual yy, scaled down by O(log⁡d)O(\log d), is feasible.

Proof

Primal grows at most twice as fast as the dual. One iteration of the loop raises ∑iyi\sum_i y_i by 1 and the primal cost by ∑j∈SicjΔxj=∑j∈Si(xj+1∣Si∣)≤1+1=2\sum_{j \in S_i} c_j \Delta x_j = \sum_{j \in S_i} \bigl(x_j + \frac{1}{\lvert S_i \rvert}\bigr) \le 1 + 1 = 2, because the loop runs only while ∑j∈Sixj<1\sum_{j \in S_i} x_j < 1.

The dual is nearly feasible. Fix jj and let Yj=∑i:j∈SiyiY_j = \sum_{i : j \in S_i} y_i, the number of iterations that raised xjx_j. Each such iteration multiplies xj+1dx_j + \frac{1}{d} by at least 1+1cj1 + \frac{1}{c_j} (check: xj(1+1cj)+1∣Si∣cj≥(xj+1d)(1+1cj)−1dx_j(1 + \frac{1}{c_j}) + \frac{1}{\lvert S_i \rvert c_j} \ge (x_j + \frac{1}{d})(1 + \frac{1}{c_j}) - \frac{1}{d}). So xj≥1d((1+1cj)Yj−1)x_j \ge \frac{1}{d}\bigl((1 + \frac{1}{c_j})^{Y_j} - 1\bigr). But xjx_j is never raised once it reaches 1, so xj<1+1cj+1cj≤3x_j < 1 + \frac{1}{c_j} + \frac{1}{c_j} \le 3 for costs cj≥1c_j \ge 1, and therefore

(1+1cj)Yj≤3d+1⟹Yj≤cjlog⁡1+1/cj(3d+1)=O(cjlog⁡d).\Bigl(1 + \frac{1}{c_j}\Bigr)^{Y_j} \le 3d + 1 \quad\Longrightarrow\quad Y_j \le c_j \log_{1 + 1/c_j}(3d + 1) = O\bigl(c_j \log d\bigr) .

So y/O(log⁡d)y / O(\log d) is dual feasible, and by weak duality ∑iyi≤O(log⁡d)⋅OPT\sum_i y_i \le O(\log d) \cdot \mathrm{OPT}. The primal cost is at most 2∑iyi=O(log⁡d) OPT2\sum_i y_i = O(\log d)\,\mathrm{OPT}.

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 γ\gamma; then the algorithm is O(γ)O(\gamma)-competitive.

Ski rental, again

Write ski rental as a covering LP. Variable xx is "bought" (cost BB) and ztz_t is "rented on day tt" (cost 1); every day tt of the season brings the constraint x+zt≥1x + z_t \ge 1. The dual has yt≤1y_t \le 1 per day and ∑tyt≤B\sum_t y_t \le B. The primal-dual algorithm for this LP, with a constant tuned to the problem, is:

text
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 x<1x < 1 raises the dual by 1 and the primal by BΔx+zt=x+1C+1−x=1+1CB\Delta x + z_t = x + \frac{1}{C} + 1 - x = 1 + \frac{1}{C}. After BB days, x=(1+1/B)B−1C=1x = \frac{(1 + 1/B)^B - 1}{C} = 1 exactly, so the algorithm stops paying, and ∑tyt≤B\sum_t y_t \le B holds: the dual is feasible. Hence the fractional solution costs at most (1+1C) OPT(1 + \frac{1}{C})\,\mathrm{OPT}, and

1+1C=(1+1/B)B(1+1/B)B−1→B→∞ee−1.1 + \frac{1}{C} = \frac{(1 + 1/B)^B}{(1 + 1/B)^B - 1} \xrightarrow{B \to \infty} \frac{e}{e - 1} .

The fractional xx after day tt is read as the probability of having bought by day tt, which turns it into a randomized strategy with the same expected cost: the ee−1\frac{e}{e-1} algorithm of Randomized Ski Rental, derived this time without guessing a distribution.

python · try it yourself
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 1+1/C1 + 1/C exactly, and for large BB it approaches ee−1\frac{e}{e-1} from above. (The equalizing distribution of the randomized ski-rental lesson is slightly better for each finite BB; both have the same limit.)

Online set cover

In online set cover, a family of mm sets with costs cS≥1c_S \ge 1 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 ∑S∋exS≥1\sum_{S \ni e} x_S \ge 1 per arriving element ee, so ONLINE-COVER is O(log⁡d)O(\log d)-competitive for fractional set cover, where dd 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 O(log⁡mlog⁡n)O(\log m \log n)-competitive algorithm, nn 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.

python · try it yourself
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 xS←2xS+1/∣S(e)∣x_S \leftarrow 2x_S + 1/\lvert S(e) \rvert) on a larger seeded instance, reporting the online cost, the dual total and the overload factor. By weak duality OPTfrac≥dual/overload\mathrm{OPT}_{\text{frac}} \ge \text{dual}/\text{overload}, so the printed certified ratio is an upper bound on the true competitive ratio of this run.

cpp · try it yourself
#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 O(log⁡k)O(\log k) for randomized weighted paging (Bansal, Buchbinder and Naor, 2007), O(log⁡n)O(\log n) for routing and load balancing problems, the MSVV bound for AdWords (AdWords and Budgeted Allocation), and the fractional engines inside the polylogarithmic kk-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 O(log⁡2k)O(\log^2 k) kk-server bound on trees and the O(log⁡2n)O(\log^2 n) bound for metrical task systems were obtained.

Test yourself

In the online covering template, which variables may the algorithm change?

  1. Any variable, up or down
  2. It may only increase the primal variables, and it raises one dual variable per arriving constraint
  3. Only the dual
  4. Only variables of the arriving constraint, up or down

Why is the primal increase per iteration at most 2 in ONLINE-COVER?

  1. Because costs are at most 2
  2. Because it equals ∑j∈Si(xj+1/∣Si∣)\sum_{j \in S_i} (x_j + 1/\lvert S_i \rvert), and the loop runs only while ∑j∈Sixj<1\sum_{j \in S_i} x_j < 1
  3. Because d≤2d \le 2
  4. Because yi≤2y_i \le 2

For primal-dual ski rental with C=(1+1/B)B−1C = (1 + 1/B)^B - 1, what is the competitive ratio?

  1. 22
  2. 1+1/C1 + 1/C, which tends to ee−1\frac{e}{e-1}
  3. CC
  4. BB

What does the online primal-dual method give for integral online set cover?

  1. O(1)O(1)
  2. An O(log⁡d)O(\log d) fractional solution, rounded online to an integral O(log⁡mlog⁡n)O(\log m \log n)-competitive algorithm
  3. Nothing, rounding is impossible online
  4. An exact algorithm