Fast Exact k-Means, k-Medians and Bregman Divergence Clustering in 1D

Allan Grønlund, Kasper Green Larsen, Alexander Mathiasen, Jesper Sindahl NielsenarXiv 2017

In the world of machine learning, $k$-Means clustering is a ubiquitous tool. We usually accept that finding the absolute best clustering is an "NP-hard" problem—meaning it's computationally intractable for large datasets—and we settle for heuristics like Lloyd’s algorithm (often called $k$-means++). However, this hardness only applies to data in two or more dimensions. In the one-dimensional case, we can actually find the exact optimal solution efficiently.

This paper by Grønlund et al. serves as both a "greatest hits" collection of forgotten 1D clustering algorithms and a significant leap forward in making these techniques practical for modern data scales.

TL;DR

While $k$-Means is NP-hard in general, 1D clustering can be solved exactly using dynamic programming. This paper unifies decades of overlooked research, generalizes exact solutions to all Bregman Divergences (like KL-divergence), and introduces techniques to reduce memory usage from $O(kn)$ to $O(n)$, making exact 1D clustering viable for massive datasets.

What Problem Does This Paper Address?

Clustering in 1D is surprisingly common—think of grouping income levels, analyzing bioinformatics sequences, or segmenting retail markets. While the machine learning community has largely relied on an $O(kn^2)$ dynamic programming (DP) approach for 1D $k$-Means, the authors discovered that faster algorithms existed in the "discrete quantization" literature from the 1980s that had been entirely overlooked.

The gaps the authors fill are:

  1. Efficiency: Standard DP is slow ($O(kn^2)$) and memory-intensive ($O(kn)$).
  2. Generalization: Most research focused only on squared Euclidean distance ($k$-Means), ignoring $k$-Medians and other distance measures.
  3. Flexibility: Existing tools didn't easily allow for "model selection" (finding the best $k$ on the fly).

Methods

The core of the paper revolves around a mathematical property called Monge Concavity. If a cost function is Monge concave, it means the "optimal breakpoints" between clusters move in a predictable way as you add more data. This allows for several powerful optimizations:

  • SMAWK Algorithm: A classic matrix-searching algorithm that reduces the time complexity of a DP step from $O(n)$ to $O(1)$ amortized. This brings the total time down to $O(kn)$.
  • Regularized $k$-Means: Instead of fixing $k$, the authors add a penalty $\lambda$ for each cluster used. They show that this regularized version can be solved in a staggering $O(n)$ time if the data is sorted.
  • Binary Search on $\lambda$ (Wilber’s Algorithm): By searching for the right penalty $\lambda$ that results in exactly $k$ clusters, they can often beat the $O(kn)$ time of standard DP.
  • Hirschberg’s Technique: A divide-and-conquer strategy used to reconstruct the optimal clusters using only $O(n)$ space, solving the "memory explosion" problem that plagues standard DP.
  • Bregman Divergence Generalization: The authors prove that the Monge property holds for all Bregman Divergences, allowing these fast algorithms to work for a wide variety of statistical distances.

Datasets

The authors evaluated their implementations on two synthetic datasets designed to test different data distributions:

  1. Uniform: Points sampled uniformly at random between 0 and 1.
  2. Gaussian Mixture: 16 distinct Gaussian distributions with high variance, placed very far apart (means 1 million units apart) to create clear but complex cluster structures.

Key Insights and Ideas

  • The 1D Advantage: In 1D, an optimal clustering is always a set of non-overlapping intervals. This structure is what makes the Monge property possible.
  • Forgotten History: The paper highlights a common issue in computer science: the "siloing" of research. Algorithms developed for signal quantization decades ago were exactly what the ML community needed for 1D clustering.
  • Space is the Real Bottleneck: For modern "Big Data," $O(kn)$ space is often more restrictive than $O(kn)$ time. The reduction to $O(n)$ space via Hirschberg’s technique is perhaps the most practical contribution for engineers.
  • The Power of Regularization: The fact that the regularized version (penalty per cluster) is solvable in linear time is a major "aha" moment, providing a path to much faster algorithms via binary search.

Key Results

  • Speedup: The binary search approach (Wilber) proved significantly faster than DP variants as $k$ increased. For $N=1,000,000$ and large $k$, Wilber took seconds while DP took minutes.
  • Memory Efficiency: The authors successfully implemented $O(n)$ space versions of all major algorithms, allowing them to process tens of millions of points on standard hardware.
  • Theoretical Unification: They provided the first proof that these fast matrix-searching techniques apply to any Bregman Divergence, not just $k$-Means.

Limitations and Future Work

  • Sorted Input: All these algorithms assume the input is already sorted. If not, an $O(n \log n)$ sorting step is required, which becomes the bottleneck for the $O(n)$ regularized version.
  • Bit Complexity: The fastest theoretical algorithm ($O(n \log U)$) depends on $U$, the number of bits used to represent the coordinates. While fast in practice, its performance can vary based on data precision.
  • Parallelization: While the authors mention that $O(kn)$ DP is easy to parallelize, they leave the parallelization of the more complex $O(n)$ regularized versions as an open challenge.
  • Open Question: The authors pose whether an $O(n \log^{O(1)} n)$ algorithm exists to compute the optimal costs for all values of $k$ simultaneously.

Summary updated

BibTeX
@misc{grnlund2017fast,
  author = {Allan Grønlund and Kasper Green Larsen and Alexander Mathiasen and Jesper Sindahl Nielsen},
  title = {{Fast Exact k-Means, k-Medians and Bregman Divergence Clustering in 1D}},
  year = {2017},
  eprint = {1701.07204},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1701.07204},
}

Loading related content...

Figure