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 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 with budgets . Queries arrive online; on arrival, query reveals the bids of all advertisers. The algorithm assigns to at most one advertiser , who pays . The goal is to maximize the total payment. Its LP relaxation and dual are
Write for the largest bid-to-budget ratio. The small-bids assumption is : 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 is the benchmark (RANKING and Its Primal-Dual Proof); with budgets but equal bids it is online -matching, where the deterministic algorithm BALANCE (give each query to the interested advertiser that has spent the smallest fraction of its budget) is -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 -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 advertisers in an upper-triangular pattern, also defeats tie-breaking games.
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 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 the fraction of spent so far and
query goes to the advertiser maximizing . A fresh advertiser's bid counts at of its value, an almost exhausted one's at nearly 0. Two extremes: with the rule is greedy, and with equal bids it is BALANCE.
Theorem (Mehta, Saberi, Vazirani and Vazirani, 2007). MSVV is -competitive as , and no deterministic or randomized online algorithm is better than -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 , which tends to as . Each advertiser has a dual variable , starting at 0.
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 , , so for every (the only grow afterwards).
- Dual cost versus revenue. When goes to , the dual objective grows by , while the revenue grows by . So (ignoring budget overruns).
- Budgets. Solving the recurrence for shows , so once advertiser has spent its budget, and it is never chosen again. It overspends by at most one bid, a factor .
By weak duality, , so revenue .
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.
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 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 .
In C++
MSVV with the 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 , so the printed ratios are lower bounds on the true ones.
#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 and a dual-based algorithm can be -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 was known for general bids, until Huang, Zhang and Zhang (2020) gave an algorithm with ratio slightly above (about ).
Test yourself
What does MSVV maximize when assigning query ?
- with
- The remaining budget
Under what assumption is MSVV -competitive?
- Equal budgets
- Small bids:
- Random arrival order
- Two advertisers
In the primal-dual algorithm, by how much does the dual objective grow when query is assigned with bid ?
Why does greedy (highest bid) achieve only about half of OPT in the worst case?
- It ignores bids
- It can exhaust a budget on queries that another advertiser could have taken, leaving later queries with no interested advertiser
- It is randomized
- It overspends budgets