Main

Lecture 4.1: Design Methods I

Catalogue of randomized-design methods; the first beats the worst case by randomizing the strategy. Paging k-hard; two-job scheduling 9/8-hard deterministically but →1 randomized; Yao's principle.

Last updated 27 June 2026

Lecture 4 (part 1) — Eliminating the Adversary

Course 2-INF-135/15 Pravdepodobnostné algoritmy, LS 2025/26. Source slides: 04_metody1.pdf (12 pages) + start of 04_metody23.pdf (slides 1–4).

Where we are: the methods block

Lectures 1–3 built individual algorithms and then the complexity-class scaffolding (RP, BPP, ZPP, …). Now begins the methods block — the recurring tricks that keep producing randomized algorithms. The whole catalogue, in one breath:

Method (Slovak) Idea Examples
Eliminating the adversary (eliminácia protihráča) Randomly choose the strategy/element that drives the computation, so the worst case becomes unlikely → focus on expected cost. DB equality, QuickSort, universal hashing, online problems
Fingerprints (odtlačky) Replace comparing complex objects by comparing simpler fingerprints. databases, hashing, Freivalds
Error reduction by repetition (znižovanie chyby) Amplify confidence — but sometimes it is smarter not to repeat the whole computation. (recurring throughout)
Witnesses (svedkovia) Randomly hunt for a witness that confirms/refutes a property. primality — a divisor witnesses compositeness
Optimization + random rounding Relax a discrete problem (Z,N\mathbb{Z}, \mathbb{N}) into R\mathbb{R}, solve efficiently there, round back. LP-rounding
Random walks (náhodné prechádzky) Find an object with a property by walking randomly through the object space. 2-SAT walk, s-t connectivity

This part of the lecture is entirely the first method, applied to its most striking arena: online problems.


1. The core idea — eliminating the adversary

By randomly choosing the strategy (or the element) that steers a computation, the worst case stops being a fixed target the adversary can aim at. We trade worst-case guarantees for expected-case ones — and that trade is often a landslide win.

The mental model is a game: you (the algorithm) move, an adversary picks the input to hurt you most. A deterministic algorithm is fully predictable, so the adversary can compute your single worst input in advance and hand it to you. A randomized algorithm makes a coin-flip the adversary cannot see — so it cannot tune one input to be bad for all your possible coin outcomes. The adversary is eliminated not by being beaten head-on, but by being denied a fixed target.

Online problems — the natural battlefield

In an online problem the input is not given all at once. It dribbles in:

x1,x2,x3,x_1, x_2, x_3, \dots

and you must irrevocably react to xtx_t knowing only x1,,xtx_1, \dots, x_t — the past, never the future. The functions f1(x1),f2(x1,x2),f_1(x_1), f_2(x_1,x_2), \dots model “your decision so far.” This is exactly where adversaries thrive: they choose the next input after watching what you just did.

Competitive ratio — grading an online algorithm

We compare an online algorithm AA to the optimal offline solution OPT(x)\mathrm{OPT}(x) — the cost an all-knowing solver, who sees the whole input in advance, would pay. The competitive ratio on input xx is the worse of the two directions:

compA(x)=max{A(x)OPT(x), OPT(x)A(x)}  1.\mathrm{comp}_A(x) = \max\left\{\frac{A(x)}{\mathrm{OPT}(x)},\ \frac{\mathrm{OPT}(x)}{A(x)}\right\} \ \ge\ 1.

δ\delta-hard problem (δ-ťažký): a problem with no dd-competitive algorithm for any d<δd < \delta. In words, δ\delta is a barrier — a proven floor on how good ANY online algorithm can be. Proving δ\delta-hardness means: for every algorithm there is an input that drags its ratio up to δ\delta. That “for every AA, there exists a bad II” is the adversary talking.

Two flavours of “probabilistic approximation”

For a randomized online algorithm AA we ask it to always output a feasible solution, and grade the quality either in expectation or with a guarantee:

Both demand a correct (feasible) answer with probability 1; only the quality is random. This is the online analogue of a Las Vegas algorithm.


2. Warm-up: Paging is kk-hard

The problem

A cache (cash) of size kk holds pages; everything else lives in main memory. Page requests arrive online. If a requested page is not in the cache, that is a fault (chyba) — you must bring it in and evict one of the kk residents. We count the number of swaps (faults).

Claim. Paging is kk-hard: no deterministic online algorithm is better than kk-competitive.

The adversary’s construction

There are k+1k+1 pages in play but only kk cache slots — so at every moment exactly one page is missing. The adversary’s rule is brutally simple:

Always request the page the algorithm just evicted (equivalently, the one page currently not in cache).

Then the online algorithm faults on every single request. Walk it through with k=4k=4, pages {1,2,3,4}\{1,2,3,4\} in cache and page 55 entering:

request online cache (faults every step) optimal offline (1 fault per block)
start {1,2,3,4} {1,2,3,4}
5 fault → evict 3 → {1,2,5,4} fault → evict 4 → {1,2,3,5}
3 fault → evict 1 → {3,2,5,4} hit ✓
1 fault → evict 2 → {3,1,5,4} hit ✓
2 fault → evict … → {3,1,5,?} hit ✓

The online algorithm pays k=4k = 4 faults; the optimal offline algorithm, seeing the future, evicts page 44 first (it is the one page never requested again in this block), and so pays only 1 fault. Hence

comp(x)=k1=k.\mathrm{comp}(x) = \frac{k}{1} = k.

The deep point

The offline optimum is Bélády’s rule: evict the page whose next use is furthest in the future. The online algorithm cannot run it because it does not know the future — and the adversary weaponizes exactly that ignorance. The gap is fundamentally about information, not cleverness.

Punchline. Determinism online is hopeless here: a kk-fold gap, and kk is the whole cache. (Randomization — the “marking algorithm” — later cuts this to O(logk)O(\log k); that is the payoff of eliminating the adversary, though it is beyond this slide.)


3. The main event: two-job scheduling

This example is the heart of the lecture, because it shows the whole arc: a deterministic lower bound, a deterministic upper bound, a hardness theorem, and then a randomized algorithm that smashes through the deterministic barrier.

Setup

Two jobs collide when they want the same machine at the same time — one must wait. A machine serves one job at a time.

The schedule as a lattice path

Picture an m×mm \times m grid. Being at cell (i,j)(i,j) means “α\alpha has finished ii steps, β\beta has finished jj steps.” A schedule SS is a monotone path from (0,0)(0,0) to (m,m)(m,m) built from three moves:

A collision cell is one where α(i+1)=β(j+1)\alpha(i{+}1) = \beta(j{+}1): there you cannot go diagonal, you must detour \rightarrow then \downarrow (two ticks instead of one).

Cost. The time is the number of ticks. With gg diagonal steps you spend time=2mg=m+(mg)\mathrm{time} = 2m - g = m + (m - g). Writing del(S)=mg\mathrm{del}(S) = m - g for the number of collisions your path was forced through,

 time(S)=m+del(S). \boxed{\ \mathrm{time}(S) = m + \mathrm{del}(S).\ }
The ideal is mm (all diagonal, perfect parallelism); every unavoidable collision adds 1.

Crucial counting fact. Each machine vv appears once in α\alpha and once in β\beta, so there is exactly one collision cell per machine — mm collision cells total, and each one lies on a single grid diagonal. Remember this; it powers the upper bound.

3a. Deterministic lower bound: timem+m8\mathrm{time} \ge m + \tfrac{m}{8}

A I: time(A(I))m+m8.\forall A\ \exists I:\ \mathrm{time}(A(I)) \ge m + \tfrac{m}{8}.

The adversary builds β\beta online, reacting to the algorithm’s moves, to force a collision every other step. Sketch of the rule (collisions (i,j)(i,j) with i,jm/2i,j \le m/2):

Whichever way AA dodges, the adversary plants the next collision right in its path. Over the first m/2\sim m/2 steps each forces an expected delay of 12\sim \tfrac12, and only on 12\sim \tfrac12 of the grid does the trap apply, giving a guaranteed delay

del  m21212 = m8.\mathrm{del} \ \ge\ \frac{m}{2}\cdot\frac12\cdot\frac12 \ =\ \frac{m}{8}.

So every deterministic online algorithm can be pushed to timem+m/8\mathrm{time} \ge m + m/8.

3b. Deterministic upper bound: OPTm+m\mathrm{OPT} \le m + \sqrt{m}

Now the offline side — how good is the best schedule (it may inspect all of β\beta)? Consider a family of shifted-diagonal strategies AjA_j, for j{m,,m}j \in \{-\sqrt m, \dots, \sqrt m\} (2m+12\sqrt m + 1 of them):

Aj={(j)(mj)(j),j0,(j)(mj)(j),j>0.A_j = \begin{cases} (\rightarrow^{|j|})(\searrow^{\,m-|j|})(\downarrow^{|j|}), & j \le 0,\\[2pt] (\downarrow^{\,j})(\searrow^{\,m-j})(\rightarrow^{\,j}), & j > 0.\end{cases}

A0A_0 is the pure main diagonal; AjA_j slides the diagonal sideways by jj to dodge collisions sitting on the main diagonal. Its cost splits into a fixed offset j|j| (stepping off the diagonal and back) plus the collisions still on its shifted track:

time(Aj)=m+j+del(Aj).\mathrm{time}(A_j) = m + |j| + \mathrm{del}(A_j).

The averaging argument (this is the elegant part). Each of the mm collision cells lies on one diagonal, so it can burden at most one strategy AjA_j. Therefore

j=mmdel(Aj)  m.\sum_{j=-\sqrt m}^{\sqrt m} \mathrm{del}(A_j) \ \le\ m.

Add up the total excess over the ideal mm across all strategies:

j=mm(j+del(Aj))  (m+m)j+mdel = 2m+m.\sum_{j=-\sqrt m}^{\sqrt m} \big(|j| + \mathrm{del}(A_j)\big) \ \le\ \underbrace{(m+\sqrt m)}_{\sum |j|} + \underbrace{m}_{\sum \mathrm{del}} \ =\ 2m + \sqrt m.

So the average excess over the 2m+12\sqrt m + 1 strategies is

2m+m2m+1=m(2m+1)2m+1=m.\frac{2m + \sqrt m}{2\sqrt m + 1} = \frac{\sqrt m\,(2\sqrt m + 1)}{2\sqrt m + 1} = \sqrt m.

Since some strategy is no worse than the average, there exists one with timem+m\mathrm{time} \le m + \sqrt m. The offline optimum can only be better:

 OPT(x)m+m. \boxed{\ \mathrm{OPT}(x) \le m + \sqrt m.\ }

The averaging trick (“there exists one no worse than the average”) is the probabilistic method in miniature — and it is the bridge to the randomized algorithm below.

3c. Hardness: the problem is (9/8ε)(9/8 - \varepsilon)-hard

Combine the two bounds. The lower bound gives a bad instance with costA(I)m+m/8=98m\mathrm{cost}_A(I) \ge m + m/8 = \tfrac98 m; the upper bound gives OPTm+m\mathrm{OPT} \le m + \sqrt m. Hence

compA(I)=costA(I)OPT(I)98mm+m=9811+1/m=98(11m+1)m98.\mathrm{comp}_A(I) = \frac{\mathrm{cost}_A(I)}{\mathrm{OPT}(I)} \ge \frac{\tfrac98 m}{m + \sqrt m} = \frac{9}{8}\cdot\frac{1}{1 + 1/\sqrt m} = \frac98\left(1 - \frac{1}{\sqrt m + 1}\right) \xrightarrow{m\to\infty} \frac98.

No deterministic online algorithm beats 9/8ε9/8 - \varepsilon. A hard, fixed floor — because a deterministic algorithm is a fixed target.

3d. The randomized algorithm DIAG — through the barrier

DIAG: pick iR{m,,m}i \in_R \{-\sqrt m, \dots, \sqrt m\} uniformly, then run strategy AiA_i.

That is the entire algorithm: a random diagonal. The analysis is just the averaging argument re-read as an expectation. Because the mm collisions are spread over the 2m+12\sqrt m + 1 strategies and the offsets average out,

E[#delays of Ai]  m+12,E[time]  m+m+12.E[\#\text{delays of } A_i] \ \le\ \lceil \sqrt m\rceil + \tfrac12, \qquad E[\mathrm{time}] \ \le\ m + \lceil \sqrt m\rceil + \tfrac12.

Using the trivial OPTm\mathrm{OPT} \ge m,

E[compDIAG]  m+m+12m = 1+1m+12m m 1.E[\mathrm{comp}_{\mathrm{DIAG}}] \ \le\ \frac{m + \lceil\sqrt m\rceil + \tfrac12}{m} \ =\ 1 + \frac{1}{\sqrt m} + \frac{1}{2m} \ \xrightarrow{m\to\infty}\ 1.

Punchline. Deterministic is stuck at 9/8\ge 9/8; randomized DIAG drives the expected ratio all the way to 11. Same strategies, the only change is choosing which diagonal at random. The adversary built its trap assuming it knew your diagonal — randomizing the diagonal eliminates that knowledge, and the forced delay collapses from m/8m/8 down to m\sqrt m. This is “eliminating the adversary” in its purest form.


4. The selection problem and Yao’s minimax principle

The problem (the secretary problem in disguise)

Values v1,,vnv_1, \dots, v_n arrive online (in random order). When you see a value you must immediately decide to take it or pass forever; you want to end up holding the maximum. How well can you do?

Result 1 — determinism is worthless: no deterministic algorithm beats 00-competitive. Suppose some deterministic AA were α\alpha-competitive, α>0\alpha > 0 (i.e. E[v(A)]αmaxiviE[v(A)] \ge \alpha \max_i v_i). The adversary watches AA’s reaction to seeing v1=1v_1 = 1 first:

A deterministic algorithm’s decision on seeing “11” is fixed, so the adversary picks whichever input punishes that fixed decision. Determinism loses completely.

Result 2 — a trivial randomized algorithm is 1n\tfrac1n-competitive. Pick a position xR{1,,n}x \in_R \{1,\dots,n\} and output vxv_x. You hit the maximum with probability exactly 1n\tfrac1n. Can we do better? — This is the question Yao answers.

Yao’s minimax principle

Yao (for minimization). Let AA be a random variable over deterministic algorithms A\mathcal A, and XX a random variable over inputs X\mathcal X. Then

maxxXE[c(A,x)]  minaAE[c(a,X)].\max_{x\in\mathcal X} E[c(A,x)] \ \ge\ \min_{a\in\mathcal A} E[c(a,X)].

Read it as a sentence: the best randomized algorithm’s cost on its worst input is at least the best deterministic algorithm’s cost against a (well-chosen) random input. This is the workhorse for lower bounds on randomized algorithms, and it makes them easy: instead of reasoning about all possible coin-flip distributions, you just

  1. invent one input distribution XX (your choice — make it nasty), and
  2. show every deterministic algorithm is expensive on average against it.

That number is then a valid lower bound for every randomized algorithm.

Proof — two one-line inequalities. With E[c(A,x)]=aPr[A=a]c(a,x)E[c(A,x)] = \sum_a \Pr[A{=}a]\,c(a,x) and E[c(a,X)]=xPr[X=x]c(a,x)E[c(a,X)] = \sum_x \Pr[X{=}x]\,c(a,x):

$$ \begin{aligned} \max_{x} E[c(A,x)] &= \max_{x} \sum_{a} \Pr[A{=}a],c(a,x) \ &\ge \sum_{x} \Pr[X{=}x] \sum_{a} \Pr[A{=}a],c(a,x) && \text{(a max is \ge any weighted average)}\ &= \sum_{a} \Pr[A{=}a] \sum_{x} \Pr[X{=}x],c(a,x) && \text{(swap the sums)}\ &\ge \min_{a} \sum_{x} \Pr[X{=}x],c(a,x) && \text{(a weighted average is \ge the min)}\ &= \min_{a} E[c(a,X)]. \end{aligned} $$

The two steps are just “max \ge average” and “average \ge min.” That is all Yao is — yet it is exactly von Neumann’s minimax / LP duality for the zero-sum game you vs. the input. For a maximization problem, set c=pc = -p to flip it:

minxE[p(A,x)]  maxaE[p(a,X)].\min_{x} E[p(A,x)] \ \le\ \max_{a} E[p(a,X)].

Applying Yao: you cannot select the maximum with probability >1n> \tfrac1n

Work over values {0,1,,n}\{0,1,\dots,n\}. Let X\mathcal X = all permutations, A\mathcal A = deterministic online algorithms,

We want minxE[p(A,x)]1n\min_x E[p(A,x)] \le \tfrac1n; by Yao (max-form) it suffices to exhibit a distribution XX with maxaE[p(a,X)]=1n\max_a E[p(a,X)] = \tfrac1n. The hard distribution:

x(t):=(1,2,,t,0,,0),TR{1,,n},X=x(T).x(t) := (1, 2, \dots, t, 0, \dots, 0), \qquad T \in_R \{1,\dots,n\},\qquad X = x(T).

A random-length increasing prefix, then zeros. Take any deterministic aa and let s:=s(a,x(n))s := s(a, x(n)) be where it selects on the full increasing input x(n)=(1,,n)x(n) = (1,\dots,n). For a prefix tnt \ne n:

Either way, p(a,x(t))=1    s=tp(a, x(t)) = 1 \iff s = t. Therefore

E[p(a,X)]=Pr[T=s]=1nfor every deterministic a.E[p(a,X)] = \Pr[T = s] = \frac1n \quad \text{for \emph{every} deterministic } a.

So maxaE[p(a,X)]=1n\max_a E[p(a,X)] = \tfrac1n, and Yao gives minxE[p(A,x)]1n\min_x E[p(A,x)] \le \tfrac1n for every randomized AA. Combined with Result 2 (which achieves 1n\tfrac1n), 1n\tfrac1n is tight.

Why the increasing prefix is diabolical. When the algorithm stands at position ii seeing value ii, it cannot tell whether the sequence stops here (t=it=i, so this is the max) or keeps climbing (t>it>i, more is coming). Every position looks identically tempting — that ambiguity is precisely what caps it at 1/n1/n.

(Oral-exam nuance.) This is the select-the-exact-maximum version. The classic secretary problem reaches 1/e\approx 1/e — but that relies on a uniformly random arrival order and rank comparisons; Yao’s adversary is allowed to choose this particular non-uniform monotone distribution, against which no strategy beats 1/n1/n.

Competitive version: no randomized algorithm is α\alpha-competitive for α>1n+ε\alpha > \tfrac1n + \varepsilon

The “probability of hitting the max” bound upgrades to a competitive-ratio bound by scaling the values exponentially so that missing the max is nearly worthless.

Suppose AA is (1n+ε)(\tfrac1n + \varepsilon)-competitive, ε>0\varepsilon > 0, and feed it vi=Mxiv_i = M^{x_i} with xi{0,,n}x_i \in \{0,\dots,n\} and M1M \gg 1. Let v=maxiviv^* = \max_i v_i. If AA misses the top, the best it can hold is the runner-up, v/M\le v^*/M. So

E[v(A)]  vPr[A selects v]+vM.E[v(A)] \ \le\ v^*\cdot\Pr[A \text{ selects } v^*] + \frac{v^*}{M}.

Competitiveness says E[v(A)](1n+ε)vE[v(A)] \ge (\tfrac1n + \varepsilon)\,v^*. Divide by vv^*:

1n+ε  Pr[A selects v]+1MPr[A selects v]  1n+ε1M.\tfrac1n + \varepsilon \ \le\ \Pr[A \text{ selects } v^*] + \tfrac1M \quad\Longrightarrow\quad \Pr[A \text{ selects } v^*] \ \ge\ \tfrac1n + \varepsilon - \tfrac1M.

Choosing M=2/εM = 2/\varepsilon gives Pr[A selects v]1n+ε2>1n\Pr[A \text{ selects } v^*] \ge \tfrac1n + \tfrac\varepsilon2 > \tfrac1n — contradicting the theorem above. Hence:

No randomized online algorithm for selection is α\alpha-competitive for α>1n+ε\alpha > \tfrac1n + \varepsilon. The trivial “pick a random position” algorithm is essentially optimal.

Punchline of Yao. A lower bound over all randomized algorithms — a quantifier over infinitely many coin-flip distributions — collapses into analyzing deterministic algorithms against one input distribution you design. Hard becomes easy because the design freedom moves to your side of the game.


Recurring themes from this part

Theme Where it appeared
Eliminate the adversary = deny it a fixed target by randomizing your strategy DIAG (§3d), random-position selection (§4)
Online ⇒ the cost of not knowing the future paging vs. Bélády (§2), scheduling lower bound (§3a)
Averaging / probabilistic method (“\exists one no worse than the average”) scheduling upper bound (§3b) → DIAG (§3d)
Deterministic barrier, randomized breakthrough (9/8 → 1) scheduling (§3)
Yao’s minimax (randomized lower bound ⇐ deterministic-vs-random-input) selection (§4)
Exponential value-scaling to turn “hit the max” into a competitive ratio selection competitive bound (§4)

The one sentence tying it together:

A deterministic online algorithm is a fixed target the adversary aims at; a coin flip the adversary cannot see turns the worst case into a merely unlikely case — and Yao’s principle tells us exactly how far that trick can ever go.

online-algorithmscompetitive-analysisYao-principleadversarylower-bounds

← Back to Randomized Algorithms