TExcelTAD
Home › Online Algorithms & Competitive Analysis › The Work-Function Algorithm

The Work-Function Algorithm

Double coverage is kk-competitive, but only on trees. For arbitrary metric spaces the best deterministic kk-server algorithm known is the work-function algorithm (WFA). Koutsoupias and Papadimitriou (1995) proved that it is (2k−1)(2k - 1)-competitive on every metric space, the first bound linear in kk and still the best one. The algorithm is easy to state: it combines a greedy term (how far to move now) with a retrospective term (how good the new configuration would have been for the requests seen so far). Its analysis is famously hard; this lesson states the result, implements the algorithm on small metrics, and compares it with greedy and double coverage.

Work functions for kk-server

A configuration is a set XX of kk points, the server positions. For a starting configuration X0X_0 and requests r1,…,rtr_1, \ldots, r_t, the work function wt(X)w_t(X) is the least cost of serving r1,…,rtr_1, \ldots, r_t starting from X0X_0 and ending in configuration XX. It obeys a simple recurrence: to end in XX after serving rtr_t, some configuration containing rtr_t was reached first, and with lazy moves that means

w0(X)=D(X0,X),wt(X)=min⁡x∈X(wt−1(X−x+rt)+d(rt,x)),w_0(X) = D(X_0, X), \qquad w_t(X) = \min_{x \in X} \Bigl( w_{t-1}\bigl(X - x + r_t\bigr) + d(r_t, x) \Bigr),

where DD is the minimum-cost matching distance between configurations. In particular wt(X)=wt−1(X)w_t(X) = w_{t-1}(X) whenever rt∈Xr_t \in X, and OPT=min⁡Xwt(X)\mathrm{OPT} = \min_X w_t(X). The same object appeared for metrical task systems, with configurations as the states.

The algorithm

On request rtr_t, WFA is in configuration At−1A_{t-1}. It serves rtr_t with the server a∈At−1a \in A_{t-1} that minimizes

wt(At−1−a+rt)+d(a,rt).w_t\bigl(A_{t-1} - a + r_t\bigr) + d(a, r_t) .
text
WFA(X0, requests)
0  A = X0 ; w = matching distances from X0 to every configuration
1  for each request r
2      w = updated work function (the recurrence above)
3      if r is not in A
4          a = server in A minimizing w(A - a + r) + d(a, r)    // ties: smallest index
5          pay d(a, r) ; A = A - a + r

The two terms pull in different directions. Minimizing d(a,rt)d(a, r_t) alone is greedy, which is not competitive. Minimizing wt(A−a+rt)w_t(A - a + r_t) alone is the retrospective rule, "move to the configuration that would have been best in hindsight", which is not competitive either (it may make long moves for small gains). The sum is.

Theorem (Koutsoupias and Papadimitriou, 1995). On every metric space, WFA is (2k−1)(2k - 1)-competitive for the kk-server problem.

The proof works with the extended cost max⁡X(wt(X)−wt−1(X))\max_X \bigl(w_t(X) - w_{t-1}(X)\bigr), shows that WFA's cost is bounded by the total extended cost minus OPT, and bounds the extended cost by a potential over sets of k+1k + 1 configurations using a quasiconvexity property of work functions. It is long and is not reproduced here; Borodin and El-Yaniv (chapter 10) give a complete account. WFA is known to be kk-competitive for k=2k = 2 (where it matches the 22 of Manasse, McGeoch and Sleator), on the line and on the star, and on metrics with k+2k + 2 points (Bartal and Koutsoupias, 2004); whether it is kk-competitive on every metric is open and would prove the kk-server conjecture.

WFA in action

The program implements WFA by brute force over all configurations of a finite point set, which is fine for small instances. It runs WFA, greedy and (on the line) double coverage against OPT, on the sequence that defeats greedy and on seeded random requests.

python · try it yourself
import random
from itertools import combinations, permutations

def wfa_run(points, d, start, reqs):
    k = len(start)
    confs = [frozenset(c) for c in combinations(points, k)]
    def match(X, Y):                                        # min-cost matching, brute force for small k
        X = list(X)
        return min(sum(d[a][b] for a, b in zip(X, p)) for p in permutations(Y))
    w = {X: match(start, X) for X in confs}
    A, cost = frozenset(start), 0
    for r in reqs:
        w = {X: w[X] if r in X else min(w[X - {x} | {r}] + d[r][x] for x in X) for X in confs}
        if r not in A:
            a = min(sorted(A), key=lambda a: w[A - {a} | {r}] + d[a][r])
            cost += d[a][r]
            A = A - {a} | {r}
    return cost, min(w.values())

def greedy_run(d, start, reqs):
    S, cost = list(start), 0
    for r in reqs:
        if r in S:
            continue
        i = min(range(len(S)), key=lambda j: (d[S[j]][r], j))
        cost += d[S[i]][r]
        S[i] = r
    return cost

def dc_run(start, reqs):
    s, cost = sorted(start), 0
    for r in reqs:
        if r in s:
            continue
        if r < s[0] or r > s[-1]:
            i = 0 if r < s[0] else len(s) - 1
            cost += abs(s[i] - r)
            s[i] = r
        else:
            i = max(j for j in range(len(s)) if s[j] < r)
            delta = min(r - s[i], s[i + 1] - r)
            s[i] += delta
            s[i + 1] -= delta
            cost += 2 * delta
    return cost

line = [0, 1, 2, 5, 9, 20]
d = {a: {b: abs(a - b) for b in line} for a in line}
alt = [1, 0] * 150
wfa, opt = wfa_run(line, d, [0, 20], alt)
print(f"alternating 1, 0 with servers at 0, 20: OPT {opt}, WFA {wfa}, DC {dc_run([0, 20], alt)}, greedy {greedy_run(d, [0, 20], alt)}")

rng = random.Random(15)
reqs = [rng.choice(line) for _ in range(300)]
wfa, opt = wfa_run(line, d, [0, 9, 20], reqs)
print(f"random, 3 servers on the line: OPT {opt}, WFA {wfa} ({wfa / opt:.2f}), "
      f"DC {dc_run([0, 9, 20], reqs)} ({dc_run([0, 9, 20], reqs) / opt:.2f}), greedy {greedy_run(d, [0, 9, 20], reqs)}")
alternating 1, 0 with servers at 0, 20: OPT 19, WFA 57, DC 57, greedy 300
random, 3 servers on the line: OPT 332, WFA 406 (1.22), DC 798 (2.40), greedy 406

On the alternating sequence WFA moves the far server early: once the work function shows that a configuration with servers at 0 and 1 would have been much cheaper in hindsight, the retrospective term outweighs the one-time cost of the long move.

A metric that is not a tree

Double coverage needs a tree; WFA does not. The next program places 7 points on a circle (distances along the circle, which is not a tree metric) and compares WFA with greedy on seeded random requests and on the cruel adversary of the lower bound, which always requests a point of a (k+1)(k+1)-point subset that WFA does not cover.

python · try it yourself
import random
from itertools import combinations, permutations

n, k = 7, 2
points = list(range(n))
d = {a: {b: min(abs(a - b), n - abs(a - b)) for b in points} for a in points}   # a 7-cycle
confs = [frozenset(c) for c in combinations(points, k)]

def match(X, Y):
    X = list(X)
    return min(sum(d[a][b] for a, b in zip(X, p)) for p in permutations(Y))

def wfa(start, next_request, steps):
    w = {X: match(start, X) for X in confs}
    A, cost, reqs = frozenset(start), 0, []
    for t in range(steps):
        r = next_request(A, t)
        reqs.append(r)
        w = {X: w[X] if r in X else min(w[X - {x} | {r}] + d[r][x] for x in X) for X in confs}
        if r not in A:
            a = min(sorted(A), key=lambda a: w[A - {a} | {r}] + d[a][r])
            cost += d[a][r]
            A = A - {a} | {r}
    return cost, min(w.values()), reqs

def greedy(start, reqs):
    S, cost = list(start), 0
    for r in reqs:
        if r not in S:
            i = min(range(k), key=lambda j: (d[S[j]][r], j))
            cost += d[S[i]][r]
            S[i] = r
    return cost

rng = random.Random(16)
cost, opt, reqs = wfa([0, 3], lambda A, t: rng.randrange(n), 500)
print(f"random requests on a 7-cycle: OPT {opt}, WFA {cost} ({cost / opt:.2f}), greedy {greedy([0, 3], reqs)} ({greedy([0, 3], reqs) / opt:.2f})")
subset = [0, 2, 5]                                           # k + 1 = 3 points
cost, opt, reqs = wfa([0, 2], lambda A, t: next(p for p in subset if p not in A), 500)
print(f"cruel adversary on {subset}: OPT {opt}, WFA {cost}, ratio {cost / opt:.2f} (k = {k})")
random requests on a 7-cycle: OPT 411, WFA 492 (1.20), greedy 472 (1.15)
cruel adversary on [0, 2, 5]: OPT 500, WFA 1000, ratio 2.00 (k = 2)

Against the cruel adversary WFA pays k=2k = 2 times OPT, the lower bound for every deterministic algorithm, and for k=2k = 2 WFA is known to be exactly 2-competitive. On random requests greedy is slightly cheaper than WFA: WFA pays a little for insurance against adversarial sequences, which greedy lacks entirely.

In C++

WFA for two servers on nn points in the plane (Euclidean distances, seeded positions), with the work function stored as an n×nn \times n table: w[i][j]w[i][j] for the configuration {i,j}\{i, j\}. Each request updates the table in O(n2)O(n^2) time.

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

int main() {
    const int n = 12, steps = 3000;
    std::mt19937 rng(18);
    std::vector<double> x(n), y(n);
    for (int i = 0; i < n; ++i) { x[i] = rng() % 1000; y[i] = rng() % 1000; }
    auto d = [&](int a, int b) { return std::hypot(x[a] - x[b], y[a] - y[b]); };
    std::vector<std::vector<double>> w(n, std::vector<double>(n));
    int A = 0, B = 1;                                           // start configuration {0, 1}
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            w[i][j] = std::min(d(A, i) + d(B, j), d(A, j) + d(B, i));
    double wfa = 0, greedy = 0;
    int gA = 0, gB = 1;
    for (int t = 0; t < steps; ++t) {
        int r = rng() % 4 == 0 ? rng() % n : rng() % 4;         // mostly requests among points 0..3
        std::vector<std::vector<double>> nw(n, std::vector<double>(n));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                nw[i][j] = std::min(w[r][j] + d(r, i), w[i][r] + d(r, j));   // move the server at i or at j to r
        w.swap(nw);
        if (r != A && r != B) {
            if (w[r][B] + d(A, r) <= w[A][r] + d(B, r)) { wfa += d(A, r); A = r; }
            else { wfa += d(B, r); B = r; }
        }
        if (r != gA && r != gB) {
            if (d(gA, r) <= d(gB, r)) { greedy += d(gA, r); gA = r; }
            else { greedy += d(gB, r); gB = r; }
        }
    }
    double opt = 1e18;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) opt = std::min(opt, w[i][j]);
    std::printf("2 servers, %d points, %d requests: OPT %.0f, WFA %.0f (%.3f), greedy %.0f (%.3f)\n",
                n, steps, opt, wfa, wfa / opt, greedy, greedy / opt);
}
2 servers, 12 points, 3000 requests: OPT 569892, WFA 676183 (1.187), greedy 626820 (1.100)

The picture is the same as on the cycle: greedy is a little cheaper on random requests, and WFA's advantage is its worst-case guarantee.

Why WFA is not used in practice

WFA's state is the whole work function: one number per configuration, (nk)\binom{n}{k} of them for nn points, and its update needs a minimum over kk predecessors each. For paging with a large universe or kk-server in a continuous space this is hopeless, so practical systems use simpler rules (LRU-like heuristics for caches, greedy with corrections for vehicle dispatch). WFA's role is theoretical: it is the candidate for proving the kk-server conjecture, and the strongest evidence that a deterministic O(k)O(k) ratio is possible on every metric.

Test yourself

What does wt(X)w_t(X) denote?

  1. The online cost after tt requests
  2. The least cost of serving the first tt requests from the start configuration and ending in configuration XX
  3. The distance between XX and the request
  4. The number of servers at the request

Which server does WFA move for request rr from configuration AA?

  1. The closest one
  2. The a∈Aa \in A minimizing wt(A−a+r)+d(a,r)w_t(A - a + r) + d(a, r)
  3. The one minimizing wt(A−a+r)w_t(A - a + r) alone
  4. A random one

What is the best competitive ratio proved for WFA on general metrics?

  1. kk
  2. 2k−12k - 1
  3. O(log⁡k)O(\log k)
  4. 2k2^k

Why is WFA rarely implemented in practice?

  1. It is randomized
  2. Its state has one value per configuration, (nk)\binom{n}{k} in total, which is far too large for real systems
  3. It is not competitive
  4. It needs the future