TL;DR
For over half a century, "k-means" has essentially meant Lloyd’s algorithm—even though a lesser-known 1979 variant by Hartigan and Wong finds strictly better clusterings in half as many epochs. Nobody uses Hartigan’s method today because its strictly one-point-at-a-time math starves modern, parallel-hungry CPUs. By flipping the triangle inequality from a simple pruning tool into a safety certificate for bulk matrix operations, this paper makes Hartigan’s algorithm up to 33× faster—and finally speed-competitive with modern k-means libraries.
What Problem Does This Paper Address?
When you call KMeans in your favorite machine learning library, you are almost certainly running a variant of Lloyd’s 1957 algorithm. Lloyd’s method looks at all data points, assigns them to their nearest cluster centers (centroids), and only updates those centroids at the very end of the pass. Because every point is evaluated against static centroids, computers can crunch all those distances simultaneously using GEMM (General Matrix Multiply)—the hyper-optimized, cache-friendly linear algebra routine at the heart of modern scientific computing.
Hartigan’s algorithm takes a smarter, more meticulous approach. Before moving a single point to a new cluster, it asks: if I move this point, how will both the old and new centroids shift, and does that net shift actually improve the overall clustering objective? Because it accounts for centroid movement on the fly, Hartigan’s method avoids empty clusters, escapes nasty high-dimensional traps where Lloyd’s gets stuck, and converges to strictly superior local minima.
The catch? Because moving Point 1 immediately changes the centroids for Point 2, the algorithm is inherently sequential. You cannot batch the distance calculations into a fast matrix multiplication, making Hartigan’s method 3× to 100× slower on modern hardware.
Methods
Instead of updating centroids after every single point (standard Hartigan) or waiting until the end of the entire dataset (standard Lloyd), the authors introduce Batched Hartigan, which processes points in medium-sized chunks ($B$ points at a time, typically 64 to 256).
Here is how they break the sequential bottleneck without sacrificing Hartigan’s exact mathematical guarantees:
- Bulk Distance Precomputation: At the start of a batch, the algorithm uses a single, blazing-fast GEMM call to compute the distances from all $B$ points to all $k$ current centroids.
- Certifying Safety via Drift Bounds: As points in the batch start changing clusters, those precomputed distances instantly become "stale" because the centroids are drifting. Using the triangle inequality, the authors bound the worst-case distance each centroid could possibly drift during the batch.
- Parallel-Then-Sequential Partitioning: Using these worst-case bounds, every point’s tentative decision is classified. If a move (or decision to stay put) is still optimal even under worst-case centroid drift, it is certified safe. All safe moves are executed simultaneously in a single bulk update. The tiny remainder of ambiguous points (fewer than 5% in steady state) are then processed sequentially using exact distances.
- Adaptive Batching & Lazy Screening: An exponential moving average tracks how frequently points change clusters, dynamically shrinking the batch size early on (when centroids move wildly) and growing it later. Furthermore, borrowing from Hamerly’s k-means, points that are deeply settled inside their clusters are screened out using cheap $O(1)$ lazy drift trackers before batches are even formed, skipping the GEMM entirely.
Datasets
The authors evaluate their method strictly on existing public benchmarks (introducing no new datasets):
- 13 Dense Benchmarks (spanning $N = 11,055$ to $5,000,000$ points, and dimensions $D = 3$ to $780$):
Letter,MAGIC Gamma,Sepsis,Shuttle,Superconductivity,aloi,cod-rna,connect-4,covtype,mnist,phishing,skin-nonskin, andSUSY. - 11 High-Dimensional / Sparse Benchmarks (up to $D = 150,360$):
breast-cancer,leukemia,duke,bibtex,sector,news20,rcv1_multi,E2006-tfidf,scotus,Wiki10-31K, andreal-sim.
Key Insights and Ideas
- Triangle Inequality for Vectorization, Not Just Pruning: Historically, fast k-means variants (like Elkan’s and Hamerly’s) used the triangle inequality to skip distance calculations—which ironically breaks up bulk matrix operations. Here, the authors flip that paradigm: they willingly compute bulk distances via GEMM first, and use the triangle inequality to prove those bulk calculations remain valid despite ongoing state mutations.
- Large Scale Helps Parallelism: When datasets and cluster sizes are large, pulling a single point out of a cluster barely nudges its centroid. Because individual nudges are tiny, worst-case drift across a batch stays tight, allowing the vast majority of a batch to be safely updated in parallel.
- Order Independence: Standard Hartigan sweeps through points in random or fixed sequential order, leading people to assume the exact sequence is sacred. The authors note that any order of strictly beneficial moves reaches a valid Hartigan fixed point—meaning you are completely free to reorder a batch so all parallel-safe points go first and sequential stragglers go last.
Key Results
Tested single-threaded on an Apple M4 Pro with k-means++ initialization:
- Faster Convergence, Better Optima: Both standard and Batched Hartigan converged in roughly half the epochs of Lloyd’s algorithm (median iteration ratio of 0.56). On dense data, Batched Hartigan matched or beat Lloyd/Elkan sum-of-squared-errors (SSE); on high-dimensional sparse data, it achieved 0.7% to 18.0% lower inertia (e.g., an inertia ratio of 0.8201 on
duke). - Massive Speedups over Prior Hartigan Code: Batched Hartigan outperformed C++ and Numba-JIT baselines on almost every dataset. On
covtype($N=581\text{k}, D=54$), it ran in 1.85s, beating the C++ baseline (31.70s) by 17× and Lederman’s implementation (61.33s) by 33×. - Competitive with Fast Lloyd Baselines: On higher-dimensional problems, Batched Hartigan beat scikit-learn’s default Elkan implementation—clocking 1.04s vs. 1.29s on
mnist($D=780$), 1.85s vs. 2.50s oncovtype, and 16.36s vs. 26.66s on the 5-million-pointSUSYdataset (where it also beat Hamerly’s 20.91s).
Limitations and Future Work
- Low-Dimensional Overhead: On very small datasets or very low dimensions (e.g.,
ShuttleorSepsiswith $D \le 10$), GEMM setup overhead outweighs its benefits, leaving Hamerly’s lightweight $O(ND)$ bound updates faster. - Different Fixed Points: Because re-ordering a batch into "safe-first, ambiguous-second" changes the exact trajectory of greedy moves, Batched Hartigan may land on a different local minimum than sequential Hartigan from the exact same initialization—though it still enjoys all the exact same theoretical guarantees.
- CPU-Only for Now: All experiments were single-threaded CPU runs. The authors candidly note that combining triangle-inequality bounds with massive GPU parallelism remains an open problem; their current batch sizes (64–256) are tuned for CPU caches and are not yet wide enough to saturate a modern GPU, pointing to GPU adaptation as a clear next step.
Summary updated