TExcelTAD
Home › Online Algorithms & Competitive Analysis › Double Coverage on the Line and on Trees

Double Coverage on the Line and on Trees

Greedy fails for kk-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 kk-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 kk-server upper bounds.

The algorithm on the line

Servers sit at points of the real line. For a request rr:

text
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 r

The 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.

python · try it yourself
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 kk-competitive on the line

Theorem (Chrobak, Karloff, Payne and Vishwanathan, 1991). For kk servers on the line, DC(σ)≤k⋅OPT(σ)+Φ0\mathrm{DC}(\sigma) \le k \cdot \mathrm{OPT}(\sigma) + \Phi_0 for every request sequence, where Φ0\Phi_0 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 s1,…,sks_1, \ldots, s_k be DC's servers and o1,…,oko_1, \ldots, o_k OPT's. Let MM be the cost of a minimum-cost matching between them (on the line: sort both and match in order), and D=∑i<j∣si−sj∣D = \sum_{i < j} \lvert s_i - s_j \rvert, the total pairwise distance between DC's servers. The potential is

Φ=k⋅M+D.\Phi = k \cdot M + D .

For each request, let OPT move first and then DC. It suffices to show: (a) when OPT moves a server by xx, ΔΦ≤kx\Delta\Phi \le kx; (b) when DC serves the request at cost cc, ΔΦ≤−c\Delta\Phi \le -c. Summing over all requests, DC+Φend−Φ0≤k⋅OPT\mathrm{DC} + \Phi_{\text{end}} - \Phi_0 \le k \cdot \mathrm{OPT}, and Φend≥0\Phi_{\text{end}} \ge 0.

Proof of (a) and (b)

(a) OPT moves. Moving one of OPT's servers by xx changes the cost of any fixed matching by at most xx, so MM grows by at most xx; DD does not change. Hence ΔΦ≤kx\Delta\Phi \le kx. After OPT's move, OPT has a server at rr.

(b) DC moves, request outside the hull. Say rr is left of all of DC's servers and DC moves s1s_1 by cc. In the sorted matching, OPT's server at rr (its leftmost position or tied with it) is matched to s1s_1, and s1s_1 moves straight towards it, so MM drops by cc. Every other server of DC is to the right of s1s_1, so each of the k−1k - 1 distances from s1s_1 grows by cc: ΔD=(k−1)c\Delta D = (k - 1)c. Hence ΔΦ=−kc+(k−1)c=−c\Delta\Phi = -kc + (k - 1)c = -c.

(b) DC moves, request between aa and bb. Both move by δ\delta, total cost c=2δc = 2\delta. There is a minimum matching in which OPT's server at rr is matched to aa or to bb (any matching that crosses can be uncrossed without increasing its cost). That server moves δ\delta closer to its partner, and the other one moves δ\delta, possibly away from its partner: ΔM≤−δ+δ=0\Delta M \le -\delta + \delta = 0. For DD: aa and bb get 2δ2\delta closer to each other, and every other server lies outside [a,b][a, b], so its distance to one of them grows by δ\delta and to the other shrinks by δ\delta. So ΔD=−2δ\Delta D = -2\delta, and ΔΦ≤−2δ=−c\Delta\Phi \le -2\delta = -c.

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 ΔΦ≤kx\Delta\Phi \le kx for every OPT move and ΔΦ≤−c\Delta\Phi \le -c for every DC move.

python · try it yourself
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 kk.

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.

cpp · try it yourself
#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 k⋅OPT+Φ0=2⋅99+100=298k \cdot \mathrm{OPT} + \Phi_0 = 2 \cdot 99 + 100 = 298, where Φ0=D0=100\Phi_0 = D_0 = 100 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 rr if no other server lies on its path to rr (servers at the same point count once). All neighbours move towards rr 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 rr. 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 kk-competitive.

The proof uses the same potential Φ=k M+D\Phi = k\,M + D, with DD the sum of tree distances between DC's servers, and follows the motion continuously. While jj neighbours move at unit speed, DC pays at rate jj. One of them moves towards OPT's server at rr (matched to it in some minimum matching) and the other j−1j - 1 may move away from their partners, so MM changes at rate at most 1⋅(−1)+(j−1)=j−21 \cdot (-1) + (j - 1) = j - 2. For DD: each pair of moving neighbours approaches at rate 2, a total of −j(j−1)-j(j-1); each of the k−jk - j servers that stand still is behind exactly one moving neighbour, which moves away from it, while the other j−1j - 1 move towards it, a rate of at most 2−j2 - j per server. Altogether

dΦdt≤k(j−2)−j(j−1)+(k−j)(2−j)=−j,\frac{d\Phi}{dt} \le k(j - 2) - j(j - 1) + (k - j)(2 - j) = -j ,

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 O(log⁡n)O(\log n) expected distortion (Fakcharoenphol, Rao and Talwar, 2003), which turns tree algorithms into randomized algorithms for general metrics with an extra O(log⁡n)O(\log n) factor.

Test yourself

Servers at 0, 10 and 20, request at 13. What does double coverage do?

  1. Moves the server at 10 to 13
  2. Moves 10 and 20 towards 13 by 3 each, ending at 13 and 17
  3. Moves all three servers
  4. Moves the server at 20 to 13

What is the potential function in the proof for the line?

  1. The number of servers away from the request
  2. kk times the minimum matching cost between DC's and OPT's servers, plus the sum of pairwise distances between DC's servers
  3. The distance between the two outermost servers
  4. OPT's cost so far

When the request lies outside the hull of DC's servers and the nearest server moves by cc, how do MM and DD change?

  1. MM falls by cc and DD grows by (k−1)c(k-1)c
  2. Both fall by cc
  3. MM grows by cc
  4. Neither changes

Why does double coverage beat greedy on the sequence 1, 0, 1, 0, ... with servers at 0 and 100?

  1. It moves the far server a little on every request between the two servers, so the far server soon arrives
  2. It never moves
  3. It moves only the far server
  4. It is randomized