TExcelTAD
Home › Online Algorithms & Competitive Analysis › AdWords and Budgeted Allocation

AdWords and Budgeted Allocation

Search engines sell ads one query at a time. Each advertiser has a daily budget and bids on the queries it cares about; each query must be shown to an advertiser the moment it arrives, without knowing which queries will come later today. Mehta, Saberi, Vazirani and Vazirani (2005; journal version 2007) formalized this as the AdWords problem and found an algorithm, now called MSVV, that earns at least 1−1e1 - \frac{1}{e} of the optimal revenue when bids are small compared with budgets, and showed that no online algorithm does better. This lesson presents the problem, shows how greedy loses half the revenue, and analyses MSVV with the primal-dual method of Buchbinder, Jain and Naor (2007).

The problem

There are advertisers i=1,…,ni = 1, \ldots, n with budgets BiB_i. Queries jj arrive online; on arrival, query jj reveals the bids bij≥0b_{ij} \ge 0 of all advertisers. The algorithm assigns jj to at most one advertiser ii, who pays min⁡(bij,remaining budget of i)\min(b_{ij}, \text{remaining budget of } i). The goal is to maximize the total payment. Its LP relaxation and dual are

max⁡∑i,jbij yijs.t. ∑jbij yij≤Bi ∀i,∑iyij≤1 ∀j,y≥0,min⁡∑iBi xi+∑jzjs.t. bij xi+zj≥bij ∀i,j,x,z≥0.\begin{aligned} &\max \sum_{i,j} b_{ij}\,y_{ij} &&\text{s.t.} \ \sum_j b_{ij}\,y_{ij} \le B_i \ \forall i, \quad \sum_i y_{ij} \le 1 \ \forall j, \quad y \ge 0, \\ &\min \sum_i B_i\,x_i + \sum_j z_j &&\text{s.t.} \ b_{ij}\,x_i + z_j \ge b_{ij} \ \forall i, j, \quad x, z \ge 0 . \end{aligned}

Write Rmax⁡=max⁡i,jbij/BiR_{\max} = \max_{i,j} b_{ij}/B_i for the largest bid-to-budget ratio. The small-bids assumption is Rmax⁡→0R_{\max} \to 0: each query is a tiny fraction of any budget, which is realistic for search ads.

Online bipartite matching is the special case with all bids and budgets equal to 1, so RANKING's 1−1/e1 - 1/e is the benchmark (RANKING and Its Primal-Dual Proof); with budgets but equal bids it is online bb-matching, where the deterministic algorithm BALANCE (give each query to the interested advertiser that has spent the smallest fraction of its budget) is (1−1/e)(1 - 1/e)-competitive as budgets grow (Kalyanasundaram and Pruhs, 2000).

Greedy loses half

Greedy gives each query to the highest bidder with enough budget left. It is 12\frac{1}{2}-competitive (when bids are small), and that is tight: two advertisers with budget 100, a first wave of 100 queries on which both bid (advertiser 1 bids slightly more), then 100 queries that only advertiser 1 wants. Greedy spends advertiser 1's budget on the first wave and has nobody for the second; OPT gives the first wave to advertiser 2. The same effect, repeated over nn advertisers in an upper-triangular pattern, also defeats tie-breaking games.

python · try it yourself
from math import exp

def allocate(budgets, queries, rule):
    spent = [0.0] * len(budgets)
    revenue = 0.0
    for bids in queries:                                   # bids: dict advertiser -> bid
        live = {i: b for i, b in bids.items() if spent[i] + b <= budgets[i] + 1e-9}
        if not live:
            continue
        if rule == "greedy":
            i = max(live, key=lambda i: (live[i], i))      # ties: higher index (the adversary's choice)
        elif rule == "balance":
            i = min(live, key=lambda i: (spent[i] / budgets[i], -i))
        else:                                              # msvv: bid times psi(fraction spent)
            i = max(live, key=lambda i: (live[i] * (1 - exp(spent[i] / budgets[i] - 1)), i))
        spent[i] += live[i]
        revenue += live[i]
    return revenue

# two advertisers, slightly higher bid by advertiser 0 on the first wave
queries = [{0: 1.0, 1: 0.99} for _ in range(100)] + [{0: 1.0} for _ in range(100)]
print("two waves:  OPT about 199;",
      ", ".join(f"{r} {allocate([100, 100], queries, r):.1f}" for r in ["greedy", "balance", "msvv"]))

# upper-triangular: n advertisers with budget B; wave j wants advertisers j..n-1, all bid 1
n, B = 30, 30
tri = [{i: 1.0 for i in range(j, n)} for j in range(n) for _ in range(B)]
print(f"triangle:   OPT {n * B};", ", ".join(f"{r} {allocate([B] * n, tri, r):.0f}" for r in ["greedy", "balance", "msvv"]),
      f"; (1 - 1/e) OPT = {(1 - exp(-1)) * n * B:.0f}")
two waves:  OPT about 199; greedy 100.0, balance 149.5, msvv 149.5
triangle:   OPT 900; greedy 450, balance 573, msvv 573 ; (1 - 1/e) OPT = 569

On the triangle, greedy (with adversarial ties) gets half; BALANCE and MSVV, which coincide when all bids are equal, get close to 1−1/e1 - 1/e of OPT, which is the best possible on this instance family for any online algorithm, deterministic or randomized (Mehta et al., 2007).

The MSVV algorithm

MSVV discounts each bid by how much of the advertiser's budget is already spent. With fif_i the fraction of BiB_i spent so far and

ψ(f)=1−ef−1,\psi(f) = 1 - e^{f - 1},

query jj goes to the advertiser maximizing bij ψ(fi)b_{ij}\,\psi(f_i). A fresh advertiser's bid counts at 1−1/e1 - 1/e of its value, an almost exhausted one's at nearly 0. Two extremes: with ψ≡1\psi \equiv 1 the rule is greedy, and with equal bids it is BALANCE.

Theorem (Mehta, Saberi, Vazirani and Vazirani, 2007). MSVV is (1−1/e)(1 - 1/e)-competitive as Rmax⁡→0R_{\max} \to 0, and no deterministic or randomized online algorithm is better than (1−1/e)(1 - 1/e)-competitive in that limit.

The primal-dual analysis

Buchbinder, Jain and Naor (2007) gave a version of MSVV that maintains a dual solution explicitly. Let c=(1+Rmax⁡)1/Rmax⁡c = (1 + R_{\max})^{1/R_{\max}}, which tends to ee as Rmax⁡→0R_{\max} \to 0. Each advertiser has a dual variable xix_i, starting at 0.

text
PRIMAL-DUAL-ADWORDS
0  x_i = 0 for every advertiser i
1  for each arriving query j
2      i = advertiser maximizing b_ij * (1 - x_i)          // only those with x_i < 1
3      if no such i: skip j
4      assign j to i ; z_j = b_ij * (1 - x_i)
5      x_i = x_i * (1 + b_ij / B_i) + b_ij / ((c - 1) * B_i)
Why it works

Three invariants, checked at every step:

  • Dual feasibility. After query jj, zj=max⁡ibij(1−xi)z_j = \max_i b_{ij}(1 - x_i), so bijxi+zj≥bijb_{ij}x_i + z_j \ge b_{ij} for every ii (the xix_i only grow afterwards).
  • Dual cost versus revenue. When jj goes to ii, the dual objective grows by Bi Δxi+zj=bijxi+bijc−1+bij(1−xi)=bij(1+1c−1)B_i\,\Delta x_i + z_j = b_{ij}x_i + \frac{b_{ij}}{c - 1} + b_{ij}(1 - x_i) = b_{ij}\bigl(1 + \frac{1}{c-1}\bigr), while the revenue grows by bijb_{ij}. So dual=cc−1⋅revenue\text{dual} = \frac{c}{c-1} \cdot \text{revenue} (ignoring budget overruns).
  • Budgets. Solving the recurrence for xix_i shows xi≥1c−1(c spenti/Bi−1)x_i \ge \frac{1}{c-1}\bigl(c^{\,\text{spent}_i / B_i} - 1\bigr), so xi≥1x_i \ge 1 once advertiser ii has spent its budget, and it is never chosen again. It overspends by at most one bid, a factor 1−Rmax⁡1 - R_{\max}.

By weak duality, OPT≤dual=cc−1 revenue\mathrm{OPT} \le \text{dual} = \frac{c}{c-1}\,\text{revenue}, so revenue ≥(1−1c)(1−Rmax⁡) OPT→(1−1e) OPT\ge (1 - \frac{1}{c})(1 - R_{\max})\,\mathrm{OPT} \to (1 - \frac{1}{e})\,\mathrm{OPT}.

The program runs the primal-dual algorithm on a seeded random instance with small bids, checks dual feasibility for every advertiser-query pair at the end, and compares revenue, dual and the guarantee.

python · try it yourself
import random

rng = random.Random(28)
n, m = 6, 600
budgets = [rng.randint(40, 80) for _ in range(n)]
queries = [{i: round(rng.uniform(0.2, 1.0), 2) for i in rng.sample(range(n), rng.randint(1, 3))} for _ in range(m)]
R = max(b / budgets[i] for q in queries for i, b in q.items())
c = (1 + R) ** (1 / R)

x, z, spent, revenue = [0.0] * n, [], [0.0] * n, 0.0
for bids in queries:
    live = {i: b for i, b in bids.items() if x[i] < 1}
    if not live:
        z.append(0.0)
        continue
    i = max(live, key=lambda i: (live[i] * (1 - x[i]), -i))
    b = live[i]
    z.append(b * (1 - x[i]))
    pay = min(b, budgets[i] - spent[i])
    spent[i] += pay
    revenue += pay
    x[i] = x[i] * (1 + b / budgets[i]) + b / ((c - 1) * budgets[i])

violations = sum(b * x[i] + z[j] < b - 1e-9 for j, q in enumerate(queries) for i, b in q.items())
dual = sum(B * xi for B, xi in zip(budgets, x)) + sum(z)
print(f"R_max = {R:.4f}, c = {c:.4f} (e = 2.7183)")
print(f"revenue {revenue:.2f}, dual {dual:.2f}, dual constraint violations {violations}")
print(f"revenue / dual = {revenue / dual:.4f} >= (1 - 1/c)(1 - R_max) = {(1 - 1 / c) * (1 - R):.4f}")
print(f"total budget {sum(budgets)}, every advertiser spent: {[round(s) for s in spent]}")
R_max = 0.0213, c = 2.6899 (e = 2.7183)
revenue 351.76, dual 562.65, dual constraint violations 0
revenue / dual = 0.6252 >= (1 - 1/c)(1 - R_max) = 0.6149
total budget 352, every advertiser spent: [47, 48, 74, 78, 51, 54]

Since OPT≤\mathrm{OPT} \le dual, the ratio revenue/dual is a certified lower bound on revenue/OPT, computed without solving the offline problem. The certificate is only as good as the guarantee: on this easy instance the revenue almost reaches the total budget (another upper bound on OPT), so the true ratio is close to 1, while the dual certifies 0.6250.625.

In C++

MSVV with the ψ\psi discount on a larger seeded instance (20 advertisers, 20000 queries with small bids), compared with greedy. Without an LP solver, the program uses the simple upper bound OPT≤min⁡(∑iBi,∑jmax⁡ibij)\mathrm{OPT} \le \min\bigl(\sum_i B_i, \sum_j \max_i b_{ij}\bigr), so the printed ratios are lower bounds on the true ones.

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

int main() {
    const int n = 20, m = 20000;
    std::mt19937 rng(29);
    std::vector<double> budget(n);
    for (auto& B : budget) B = 300 + rng() % 400;
    struct Bid { int i; double b; };
    std::vector<std::vector<Bid>> q(m);
    double best_sum = 0;
    for (auto& bids : q) {
        int k = 1 + rng() % 3;
        int hot = rng() % 4 == 0 ? 0 : 1;                       // advertisers 0..4 are wanted by many queries
        double mx = 0;
        for (int t = 0; t < k; ++t) {
            int i = hot && t == 0 ? rng() % 5 : rng() % n;
            double b = 0.1 + (rng() % 90) / 100.0;
            bids.push_back({i, b});
            mx = std::max(mx, b);
        }
        best_sum += mx;
    }
    double total_budget = 0;
    for (double B : budget) total_budget += B;
    double ub = std::min(total_budget, best_sum);
    for (int rule = 0; rule < 2; ++rule) {
        std::vector<double> spent(n, 0);
        double rev = 0;
        for (const auto& bids : q) {
            int best = -1;
            double score = -1;
            for (auto [i, b] : bids) {
                if (spent[i] + b > budget[i]) continue;
                double s = rule == 0 ? b : b * (1 - std::exp(spent[i] / budget[i] - 1));
                if (s > score) { score = s; best = i; }
            }
            if (best < 0) continue;
            for (auto [i, b] : bids) if (i == best) { spent[i] += b; rev += b; break; }
        }
        std::printf("%-6s revenue %.1f, at least %.3f of OPT (upper bound on OPT %.1f)\n",
                    rule == 0 ? "greedy" : "MSVV", rev, rev / ub, ub);
    }
}
greedy revenue 8879.1, at least 0.934 of OPT (upper bound on OPT 9510.0)
MSVV   revenue 9187.8, at least 0.966 of OPT (upper bound on OPT 9510.0)

Beyond the worst case

Real query streams are not adversarial. Under random order arrivals, greedy-like algorithms do better than 1−1/e1 - 1/e and a dual-based algorithm can be (1−ε)(1 - \varepsilon)-competitive when bids are small enough (Devanur and Hayes, 2009, learning the dual prices from a first sample of queries). With large bids the problem is harder: for a long time nothing better than greedy's 12\frac{1}{2} was known for general bids, until Huang, Zhang and Zhang (2020) gave an algorithm with ratio slightly above 12\frac{1}{2} (about 0.50160.5016).

Test yourself

What does MSVV maximize when assigning query jj?

  1. bijb_{ij}
  2. bij ψ(fi)b_{ij}\,\psi(f_i) with ψ(f)=1−ef−1\psi(f) = 1 - e^{f - 1}
  3. The remaining budget
  4. 1−fi1 - f_i

Under what assumption is MSVV (1−1/e)(1 - 1/e)-competitive?

  1. Equal budgets
  2. Small bids: max⁡bij/Bi→0\max b_{ij}/B_i \to 0
  3. Random arrival order
  4. Two advertisers

In the primal-dual algorithm, by how much does the dual objective grow when query jj is assigned with bid bb?

  1. bb
  2. b(1+1c−1)=cc−1 bb\bigl(1 + \frac{1}{c - 1}\bigr) = \frac{c}{c-1}\,b
  3. c bc\,b
  4. 00

Why does greedy (highest bid) achieve only about half of OPT in the worst case?

  1. It ignores bids
  2. It can exhaust a budget on queries that another advertiser could have taken, leaving later queries with no interested advertiser
  3. It is randomized
  4. It overspends budgets