TExcelTAD
Home › Online Algorithms & Competitive Analysis › Metrical Task Systems

Metrical Task Systems

Paging, list update and ski rental look different, but they share a structure: a system is in one of several states, each request makes some states expensive, and changing state costs something too. Borodin, Linial and Saks (1987; journal version 1992) captured this in one model, the metrical task system (MTS). It is general enough to contain almost every online problem of this course, and simple enough that its competitive ratio is known exactly for deterministic algorithms: 2n−12n - 1 on every metric with nn states. This lesson defines the model, computes OPT with the work function, runs the work-function algorithm and a greedy rule, and summarizes the randomized story.

The model

A metrical task system consists of a finite metric space (S,d)(S, d) with n=∣S∣n = \lvert S \rvert states and a starting state s0s_0. A request is a task τ:S→[0,∞]\tau : S \to [0, \infty], the cost of processing it in each state. On task τt\tau_t an online algorithm moves from its state st−1s_{t-1} to a state sts_t of its choice, after seeing τt\tau_t, and pays

d(st−1,st)+τt(st).d(s_{t-1}, s_t) + \tau_t(s_t) .

The total cost is the sum over all tasks. Infinite task costs forbid states. Examples:

  • Paging with NN pages and cache size kk: states are the (Nk)\binom{N}{k} possible cache contents, the distance between two contents is the number of pages in which they differ, and a request for page pp costs 0 in every state containing pp and ∞\infty elsewhere.
  • kk-server (next lesson): states are server configurations, distances are minimum-cost matchings.
  • Ski rental is a two-state system {renting,owning}\{\text{renting}, \text{owning}\} with d=Bd = B and every day's task costing 1 in "renting" and 0 in "owning". (MTS distances are symmetric, so moving back is allowed but never useful here.)

OPT and the work function

The offline optimum is a shortest path in a layered graph, computed by dynamic programming. The work function wt(s)w_t(s) is the least cost of serving τ1,…,τt\tau_1, \ldots, \tau_t and ending in state ss:

w0(s)=d(s0,s),wt(s)=min⁡x∈S(wt−1(x)+τt(x)+d(x,s)),OPT=min⁡swm(s).w_0(s) = d(s_0, s), \qquad w_t(s) = \min_{x \in S} \bigl( w_{t-1}(x) + \tau_t(x) + d(x, s) \bigr), \qquad \mathrm{OPT} = \min_s w_m(s).

(The move into xx before processing is already included in wt−1(x)w_{t-1}(x), because a work function satisfies w(y)≤w(x)+d(x,y)w(y) \le w(x) + d(x, y).) The work function summarizes everything about the past that matters for the future, and it is the basis of the best deterministic algorithm.

Two online algorithms

  • Greedy: move to the state minimizing d(st−1,s)+τt(s)d(s_{t-1}, s) + \tau_t(s), the cheapest response to the current task alone.
  • Work-function algorithm (WFA): move to the state minimizing wt(s)+d(st−1,s)w_t(s) + d(s_{t-1}, s). It balances the offline cost of ending in ss against the cost of getting there now. Among the minimizers it picks one where the offline optimum ending there actually processed the task there, wt(s)=wt−1(s)+τt(s)w_t(s) = w_{t-1}(s) + \tau_t(s); such a minimizer always exists, and without this tie rule WFA can stall in a state whose cost the work function no longer sees.
text
WFA-STEP(state, w, task)            // w = work function before the task
0  for every s:  w'(s) = min over x of ( w(x) + task(x) + d(x, s) )
1  next = a state minimizing w'(s) + d(state, s)
          with w'(next) = w(next) + task(next)    // exists; ties: stay
2  pay d(state, next) + task(next)
3  return next, w'

Theorem (Borodin, Linial and Saks, 1992). For every metric space with nn states, the deterministic competitive ratio of metrical task systems is exactly 2n−12n - 1: some deterministic algorithm is (2n−1)(2n - 1)-competitive, and no deterministic algorithm is better on any nn-point metric. The work-function algorithm achieves 2n−12n - 1 as well.

The program compares greedy and WFA with OPT on seeded random tasks over 6 states on a line, and then runs an adversarial task sequence on two states.

python · try it yourself
import random

def wfa_step(d, w, cur, tau):
    n = len(d)
    new = [min(w[x] + tau[x] + d[x][s] for x in range(n)) for s in range(n)]
    best = min(new[s] + d[cur][s] for s in range(n))
    cands = [s for s in range(n) if new[s] + d[cur][s] == best and new[s] == w[s] + tau[s]]
    nxt = cur if cur in cands else cands[0]                  # such a state always exists
    return nxt, new, d[cur][nxt] + tau[nxt]

def greedy_step(d, cur, tau):
    nxt = min(range(len(d)), key=lambda s: (d[cur][s] + tau[s], s != cur, s))
    return nxt, d[cur][nxt] + tau[nxt]

def run(d, tasks, start=0):
    w = [d[start][s] for s in range(len(d))]
    cw = cg = start
    cost_w = cost_g = 0
    for tau in tasks:
        cw, w, c = wfa_step(d, w, cw, tau)
        cost_w += c
        cg, c = greedy_step(d, cg, tau)
        cost_g += c
    return cost_w, cost_g, min(w)

n = 6
d = [[30 * abs(i - j) for j in range(n)] for i in range(n)]     # states on a line, spacing 30
rng = random.Random(6)
tasks, centre = [], 0
for _ in range(2000):                                            # a cheap region that jumps now and then
    if rng.random() < 0.05:
        centre = rng.randrange(n)
    tasks.append([abs(s - centre) * rng.randint(1, 3) for s in range(n)])
wfa, greedy, opt = run(d, tasks)
print(f"random tasks on a 6-state line: OPT {opt}, WFA {wfa} ({wfa / opt:.2f}), greedy {greedy} ({greedy / opt:.2f})")

# two states at distance 100; the adversary charges 1 to whichever state the algorithm is in
d2 = [[0, 100], [100, 0]]
for name in ["WFA", "greedy"]:
    w, cur, cost = [0, 100], 0, 0
    for t in range(6000):
        tau = [1 if s == cur else 0 for s in range(2)]
        if name == "WFA":
            cur, w, c = wfa_step(d2, w, cur, tau)
        else:
            w = [min(w[x] + tau[x] + d2[x][s] for x in range(2)) for s in range(2)]
            cur, c = greedy_step(d2, cur, tau)
        cost += c
    print(f"adversary on 2 states, {name:6}: ALG {cost:5}, OPT {min(w):4}, ratio {cost / min(w):.3f}")
random tasks on a 6-state line: OPT 3412, WFA 5961 (1.75), greedy 8500 (2.49)
adversary on 2 states, WFA   : ALG  8871, OPT 3000, ratio 2.957
adversary on 2 states, greedy: ALG  6000, OPT  100, ratio 60.000

Greedy never moves against this adversary (staying costs only 1 per task, moving costs 100), so it pays forever while OPT moves once and pays nothing more: greedy's ratio grows with the length of the sequence. WFA moves when the work function says the other state has become cheap enough, and its ratio is close to 2n−1=32n - 1 = 3, the best possible for deterministic algorithms on two states.

In C++

The work-function algorithm on a line metric with nn states, with tasks that make one moving region cheap, compared with the offline optimum. The work function update costs O(n2)O(n^2) per task.

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

int main() {
    const int n = 8, m = 5000;
    std::vector<std::vector<long long>> d(n, std::vector<long long>(n));
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = 30LL * std::abs(i - j);
    std::mt19937 rng(8);
    std::vector<long long> w(n);
    for (int s = 0; s < n; ++s) w[s] = d[0][s];
    int cur = 0, centre = 0;
    long long cost = 0;
    for (int t = 0; t < m; ++t) {
        if (rng() % 20 == 0) centre = rng() % n;                 // the cheap region jumps
        std::vector<long long> tau(n), nw(n);
        for (int s = 0; s < n; ++s) tau[s] = std::abs(s - centre) * (1 + rng() % 3);
        for (int s = 0; s < n; ++s) {
            nw[s] = w[0] + tau[0] + d[0][s];
            for (int x = 1; x < n; ++x) nw[s] = std::min(nw[s], w[x] + tau[x] + d[x][s]);
        }
        long long target = nw[0] + d[cur][0];
        for (int s = 1; s < n; ++s) target = std::min(target, nw[s] + d[cur][s]);
        int best = -1;                                            // a minimizer that processed the task there
        for (int s = 0; s < n; ++s)
            if (nw[s] + d[cur][s] == target && nw[s] == w[s] + tau[s] && (best < 0 || s == cur)) best = s;
        w = nw;
        cost += d[cur][best] + tau[best];
        cur = best;
    }
    long long opt = *std::min_element(w.begin(), w.end());
    std::printf("n = %d states, %d tasks: OPT %lld, WFA %lld, ratio %.3f (bound 2n - 1 = %d)\n",
                n, m, opt, cost, double(cost) / opt, 2 * n - 1);
}
n = 8 states, 5000 tasks: OPT 11228, WFA 23402, ratio 2.084 (bound 2n - 1 = 15)

Ski rental as a task system

The general guarantee has a price. Run WFA on ski rental written as a two-state task system (renting and owning at distance BB, each day costing 1 in "renting" and 0 in "owning"), and compare it with the break-even rule, which knows that moving back is never useful.

python · try it yourself
def wfa_step(d, w, cur, tau):
    n = len(d)
    new = [min(w[x] + tau[x] + d[x][s] for x in range(n)) for s in range(n)]
    best = min(new[s] + d[cur][s] for s in range(n))
    cands = [s for s in range(n) if new[s] + d[cur][s] == best and new[s] == w[s] + tau[s]]
    return (cur if cur in cands else cands[0]), new

B = 10
d = [[0, B], [B, 0]]                     # state 0 = renting, state 1 = owning
worst_wfa = worst_be = 0
for season in range(1, 4 * B):
    w, cur, cost = [0, B], 0, 0
    for day in range(season):
        tau = [1, 0]
        nxt, w = wfa_step(d, w, cur, tau)
        cost += d[cur][nxt] + tau[nxt]
        cur = nxt
    opt = min(season, B)
    break_even = season if season < B else 2 * B - 1
    worst_wfa = max(worst_wfa, cost / opt)
    worst_be = max(worst_be, break_even / opt)
print(f"B = {B}: worst ratio of WFA {worst_wfa:.2f} (2n - 1 = 3), of break-even {worst_be:.2f} (2 - 1/B)")
B = 10: worst ratio of WFA 3.00 (2n - 1 = 3), of break-even 1.90 (2 - 1/B)

WFA rents for 2B2B days before buying, because in a symmetric task system the adversary could later make "owning" expensive and force a move back; it pays 3B3B against OPT=B\mathrm{OPT} = B, exactly the bound 2n−1=32n - 1 = 3. The break-even rule exploits the special structure of ski rental and achieves 2−1/B2 - 1/B. The MTS bound is the price of handling every task sequence on the metric, not just this one.

The lower bound idea

The lower bound 2n−12n - 1 uses the adversary of the two-state experiment, generalized: it always gives a small positive cost to the algorithm's current state and 0 elsewhere. Every online algorithm must eventually move or pay forever; when it moves, it pays the distance. Borodin, Linial and Saks compare the online cost with the average of nn offline strategies and show that the online algorithm pays at least 2n−12n - 1 times that average. For the uniform metric this is easy to see in the limit ε→0\varepsilon \to 0: the online algorithm is chased through all states in a cycle, while an offline algorithm can sit in the state that the online algorithm will visit last.

Randomized MTS

Against an oblivious adversary randomization helps exponentially, as in paging:

Metric Randomized ratio Reference
uniform, nn states between HnH_n and 2Hn2H_n Borodin, Linial and Saks, 1992 (refined by Irani and Seiden, 1998)
every nn-point metric (upper bound) O(log⁡2n)O(\log^2 n) Bubeck, Cohen, Lee and Lee, 2019; Coester and Lee, 2019
every nn-point metric (lower bound) Ω(log⁡n)\Omega(\log n) Bubeck, Coester and Rabani, 2023 (earlier Ω(log⁡n/log⁡log⁡n)\Omega(\log n / \log\log n))
some nn-point metrics (lower bound) Ω(log⁡2n)\Omega(\log^2 n) Bubeck, Coester and Rabani, 2023

So the worst-case randomized ratio over all nn-point metrics is Θ(log⁡2n)\Theta(\log^2 n), settled only in 2023, while for specific metrics (uniform, for instance) it is Θ(log⁡n)\Theta(\log n). The upper bounds come from embedding the metric into hierarchically separated trees and running online mirror descent with an entropic regularizer on the tree, a technique also behind recent kk-server results (Randomized k-Server).

Test yourself

In a metrical task system, what does an online algorithm pay for task τt\tau_t when it moves from st−1s_{t-1} to sts_t?

  1. τt(st−1)\tau_t(s_{t-1})
  2. d(st−1,st)+τt(st)d(s_{t-1}, s_t) + \tau_t(s_t)
  3. min⁡sτt(s)\min_s \tau_t(s)
  4. d(s0,st)d(s_0, s_t)

What is the deterministic competitive ratio of metrical task systems on an nn-point metric?

  1. nn
  2. 2n−12n - 1
  3. O(log⁡n)O(\log n)
  4. n!n!

Why is the greedy rule not competitive for MTS?

  1. It is too slow
  2. An adversary charging a tiny cost to its current state keeps it from ever moving, so it pays forever while OPT moves once
  3. It always moves
  4. It ignores distances

What did Bubeck, Coester and Rabani (2023) show about randomized MTS?

  1. That O(log⁡n)O(\log n) holds on every metric
  2. That some nn-point metrics need ratio Ω(log⁡2n)\Omega(\log^2 n), matching the known O(log⁡2n)O(\log^2 n) upper bound
  3. That randomization does not help
  4. That the deterministic ratio is log⁡n\log n