TExcelTAD
Home › Online Algorithms & Competitive Analysis › Oblivious and Adaptive Adversaries

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 σt\sigma_t knowing Serves the sequence Measure
oblivious the algorithm and its distribution, nothing about the coin outcomes; writes the whole sequence in advance optimally, offline E[ALG(σ)]≤c OPT(σ)+α\mathbb{E}[\mathrm{ALG}(\sigma)] \le c\,\mathrm{OPT}(\sigma) + \alpha
adaptive online the algorithm's answers a1,…,at−1a_1, \ldots, a_{t-1} so far online: must answer σt\sigma_t itself before seeing ata_t E[ALG−c ADV]≤α\mathbb{E}[\mathrm{ALG} - c\,\mathrm{ADV}] \le \alpha
adaptive offline the algorithm's answers so far optimally, after the sequence ends E[ALG−c OPT(σ)]≤α\mathbb{E}[\mathrm{ALG} - c\,\mathrm{OPT}(\sigma)] \le \alpha

For adaptive adversaries the sequence σ\sigma 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 CoblC_{\text{obl}}, Cad-onC_{\text{ad-on}}, Cad-offC_{\text{ad-off}} and CdetC_{\text{det}} 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

Cobl≤Cad-on≤Cad-off≤Cdet.C_{\text{obl}} \le C_{\text{ad-on}} \le C_{\text{ad-off}} \le C_{\text{det}} .

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 J∈{1,…,B}J \in \{1, \ldots, B\} at random and achieves expected ratio 1/(1−(1−1/B)B)1/(1 - (1 - 1/B)^B) against an oblivious adversary. An adaptive adversary watches the skier: on the morning the skier buys, it ends the season. The skier then pays (J−1)+B(J - 1) + B for a season of JJ days, whose optimal cost is min⁡(J,B)=J\min(J, B) = J. Since (J−1+B)/J≥2−1/B(J - 1 + B)/J \ge 2 - 1/B for every J≤BJ \le B, the coins no longer help.

python · try it yourself
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 2−1/B2 - 1/B on every outcome of the coin, and the ratio of the expectations, about 2.422.42, 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 2Hk2H_k-competitive against an oblivious adversary. An adaptive adversary with k+1k + 1 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 k=6k = 6; the oblivious one uses the cyclic sequence that is worst for LRU.

python · try it yourself
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 kk; against the adaptive adversary the ratio is close to k=6k = 6, the deterministic bound. This is not an artefact of marking: against an adaptive online adversary no randomized paging algorithm is better than kk-competitive (Raghavan and Snir, 1989), so for paging Cad-on=Cdet=kC_{\text{ad-on}} = C_{\text{det}} = k while Cobl=HkC_{\text{obl}} = H_k.

How the ratios relate

Theorem (Ben-David, Borodin, Karp, Tardos and Wigderson, 1990). For request-answer games:

  1. If some randomized algorithm is cc-competitive against every adaptive offline adversary, then some deterministic algorithm is cc-competitive. Hence Cad-off=CdetC_{\text{ad-off}} = C_{\text{det}}.
  2. If GG is cc-competitive against every adaptive online adversary and some algorithm is dd-competitive against every oblivious adversary, then GG is cdcd-competitive against every adaptive offline adversary.
  3. Consequently, if some randomized algorithm is cc-competitive against every adaptive online adversary, then some deterministic algorithm is c2c^2-competitive: Cdet≤Cad-on2C_{\text{det}} \le C_{\text{ad-on}}^2.
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 ALG−c OPT\mathrm{ALG} - c\,\mathrm{OPT} above every bound. If no deterministic algorithm is cc-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 cc-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 dd-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?

  1. Oblivious
  2. Adaptive online
  3. Adaptive offline
  4. None of them

Why do the three adversaries coincide for deterministic algorithms?

  1. Deterministic algorithms have no adversaries
  2. An oblivious adversary can simulate a deterministic algorithm and compute all its answers in advance
  3. Deterministic algorithms are always optimal
  4. 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?

  1. HkH_k
  2. 2Hk2H_k
  3. kk
  4. k\sqrt{k}

Some randomized algorithm is 3-competitive against adaptive online adversaries. What does the BBKTW theorem guarantee?

  1. A deterministic 3-competitive algorithm
  2. A deterministic 9-competitive algorithm
  3. A deterministic 6-competitive algorithm
  4. Nothing about deterministic algorithms