The Competitive Ratio, Precisely
Online Problems and the Competitive Ratio introduced the competitive ratio with paging as the example. This lesson states the definitions in the generality the rest of the course needs: online problems as games between an algorithm and a request sequence, the difference between strict and non-strict ratios and why the additive constant is there, maximization problems, and the step from deterministic to randomized algorithms. The worked example is Graham's list scheduling, the oldest competitive analysis on record (1966), two decades before the term existed.
Online problems as request-answer games
An online minimization problem consists of a set of requests, a set of possible answers, and a cost function. A request sequence is revealed one request at a time. An online algorithm ALG must produce an answer to that depends only on (and, for a randomized algorithm, on its random bits). Its cost is the cost of the whole answer sequence. The offline optimum
minimizes over all answer sequences, with full knowledge of . Borodin and El-Yaniv call this framework a request-answer game. Paging fits it (requests are pages, answers are eviction choices), and so do scheduling, routing, data structures and the -server problem.
Nothing in the definition limits the online algorithm's computation time. Competitive analysis measures the cost of not knowing the future, so even an online algorithm with unlimited computing power may have a large ratio. Conversely OPT may be NP-hard to compute; that does not matter for the definitions, only for experiments.
Strict and non-strict ratios
Definition. A deterministic online algorithm ALG for a minimization problem is -competitive if there is a constant , independent of , such that
It is strictly -competitive if this holds with . The competitive ratio of ALG is the infimum of all for which it is -competitive; the strict competitive ratio is defined in the same way with . For a problem, the deterministic competitive ratio is the infimum over all deterministic online algorithms.
Some authors call the non-strict version the asymptotic ratio, because it only constrains sequences with large optimal cost: if , the constant becomes negligible. That is also why lower-bound proofs for the non-strict ratio must build sequences with unbounded optimal cost; a lower bound from a single short sequence only bounds the strict ratio.
Why allow at all? Because many problems have a start-up effect that has nothing to do with the future. In paging, suppose the online cache and the offline cache start with different contents. Then a short sequence can cost the online algorithm a miss on every request while OPT, which happens to start with the right pages, pays nothing:
def lru_misses(reqs, start, k):
cache, misses = list(start), 0
for p in reqs:
if p in cache:
cache.remove(p)
else:
misses += 1
if len(cache) == k:
cache.pop(0)
cache.append(p)
return misses
k = 3
online_start, offline_start = ["a", "b", "c"], ["x", "y", "z"]
for reps in [1, 2, 5, 20]:
reqs = ["x", "y", "z"] * reps
alg = lru_misses(reqs, online_start, k)
opt = 0 # the offline cache already holds x, y, z
print(f"{len(reqs):3} requests: LRU {alg} misses, OPT {opt}, "
f"ALG <= k*OPT + k holds: {alg <= k * opt + k}") 3 requests: LRU 3 misses, OPT 0, ALG <= k*OPT + k holds: True
6 requests: LRU 3 misses, OPT 0, ALG <= k*OPT + k holds: True
15 requests: LRU 3 misses, OPT 0, ALG <= k*OPT + k holds: True
60 requests: LRU 3 misses, OPT 0, ALG <= k*OPT + k holds: True
No strict bound is possible here, since for every , yet the start-up loss is bounded by forever. The non-strict definition absorbs it. When both caches start empty, LRU is in fact strictly -competitive (LRU and FIFO Are k-Competitive). Ski rental shows the opposite case: for a fixed price every cost is below , so an additive constant would make every strategy "1-competitive". Ski-rental bounds are therefore always strict ratios (Deterministic Ski Rental).
Maximization problems
For a maximization problem (matching, allocation, admission control), ALG is -competitive with if for all . Some texts use the reciprocal instead; this course always writes the ratio as a number at most 1 for maximization, so "greedy matching is -competitive" means it finds at least half the optimum.
Randomized algorithms
A randomized online algorithm is a probability distribution over deterministic online algorithms (or, equivalently, an algorithm that flips coins as it goes). Its cost on a fixed is a random variable. The basic definition fixes the sequence before the coins are flipped:
This is competitiveness against an oblivious adversary, who knows the algorithm, including its probability distribution, but not the outcomes of its coins. Stronger adversaries, who see the coins as they fall, are the topic of Oblivious and Adaptive Adversaries. Against an oblivious adversary randomization often helps a great deal: paging improves from to , ski rental from to .
A worked example: list scheduling
Online makespan scheduling. There are identical machines. Jobs arrive one by one with processing times , and each must be assigned to a machine at once. The cost is the makespan, the largest total load of a machine. Graham's list scheduling assigns each job to a currently least-loaded machine.
LIST-SCHEDULE(m, jobs)
0 load = [0] * m
1 for each arriving job p
2 i = a machine with the smallest load[i] // ties: smallest index
3 load[i] = load[i] + p
4 return max(load)Theorem (Graham, 1966). List scheduling is strictly -competitive.
Proof
Let job with time be the one that finishes last, and let be its machine's load before it was added. Every machine had load at least at that moment (job went to the least-loaded one), so . Two easy lower bounds hold for OPT: (some machine gets at least the average) and (job runs somewhere). Then
The bound is tight for list scheduling: jobs of size 1 followed by one job of size . The small jobs spread evenly to load everywhere, then the big job lands on top, giving , while OPT puts the big job alone on one machine and the small jobs on the others, for makespan . The program runs that adversary and, for comparison, random small instances solved exactly by brute force.
import random
from itertools import product
def list_schedule(m, jobs):
load = [0] * m
for p in jobs:
i = load.index(min(load))
load[i] += p
return max(load)
def opt_makespan(m, jobs):
best = sum(jobs)
for assign in product(range(m), repeat=len(jobs)):
load = [0] * m
for p, i in zip(jobs, assign):
load[i] += p
best = min(best, max(load))
return best
for m in [2, 3, 4]:
jobs = [1] * (m * (m - 1)) + [m]
alg, opt = list_schedule(m, jobs), m # OPT = m for this family
print(f"m = {m}: adversary jobs give ALG {alg}, OPT {opt}, ratio {alg / opt:.3f} = 2 - 1/m")
rng = random.Random(3)
m, worst = 3, 0
for _ in range(120):
jobs = [rng.randint(1, 9) for _ in range(6)]
worst = max(worst, list_schedule(m, jobs) / opt_makespan(m, jobs))
print(f"m = 3, 120 random instances of 6 jobs: worst ratio {worst:.3f} (bound {2 - 1 / 3:.3f})")m = 2: adversary jobs give ALG 3, OPT 2, ratio 1.500 = 2 - 1/m
m = 3: adversary jobs give ALG 5, OPT 3, ratio 1.667 = 2 - 1/m
m = 4: adversary jobs give ALG 7, OPT 4, ratio 1.750 = 2 - 1/m
m = 3, 120 random instances of 6 jobs: worst ratio 1.500 (bound 1.667)
On random inputs the worst ratio found stays below the bound, and most instances are much closer to 1, as is typical: competitive ratios are worst-case guarantees, and an adversary has to work to reach them.
In C++
List scheduling with a min-heap of machine loads, run on the same adversarial family. A heap makes each assignment ; ties between equal loads go to the smaller machine index, as in the Python version.
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
long long list_schedule(int m, const std::vector<long long>& jobs) {
using Item = std::pair<long long, int>; // (load, machine)
std::priority_queue<Item, std::vector<Item>, std::greater<Item>> heap;
for (int i = 0; i < m; ++i) heap.push({0, i});
long long makespan = 0;
for (long long p : jobs) {
auto [load, i] = heap.top();
heap.pop();
heap.push({load + p, i});
if (load + p > makespan) makespan = load + p;
}
return makespan;
}
int main() {
for (int m : {2, 3, 4, 10, 100}) {
std::vector<long long> jobs(m * (m - 1), 1);
jobs.push_back(m);
long long alg = list_schedule(m, jobs);
std::printf("m = %3d: ALG %lld, OPT %d, ratio %.4f, 2 - 1/m = %.4f\n",
m, alg, m, double(alg) / m, 2.0 - 1.0 / m);
}
}m = 2: ALG 3, OPT 2, ratio 1.5000, 2 - 1/m = 1.5000
m = 3: ALG 5, OPT 3, ratio 1.6667, 2 - 1/m = 1.6667
m = 4: ALG 7, OPT 4, ratio 1.7500, 2 - 1/m = 1.7500
m = 10: ALG 19, OPT 10, ratio 1.9000, 2 - 1/m = 1.9000
m = 100: ALG 199, OPT 100, ratio 1.9900, 2 - 1/m = 1.9900
Better ratios exist for this problem: about deterministic for large (Albers, 1999; Fleischer and Wahl, 2000), and no deterministic algorithm beats about for large (Rudin, 2001). Those algorithms deliberately keep some machines lightly loaded, which list scheduling never does.
Common mistakes
- Comparing with the wrong benchmark. The ratio compares with the optimal offline cost on the same sequence, not with another online algorithm and not with an average.
- Forgetting the quantifier order. In " for all ", the constants and come first. A bound with depending on the length of is no bound at all.
- Lower bounds from short sequences. A sequence with and shows only that the strict ratio is at least 3. For the non-strict ratio you need a family with .
Test yourself
ALG satisfies for every . What can you conclude?
- ALG is strictly 3-competitive
- ALG is 3-competitive (non-strict)
- ALG is 13-competitive and nothing better
- Nothing, because of the constant
To prove that no deterministic algorithm has a non-strict ratio below , what must the adversarial sequences have?
- Length exactly
- Optimal cost that grows without bound
- Only distinct requests
- A uniform random distribution
List scheduling on machines: what is its competitive ratio?
A randomized algorithm is -competitive against an oblivious adversary. Which statement is the definition?
- for every outcome of the coins
- for every fixed
- for random
- The algorithm is -competitive with probability