Double Coverage on the Line and on Trees
Greedy fails for -server because it never moves a far server (The k-Server Problem). Double coverage (DC) fixes that in the simplest possible way: when a request falls between two servers, both neighbours move towards it at the same speed, so the far server is always dragged along a little. Chrobak, Karloff, Payne and Vishwanathan (1991) proved that DC is -competitive on the line, matching the lower bound, and Chrobak and Larmore (1991) extended the algorithm and the bound to trees. The proof is a potential-function argument worth knowing by heart: it is the model for most -server upper bounds.
The algorithm on the line
Servers sit at points of the real line. For a request :
DOUBLE-COVERAGE(servers, r)
0 if some server is at r: do nothing
1 else if r is left of every server or right of every server
2 move the nearest server to r
3 else // r lies between two adjacent servers a < r < b
4 delta = min(r - a, b - r)
5 move a right by delta and b left by delta // one of them reaches rThe algorithm is not lazy: in case 3 it moves a server that does not serve the request. That extra movement is the point, and it costs at most a factor 2 per request. The program traces DC with three servers.
def double_coverage(servers, r):
s = sorted(servers)
if r in s:
return s, 0, "already covered"
if r < s[0]:
cost, s[0] = s[0] - r, r
return s, cost, f"left of all: move {s[0] + cost} -> {r}"
if r > s[-1]:
cost, s[-1] = r - s[-1], r
return s, cost, f"right of all: move {s[-1] - cost} -> {r}"
i = max(j for j in range(len(s)) if s[j] < r) # neighbours s[i] < r < s[i + 1]
a, b = s[i], s[i + 1]
delta = min(r - a, b - r)
s[i], s[i + 1] = a + delta, b - delta
return s, 2 * delta, f"between {a} and {b}: both move {delta} -> {s[i]}, {s[i + 1]}"
servers, total = [0, 10, 20], 0
for r in [12, 16, 5, 25, 15, 14, 0]:
servers, cost, note = double_coverage(servers, r)
total += cost
print(f"request {r:2}: {note:38} servers {servers}, cost {cost}")
print("total", total)request 12: between 10 and 20: both move 2 -> 12, 18 servers [0, 12, 18], cost 4
request 16: between 12 and 18: both move 2 -> 14, 16 servers [0, 14, 16], cost 4
request 5: between 0 and 14: both move 5 -> 5, 9 servers [5, 9, 16], cost 10
request 25: right of all: move 16 -> 25 servers [5, 9, 25], cost 9
request 15: between 9 and 25: both move 6 -> 15, 19 servers [5, 15, 19], cost 12
request 14: between 5 and 15: both move 1 -> 6, 14 servers [6, 14, 19], cost 2
request 0: left of all: move 6 -> 0 servers [0, 14, 19], cost 6
total 47
The proof: DC is -competitive on the line
Theorem (Chrobak, Karloff, Payne and Vishwanathan, 1991). For servers on the line, for every request sequence, where depends only on the initial positions (and is 0 if DC and OPT start in the same configuration with all servers at one point).
Let be DC's servers and OPT's. Let be the cost of a minimum-cost matching between them (on the line: sort both and match in order), and , the total pairwise distance between DC's servers. The potential is
For each request, let OPT move first and then DC. It suffices to show: (a) when OPT moves a server by , ; (b) when DC serves the request at cost , . Summing over all requests, , and .
Proof of (a) and (b)
(a) OPT moves. Moving one of OPT's servers by changes the cost of any fixed matching by at most , so grows by at most ; does not change. Hence . After OPT's move, OPT has a server at .
(b) DC moves, request outside the hull. Say is left of all of DC's servers and DC moves by . In the sorted matching, OPT's server at (its leftmost position or tied with it) is matched to , and moves straight towards it, so drops by . Every other server of DC is to the right of , so each of the distances from grows by : . Hence .
(b) DC moves, request between and . Both move by , total cost . There is a minimum matching in which OPT's server at is matched to or to (any matching that crosses can be uncrossed without increasing its cost). That server moves closer to its partner, and the other one moves , possibly away from its partner: . For : and get closer to each other, and every other server lies outside , so its distance to one of them grows by and to the other shrinks by . So , and .
The program checks (a) and (b) numerically: it computes OPT with dynamic programming, recovers OPT's actual moves, interleaves them with DC's, and verifies for every OPT move and for every DC move.
import random
def matching(a, b):
return sum(abs(x - y) for x, y in zip(sorted(a), sorted(b)))
def pairwise(s):
return sum(abs(s[i] - s[j]) for i in range(len(s)) for j in range(i + 1, len(s)))
def dc_step(s, r):
s = sorted(s)
if r in s:
return s, 0
if r < s[0] or r > s[-1]:
i = 0 if r < s[0] else len(s) - 1
c, s[i] = abs(s[i] - r), r
return s, c
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
return s, 2 * delta
def opt_path(start, reqs):
"""Lazy offline optimum with back pointers: returns the configurations after each request."""
layers = [{tuple(sorted(start)): (0, None)}]
for r in reqs:
nxt = {}
for conf, (c, _) in layers[-1].items():
opts = [(conf, 0)] if r in conf else [(tuple(sorted(conf[:i] + (r,) + conf[i + 1:])), abs(conf[i] - r)) for i in range(len(conf))]
for new, extra in opts:
if new not in nxt or c + extra < nxt[new][0]:
nxt[new] = (c + extra, conf)
layers.append(nxt)
conf = min(layers[-1], key=lambda x: layers[-1][x][0])
path = [conf]
for t in range(len(reqs), 0, -1):
conf = layers[t][conf][1]
path.append(conf)
return path[::-1], min(c for c, _ in layers[-1].values())
rng = random.Random(13)
k = 3
start = [0, 0, 0]
reqs = [rng.randrange(0, 13) for _ in range(150)]
path, opt = opt_path(start, reqs)
dc, dc_cost, worst_a, worst_b = list(start), 0, -1e9, -1e9
phi = lambda s, o: k * matching(s, o) + pairwise(s)
for t, r in enumerate(reqs):
o_old, o_new = path[t], path[t + 1]
x = matching(o_old, o_new) # OPT's movement
worst_a = max(worst_a, phi(dc, o_new) - phi(dc, o_old) - k * x)
new_dc, c = dc_step(dc, r)
worst_b = max(worst_b, phi(new_dc, o_new) - phi(dc, o_new) + c)
dc, dc_cost = new_dc, dc_cost + c
print(f"k = {k}, {len(reqs)} random requests on 0..12: DC {dc_cost}, OPT {opt}, ratio {dc_cost / opt:.3f}")
print(f"max of dPhi - k*x over OPT moves: {worst_a} (must be <= 0)")
print(f"max of dPhi + c over DC moves: {worst_b} (must be <= 0)")k = 3, 150 random requests on 0..12: DC 302, OPT 172, ratio 1.756
max of dPhi - k*x over OPT moves: 0 (must be <= 0)
max of dPhi + c over DC moves: 0 (must be <= 0)
Both maxima are at most 0, as the proof requires; on random requests DC's actual ratio is far below .
In C++
DC with the servers kept in a sorted vector, so the neighbours of a request are found by binary search. The program replays the sequence that defeats greedy (two servers, requests alternating at 1 and 0 with the second server at 100) and a seeded random sequence.
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <vector>
long long dc_serve(std::vector<long long>& s, long long r) { // s sorted
auto it = std::lower_bound(s.begin(), s.end(), r);
if (it != s.end() && *it == r) return 0;
if (it == s.begin()) { long long c = s.front() - r; s.front() = r; return c; }
if (it == s.end()) { long long c = r - s.back(); s.back() = r; return c; }
long long& b = *it;
long long& a = *(it - 1);
long long delta = std::min(r - a, b - r);
a += delta;
b -= delta;
return 2 * delta;
}
int main() {
std::vector<long long> s = {0, 100};
long long cost = 0;
for (int t = 0; t < 1000; ++t) cost += dc_serve(s, t % 2 == 0 ? 1 : 0);
std::printf("alternating 1, 0 (1000 requests): DC cost %lld, servers end at %lld and %lld (OPT 99)\n",
cost, s[0], s[1]);
std::mt19937 rng(14);
std::vector<long long> t = {0, 250, 500, 750, 1000};
long long total = 0;
for (int i = 0; i < 100000; ++i) total += dc_serve(t, rng() % 1001);
std::printf("5 servers, 100000 uniform requests on [0, 1000]: average cost per request %.2f\n", total / 100000.0);
}alternating 1, 0 (1000 requests): DC cost 297, servers end at 0 and 1 (OPT 99)
5 servers, 100000 uniform requests on [0, 1000]: average cost per request 111.85
On the alternating sequence each request between the two servers drags the far one closer, so after a few requests both servers are near the action and DC stops paying. Its total cost, 297, respects the theorem's bound , where is the initial distance between the servers; the bound is almost tight here. Greedy's cost, by contrast, grows forever.
Double coverage on trees
On a tree metric (a weighted tree, with points anywhere on its edges), Chrobak and Larmore (1991) generalized DC as follows. A server is a neighbour of the request if no other server lies on its path to (servers at the same point count once). All neighbours move towards at the same speed; whenever a moving server's path becomes blocked by another server reaching a point on it, it stops; the motion ends when one server reaches . On a line, the neighbours are the two adjacent servers (or one, outside the hull), so this is the line algorithm.
Theorem (Chrobak and Larmore, 1991). DC on trees is -competitive.
The proof uses the same potential , with the sum of tree distances between DC's servers, and follows the motion continuously. While neighbours move at unit speed, DC pays at rate . One of them moves towards OPT's server at (matched to it in some minimum matching) and the other may move away from their partners, so changes at rate at most . For : each pair of moving neighbours approaches at rate 2, a total of ; each of the servers that stand still is behind exactly one moving neighbour, which moves away from it, while the other move towards it, a rate of at most per server. Altogether
which is minus DC's cost rate, exactly as on the line. Trees matter beyond themselves: every metric can be embedded into a distribution over trees with expected distortion (Fakcharoenphol, Rao and Talwar, 2003), which turns tree algorithms into randomized algorithms for general metrics with an extra factor.
Test yourself
Servers at 0, 10 and 20, request at 13. What does double coverage do?
- Moves the server at 10 to 13
- Moves 10 and 20 towards 13 by 3 each, ending at 13 and 17
- Moves all three servers
- Moves the server at 20 to 13
What is the potential function in the proof for the line?
- The number of servers away from the request
- times the minimum matching cost between DC's and OPT's servers, plus the sum of pairwise distances between DC's servers
- The distance between the two outermost servers
- OPT's cost so far
When the request lies outside the hull of DC's servers and the nearest server moves by , how do and change?
- falls by and grows by
- Both fall by
- grows by
- Neither changes
Why does double coverage beat greedy on the sequence 1, 0, 1, 0, ... with servers at 0 and 100?
- It moves the far server a little on every request between the two servers, so the far server soon arrives
- It never moves
- It moves only the far server
- It is randomized