Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

Online Algorithms & Competitive Analysis

Ski rental, paging and move-to-front: deciding without knowing the future.

Should you rent skis or buy them, when you do not know how many days you will ski? Should a cache evict the page used least recently, or the oldest one? An online algorithm must decide now, without seeing the future, and competitive analysis measures how much that ignorance costs: against an opponent who sees everything, the best strategy pays at most twice the optimum for skis, and kk times for a cache of kk pages.

What you’ll learn

  • Define online algorithms, the competitive ratio, and oblivious adversaries.
  • Prove that the break-even strategy for ski rental is (2−1/B)(2-1/B)-competitive and optimal among deterministic algorithms, and explain how randomization reaches e/(e−1)e/(e-1).
  • Prove that Belady's rule is optimal offline, that LRU is kk-competitive, and that no deterministic paging algorithm does better.
  • Prove that move-to-front is 2-competitive with an inversion potential, and describe the k-server problem and learning-augmented algorithms.

Deciding without the future

The ratio is a worst case over all request sequences, so think of an adversary who knows your algorithm and writes the sequence that hurts it most. For randomized algorithms the usual opponent is the oblivious adversary, who knows the algorithm but not its coin flips and must fix σ\sigma in advance; the guarantee is then on the expected cost, E[ALG(σ)]≤c⋅OPT(σ)+α\E[\mathrm{ALG}(\sigma)]\le c\cdot\mathrm{OPT}(\sigma)+\alpha.

Ski rental is the simplest case. Renting costs 1 per day and buying costs BB, an integer. You learn each morning whether you will ski that day, and the season ends without warning. If the season lasts dd days, the offline optimum is min⁡(d,B)\min(d,B).

DerivationProof
  1. (B−1)+BB = 2−1B\frac{(B-1)+B}{B}\ =\ 2-\frac1B

    Upper bound. If the season ends before day BB, break-even only rented and paid d=OPTd=\mathrm{OPT}. Otherwise it paid B−1B-1 in rent plus BB, while OPT=B\mathrm{OPT}=B:

  2. t−1+Bmin⁡(t,B) = {1+B−1t ≥ 2−1Bt≤Bt−1+BB ≥ 2t>B\frac{t-1+B}{\min(t,B)}\ =\ \begin{cases}1+\dfrac{B-1}{t}\ \ge\ 2-\dfrac1B & t\le B\\[8pt] \dfrac{t-1+B}{B}\ \ge\ 2 & t>B\end{cases}

    Lower bound. A deterministic algorithm is fully described by the day tt on which it would buy. The adversary ends the season right after that purchase, so the algorithm pays (t−1)+B(t-1)+B for tt days of skiing:

  3. min⁡t max⁡d ALGt(d)OPT(d) = 2−1B\min_t\ \max_d\ \frac{\mathrm{ALG}_t(d)}{\mathrm{OPT}(d)}\ =\ 2-\frac1B

    An algorithm that never buys is not competitive at all: a season of dd days costs it dd against BB. So t=Bt=B is optimal, with ratio exactly 2−1/B2-1/B.

Predict before you look

Skis cost 10 days of rent. What competitive ratio does the break-even strategy guarantee?

Show the answer

1.9

2−1/10=1.92-1/10=1.9: in the worst case you rent for 9 days, buy on day 10, and never ski again, paying 19 where buying on day 1 would have cost 10. The same logic, pay for the cheap option until it has cost as much as the expensive one, guides spin-then-block locks and TCP acknowledgment delays.

Randomness and predictions

Against an oblivious adversary a randomized algorithm can do better, because the adversary cannot end the season "right after the purchase" if it does not know when that is. Choose the purchase day at random, with probabilities rising geometrically toward day BB. Karlin, Manasse, McGeoch and Owicki (1990, journal 1994) showed that the best such distribution achieves

ee−1≈1.582\frac{e}{e-1}\approx1.582

as BB grows. For each finite BB, solving the linear program of the best distribution gives exactly 1/(1−(1−1/B)B)1/\big(1-(1-1/B)^B\big): 1.333 for B=2B=2, 1.535 for B=10B=10, rising toward e/(e−1)e/(e-1).

Competitive analysis is pessimistic by design. Learning-augmented algorithms add a prediction, say of the season's length, and ask for two guarantees at once: near-optimal cost when the prediction is right (consistency) and a bounded ratio whatever it says (robustness). Purohit, Svitkina and Kumar (2018) gave a deterministic ski-rental algorithm with a trust parameter λ∈(0,1)\lambda\in(0,1): if the prediction says the season will be long, buy early, on day ⌈λB⌉\lceil\lambda B\rceil; if it says short, buy late, on day ⌈B/λ⌉\lceil B/\lambda\rceil. It is (1+λ)(1+\lambda)-consistent and (1+1/λ)(1+1/\lambda)-robust, interpolating between trusting the prediction and ignoring it.

Think first With λ=0.5\lambda=0.5 and a prediction that the season will be long, the algorithm buys on day ⌈B/2⌉\lceil B/2\rceil. What happens if the prediction was wrong and the season ends that very day?

You paid about B/2−1B/2-1 in rent plus BB for skis, roughly 1.5B1.5B, against an optimum of B/2B/2: a ratio near 3=1+1/λ3=1+1/\lambda. That is the robustness guarantee at work. Had the prediction been right, you would have paid at most 1.5B1.5B against BB: the consistency 1+λ=1.51+\lambda=1.5, better than the 2−1/B2-1/B of break-even.

Paging: the furthest future, and LRU

In paging, a fast cache holds kk pages; a request for a page in the cache is free, and a request for any other page is a fault that costs 1 and must evict some page if the cache is full. The offline problem has a clean answer, found by Belady (1966) for virtual memory.

DerivationProof for LRU, by phases
  1. LRU(σ) ≤ k⋅#phases\mathrm{LRU}(\sigma)\ \le\ k\cdot\#\text{phases}

    Cut σ\sigma into phases: each phase is a maximal block of requests to at most kk distinct pages, and the next phase starts at the request for a (k+1)(k+1)-st distinct page. Inside a phase, a page that LRU loads stays among the kk most recently used until the phase ends, so LRU faults at most once per distinct page:

  2. OPT(σ) ≥ #phases−1⟹LRU(σ) ≤ k⋅OPT(σ)+k\mathrm{OPT}(\sigma)\ \ge\ \#\text{phases}-1\qquad\Longrightarrow\qquad\mathrm{LRU}(\sigma)\ \le\ k\cdot\mathrm{OPT}(\sigma)+k

    Let phase ii start with page pp. From its second request through the first request of phase i+1i+1, at least kk distinct pages other than pp are requested. Right after serving pp, OPT holds pp and only k−1k-1 other pages, so it faults at least once in that window, and the windows of different phases do not overlap:

  3. ALG(σ)=∣σ∣whileOPT(σ) ≤ ∣σ∣/k+1\mathrm{ALG}(\sigma)=\abs{\sigma}\qquad\text{while}\qquad\mathrm{OPT}(\sigma)\ \le\ \abs{\sigma}/k+1

    Lower bound. With k+1k+1 pages, the adversary always requests the one page missing from the online cache: every request faults. Belady's rule, after each fault, evicts the page needed furthest ahead, so its next fault is at least kk requests away:

A ratio of kk for caches with thousands of pages sounds damning, yet LRU works well in practice. Two refinements explain part of the gap. With resource augmentation, LRU with kk pages faults at most about k/(k−h+1)k/(k-h+1) times as often as the optimum with only h≤kh\le k pages: with twice the memory, about twice the optimum (Sleator & Tarjan, 1985). And randomization helps exponentially: the marking algorithm, which evicts a uniformly random unmarked page and starts a new phase when all are marked, is 2Hk2H_k-competitive, where Hk=1+12+⋯+1k≈ln⁡kH_k=1+\tfrac12+\dots+\tfrac1k\approx\ln k, and no randomized algorithm beats HkH_k against an oblivious adversary (Fiat et al., 1991). McGeoch and Sleator (1991) closed the gap with an HkH_k-competitive algorithm.

Move-to-front and a potential proof

In the list update problem, ℓ\ell items sit in a linked list and accessing the item at position ii costs ii. After an access, the accessed item may be moved closer to the front for free; any other swap of adjacent items costs 1. Move-to-front (MTF) moves each accessed item to the front. The comparison is with any algorithm AA, even an offline one that rearranges the list cleverly.

DerivationProof with the inversion potential
  1. k−1−v ≤ j−1k-1-v\ \le\ j-1

    Let Φ\Phi be the number of inversions: pairs of items that appear in different orders in MTF's list and in AA's list. It starts at 0 and is never negative. Suppose xx is accessed, at position kk in MTF's list and jj in AA's, and vv of the items ahead of xx in MTF's list are behind it in AA's. The other k−1−vk-1-v items ahead of xx in MTF's list are ahead of it in AA's list too, so

  2. k+(k−1−v)−v = 2(k−v)−1 ≤ 2j−1k+(k-1-v)-v\ =\ 2(k-v)-1\ \le\ 2j-1

    Moving xx to the front creates an inversion with each of those k−1−vk-1-v items and removes the vv inversions. MTF's amortized cost for the access is therefore

  3. MTF(σ) ≤ ∑t(2jt−1)+PA ≤ 2(∑tjt+PA)−m = 2 CA(σ)−m\mathrm{MTF}(\sigma)\ \le\ \sum_t\big(2j_t-1\big)+P_A\ \le\ 2\Big(\sum_tj_t+P_A\Big)-m\ =\ 2\,C_A(\sigma)-m

    Now let AA act. Its access costs jj. Each free move of xx toward the front of AA's list removes an inversion, since xx is already first in MTF's list, and each paid swap creates at most one. Summing over the mm accesses, with PAP_A paid swaps:

The factor 2 is essentially optimal for deterministic algorithms. A cruel adversary always requests the last item of the online list, so the online algorithm pays ℓ\ell per access, while the best of the ℓ!\ell! fixed orderings, which pays (ℓ+1)/2(\ell+1)/2 per access on average over orderings, pays at most that. The ratio tends to 2ℓ/(ℓ+1)=2−2/(ℓ+1)2\ell/(\ell+1)=2-2/(\ell+1); against MTF with ℓ=5\ell=5 and 1000 such requests, MTF pays 5000 and the best fixed list 3000. MTF also earns its keep in compression: after the Burrows–Wheeler transform, bzip2 applies a move-to-front transform, which turns runs of repeated symbols into runs of small numbers.

Check your understanding

In the proof above, why does a free move of xx toward the front of AA's list decrease the potential?

  1. Because free moves cost nothing
  2. Because xx is at the front of MTF's list, so moving it forward in AA's list makes the two lists agree on one more pair
  3. Because every move changes the number of inversions by exactly 2
  4. Because AA is optimal
Show the answer

B. Because xx is at the front of MTF's list, so moving it forward in AA's list makes the two lists agree on one more pair

After MTF's move, xx precedes every item in MTF's list. Each free step of xx past an item yy in AA's list puts xx ahead of yy there too, removing the inversion on the pair {x,y}\{x,y\}. Paid swaps can go either way, which is why they appear in CAC_A.

The frontier: k servers and predictions

Paging is a special case of the kk-server problem (Manasse, McGeoch & Sleator, 1988): kk servers move in a metric space, each request is a point that some server must move to, and the cost is the total distance travelled. Paging is the metric where every distance is 1. No deterministic algorithm beats ratio kk on any metric with more than kk points, and the work function algorithm is (2k−1)(2k-1)-competitive on every metric (Koutsoupias & Papadimitriou, 1995).

Key takeaways

  • An online algorithm is cc-competitive if ALG(σ)≤c⋅OPT(σ)+α\mathrm{ALG}(\sigma)\le c\cdot\mathrm{OPT}(\sigma)+\alpha for every request sequence; the adversary knows the algorithm, and an oblivious one does not see its coins.
  • Ski rental: break-even is (2−1/B)(2-1/B)-competitive and optimal among deterministic algorithms; randomization reaches e/(e−1)≈1.58e/(e-1)\approx1.58, and predictions buy near-optimal cost when they are right, at a price in robustness.
  • Paging: Belady's furthest-in-future rule is optimal offline; LRU and FIFO are kk-competitive and no deterministic algorithm does better, while randomized marking is 2Hk2H_k-competitive against a lower bound of HkH_k.
  • Move-to-front is 2-competitive by an inversion potential, a direct descendant of amortized analysis. The deterministic kk-server conjecture is open, and the randomized one was refuted in 2023.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why can no deterministic ski-rental algorithm guarantee a ratio below 2−1/B2-1/B?

  1. Because renting is always cheaper than buying in the long run
  2. Because the adversary can end the season on the day right after the algorithm buys
  3. Because the algorithm does not know BB
  4. Because the season can last forever
Show the answer

B. Because the adversary can end the season on the day right after the algorithm buys

A deterministic algorithm is a purchase day tt, which the adversary knows. Ending the season right after day tt charges (t−1)+B(t-1)+B for tt days, and the best choice t=Bt=B still pays 2B−12B-1 against an optimum of BB.

Question 2

Which eviction rule minimizes the number of page faults when the whole request sequence is known in advance?

  1. Evict the least recently used page
  2. Evict the page that has been in the cache longest
  3. Evict the least frequently used page so far
  4. Evict the page whose next request is furthest in the future
Show the answer

D. Evict the page whose next request is furthest in the future

Belady's rule is optimal offline, by an exchange argument: any optimal schedule can be transformed step by step into Belady's without adding faults. It needs the future, which is why online algorithms are measured against it.

Question 3

Why can no deterministic online paging algorithm with a cache of kk pages be better than kk-competitive?

  1. Because with k+1k+1 pages the adversary can always request the page the algorithm does not hold, while the optimum faults at most once every kk requests
  2. Because LRU is optimal among online algorithms
  3. Because every algorithm must fault kk times to fill the cache
  4. Because the cache cannot hold more than kk pages
Show the answer

A. Because with k+1k+1 pages the adversary can always request the page the algorithm does not hold, while the optimum faults at most once every kk requests

The algorithm is deterministic, so the adversary can simulate it and always request the one page missing from its cache: a fault every time. Belady's rule on the same sequence evicts the page needed furthest ahead, so its faults are at least kk requests apart.

Question 4

What does the inversion-potential proof show about move-to-front on mm accesses?

  1. MTF is optimal among online list-update algorithms
  2. MTF(σ)≤2 CA(σ)−m\mathrm{MTF}(\sigma)\le2\,C_A(\sigma)-m for every algorithm AA, including offline ones
  3. MTF costs at most twice a fixed best list, but can lose arbitrarily against algorithms that rearrange the list
  4. MTF is 2-competitive only on random access sequences
Show the answer

B. MTF(σ)≤2 CA(σ)−m\mathrm{MTF}(\sigma)\le2\,C_A(\sigma)-m for every algorithm AA, including offline ones

The amortized cost of each MTF access is at most 2j−12j-1, where jj is the accessed item's position in AA's list, and AA's free moves only lower the potential. The bound holds against every AA and every sequence, with no randomness.

Further reading

  • A. Borodin & R. El-Yaniv, Online Computation and Competitive Analysis (Cambridge University Press, 1998).
  • D. D. Sleator & R. E. Tarjan, "Amortized efficiency of list update and paging rules," Communications of the ACM 28(2) (1985), 202–208.
  • A. R. Karlin, M. S. Manasse, L. A. McGeoch & S. Owicki, "Competitive randomized algorithms for nonuniform problems," Algorithmica 11(6) (1994), 542–571.
  • A. Fiat, R. M. Karp, M. Luby, L. A. McGeoch, D. D. Sleator & N. E. Young, "Competitive paging algorithms," Journal of Algorithms 12(4) (1991), 685–699.
  • S. Bubeck, C. Coester & Y. Rabani, "The randomized k-server conjecture is false!," Proceedings of STOC (2023).