Uber wants to cluster pickup points into k stops minimizing total walking distance. Why is this k-median not k-means, and what algorithm actually solves it?

The Problem: A shared-ride team must place pickup stops for 1,200 hourly pickup requests, choosing among 300 legal curb points so that riders’ total walking distance is as small as possible. A teammate’s KMeans prototype reports a good inertia, yet 3 of its 12 centroids sit inside buildings or a park and riders complain about long walks.
Answer
Total walking distance adds up plain distances, so the objective is k-median, while k-means minimizes the sum of squared distances. For a single stop on a street, the median minimizes total walking and the mean minimizes squared walking, and squaring lets a few distant riders drag the stop away from everyone else. Walking also follows sidewalks rather than straight lines, and a stop must sit at a legal curb point. That makes the real problem discrete metric k-median, also called the p-median or facility-location problem, solved over a precomputed walking-distance matrix. Like k-means it is NP-hard, so Lloyd-style alternation only reaches a local optimum with no quality bound. What actually solves it is distance-weighted seeding plus swap local search, provably within of optimal, backed by the exact p-median integer program or its LP bound at city-zone sizes.
(1) Linear Cost, Not Squared: Walking cost grows linearly with distance, while k-means squares it. One rider 300 m away then weighs as much as nine riders 100 m away and pulls the stop toward the fringe.
(2) Median, Not Mean: On a line, total distance is minimized by the median and squared distance by the mean. On a Manhattan grid the optimum is the coordinate-wise median, and with straight-line distance it is the geometric median, found iteratively by Weiszfeld’s algorithm.
(3) Network Distance And Candidate Stops: Walking distance is a shortest path on the pedestrian graph, so is a precomputed matrix, not a formula on coordinates. A mean of graph nodes is undefined, so any update step must pick a medoid from the legal curb points.
(4) NP-Hard, With A Floor: Discrete k-median is NP-hard even for points in the plane, and metric k-median cannot be approximated below a factor of unless P = NP. k-means is NP-hard too, so Lloyd converging is never the same as Lloyd solving.
(5) Swap Search Carries The Guarantee: Local search that swaps up to open stops for closed candidates ends within
of optimal, which is 5 for single swaps. Seeding with probability proportional to distance (not squared distance) gives an expected
-approximate starting point.
Mathematical Formulation:
Where:
is the set of opened stops, chosen from the candidate set
of legal curb points, and
is the number of stops.
is the set of demand points (pickups, or grid cells that aggregate them), indexed by
, and
is the number of requests at point
.
is the walking distance from candidate
to demand point
along the pedestrian network, and
is the distance to the nearest open stop.
assigns demand
to stop
and
opens stop
. The constraints assign every demand point once, only to an open stop, with exactly
stops open. Relaxing both to
gives the LP lower bound.
is the set of k-means centers, free to sit anywhere in the plane,
are pickup coordinates, and
is squared straight-line distance.
and
are the median and mean of rider positions on a single street.
is any solution where no swap of up to
stops lowers the cost, and
is the optimal k-median cost.
Worked Example:
Five riders wait along one street at 0, 10, 20, 30 and 400 m, and we place a single stop (). The mean is
m and the median is 20 m. At the mean the walks are 92, 82, 72, 62 and 308 m, for a total of 616 m. At the median they are 20, 10, 0, 10 and 380 m, for a total of 420 m. The k-means stop therefore costs 47% more walking (
). It still wins its own objective, with a squared cost of 119,080 square meters at the mean against 145,000 at the median.

Figure 1: (a) Per-rider walks for the worked example. Moving the stop from the median (20 m) to the mean (92 m) saves rider E 72 m but adds 268 m for riders A to D, so total walking rises from 420 m to 616 m. (b) Each cost divided by its own minimum. Total walking bottoms out at the median and squared distance at the mean, so neither stop is optimal for the other objective.
Which Algorithm Actually Solves It:
Because the problem is NP-hard, “solves” means either an exact method fast enough for the instance at hand or an approximation with a proven ratio. The Lloyd analogue for k-median alternates nearest-stop assignment with a per-cluster median or medoid update. It is quick but stalls in local optima, because it never moves a stop from one neighborhood to another. Swap local search (the PAM family) tries replacing an open stop with a closed candidate and keeps any swap that lowers total walking, which is exactly the move Lloyd cannot make. Arya et al. proved every swap-local optimum is within 5 times the global optimum, and within when
stops are swapped at once. LP-rounding algorithms push the best proven ratio below 2.7, but in production the swap solution is usually certified against the LP relaxation rather than replaced by a rounding algorithm.
| Property | Swap local search (PAM, FasterPAM) | Lloyd-style k-medians | k-means (Lloyd) | p-median integer program |
|---|---|---|---|---|
| Objective | Total walking distance, any metric | Total walking distance | Sum of squared straight-line distances | Total walking distance, any distance matrix |
| Where stops go | Only candidate curb points | Medoids among candidates, or off-network geometric medians | Anywhere, often inside blocks | Only candidate curb points |
| Guarantee | Within 3 + 2/t of optimal (5 for single swaps) | Local optimum, no bound | Local optimum of the wrong objective | Optimal, or a certified gap from the LP bound |
| Cost | About k times |F| swap checks per pass; FasterPAM removes a factor of k | One assignment plus one medoid update per iteration | Cheapest, O(nk) per iteration | Exponential worst case; p-median LPs are often nearly integral |
| Best use | Default solver at production scale | Fast warm start | When the true cost really is variance | Offline planning and certifying heuristics |







