TExcelTAD
Home › Online Algorithms & Competitive Analysis › The Competitive Ratio, Precisely

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 σ=σ1σ2⋯σn\sigma = \sigma_1 \sigma_2 \cdots \sigma_n is revealed one request at a time. An online algorithm ALG must produce an answer ata_t to σt\sigma_t that depends only on σ1,…,σt\sigma_1, \ldots, \sigma_t (and, for a randomized algorithm, on its random bits). Its cost ALG(σ)\mathrm{ALG}(\sigma) is the cost of the whole answer sequence. The offline optimum

OPT(σ)=min⁡a1,…,ancost(σ;a1,…,an)\mathrm{OPT}(\sigma) = \min_{a_1, \ldots, a_n} \mathrm{cost}(\sigma; a_1, \ldots, a_n)

minimizes over all answer sequences, with full knowledge of σ\sigma. 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 kk-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 cc-competitive if there is a constant α\alpha, independent of σ\sigma, such that

ALG(σ)≤c⋅OPT(σ)+αfor every request sequence σ.\mathrm{ALG}(\sigma) \le c \cdot \mathrm{OPT}(\sigma) + \alpha \qquad \text{for every request sequence } \sigma .

It is strictly cc-competitive if this holds with α=0\alpha = 0. The competitive ratio of ALG is the infimum of all cc for which it is cc-competitive; the strict competitive ratio is defined in the same way with α=0\alpha = 0. 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 OPT(σ)→∞\mathrm{OPT}(\sigma) \to \infty, the constant α\alpha 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 α\alpha 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:

python · try it yourself
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 ALG=3>c⋅0\mathrm{ALG} = 3 > c \cdot 0 for every cc, yet the start-up loss is bounded by kk forever. The non-strict definition absorbs it. When both caches start empty, LRU is in fact strictly kk-competitive (LRU and FIFO Are k-Competitive). Ski rental shows the opposite case: for a fixed price BB every cost is below 2B2B, so an additive constant α=2B\alpha = 2B 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 cc-competitive with c≤1c \le 1 if ALG(σ)≥c⋅OPT(σ)−α\mathrm{ALG}(\sigma) \ge c \cdot \mathrm{OPT}(\sigma) - \alpha for all σ\sigma. Some texts use the reciprocal OPT/ALG≥1\mathrm{OPT}/\mathrm{ALG} \ge 1 instead; this course always writes the ratio as a number at most 1 for maximization, so "greedy matching is 12\frac{1}{2}-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 σ\sigma is a random variable. The basic definition fixes the sequence before the coins are flipped:

E[ALG(σ)]≤c⋅OPT(σ)+αfor every σ.\mathbb{E}\bigl[\mathrm{ALG}(\sigma)\bigr] \le c \cdot \mathrm{OPT}(\sigma) + \alpha \qquad \text{for every } \sigma .

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 kk to Hk≈ln⁡kH_k \approx \ln k, ski rental from 22 to ee−1\frac{e}{e-1}.

A worked example: list scheduling

Online makespan scheduling. There are mm identical machines. Jobs arrive one by one with processing times p1,p2,…p_1, p_2, \ldots, 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.

text
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 (2−1m)\left(2 - \frac{1}{m}\right)-competitive.

Proof

Let job jj with time pjp_j be the one that finishes last, and let LL be its machine's load before it was added. Every machine had load at least LL at that moment (job jj went to the least-loaded one), so mL+pj≤∑ipimL + p_j \le \sum_i p_i. Two easy lower bounds hold for OPT: OPT≥1m∑ipi\mathrm{OPT} \ge \frac{1}{m}\sum_i p_i (some machine gets at least the average) and OPT≥pj\mathrm{OPT} \ge p_j (job jj runs somewhere). Then

ALG=L+pj≤1m(∑ipi−pj)+pj=1m∑ipi+(1−1m)pj≤OPT+(1−1m)OPT=(2−1m)OPT.\begin{aligned} \mathrm{ALG} &= L + p_j \le \frac{1}{m}\Bigl(\sum_i p_i - p_j\Bigr) + p_j \\ &= \frac{1}{m}\sum_i p_i + \Bigl(1 - \frac{1}{m}\Bigr) p_j \le \mathrm{OPT} + \Bigl(1 - \frac{1}{m}\Bigr)\mathrm{OPT} = \Bigl(2 - \frac{1}{m}\Bigr)\mathrm{OPT}. \end{aligned}

The bound is tight for list scheduling: m(m−1)m(m-1) jobs of size 1 followed by one job of size mm. The small jobs spread evenly to load m−1m - 1 everywhere, then the big job lands on top, giving 2m−12m - 1, while OPT puts the big job alone on one machine and the small jobs on the others, for makespan mm. The program runs that adversary and, for comparison, random small instances solved exactly by brute force.

python · try it yourself
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 O(log⁡m)O(\log m); ties between equal loads go to the smaller machine index, as in the Python version.

cpp · try it yourself
#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 1.921.92 deterministic for large mm (Albers, 1999; Fleischer and Wahl, 2000), and no deterministic algorithm beats about 1.881.88 for large mm (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 "ALG(σ)≤c⋅OPT(σ)+α\mathrm{ALG}(\sigma) \le c \cdot \mathrm{OPT}(\sigma) + \alpha for all σ\sigma", the constants cc and α\alpha come first. A bound with α\alpha depending on the length of σ\sigma is no bound at all.
  • Lower bounds from short sequences. A sequence with OPT=3\mathrm{OPT} = 3 and ALG=9\mathrm{ALG} = 9 shows only that the strict ratio is at least 3. For the non-strict ratio you need a family with OPT→∞\mathrm{OPT} \to \infty.

Test yourself

ALG satisfies ALG(σ)≤3 OPT(σ)+10\mathrm{ALG}(\sigma) \le 3\,\mathrm{OPT}(\sigma) + 10 for every σ\sigma. What can you conclude?

  1. ALG is strictly 3-competitive
  2. ALG is 3-competitive (non-strict)
  3. ALG is 13-competitive and nothing better
  4. Nothing, because of the constant

To prove that no deterministic algorithm has a non-strict ratio below cc, what must the adversarial sequences have?

  1. Length exactly cc
  2. Optimal cost that grows without bound
  3. Only distinct requests
  4. A uniform random distribution

List scheduling on m=5m = 5 machines: what is its competitive ratio?

  1. 1.51.5
  2. 1.81.8
  3. 22
  4. 55

A randomized algorithm is cc-competitive against an oblivious adversary. Which statement is the definition?

  1. ALG(σ)≤c OPT(σ)+α\mathrm{ALG}(\sigma) \le c\,\mathrm{OPT}(\sigma) + \alpha for every outcome of the coins
  2. E[ALG(σ)]≤c OPT(σ)+α\mathbb{E}[\mathrm{ALG}(\sigma)] \le c\,\mathrm{OPT}(\sigma) + \alpha for every fixed σ\sigma
  3. Eσ[ALG(σ)]≤c Eσ[OPT(σ)]\mathbb{E}_{\sigma}[\mathrm{ALG}(\sigma)] \le c\,\mathbb{E}_{\sigma}[\mathrm{OPT}(\sigma)] for random σ\sigma
  4. The algorithm is cc-competitive with probability 1/21/2