TExcelTAD
Home › Online Algorithms & Competitive Analysis › Deterministic Ski Rental and Its Lower Bound

Deterministic Ski Rental and Its Lower Bound

Ski rental is the "hello world" of competitive analysis, and Ski Rental: Rent or Buy? already found the break-even rule and its ratio 2−1/B2 - 1/B. This lesson treats the deterministic problem completely: the exact worst-case ratio of every deterministic strategy, the matching lower bound with its quantifiers spelled out, the general version with arbitrary rent and buy prices, the continuous-time limit, and where rent-or-buy decisions appear in real systems. The randomized strategy comes next, in Randomized Ski Rental.

The model

Renting costs 1 per day, buying costs an integer B≥2B \ge 2. The season lasts D≥1D \ge 1 days, unknown to the skier, who learns each morning only that the season is still on. A deterministic strategy is a single number j∈{1,2,…}∪{∞}j \in \{1, 2, \ldots\} \cup \{\infty\}: buy on the morning of day jj if the season has lasted that long. Then

ALGj(D)={DD<j,(j−1)+BD≥j,OPT(D)=min⁡(D,B).\mathrm{ALG}_j(D) = \begin{cases} D & D < j, \\ (j - 1) + B & D \ge j, \end{cases} \qquad \mathrm{OPT}(D) = \min(D, B).

All costs lie between 1 and 2B2B, so an additive constant of size BB would hide every difference between strategies. Ski-rental bounds are therefore strict ratios (α=0\alpha = 0), and that is what "competitive" means throughout this section.

The worst case of every strategy

For a strategy jj, the ratio ALGj(D)/OPT(D)\mathrm{ALG}_j(D)/\mathrm{OPT}(D) is largest at D=jD = j, the season ending on the day of the purchase: for D<jD < j the strategy has only rented, so its ratio is D/min⁡(D,B)D/\min(D, B), which is 1 for D≤BD \le B and below c(j)c(j) otherwise, and for D>jD > j the strategy's cost stays fixed while OPT\mathrm{OPT} can only grow. So

c(j)=(j−1)+Bmin⁡(j,B)={1+B−1jj≤B,1+j−1Bj≥B,c(j) = \frac{(j - 1) + B}{\min(j, B)} = \begin{cases} 1 + \dfrac{B - 1}{j} & j \le B, \\[2mm] 1 + \dfrac{j - 1}{B} & j \ge B, \end{cases}

decreasing up to j=Bj = B and increasing after it, with minimum c(B)=2−1/Bc(B) = 2 - 1/B. The program checks the closed form against brute force over all seasons and prints the curve for B=8B = 8. The lab below shows the same curve as a chart for its strategies.

python · try it yourself
from fractions import Fraction

def alg(j, B, D):
    return D if D < j else (j - 1) + B

def worst_ratio_brute(j, B, horizon=200):
    return max(Fraction(alg(j, B, D), min(D, B)) for D in range(1, horizon))

def worst_ratio_formula(j, B):
    return Fraction(j - 1 + B, min(j, B))

B = 8
print("  j   c(j) brute   c(j) formula")
for j in [1, 2, 4, 6, 7, 8, 9, 10, 12, 16, 24]:
    b, f = worst_ratio_brute(j, B), worst_ratio_formula(j, B)
    assert b == f
    print(f"{j:3}   {float(b):10.4f}   {str(f):>12}")
best = min(range(1, 40), key=lambda j: worst_ratio_formula(j, B))
print(f"best buying day {best}, ratio {worst_ratio_formula(best, B)} = 2 - 1/B")
  j   c(j) brute   c(j) formula
  1       8.0000              8
  2       4.5000            9/2
  4       2.7500           11/4
  6       2.1667           13/6
  7       2.0000              2
  8       1.8750           15/8
  9       2.0000              2
 10       2.1250           17/8
 12       2.3750           19/8
 16       2.8750           23/8
 24       3.8750           31/8
best buying day 8, ratio 15/8 = 2 - 1/B
Season of 8 days, buying costs 8rent (1)buy (8)own (0)12341B = 840season length Dcost / OPT

With the season set to D=BD = B the lab shows the break-even strategy's worst case: B−1B - 1 days of rent, then the purchase on the last day. Switch the strategy to "buy at once" and lower DD to 1 to see ratio BB, or to "always rent" and raise DD to see the ratio grow without bound.

The lower bound, with its quantifiers

Theorem. For every B≥2B \ge 2, every deterministic online ski-rental strategy has strict competitive ratio at least 2−1/B2 - 1/B. The break-even strategy j=Bj = B attains it.

Proof

Fix a deterministic strategy; it is some jj. If j=∞j = \infty (never buy), the season D=cBD = cB costs it cBcB against OPT=B\mathrm{OPT} = B, ratio cc for every cc. Otherwise the adversary picks D=jD = j, and the ratio is c(j)≥c(B)=2−1/Bc(j) \ge c(B) = 2 - 1/B by the case analysis above. The adversary needs to know jj, which it does because the strategy is deterministic.

The order of quantifiers is the whole point: for every strategy there is a bad season. No single season is bad for every strategy (for each fixed DD, buying on day 1 if D≥BD \ge B and never otherwise is optimal), and that gap is exactly what randomization will exploit.

General prices and the continuous limit

With rent rr per day and price PP (both positive, P/rP/r not necessarily an integer), the break-even rule rents while the rent paid so far plus one more day stays below PP and buys on the first day jj with j⋅r≥Pj \cdot r \ge P, that is j=⌈P/r⌉j = \lceil P/r \rceil. Its worst ratio is (j−1)r+Pmin⁡(jr,P)\frac{(j-1)r + P}{\min(jr, P)}, which is 2−r/P2 - r/P when P/rP/r is an integer and slightly different otherwise. As the time step shrinks (r→0r \to 0 with the rate fixed) the problem becomes continuous: rent accrues at rate 1, buying costs BB at any real time tt, and the best deterministic ratio is exactly 2, attained by buying at t=Bt = B.

python · try it yourself
from fractions import Fraction
from math import ceil

def break_even_ratio(r, P, horizon=400):
    j = ceil(P / r)
    cost = lambda D: D * r if D < j else (j - 1) * r + P
    return max(cost(D) / min(D * r, P) for D in range(1, horizon))

for r, P in [(Fraction(1), Fraction(10)), (Fraction(3), Fraction(10)), (Fraction(7), Fraction(10)),
             (Fraction(1, 10), Fraction(10)), (Fraction(1, 100), Fraction(10))]:
    j = ceil(P / r)
    print(f"rent {str(r):>5}, price {P}: buy on day {j:4}, worst ratio {float(break_even_ratio(r, P, 3 * j + 5)):.4f}")
rent     1, price 10: buy on day   10, worst ratio 1.9000
rent     3, price 10: buy on day    4, worst ratio 1.9000
rent     7, price 10: buy on day    2, worst ratio 1.7000
rent  1/10, price 10: buy on day  100, worst ratio 1.9900
rent 1/100, price 10: buy on day 1000, worst ratio 1.9990

Rent-or-buy in systems

Many system decisions have ski rental's shape, and the break-even rule is used in practice under other names:

  • Spin or block (Karlin, Manasse, McGeoch and Owicki, 1990). A thread waiting for a lock can spin (rent: CPU time per microsecond) or block (buy: a fixed context-switch cost CC). Spinning for CC time units and then blocking is 2-competitive.
  • Power-down. A disk or a radio idles at a power cost per second or sleeps for a fixed wake-up cost; with several sleep states this becomes multislope ski rental (Ski Rental Variants).
  • Snoopy caching (Karlin, Manasse, Rudolph and Sleator, 1988), the paper that named competitive analysis: a cache keeps paying to stay coherent with a shared block, or drops it and pays to fetch it again.

In C++

Spin-or-block on a seeded list of lock waiting times. Each wait has a length ww; spinning costs 1 per time unit, blocking costs C=50C = 50 (and the thread then sleeps for free). The program compares always-block, always-spin and break-even (spin up to CC, then block) with the offline optimum min⁡(w,C)\min(w, C) per wait. Waits are drawn from a mix of short and long ones.

cpp · try it yourself
#include <algorithm>
#include <cstdio>
#include <random>
#include <vector>

int main() {
    const long long C = 50;                         // cost of blocking (a context switch)
    std::mt19937 rng(2024);
    std::vector<long long> waits;
    for (int i = 0; i < 1000; ++i) {
        bool long_wait = rng() % 10 == 0;               // one wait in ten is long
        waits.push_back(long_wait ? 100 + rng() % 400 : 1 + rng() % 30);
    }
    long long opt = 0, block = 0, spin = 0, breakeven = 0;
    double worst = 0;
    for (long long w : waits) {
        long long o = std::min(w, C);
        long long be = (w <= C) ? w : C + C;            // spun C units, then blocked
        opt += o;
        block += C;
        spin += w;
        breakeven += be;
        worst = std::max(worst, double(be) / o);
    }
    std::printf("total cost: OPT %lld, always block %lld, always spin %lld, break-even %lld\n",
                opt, block, spin, breakeven);
    std::printf("ratios: block %.3f, spin %.3f, break-even %.3f (worst single wait %.3f)\n",
                double(block) / opt, double(spin) / opt, double(breakeven) / opt, worst);
}
total cost: OPT 19643, always block 50000, always spin 47318, break-even 25593
ratios: block 2.545, spin 2.409, break-even 1.303 (worst single wait 2.000)

The break-even rule is never worse than twice OPT, wait by wait. Always-block is good when most waits are long and always-spin when they are short; only break-even is safe without knowing which. Note that here the continuous model applies (spinning exactly CC units costs CC, then blocking adds CC), so the worst single ratio is 22 rather than 2−1/B2 - 1/B.

Test yourself

With B=12B = 12, a strategy buys on day 6. What is its worst-case ratio?

  1. 1.51.5
  2. 17/6≈2.8317/6 \approx 2.83
  3. 2−1/122 - 1/12
  4. 1212

Why are ski-rental bounds stated as strict ratios?

  1. Because the strategies are deterministic
  2. Because all costs are at most about 2B2B, so an additive constant could absorb any difference
  3. Because OPT is unknown
  4. Because the season is infinite

In the continuous version (rent accrues at rate 1, buying costs BB at any time), what is the best deterministic ratio?

  1. 2−1/B2 - 1/B
  2. 22
  3. e/(e−1)e/(e-1)
  4. 11

Which statement is the correct order of quantifiers in the lower bound?

  1. There is one season that is bad for every strategy
  2. For every strategy there is a season on which it pays at least (2−1/B)(2 - 1/B) times OPT
  3. Every season is bad for every strategy
  4. For every season some strategy is bad