Oblivious and Adaptive Adversaries
A deterministic online algorithm is predictable: whoever chooses the requests can simulate it and always knows its next move. Randomization takes that knowledge away, but only if the request sequence cannot react to the coin flips. How much the sequence may react is a modelling decision, and it changes the answers dramatically. This lesson defines the three standard adversaries, proves on two examples that the stronger ones cancel the benefit of randomness, and states the theorems of Ben-David, Borodin, Karp, Tardos and Wigderson (1990) that relate the three ratios.
Three adversaries
Think of competitive analysis as a game. The adversary produces the requests and also serves them; the online algorithm's ratio is measured against the adversary's cost.
| Adversary | Chooses knowing | Serves the sequence | Measure |
|---|---|---|---|
| oblivious | the algorithm and its distribution, nothing about the coin outcomes; writes the whole sequence in advance | optimally, offline | |
| adaptive online | the algorithm's answers so far | online: must answer itself before seeing | |
| adaptive offline | the algorithm's answers so far | optimally, after the sequence ends |
For adaptive adversaries the sequence is itself random (it depends on the coins through the answers), so both costs are random variables and the definition bounds the expected difference. Let , , and be the best ratios achievable for a given problem against each adversary, and by deterministic algorithms. Every deterministic algorithm is a randomized one that ignores its coins, and each adversary in the table is at least as strong as the one above it, so
Against a deterministic algorithm all three adversaries are the same: the oblivious adversary can compute the answers itself, so the distinction only matters for randomized algorithms.
Ski rental against an adaptive adversary
The randomized ski-rental strategy of Randomized Ski Rental picks a buying day at random and achieves expected ratio against an oblivious adversary. An adaptive adversary watches the skier: on the morning the skier buys, it ends the season. The skier then pays for a season of days, whose optimal cost is . Since for every , the coins no longer help.
from fractions import Fraction
def randomized_distribution(B):
"""Pr[buy on day i], i = 1..B: proportional to (1 - 1/B)^(B - i)."""
r = Fraction(B - 1, B)
w = [r ** (B - i) for i in range(1, B + 1)]
total = sum(w)
return [x / total for x in w]
B = 10
p = randomized_distribution(B)
# oblivious adversary: fixes the season length D in advance
def expected_cost(D):
return sum(pi * ((i - 1 + B) if D >= i else D) for i, pi in enumerate(p, start=1))
oblivious = max(expected_cost(D) / min(D, B) for D in range(1, 3 * B))
# adaptive adversary: sees the purchase and stops the season that day (D = J)
e_alg = sum(pi * (i - 1 + B) for i, pi in enumerate(p, start=1))
e_adv = sum(pi * i for i, pi in enumerate(p, start=1)) # OPT of a J-day season with J <= B
best_c = min(Fraction(i - 1 + B, i) for i in range(1, B + 1))
print(f"B = {B}")
print(f"oblivious adversary: worst E[ALG]/OPT = {float(oblivious):.4f}")
print(f"adaptive adversary : E[ALG] = {float(e_alg):.3f}, E[ADV] = {float(e_adv):.3f}, "
f"ratio of expectations {float(e_alg / e_adv):.4f}")
print(f"every outcome J has ALG/ADV >= {float(best_c):.4f} = 2 - 1/B")B = 10
oblivious adversary: worst E[ALG]/OPT = 1.5353
adaptive adversary : E[ALG] = 15.353, E[ADV] = 6.353, ratio of expectations 2.4166
every outcome J has ALG/ADV >= 1.9000 = 2 - 1/B
The adaptive adversary pays the offline optimum of the season it created, so it is adaptive offline here; but since the season simply ends, serving it online costs the same. Against this adversary the skier's ratio is at least the deterministic on every outcome of the coin, and the ratio of the expectations, about , is even worse than the deterministic break-even rule: randomizing only spread the purchase over days the adversary could exploit.
Paging against an adaptive adversary
In paging the effect is starker. The randomized marking algorithm (The Randomized Marking Algorithm) is -competitive against an oblivious adversary. An adaptive adversary with pages sees the cache after each eviction and always requests the one page that is not in it: every request is a miss, exactly as for a deterministic algorithm. The program runs both adversaries against marking with ; the oblivious one uses the cyclic sequence that is worst for LRU.
import random
def marking_run(k, n_pages, next_request, n, rng):
"""Serve n requests with the marking algorithm; next_request(cache, t) picks the request."""
cache, marked, misses, reqs = set(), set(), 0, []
for t in range(n):
p = next_request(cache, t)
reqs.append(p)
if p not in cache:
misses += 1
if len(cache) == k:
if marked == cache: # every cached page marked: new phase
marked = set()
victim = rng.choice(sorted(cache - marked))
cache.remove(victim)
cache.add(p)
marked.add(p)
return misses, reqs
def opt_misses(reqs, k):
cache, misses = set(), 0
for t, p in enumerate(reqs):
if p not in cache:
misses += 1
if len(cache) == k:
rest = reqs[t + 1:]
cache.remove(max(cache, key=lambda q: rest.index(q) if q in rest else len(rest)))
cache.add(p)
return misses
k, n = 6, 700
rng = random.Random(8)
cyclic = lambda cache, t: t % (k + 1)
missing = lambda cache, t: t if t < k else min(set(range(k + 1)) - cache)
for name, adv in [("oblivious, cyclic", cyclic), ("adaptive, missing page", missing)]:
alg, reqs = marking_run(k, k + 1, adv, n, rng)
opt = opt_misses(reqs, k)
print(f"{name:24} marking {alg:4} misses, OPT {opt:4}, ratio {alg / opt:.2f}")
H = sum(1 / i for i in range(1, k + 1))
print(f"k = {k}, 2H_k = {2 * H:.2f}")oblivious, cyclic marking 290 misses, OPT 122, ratio 2.38
adaptive, missing page marking 700 misses, OPT 122, ratio 5.74
k = 6, 2H_k = 4.90
Against the oblivious cyclic sequence marking is far below ; against the adaptive adversary the ratio is close to , the deterministic bound. This is not an artefact of marking: against an adaptive online adversary no randomized paging algorithm is better than -competitive (Raghavan and Snir, 1989), so for paging while .
How the ratios relate
Theorem (Ben-David, Borodin, Karp, Tardos and Wigderson, 1990). For request-answer games:
- If some randomized algorithm is -competitive against every adaptive offline adversary, then some deterministic algorithm is -competitive. Hence .
- If is -competitive against every adaptive online adversary and some algorithm is -competitive against every oblivious adversary, then is -competitive against every adaptive offline adversary.
- Consequently, if some randomized algorithm is -competitive against every adaptive online adversary, then some deterministic algorithm is -competitive: .
Proof idea for part 1
View the game as a tree: the adversary's moves are requests, the algorithm's moves are answers. Say a node is winning for the adversary if from there the adversary has a strategy that forces above every bound. If no deterministic algorithm is -competitive, the root is winning for the adversary: otherwise the algorithm could always move to a node that is not winning for the adversary, and that rule would be a -competitive deterministic algorithm. An adaptive offline adversary plays its winning strategy against the randomized algorithm, answer by answer; it wins on every outcome of the coins, so it also wins in expectation. The real proof has to handle infinite games and the additive constant, and it is non-constructive: it shows that a deterministic algorithm exists without saying how to find it.
Part 2 is proved by letting the adaptive offline adversary's cost be compared with an imagined adaptive online adversary that runs the -competitive oblivious algorithm; see Borodin and El-Yaniv (chapter 7) for the full argument.
So randomization can only help against the oblivious adversary, and against adaptive online adversaries it helps at most quadratically. That is why "randomized competitive ratio" without qualification means against an oblivious adversary, and why every randomized bound in this course says which adversary it assumes.
Which adversary is realistic?
- An oblivious adversary models inputs that do not depend on the algorithm's random choices: a program's memory accesses do not react to the cache's coin flips. This is the usual and usually reasonable model.
- An adaptive adversary models inputs that can observe the algorithm: an attacker timing cache misses, a competitor watching which ads were shown, a self-interested user of a load balancer. If the random choices leak, guarantees against oblivious adversaries say nothing.
Test yourself
Which adversary must serve each request itself before seeing the algorithm's answer to it?
- Oblivious
- Adaptive online
- Adaptive offline
- None of them
Why do the three adversaries coincide for deterministic algorithms?
- Deterministic algorithms have no adversaries
- An oblivious adversary can simulate a deterministic algorithm and compute all its answers in advance
- Deterministic algorithms are always optimal
- Only randomized algorithms have competitive ratios
A randomized paging algorithm is analysed against an adaptive online adversary. What is the best ratio it can have?
Some randomized algorithm is 3-competitive against adaptive online adversaries. What does the BBKTW theorem guarantee?
- A deterministic 3-competitive algorithm
- A deterministic 9-competitive algorithm
- A deterministic 6-competitive algorithm
- Nothing about deterministic algorithms