TExcelTAD
Home › Approximation Algorithms › k-Center and Farthest-First Traversal

k-Center and Farthest-First Traversal

Where should kk fire stations go so that no house is far from its nearest station? The kk-center problem formalizes this: given points VV with a metric dd and an integer kk, choose a set S⊆VS \subseteq V of kk centres minimizing the radius

r(S)=max⁡v∈Vd(v,S),d(v,S)=min⁡s∈Sd(v,s).r(S) = \max_{v \in V} d(v, S), \qquad d(v, S) = \min_{s \in S} d(v, s) .

It is NP-hard, and this lesson shows both sides of a sharp threshold: a greedy algorithm achieves factor 2, and factor 2−ε2 - \varepsilon is NP-hard for every ε>0\varepsilon > 0.

Farthest-first traversal

text
GONZALEZ(V, d, k)
1  S = { an arbitrary point }
2  while |S| < k
3      v = a point maximizing d(v, S)       -- the worst-served point so far
4      S = S + {v}
5  return S

Keeping d(v,S)d(v, S) for every point and updating it after each new centre gives O(nk)O(nk) time. The algorithm is due to Gonzalez (1985); the same traversal is used to seed clustering heuristics such as kk-means++ (which samples instead of maximizing).

Theorem (Gonzalez, 1985). Farthest-first traversal returns SS with r(S)≤2 OPTr(S) \le 2\,\text{OPT}.

Proof. Let v∗v^* be a point farthest from SS, at distance r=r(S)r = r(S), and consider the k+1k + 1 points s1,…,sk,v∗s_1, \ldots, s_k, v^*. When sjs_j was chosen it was the farthest point from {s1,…,sj−1}\{s_1, \ldots, s_{j-1}\}, and distances to a growing set only shrink, so it was at distance at least rr from all earlier centres. Hence these k+1k + 1 points are pairwise at distance at least rr. An optimal solution has only kk centres, so two of the k+1k + 1 points, say xx and yy, share the same optimal centre c∗c^*. Then r≤d(x,y)≤d(x,c∗)+d(c∗,y)≤2 OPTr \le d(x, y) \le d(x, c^*) + d(c^*, y) \le 2\,\text{OPT}. □\square

The lower bound is a set of k+1k + 1 points that are pairwise far apart: no kk balls of radius below half their distance can cover them. Note that the proof does not care which point starts.

python · try it yourself
import random
from itertools import combinations
from math import dist

def radius(pts, S):
    return max(min(dist(p, pts[s]) for s in S) for p in pts)

def gonzalez(pts, k, start=0):
    S = [start]
    near = [dist(p, pts[start]) for p in pts]
    while len(S) < k:
        v = max(range(len(pts)), key=lambda i: near[i])
        S.append(v)
        near = [min(near[i], dist(pts[i], pts[v])) for i in range(len(pts))]
    return S

def optimum(pts, k):
    return min(radius(pts, S) for S in combinations(range(len(pts)), k))

rng = random.Random(9)
print(" n  k   greedy   OPT    ratio")
for n, k in [(12, 2), (12, 3), (14, 3), (14, 4), (16, 4)]:
    pts = [(rng.uniform(0, 100), rng.uniform(0, 100)) for _ in range(n)]
    g = radius(pts, gonzalez(pts, k))
    o = optimum(pts, k)
    print(f"{n:2} {k:2} {g:8.2f} {o:6.2f} {g / o:7.3f}")
 n  k   greedy   OPT    ratio
12  2    52.44  49.72   1.055
12  3    37.93  37.69   1.007
14  3    57.89  45.97   1.259
14  4    41.51  26.75   1.552
16  4    45.27  38.06   1.189

In C++

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

int main() {
    std::vector<std::pair<double, double>> p = {{0, 0}, {2, 1}, {1, 3}, {10, 10}, {11, 12}, {9, 11}, {20, 0}, {21, 2}, {19, 1}, {10, 0}};
    int n = p.size(), k = 3;
    auto d = [&](int a, int b) { return std::hypot(p[a].first - p[b].first, p[a].second - p[b].second); };
    std::vector<int> S = {0};
    std::vector<double> near(n);
    for (int i = 0; i < n; i++) near[i] = d(i, 0);
    while ((int)S.size() < k) {
        int v = 0;
        for (int i = 1; i < n; i++) if (near[i] > near[v]) v = i;
        S.push_back(v);
        for (int i = 0; i < n; i++) near[i] = std::min(near[i], d(i, v));
    }
    double r = 0;
    for (int i = 0; i < n; i++) r = std::max(r, near[i]);
    std::printf("centres:");
    for (int s : S) std::printf(" %d", s);
    std::printf("\nradius %.3f\n", r);
}
centres: 0 7 5
radius 10.000

The threshold approach

Hochbaum and Shmoys (1985) gave a second 2-approximation that generalizes better (for example to weighted versions). The optimal radius is one of the O(n2)O(n^2) pairwise distances. For a guess rr, build the graph GrG_r joining points at distance at most rr, and greedily pick a maximal set II of points pairwise at distance more than 2r2r (an independent set in the square of GrG_r). If ∣I∣>k\lvert I \rvert > k, then r<OPTr < \text{OPT}, because no centre can serve two points at distance more than 2r2r within radius rr. If ∣I∣≤k\lvert I \rvert \le k, then II itself covers everything within 2r2r by maximality. The smallest rr that passes gives a solution of radius at most 2 OPT2\,\text{OPT}.

Why 2 is the end of the road

Theorem (Hsu and Nemhauser, 1979). For every ε>0\varepsilon > 0, there is no (2−ε)(2 - \varepsilon)-approximation for metric kk-center unless P = NP.

Proof. Reduce from dominating set: given a graph G=(V,E)G = (V, E) and kk, is there a set DD of kk vertices such that every vertex is in DD or adjacent to it? It is NP-complete. Define d(u,v)=1d(u, v) = 1 for edges and d(u,v)=2d(u, v) = 2 for non-adjacent pairs (u≠vu \ne v). This is a metric: every distance is 1 or 2, and 2≤1+12 \le 1 + 1. If GG has a dominating set of size kk, the optimal radius is 1; otherwise it is 2. A (2−ε)(2 - \varepsilon)-approximation would return radius below 2, hence exactly 1, in the first case, and radius 2 in the second, deciding dominating set. □\square

This is the simplest example of a gap-introducing reduction: the instances it builds have an optimum of either 1 or 2, with nothing in between, so any algorithm with a ratio below the gap must decide an NP-complete problem. Gap-Introducing Reductions develops the method.

python · try it yourself
from itertools import combinations

def kcenter_instance(n, edges):
    E = {frozenset(e) for e in edges}
    return [[0 if u == v else 1 if frozenset((u, v)) in E else 2 for v in range(n)] for u in range(n)]

def best_radius(d, k):
    n = len(d)
    return min(max(min(d[v][s] for s in S) for v in range(n)) for S in combinations(range(n), k))

def dominating(n, edges, k):
    adj = {v: {v} for v in range(n)}
    for u, v in edges:
        adj[u].add(v); adj[v].add(u)
    return any(set().union(*(adj[s] for s in S)) == set(range(n)) for S in combinations(range(n), k))

cycle6 = [(i, (i + 1) % 6) for i in range(6)]
for name, n, E in [("6-cycle", 6, cycle6), ("wheel: 6-cycle plus hub 6", 7, cycle6 + [(6, i) for i in range(6)])]:
    d = kcenter_instance(n, E)
    for k in [1, 2]:
        print(f"{name}, k = {k}: dominating set of size k: {dominating(n, E, k)}, optimal radius {best_radius(d, k)}")
6-cycle, k = 1: dominating set of size k: False, optimal radius 2
6-cycle, k = 2: dominating set of size k: True, optimal radius 1
wheel: 6-cycle plus hub 6, k = 1: dominating set of size k: True, optimal radius 1
wheel: 6-cycle plus hub 6, k = 2: dominating set of size k: True, optimal radius 1

The radius is 1 exactly when a dominating set of size kk exists, and 2 otherwise.

Without the triangle inequality the situation is worse still: the same reduction with non-edges of length LL for any huge LL shows that no polynomial algorithm achieves any ratio below LL, so general kk-center has no approximation at all unless P = NP. The related kk-median problem (minimize the sum of distances to the centres) behaves differently: it has constant-factor approximations through LP rounding and local search, with ratio 2.675 (Byrka, Pensyl, Rybicki, Srinivasan and Trinh, 2015) and small improvements since, and is hard to approximate below 1+2e1 + \frac{2}{e} (Jain, Mahdian and Saberi, 2002).

Test yourself

In the proof for farthest-first traversal, why are the kk centres and the farthest point pairwise at distance at least rr?

  1. Because the points are in the plane
  2. Each centre was the farthest point from the earlier centres when chosen, and distances to a growing set only shrink, so each was at least rr away
  3. Because OPT is at least rr
  4. Because kk is small

What finishes the proof that r(S)≤2 OPTr(S) \le 2\,\text{OPT}?

  1. The triangle inequality applied to two of the k+1k + 1 far-apart points that share an optimal centre
  2. Linear programming duality
  3. The fact that the points are integers
  4. A greedy exchange argument

Why does a (2−ε)(2 - \varepsilon)-approximation for metric kk-center decide dominating set?

  1. Because dominating set is in P
  2. In the reduction every distance is 1 or 2, so the optimum is 1 (a dominating set of size kk exists) or 2, and a ratio below 2 must return 1 in the first case
  3. Because kk-center has an FPTAS
  4. Because the instance is not metric