BITS Pilani
BITS Logo
CS G526: Advanced Algorithms and Complexity

Online Algorithms I: Competitive Analysis & Ski Rental

Lecture 21 |2026-09-28
Tulasimohan Molli
BITS Pilani, Hyderabad Campus

Agenda

  1. The Online Paradigm: Making irrevocable decisions under uncertainty.
  2. Competitive Analysis: The omniscient offline adversary benchmark (\text{OPT}).
  3. The Ski Rental Archetype: Concrete numbers (B = \$50), deterministic break-even, and the lower bound of 2.
  4. Beating the Adversary via Randomization: Discrete coin flips \to continuous distribution \to optimal \frac{e}{e-1} \approx 1.58.

Offline vs. Online Computation

In classical algorithms, the entire input is known upfront:

The Offline Ideal

  • Sorting: Given array A[1 \dots n] in advance.
  • Shortest Path: Complete graph G = (V, E) with edge weights available at start.
  • Goal: Minimize runtime T(n) to produce the global optimal solution \text{OPT}.

The Real-World Online Reality

  • Input arrives as an unpredictable sequence: \sigma = \langle \sigma_1, \sigma_2, \dots, \sigma_m \rangle.
  • Algorithm must make an irrevocable decision upon receiving \sigma_t before seeing \sigma_{t+1}, \sigma_{t+2}, \dots.
  • The future is completely unknown.

Examples of Online Decisions All Around Us

1 Caching
Memory Paging & Eviction
Evict $p \in k$?
2 Cloud Compute
Resource Provisioning
On-Demand vs Reserved
3 Trading
Financial Portfolio
Sell at $p_t$ or Hold?
4 Scheduling
Cluster Load Balancing
Route $J_t$ to CPU $m$?
5 Ridesharing
Dynamic Fleet Dispatch
Dispatch now or wait?

Measuring Performance: Competitive Analysis

How do we fairly evaluate an algorithm operating under partial information?

Sleator & Tarjan’s Framework (1985)

Benchmark the online algorithm \text{ALG} directly against an omniscient offline optimal algorithm \text{OPT} that knows the entire future sequence \sigma in advance!

  • Definition (c-Competitive for Minimization Problems): An online algorithm \text{ALG} is c-competitive if for all input sequences \sigma: \text{cost}(\text{ALG}(\sigma)) \le c \cdot \text{cost}(\text{OPT}(\sigma)) + \alpha where \alpha is a constant independent of \sigma.

  • The Competitive Ratio: c = \sup_{\sigma} \frac{\text{cost}(\text{ALG}(\sigma))}{\text{cost}(\text{OPT}(\sigma))}

  • The closer c is to 1, the better the online algorithm performs against omniscience.

The Two-Player Game: Algorithm vs. Adversary

Competitive analysis is viewed as a game played against a malicious Adversary:

The Two Players

  • Algorithm (\text{ALG}): Minimizes total cost knowing only history \sigma_1, \dots, \sigma_t.
  • Adversary: Observes \text{ALG}’s behavior and constructs the worst-case sequence \sigma to maximize: \frac{\text{cost}(\text{ALG}(\sigma))}{\text{cost}(\text{OPT}(\sigma))}

Adversary Models for Randomized \text{ALG}

  1. Oblivious Adversary (Standard):
    • Must fix the entire sequence \sigma before the algorithm rolls its random coins.
  2. Adaptive Adversary:
    • Chooses next request \sigma_{t+1} dynamically after seeing the algorithm’s past coin flips.

The Ski Rental Archetype (Rent-or-Buy)

The Setup & Costs

You go on a ski vacation of unknown duration k days (could break a leg tomorrow!):

Cost Structure

  • Rent: Costs \$1 per day.
  • Buy: Costs \$B (one-time purchase, say B = \$50). Once bought, skiing is free forever.
  • The Daily Choice: Each morning, decide whether to pay \$1 to rent or \$B to buy.

What Would Offline \text{OPT} Do?

If \text{OPT} knows total days k in advance: \text{cost}(\text{OPT}) = \min(k, B)

  • If k < B: Rent every day (cost = k).
  • If k \ge B: Buy on day 1 (cost = B).

The Online Dilemma

  • Buy on Day 1, ski 1 day \implies \text{cost} = B vs. \text{OPT} = 1 (Ratio B).
  • Rent forever, ski 2B days \implies \text{cost} = 2B vs. \text{OPT} = B (Ratio 2).

The Deterministic Break-Even Algorithm

What is the best deterministic decision rule?

The Break-Even Strategy

“Rent for B - 1 days. If you ski on day B, buy immediately!”

Why Day B?

  • By day B - 1, you have spent \$(B - 1) on rent.
  • If you decide to ski on day B, you have proven your vacation is long enough to justify owning.
  • Total worst-case expenditure is capped at (B - 1) \times 1 + B = 2B - 1.

Tracing the Strategy: A Concrete Example (B = \$10)

Let purchase cost B = \$10 (Rent = \$1/\text{day}). You buy on Day 10 if you ski:

  • Case 1: k = 4 days (k < B, vacation ends early):
    • You rented 4 days: \text{cost}(\text{ALG}) = \$4.
    • \text{cost}(\text{OPT}) = \min(4, 10) = \$4 \implies \text{Ratio} = \frac{4}{4} = \mathbf{1.0}.
  • Case 2: k = 10 days (k = B, break-even threshold):
    • Rented 9 days (\$9), bought on day 10 (\$10): \text{cost}(\text{ALG}) = 9 + 10 = \mathbf{\$19}.
    • \text{cost}(\text{OPT}) = \min(10, 10) = \mathbf{\$10} \implies \text{Ratio} = \frac{19}{10} = \mathbf{1.9} = 2 - \frac{1}{B}.
  • Case 3: k = 100 days (k \gg B, long vacation):
    • \text{cost}(\text{ALG}) = \$19. \text{cost}(\text{OPT}) = \$10 \implies \text{Ratio} = \frac{19}{10} = \mathbf{1.9}.

General Deterministic Upper Bound

For any purchase cost B and any vacation length k:

Formal Bound

  • If k < B: \frac{\text{cost}(\text{ALG})}{\text{cost}(\text{OPT})} = \frac{k}{k} = 1

  • If k \ge B: \frac{\text{cost}(\text{ALG})}{\text{cost}(\text{OPT})} = \frac{(B - 1) \cdot 1 + B}{B} = \frac{2B - 1}{B} = 2 - \frac{1}{B} < 2

\text{Competitive Ratio } c = \sup_k \frac{\text{cost}(\text{ALG})}{\text{cost}(\text{OPT})} = 2 - \frac{1}{B} < 2

Can Any Deterministic Algorithm Beat 2?

Theorem: The Deterministic Lower Bound

No deterministic online algorithm for Ski Rental can achieve a competitive ratio strictly less than 2 (as B \to \infty).

The Intuition

  • If you buy early (j \le B), the adversary stops the vacation the very day you buy.
  • If you buy late (j > B), the adversary keeps the trip going, forcing you to pay heavy rental costs before finally buying.

Proof: The Adversary’s Trap (c \ge 2)

Suppose a deterministic algorithm decides to buy on fixed day j:

  • Case 1 (j \le B): Adversary terminates vacation immediately after day j (k = j). \text{cost}(\text{ALG}) = (j - 1) + B, \quad \text{cost}(\text{OPT}) = j \text{Ratio} = \frac{j - 1 + B}{j} = 1 + \frac{B - 1}{j} \ge 1 + \frac{B - 1}{B} = 2 - \frac{1}{B} \xrightarrow{B \to \infty} \mathbf{2}

  • Case 2 (j > B): Adversary continues vacation to day j. \text{Ratio} = \frac{j - 1 + B}{B} > \frac{B - 1 + B}{B} = 2 - \frac{1}{B} \xrightarrow{B \to \infty} \mathbf{2}

  • Conclusion: The adversary exploits deterministic predictability. c = 2 is a hard barrier.

Breaking the Barrier with Randomization

Why did the deterministic algorithm fail to beat 2?

The Core Insight

The adversary stopped the vacation on day B because it knew with 100% certainty that we would buy on day B!
If our buying day is a random variable, the oblivious adversary does not know when we will buy.

A Toy 2-Day Example (B = 2):

  • Deterministic rule: Rent day 1, buy day 2.
    • If vacation lasts 2 days: \text{ALG} = 1 + 2 = 3, \text{OPT} = 2 \implies \text{Ratio} = 1.5.
  • Randomized Rule:
    • Flip a coin on Day 1: Buy immediately with probability p, or rent with probability 1 - p.
    • If we randomize, the adversary cannot tailor the stopping day to hurt both outcomes!

The Continuous Formulation: Setup

Scale costs continuously to [0, 1] by dividing by B (as B \to \infty):

  • Purchase Cost: Normalized to \$1 (was \$B)  |  Rental Cost: \$1/unit time (duration t costs t).
  • Adversary Choice: Vacation length t \in [0, 1] \implies \text{cost}(\text{OPT}) = t.
  • Algorithm Choice: Buying time z \in [0, 1] drawn from density f(z), where \int_0^1 f(z)\, dz = 1.

Fixed Purchase Time z vs. Trip Length t:

\text{cost}(z, t) = \begin{cases} z + 1 & \text{if } z \le t \text{ (bought at } z) \\ t & \text{if } z > t \text{ (rented until } t) \end{cases}

Expected Cost \mathbb{E}[\text{cost}(t)]:

\mathbb{E}[\text{cost}(t)] = \int_0^t (z + 1) f(z)\, dz + t \int_t^1 f(z)\, dz

(Averaged over all randomized buying times z)

The Equalizer Principle

How should we choose the distribution f(z)?

The Equalizer Idea

If any trip length t resulted in a lower ratio than another, a clever adversary would always pick the length that maximizes the ratio.
Therefore, the optimal distribution makes the ratio constant for all t: \frac{\mathbb{E}[\text{cost}(t)]}{\text{OPT}(t)} = c \quad \iff \quad \mathbb{E}[\text{cost}(t)] = c \cdot t

Substitute our expected cost into the equalizer equation: \int_0^t (z + 1) f(z) dz + t \int_t^1 f(z) dz = c \cdot t

Solving for f(t): Step-by-Step

Differentiating both sides with respect to t (using Leibniz’s Rule):

\frac{d}{dt}\left[ \int_0^t (z + 1) f(z) dz \right] + \frac{d}{dt}\left[ t \int_t^1 f(z) dz \right] = \frac{d}{dt}[c \cdot t]

  • First term: (t + 1) f(t).

  • Second term (product rule): 1 \cdot \int_t^1 f(z) dz + t \cdot (-f(t)) = \int_t^1 f(z) dz - t f(t).

  • Combining: (t + 1) f(t) + \int_t^1 f(z) dz - t f(t) = c \implies \mathbf{f(t) + \int_t^1 f(z) dz = c}

  • Differentiating once more with respect to t: f'(t) - f(t) = 0 \implies \mathbf{f'(t) = f(t)}

  • The unique solution is an exponential distribution: \mathbf{f(t) = C e^t}!

Normalizing & Finding the Ratio c

Step 1: Normalize total probability to 1: \int_0^1 f(t) dt = 1 \implies C \int_0^1 e^t dt = C(e - 1) = 1 \implies \mathbf{C = \frac{1}{e - 1}} f(t) = \frac{e^t}{e - 1}

Step 2: Evaluate competitive ratio c (evaluate at t = 0): c = f(0) + \int_0^1 f(z) dz = \frac{e^0}{e - 1} + 1 = \frac{1}{e - 1} + 1 = \mathbf{\frac{e}{e - 1}}

Theorem (Karlin, Manasse, Rudolph, Sleator 1988)

Randomization strictly beats the deterministic lower bound of 2: c = \frac{e}{e - 1} \approx \mathbf{1.58197} < 2 No randomized online algorithm against an oblivious adversary can beat \frac{e}{e-1}!

Summary & Lecture Takeaways

Core Concepts Mastered:

  • Online vs. Offline: The penalty of zero future visibility.
  • Competitive Ratio: Benchmark against omniscient \text{OPT}.
  • Deterministic Barrier: Break-even strategy achieves 2 - 1/B, and adversary prevents beating 2.
  • Randomization: Hiding the decision threshold drops the competitive ratio from 2 \to 1.58.

Next Lecture:

  • Online Paging & Caching:
    • Cache of capacity k, slow memory of N pages.
    • Belady’s \text{MIN} (Longest Forward Distance).
    • Sleator–Tarjan \Omega(k) deterministic lower bound.
    • How randomized marking drops competitiveness from k \to O(\log k)!

In-Class Quiz 4: Resolvent & Reachability

Setup

Let G = (V, E) be a directed graph on n vertices with adjacency matrix A \in \{0, 1\}^{n \times n}. Let \varepsilon > 0 be a sufficiently small scalar such that the series converges.

Part (a) — Resolvent Identity [5 Marks]

Prove the matrix identity:

(I - \varepsilon A)^{-1} = \sum_{k=0}^{\infty} \varepsilon^k A^k

Part (b) — Graph Reachability [5 Marks]

Using the identity from Part (a):

  1. Why is v reachable from u if and only if: \left((I - \varepsilon A)^{-1}\right)_{uv} > 0 \quad ?

  2. How does this algebraic test avoid computing individual powers A, A^2, \dots, A^{n-1}?

Caution

⏱ Duration: 10 Minutes  |  📝 Mode: Pen & Paper  |  🎯 Total: 10 Marks