arXiv:2611.01207cs.LGOct 2026

Manuscript Offprint

arXiv:2611.01207

A Batched Hartigan's kk-Means Clustering

Edward Raff, Michael Slawinski

Abstract

Hartigan's kk-means algorithm has theoretical advantages over the standard Lloyd's variant, which has become synonymous with “kk-means”. Yet, Hartigan's algorithm is far slower in practice due to its inherently sequential calculations. We devise a Batched Hartigan's method that enables vectorized calculation by exploiting the triangle inequality to expose a set of parallel-safe updates and a smaller set of sequential updates. This makes Hartigan's method competitive with Lloyd's in execution speed for the first time.

1 Introduction

The kk-means algorithm has been a workhorse of machine learning for over 50 years [21], with the simple procedure originally proposed by Lloyd still the dominant version of the algorithm fundamentally in use continuously since its proposal [19] and first presented in 1957. Yet, another old variant of kk-means was proposed by Hartigan and Wong [14] has been consistently shown to be a theoretical Pareto improvement over the original Lloyd variant. As we will review soon, Hartigan and Wong's method (or Hartigan for short) converges to a strictly superior set of minima, in fewer epochs, with monotonically decreasing objective values in each iteration of the dataset. We posit that Hartigan's variant of kk-means has been neglected due to computational cost. Despite having theoretically fewer calculations to perform, the original algorithm is entirely sequential and thus eliminates almost all opportunities for efficient vectorized operations afforded by Basic Linear Algebra Subprograms (BLAS), the standard compiled routines for vectorized linear algebra available in every scientific computing environment [7, 8, 15]. Our implementation relies only on BLAS, specifically GEneral Matrix Multiply (GEMM), the level-3 BLAS operation computing C=αAB+βCC = \alpha AB + \beta C, which achieves near-peak CPU throughput through cache blocking and single-instruction, multiple-data (SIMD) vectorization. We use GEMM for batch distance computation via the identity ∥x−c∥2=∥x∥2−2x⋅c+∥c∥2\|\mathbf{x} - \mathbf{c}\|^2 = \|\mathbf{x}\|^2 - 2\mathbf{x} \cdot \mathbf{c} + \|\mathbf{c}\|^2. Production BLAS uses the classical matrix-multiplication complexity: sub-cubic algorithms such as Strassen have large constants and weaker numerical stability and, to our knowledge, are not used in production BLAS. The advantage here is therefore constant-factor cache and SIMD efficiency, not a different asymptotic complexity.

By using the triangle inequality, we expose parallelism in the computation of Hartigan's algorithm that was not possible previously. Two key insights are required for our approach. First, in a modern system with large datasets and large kk, the maximum change on a per-item basis to a given centroid is small, which enables safely determining most cluster assignments in bulk operations. Second, that the random order normally used for Hartigan's algorithm is not a necessary condition for any of its desirable properties, and so bulk operations can re-order points into a “parallel” set and a smaller “sequential” set, allowing us to vectorize the implementation and significantly improve runtime.

The rest of this article is organized as follows. First, we will review the related work to our own in section 2, covering the theory motivating the use of Hartigan's algorithm, along with the development of triangle inequality and faster kk-means variants. Second, we will describe Hartigan-Wong's variation of kk-means in detail in section 3, and its inherently sequential design. This motivates our improved

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

variant in section 4, where we use the triangle inequality to not just prune calculations, but expose batch calculations not previously available for efficient vectorization. Our results will show in section 5 that our Batched Hartigan algorithm maintains the same solution quality as the standard Hartigan algorithm, is usually 10× faster than previous implementations of Hartigan's algorithm, and is competitive with modern fast implementations of Lloyd's k-means algorithm. Finally, we will conclude in section 6.

References (26)