NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Exact Acceleration of K-Means++ and K-Means||

Edward Raff

Abstract

K-means clustering is a fundamental tool in machine learning, but its performance is highly dependent on initial seed selection. While K-Means++ and its distributed variant K-Means|| provide theoretical quality guarantees, they are computationally expensive, often accounting for a significant portion of total clustering time. This paper introduces the first "exact" acceleration techniques for these seeding algorithms. By leveraging triangle inequality pruning and a novel dynamic priority queue, the authors achieve speedups of up to 17× for K-Means++ and 551× for K-Means|| without altering the final clustering results.

Introduction

The $K$-means algorithm is a cornerstone of unsupervised learning, yet its initialization phase is a notorious bottleneck. Selecting $K$ seeds for a dataset of $n$ items requires $O(nK)$ distance computations. In modern large-scale applications where $K$ can reach tens of thousands, this seeding process can consume nearly half of the total execution time. While triangle inequality pruning has long been used to accelerate the iterative phase of $K$-means, this work successfully applies it to the initialization phase for the first time, providing a mathematically equivalent but significantly faster implementation.

Methodology

The proposed acceleration relies on three primary innovations designed to reduce redundant distance calculations:

1. Triangle Inequality Pruning

The algorithm tracks the distance $\alpha_i$ from each point $x_i$ to its nearest already-selected seed. When a new seed $m_k$ is chosen, the distance $d(x_i, m_k)$ is only computed if there is a mathematical possibility that $m_k$ is closer than the current best seed. By utilizing the distance between the previous seeds and the new seed, the algorithm prunes a vast majority of unnecessary computations.

2. Dynamic Priority Queue

Standard sampling in K-Means++ is often hindered by "subnormal" floating-point slowdowns during normalization. To circumvent this, the authors implement a weighted sampling-without-replacement strategy using a priority queue. Because the distance $\alpha_i$ is monotonically non-increasing as more seeds are added, the algorithm can perform "lazy" updates. Items in the queue are marked as "dirty" and their priorities are only recalculated when they reach the top of the queue.

The dynamic priority queue mechanism allows for lazy updates by marking items as "dirty" and only re-calculating priorities when they reach the top of the queue.

3. NearestInRange Queries for K-Means||

For the distributed K-Means|| variant, which samples multiple candidates simultaneously, the authors build a Vantage Point (VP) tree on the new candidates. They introduce a NearestInRange query that uses the existing distance $\alpha_i$ as a pruning radius. If no new candidate falls within this radius, the search terminates early, skipping entire branches of the tree.

Experimental Evaluation

The authors evaluated their approach on 11 diverse datasets, ranging from low-dimensional spatial data to high-dimensional image features (up to $D=780$) and scales reaching 45 million samples.

Key Results

  • Distance Computation Reduction: In the most favorable cases, distance computations were reduced by over 500×.
  • Wall-Clock Speedup: K-Means++ achieved up to a 17× speedup (notably on the covertype dataset). K-Means|| demonstrated even more dramatic improvements, reaching a staggering 551× speedup on the Activity Recognition dataset.
  • Efficiency: By employing the dynamic priority queue, the algorithm frequently examines less than 1% of the dataset to select the next seed.
Speedup of K-Means|| across various datasets as a function of K, demonstrating significant performance gains reaching over 500x.

Discussion and Conclusion

The core insight of this work is that the monotonicity of the distance to the nearest seed ($\alpha_i$) allows for highly efficient lazy updates. Because these methods are "exact," they serve as a drop-in replacement for any existing library, offering identical results with significantly lower latency.

However, the effectiveness of triangle inequality pruning is subject to the "curse of dimensionality"; for datasets where $D > 1024$, the pruning becomes less effective. Future research may extend these lazy-update priority queue techniques to other distance-based sampling algorithms, such as coreset construction or streaming change detection.

Summary updated

BibTeX
@inproceedings{raff2021exact,
  author = {Edward Raff},
  title = {{Exact Acceleration of K-Means++ and K-Means||}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2021},
  doi = {10.24963/ijcai.2021/403},
}
Figure