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 . 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 . The season lasts days, unknown to the skier, who learns each morning only that the season is still on. A deterministic strategy is a single number : buy on the morning of day if the season has lasted that long. Then
All costs lie between 1 and , so an additive constant of size would hide every difference between strategies. Ski-rental bounds are therefore strict ratios (), and that is what "competitive" means throughout this section.
The worst case of every strategy
For a strategy , the ratio is largest at , the season ending on the day of the purchase: for the strategy has only rented, so its ratio is , which is 1 for and below otherwise, and for the strategy's cost stays fixed while can only grow. So
decreasing up to and increasing after it, with minimum . The program checks the closed form against brute force over all seasons and prints the curve for . The lab below shows the same curve as a chart for its strategies.
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
With the season set to the lab shows the break-even strategy's worst case: days of rent, then the purchase on the last day. Switch the strategy to "buy at once" and lower to 1 to see ratio , or to "always rent" and raise to see the ratio grow without bound.
The lower bound, with its quantifiers
Theorem. For every , every deterministic online ski-rental strategy has strict competitive ratio at least . The break-even strategy attains it.
Proof
Fix a deterministic strategy; it is some . If (never buy), the season costs it against , ratio for every . Otherwise the adversary picks , and the ratio is by the case analysis above. The adversary needs to know , 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 , buying on day 1 if and never otherwise is optimal), and that gap is exactly what randomization will exploit.
General prices and the continuous limit
With rent per day and price (both positive, not necessarily an integer), the break-even rule rents while the rent paid so far plus one more day stays below and buys on the first day with , that is . Its worst ratio is , which is when is an integer and slightly different otherwise. As the time step shrinks ( with the rate fixed) the problem becomes continuous: rent accrues at rate 1, buying costs at any real time , and the best deterministic ratio is exactly 2, attained by buying at .
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 ). Spinning for 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 ; spinning costs 1 per time unit, blocking costs (and the thread then sleeps for free). The program compares always-block, always-spin and break-even (spin up to , then block) with the offline optimum per wait. Waits are drawn from a mix of short and long ones.
#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 units costs , then blocking adds ), so the worst single ratio is rather than .
Test yourself
With , a strategy buys on day 6. What is its worst-case ratio?
Why are ski-rental bounds stated as strict ratios?
- Because the strategies are deterministic
- Because all costs are at most about , so an additive constant could absorb any difference
- Because OPT is unknown
- Because the season is infinite
In the continuous version (rent accrues at rate 1, buying costs at any time), what is the best deterministic ratio?
Which statement is the correct order of quantifiers in the lower bound?
- There is one season that is bad for every strategy
- For every strategy there is a season on which it pays at least times OPT
- Every season is bad for every strategy
- For every season some strategy is bad