k-Center and Farthest-First Traversal
Where should fire stations go so that no house is far from its nearest station? The -center problem formalizes this: given points with a metric and an integer , choose a set of centres minimizing the radius
It is NP-hard, and this lesson shows both sides of a sharp threshold: a greedy algorithm achieves factor 2, and factor is NP-hard for every .
Farthest-first traversal
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 SKeeping for every point and updating it after each new centre gives time. The algorithm is due to Gonzalez (1985); the same traversal is used to seed clustering heuristics such as -means++ (which samples instead of maximizing).
Theorem (Gonzalez, 1985). Farthest-first traversal returns with .
Proof. Let be a point farthest from , at distance , and consider the points . When was chosen it was the farthest point from , and distances to a growing set only shrink, so it was at distance at least from all earlier centres. Hence these points are pairwise at distance at least . An optimal solution has only centres, so two of the points, say and , share the same optimal centre . Then .
The lower bound is a set of points that are pairwise far apart: no balls of radius below half their distance can cover them. Note that the proof does not care which point starts.
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++
#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 pairwise distances. For a guess , build the graph joining points at distance at most , and greedily pick a maximal set of points pairwise at distance more than (an independent set in the square of ). If , then , because no centre can serve two points at distance more than within radius . If , then itself covers everything within by maximality. The smallest that passes gives a solution of radius at most .
Why 2 is the end of the road
Theorem (Hsu and Nemhauser, 1979). For every , there is no -approximation for metric -center unless P = NP.
Proof. Reduce from dominating set: given a graph and , is there a set of vertices such that every vertex is in or adjacent to it? It is NP-complete. Define for edges and for non-adjacent pairs (). This is a metric: every distance is 1 or 2, and . If has a dominating set of size , the optimal radius is 1; otherwise it is 2. A -approximation would return radius below 2, hence exactly 1, in the first case, and radius 2 in the second, deciding dominating set.
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.
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 exists, and 2 otherwise.
Without the triangle inequality the situation is worse still: the same reduction with non-edges of length for any huge shows that no polynomial algorithm achieves any ratio below , so general -center has no approximation at all unless P = NP. The related -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 (Jain, Mahdian and Saberi, 2002).
Test yourself
In the proof for farthest-first traversal, why are the centres and the farthest point pairwise at distance at least ?
- Because the points are in the plane
- 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 away
- Because OPT is at least
- Because is small
What finishes the proof that ?
- The triangle inequality applied to two of the far-apart points that share an optimal centre
- Linear programming duality
- The fact that the points are integers
- A greedy exchange argument
Why does a -approximation for metric -center decide dominating set?
- Because dominating set is in P
- In the reduction every distance is 1 or 2, so the optimum is 1 (a dominating set of size exists) or 2, and a ratio below 2 must return 1 in the first case
- Because -center has an FPTAS
- Because the instance is not metric