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 times for a cache of pages.
What you’ll learn
- Define online algorithms, the competitive ratio, and oblivious adversaries.
- Prove that the break-even strategy for ski rental is -competitive and optimal among deterministic algorithms, and explain how randomization reaches .
- Prove that Belady's rule is optimal offline, that LRU is -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 in advance; the guarantee is then on the expected cost, .
Ski rental is the simplest case. Renting costs 1 per day and buying costs , an integer. You learn each morning whether you will ski that day, and the season ends without warning. If the season lasts days, the offline optimum is .
Upper bound. If the season ends before day , break-even only rented and paid . Otherwise it paid in rent plus , while :
Lower bound. A deterministic algorithm is fully described by the day on which it would buy. The adversary ends the season right after that purchase, so the algorithm pays for days of skiing:
An algorithm that never buys is not competitive at all: a season of days costs it against . So is optimal, with ratio exactly .
Skis cost 10 days of rent. What competitive ratio does the break-even strategy guarantee?
Show the answer
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 . Karlin, Manasse, McGeoch and Owicki (1990, journal 1994) showed that the best such distribution achieves
as grows. For each finite , solving the linear program of the best distribution gives exactly : 1.333 for , 1.535 for , rising toward .
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 : if the prediction says the season will be long, buy early, on day ; if it says short, buy late, on day . It is -consistent and -robust, interpolating between trusting the prediction and ignoring it.
Think first With and a prediction that the season will be long, the algorithm buys on day . What happens if the prediction was wrong and the season ends that very day?
You paid about in rent plus for skis, roughly , against an optimum of : a ratio near . That is the robustness guarantee at work. Had the prediction been right, you would have paid at most against : the consistency , better than the of break-even.
Paging: the furthest future, and LRU
In paging, a fast cache holds 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.
Cut into phases: each phase is a maximal block of requests to at most distinct pages, and the next phase starts at the request for a -st distinct page. Inside a phase, a page that LRU loads stays among the most recently used until the phase ends, so LRU faults at most once per distinct page:
Let phase start with page . From its second request through the first request of phase , at least distinct pages other than are requested. Right after serving , OPT holds and only other pages, so it faults at least once in that window, and the windows of different phases do not overlap:
Lower bound. With 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 requests away:
A ratio of 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 pages faults at most about times as often as the optimum with only 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 -competitive, where , and no randomized algorithm beats against an oblivious adversary (Fiat et al., 1991). McGeoch and Sleator (1991) closed the gap with an -competitive algorithm.
Move-to-front and a potential proof
In the list update problem, items sit in a linked list and accessing the item at position costs . 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 , even an offline one that rearranges the list cleverly.
Let be the number of inversions: pairs of items that appear in different orders in MTF's list and in 's list. It starts at 0 and is never negative. Suppose is accessed, at position in MTF's list and in 's, and of the items ahead of in MTF's list are behind it in 's. The other items ahead of in MTF's list are ahead of it in 's list too, so
Moving to the front creates an inversion with each of those items and removes the inversions. MTF's amortized cost for the access is therefore
Now let act. Its access costs . Each free move of toward the front of 's list removes an inversion, since is already first in MTF's list, and each paid swap creates at most one. Summing over the accesses, with 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 per access, while the best of the fixed orderings, which pays per access on average over orderings, pays at most that. The ratio tends to ; against MTF with 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.
In the proof above, why does a free move of toward the front of 's list decrease the potential?
- Because free moves cost nothing
- Because is at the front of MTF's list, so moving it forward in 's list makes the two lists agree on one more pair
- Because every move changes the number of inversions by exactly 2
- Because is optimal
Show the answer
B. Because is at the front of MTF's list, so moving it forward in 's list makes the two lists agree on one more pair
After MTF's move, precedes every item in MTF's list. Each free step of past an item in 's list puts ahead of there too, removing the inversion on the pair . Paid swaps can go either way, which is why they appear in .
The frontier: k servers and predictions
Paging is a special case of the -server problem (Manasse, McGeoch & Sleator, 1988): 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 on any metric with more than points, and the work function algorithm is -competitive on every metric (Koutsoupias & Papadimitriou, 1995).
Key takeaways
- An online algorithm is -competitive if for every request sequence; the adversary knows the algorithm, and an oblivious one does not see its coins.
- Ski rental: break-even is -competitive and optimal among deterministic algorithms; randomization reaches , 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 -competitive and no deterministic algorithm does better, while randomized marking is -competitive against a lower bound of .
- Move-to-front is 2-competitive by an inversion potential, a direct descendant of amortized analysis. The deterministic -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.
Why can no deterministic ski-rental algorithm guarantee a ratio below ?
- Because renting is always cheaper than buying in the long run
- Because the adversary can end the season on the day right after the algorithm buys
- Because the algorithm does not know
- 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 , which the adversary knows. Ending the season right after day charges for days, and the best choice still pays against an optimum of .
Which eviction rule minimizes the number of page faults when the whole request sequence is known in advance?
- Evict the least recently used page
- Evict the page that has been in the cache longest
- Evict the least frequently used page so far
- 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.
Why can no deterministic online paging algorithm with a cache of pages be better than -competitive?
- Because with pages the adversary can always request the page the algorithm does not hold, while the optimum faults at most once every requests
- Because LRU is optimal among online algorithms
- Because every algorithm must fault times to fill the cache
- Because the cache cannot hold more than pages
Show the answer
A. Because with pages the adversary can always request the page the algorithm does not hold, while the optimum faults at most once every 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 requests apart.
What does the inversion-potential proof show about move-to-front on accesses?
- MTF is optimal among online list-update algorithms
- for every algorithm , including offline ones
- MTF costs at most twice a fixed best list, but can lose arbitrarily against algorithms that rearrange the list
- MTF is 2-competitive only on random access sequences
Show the answer
B. for every algorithm , including offline ones
The amortized cost of each MTF access is at most , where is the accessed item's position in 's list, and 's free moves only lower the potential. The bound holds against every 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).