The Work-Function Algorithm
Double coverage is -competitive, but only on trees. For arbitrary metric spaces the best deterministic -server algorithm known is the work-function algorithm (WFA). Koutsoupias and Papadimitriou (1995) proved that it is -competitive on every metric space, the first bound linear in 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 -server
A configuration is a set of points, the server positions. For a starting configuration and requests , the work function is the least cost of serving starting from and ending in configuration . It obeys a simple recurrence: to end in after serving , some configuration containing was reached first, and with lazy moves that means
where is the minimum-cost matching distance between configurations. In particular whenever , and . The same object appeared for metrical task systems, with configurations as the states.
The algorithm
On request , WFA is in configuration . It serves with the server that minimizes
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 + rThe two terms pull in different directions. Minimizing alone is greedy, which is not competitive. Minimizing 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 -competitive for the -server problem.
The proof works with the extended cost , 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 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 -competitive for (where it matches the of Manasse, McGeoch and Sleator), on the line and on the star, and on metrics with points (Bartal and Koutsoupias, 2004); whether it is -competitive on every metric is open and would prove the -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.
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 -point subset that WFA does not cover.
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 times OPT, the lower bound for every deterministic algorithm, and for 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 points in the plane (Euclidean distances, seeded positions), with the work function stored as an table: for the configuration . Each request updates the table in time.
#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, of them for points, and its update needs a minimum over predecessors each. For paging with a large universe or -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 -server conjecture, and the strongest evidence that a deterministic ratio is possible on every metric.
Test yourself
What does denote?
- The online cost after requests
- The least cost of serving the first requests from the start configuration and ending in configuration
- The distance between and the request
- The number of servers at the request
Which server does WFA move for request from configuration ?
- The closest one
- The minimizing
- The one minimizing alone
- A random one
What is the best competitive ratio proved for WFA on general metrics?
Why is WFA rarely implemented in practice?
- It is randomized
- Its state has one value per configuration, in total, which is far too large for real systems
- It is not competitive
- It needs the future