Decision-making without future information. Competitive analysis.
Multi-session curriculum - substantial prior knowledge and complex material. Use mastery gates and deliberate practice.
Some algorithm problems feel “unfair”: you must act now, but the input will only be revealed later. Online algorithms are the formal way to reason about decision-making under uncertainty—where the adversary controls the future and your choices are (usually) irreversible.
An online algorithm makes decisions using only the past and present, while OPT is a clairvoyant offline algorithm that knows the whole future. We evaluate online algorithms via competitive analysis: compare ALG to OPT on every input sequence and bound the worst-case ratio (or additive gap). Canonical examples like Ski Rental and Paging show how to design and prove guarantees, often using clean inequality chains or potential functions to “telescope” costs over time.
In many real systems, you don’t get the whole input upfront.
If you had the full future sequence, you could compute a globally optimal plan. But in reality, decisions must be made as the sequence unfolds.
Online algorithms capture this: input arrives piecewise, and the algorithm must decide using only information revealed so far.
Think of time steps .
Formally, an online algorithm is a rule that maps the prefix to an action:
Often, the action is irrevocable (or at least has irreversible consequences). For example, if you evict a page from cache, you can’t magically have kept it.
To judge an online algorithm, we need a gold standard.
We’ll write:
Sometimes you can assume a distribution (average-case). Competitive analysis is different: it’s worst-case, and it doesn’t require any distribution.
The mindset:
This is a powerful lens for systems where “rare bad cases” are unacceptable.
On an interactive canvas, represent the request sequence as a timeline.
Visualization idea: Prefix timeline (core mental model)
Side-by-side panels
This is not just decoration: it concretizes the asymmetry—ALG must commit with partial info while OPT is clairvoyant.
There are two common cases:
1) Cost minimization (paging, ski rental, scheduling with penalties):
An online algorithm is $c$-competitive if there exists a constant such that for all sequences ,
2) Value maximization (online matching, ad allocation):
An online algorithm is $c$-competitive if for all sequences ,
In this lesson we’ll mostly use cost minimization.
Competitive analysis can be stated against different adversary models:
| Adversary type | What it can do | Typical use |
|---|---|---|
| Oblivious | Fixes the whole sequence in advance | Standard for randomized algorithms |
| Adaptive | Chooses next request after seeing your past actions | Stronger (harder) |
When we discuss randomized algorithms later, we’ll implicitly assume an oblivious adversary unless stated otherwise.
In offline algorithm analysis, you often prove optimality or approximation via structural arguments (“greedy stays ahead”).
In online algorithms, you frequently prove inequalities of the form:
Because OPT knows the future, you cannot directly compare step-by-step decisions. Instead, you compare costs using one of these strategies:
1) Charging arguments: charge each online cost to something OPT must also pay.
2) Partition into phases: group the sequence into chunks where OPT must pay at least 1 per chunk.
3) Potential functions: define a “stored credit” so that
Summing over time telescopes the potential.
We’ll use all three.
You want to ski for an unknown number of days .
You must decide each day whether to rent again or buy now, without knowing .
If OPT knows :
So:
Define ALG:
ALG cost:
We want .
Case 1:
So ratio is 1.
Case 2:
and
Thus
So the algorithm is 2-competitive (and in fact -competitive with tighter arithmetic).
There is a standard lower bound: no deterministic online algorithm can have competitive ratio < 2 for ski rental.
Intuition:
A deterministic strategy has a fixed “buy day” . The adversary chooses:
to force ratio approaching 2.
Canvas layout
Key interaction
This makes the adversarial nature of competitive analysis tangible.
When you face a new online problem, ask:
1) What is the state? What decisions are irreversible?
2) What does OPT do with full knowledge?
3) What is a natural online heuristic? (Often greedy.)
4) How can you lower-bound OPT’s cost on any sequence?
5) How can you upper-bound ALG’s cost and connect it to that lower bound?
This becomes especially important in paging, where the proof technique is more “global”.
You have a cache that holds pages.
Goal: minimize total misses.
This is a quintessential online problem: you do not know future requests, so eviction is hard.
OPT is the optimal offline caching algorithm. A famous optimal offline rule is Belady’s algorithm:
You cannot implement this online (it needs the future), but it defines OPT.
Two classic online strategies:
Both are simple and widely used.
LRU and FIFO are $k$-competitive for paging with cache size .
Meaning: for all sequences ,
Usually can be taken as or .
We’ll focus on the proof idea and a potential-function visualization.
If we can partition the request sequence into chunks where:
then ALG is -competitive.
A standard definition:
So each phase contains requests to at most distinct pages.
1) ALG incurs ≤ k misses per phase (for LRU/FIFO).
2) OPT incurs ≥ 1 miss per phase boundary.
Thus:
So:
This yields $k$-competitiveness.
Canvas elements
Step-through behavior
Side-by-side miss counts
This makes the proof’s core comparison visual: k tokens vs 1 token per phase.
Phase arguments are powerful but sometimes too coarse. Potential functions let you prove more refined statements and are reusable.
Define a potential based on the algorithm’s state and OPT’s state at time .
You aim to prove an inequality per step:
Then sum over :
\begin{align}
\sum_t \text{ALG}_t + \sum_t (\Phi_t - \Phi_{t-1}) &\le c \sum_t \text{OPT}_t \\
\mathrm{ALG}(\sigma) + (\Phi_T - \Phi_0) &\le c\,\mathrm{OPT}(\sigma).
\end{align}
Because the potential telescopes, you get:
If is always nonnegative, then , so the additive term is at most .
For LRU, one classic proof uses a potential related to “how many pages are in OPT’s cache but not in ALG’s cache,” but the full tight potential proof is intricate.
Instead, use potential to visualize telescoping on the canvas:
Visualization: Potential telescope meter
When a miss happens:
Even if you don’t complete the full rigorous inequality for LRU in this lesson, the visualization teaches the method: potential stores “debt/credit” that can go up and down, and the proof is about bounding cost + Δpotential.
Potential functions are the online-algorithms cousin of amortized analysis in data structures. The same “telescope” trick appears in splay trees, dynamic arrays, and primal-dual online methods.
Deterministic paging has a lower bound: no deterministic online paging algorithm can beat competitive ratio .
Randomization can do better:
You don’t need to master these yet, but keep the principle:
If you later study the randomized paging algorithm (e.g., MARK), the phase concept returns, but the expectation is handled carefully.
Core canvas design
Step controls
Metrics
The goal: learners see that OPT’s evictions look “weird” locally but are globally optimal because OPT sees the future.
Competitive analysis is a theory tool, but it maps to real engineering constraints:
Competitive ratio is worst-case; it can be pessimistic. But it is useful when:
In more benign environments, you may combine competitive analysis with:
Many proofs become clearer if you imagine:
If you are implementing this node in an interactive tech tree UI, these interactions do real teaching work:
1) Ski rental adversary game
2) Paging step-through with phase highlights
3) Potential telescope meter
These visualizations are not optional decoration—they mirror the structure of the analysis.
Rent costs 1/day, buy costs B. Consider the deterministic online algorithm ALG: rent for B−1 days, then if still skiing on day B, buy. Let D be the (unknown) number of skiing days.
Compute OPT(D):
OPT knows D.
Therefore:
Compute ALG(D) by cases:
Bound the ratio when D < B:
Here OPT(D)=D, so
Bound the ratio when D ≥ B:
Here OPT(D)=B, and
Conclude competitiveness:
For all D,
So ALG is 2-competitive (with additive constant α=0).
Insight: This proof works because OPT has only two meaningful behaviors (rent-all or buy-immediately). The online difficulty is exactly at the unknown threshold D≈B, where an adversary can force either regret (buy too early) or regret (buy too late).
Paging with cache size k. Requests form a sequence σ. Define phases so that each phase contains requests to at most k distinct pages, and the next request would introduce the (k+1)th distinct page, starting a new phase. Consider ALG = LRU or FIFO.
Define the phase partition precisely:
Start phase 1 at t=1.
Maintain a set S of distinct pages seen in the current phase.
When processing request σ_t:
Thus each phase contains ≤ k distinct pages.
Upper-bound ALG’s misses per phase:
Within a single phase, there are at most k distinct pages.
ALG can miss at most once per distinct page when it first appears in the phase.
So misses in a phase ≤ k.
(After a page is loaded, subsequent requests in the same phase can be hits unless it gets evicted; for LRU/FIFO under this phase definition, the standard argument shows it won’t evict a page needed again within the phase before all k distinct pages have been loaded.)
Lower-bound OPT’s misses across phases:
Consider two consecutive phases i and i+1.
By construction, phase i has k distinct pages, and phase i+1 starts with a new page not in phase i’s distinct set (otherwise we wouldn’t start a new phase).
Therefore, across the boundary there are at least k+1 distinct pages that are requested in the union of the two phases.
OPT’s cache holds only k pages, so it cannot have all k+1 pages resident at the times they are needed.
Hence OPT must incur at least 1 miss attributable to each phase boundary.
Relate totals:
Let P be the number of phases.
So:
Conclude k-competitiveness:
ALG is k-competitive with additive constant α = k.
Insight: The phase partition turns a messy streaming process into a counting argument: ALG spends ≤ k “tokens” per phase, while OPT must spend ≥ 1 token per phase boundary. This is the backbone of many competitive proofs: invent a structure where OPT is forced to pay regularly.
You want to understand the telescoping pattern used in online proofs. Suppose an online process has per-step cost a_t, and you define a potential Φ_t ≥ 0. You manage to prove for every step t:
where o_t is OPT’s per-step cost.
Write the per-step inequality:
For each t,
Sum over t = 1..T:
Recognize the telescope:
The second sum cancels internally:
\begin{align}
\sum_{t=1}^T (\Phi_t - \Phi_{t-1})
&= (\Phi_1-\Phi_0) + (\Phi_2-\Phi_1) + \dots + (\Phi_T-\Phi_{T-1}) \\
&= \Phi_T - \Phi_0.
\end{align}
Rewrite in total-cost form:
Let ALG = ∑ a_t and OPT = ∑ o_t. Then
Use nonnegativity of potential:
If Φ_T ≥ 0, then
Insight: A potential function is a bookkeeping device that allows you to “move” cost between time steps. The proof succeeds when the potential changes cancel (telescope), leaving only endpoints. A good visualization is literally a stack of blocks that move left/right, showing cancellation as you sum.
Online algorithms act on prefixes: at time t they see only σ₁:t, not the future, and decisions often have irreversible consequences.
OPT is the optimal offline (clairvoyant) benchmark; competitive analysis compares ALG against OPT on every input sequence.
For minimization problems, c-competitiveness typically means ALG(σ) ≤ c·OPT(σ) + α, where α handles small startup effects.
Ski Rental illustrates threshold strategies and adversarial worst-case reasoning; a simple deterministic strategy is 2-competitive and 2 is essentially optimal deterministically.
Paging formalizes caching; LRU/FIFO achieve k-competitive guarantees via phase partitioning and lower-bounding OPT’s unavoidable misses.
Phase arguments convert streaming behavior into counting tokens per chunk (≤ k for ALG, ≥ 1 for OPT), making proofs modular and visualizable.
Potential functions support amortized competitive proofs by bounding (ALG cost + ΔΦ) per step and letting ΔΦ telescope over time.
Interactive step-through simulations (ALG vs OPT, phase boundaries, potential meters) directly mirror the proof structure and improve intuition.
Comparing ALG to OPT step-by-step without acknowledging OPT’s future knowledge; many online proofs must compare via lower bounds, phases, or potential, not local decisions.
Forgetting the additive constant α and claiming a pure multiplicative bound when initialization effects (e.g., empty cache) matter.
Defining phases incorrectly (e.g., ending after k+1 distinct pages rather than before), which breaks the “OPT must miss per boundary” argument.
Using randomization claims without specifying the adversary model (oblivious vs adaptive), which can invalidate the guarantee.
Ski Rental variant: Rent costs r per day, buy costs B. Consider the threshold algorithm: rent for ⌈B/r⌉−1 days, then buy. Prove an upper bound on its competitive ratio.
Hint: Let m = ⌈B/r⌉. Do a case split on whether D < m. Express OPT(D) = min(rD, B) and compare to ALG(D).
Let m = ⌈B/r⌉.
OPT(D) = min(rD, B).
ALG rents for m−1 days and buys on day m if needed.
Case 1: D < m.
ALG(D) = rD = OPT(D), ratio 1.
Case 2: D ≥ m.
ALG(D) = r(m−1) + B.
Since m = ⌈B/r⌉, we have (m−1) < B/r, so r(m−1) < B.
Thus ALG(D) < B + B = 2B = 2·OPT(D) (because OPT(D)=B when D≥m).
So the algorithm is 2-competitive (more tightly, < 2).
Paging warm-up: Let k=2 and consider the request sequence A, B, C, A, B, C, … (repeat). Simulate LRU for the first 9 requests and count misses. What is OPT’s miss count for the same prefix? (You may reason rather than fully compute OPT.)
Hint: With k=2, LRU keeps the two most recently used pages. The repeating pattern of 3 distinct pages forces frequent evictions. For OPT, argue a lower bound: cache size 2 cannot hold all 3 pages, so every third request must miss in steady state.
LRU simulation (cache shown as most-recently-used order):
1) A miss, cache {A}
2) B miss, cache {B,A}
3) C miss, evict A, cache {C,B}
4) A miss, evict B, cache {A,C}
5) B miss, evict C, cache {B,A}
6) C miss, evict A, cache {C,B}
7) A miss, evict B, cache {A,C}
8) B miss, evict C, cache {B,A}
9) C miss, evict A, cache {C,B}
So LRU misses on all 9 requests.
For OPT with k=2 on a 3-cycle, OPT still must miss frequently because it can only hold 2 of 3 pages. In steady state, at least 1 of each block of 3 requests must miss, giving a lower bound of ≥ 3 misses for 9 requests. In fact, OPT can do no better than 9 as well in this exact alternating-with-3 pattern under k=2? Reason carefully: because every request is to the page not in cache if OPT keeps the other two, OPT will also miss every time. Indeed, whichever 2 pages OPT holds, the next request is the third page at some point, forcing a miss each step in this strict cycle. Thus OPT also has 9 misses on the first 9 requests, so the ratio here is 1.
Phase argument practice: With cache size k, define phases as ‘maximal contiguous blocks containing at most k distinct pages’. Prove: the number of phases P satisfies P−1 ≤ OPT(σ).
Hint: Look at two consecutive phases. Show that their union contains at least k+1 distinct pages, and argue that OPT must miss at least once somewhere across the boundary because its cache holds only k pages.
Let phases be maximal blocks with ≤ k distinct pages. Consider boundary between phase i and phase i+1. Because phase i is maximal, the first request of phase i+1 is to a page not among the distinct pages of phase i; otherwise, phase i could be extended while still using ≤ k distinct pages.
Therefore, the union of distinct pages in phases i and i+1 has size at least k+1.
OPT’s cache holds only k pages, so it cannot contain all k+1 pages simultaneously at the moments they are requested. Hence, among the requests spanning the boundary (i plus the first request of i+1, and possibly more), OPT must incur at least one miss attributable to the inability to hold all k+1 pages.
Thus each boundary forces ≥ 1 OPT miss. With P phases, there are P−1 boundaries, so P−1 ≤ OPT(σ).
Next nodes that commonly build on this: