Relational Algorithms for k-Means Clustering

Benjamin Moseley, Kirk Pruhs, Alireza Samadian, Yuyan Wang

TL;DR

Performing machine learning directly on relational databases usually requires executing costly joins that produce an exponentially large "design matrix." This paper introduces the first polynomial-time randomized algorithm that computes a provable $O(1)$-approximation for $k$-means clustering directly over acyclic relational databases without materializing the join.

What Problem Does This Paper Address?

Data scientists frequently work with normalized relational databases split across tables $T_1, T_2, \dots, T_m$. Standard machine learning libraries expect a single matrix $J = T_1 \bowtie \dots \bowtie T_m$, where each row is an observation and each column is a feature. However, joining these tables can result in an exponential explosion in data size: a database with tables containing at most $n$ rows can produce a design matrix $J$ with $N = \Omega(n^{m/2})$ rows.

Materializing $J$ can exhaust memory and computing resources. Prior work either scaled exponentially with the dimension $d$ (e.g., $k^d$ running time) or relied on heuristics without theoretical guarantees. Seemingly simple tasks also become intractably hard in relational form: the authors prove that simply counting the number of points closer to one center than another is #P-hard, and approximating cluster sizes for $k \ge 3$ is NP-hard. Hence, standard clustering tools like Lloyd’s algorithm cannot be directly evaluated over relational tables.

Methods

To circumvent these computational barriers, the authors develop a two-stage relational pipeline based on coreset construction:

  1. Relational $k$-means++ via Rejection Sampling:
  2. Standard $k$-means++ iteratively samples the next center with probability proportional to its squared distance $L(x) = \min_{j} \|x - c_j\|_2^2$ to previously chosen centers. Because Voronoi cells cannot be efficiently evaluated over relational joins, direct sampling from this target distribution $P$ is intractable.
  3. The authors construct a laminar hierarchy of bounding boxes (hyper-rectangles) around existing centers using a doubling-and-melding procedure. Each hyper-rectangle acts as an axis-aligned filter, which translates into row deletions within individual relational tables.
  4. This structure defines an "easy" proposal distribution $Q(x)$ that upper-bounds $P(x)$ within a factor of $O(i^2 d)$ at step $i$. Sums of squared distances under $Q(x)$ can be calculated efficiently via relational SumProd queries.
  5. Rejection sampling draws candidates from $Q(x)$ and accepts them with probability $P(x)/Q(x)$, simulating exact $k$-means++ seeding in expected polynomial time.
  6. Alternative Coreset Weighting via Network Flow:
  7. Classic coresets weight sampled centers by the exact number of points in their Voronoi cells—a step proved to be NP-hard to approximate.
  8. Instead, the algorithm draws concentric hyperspheres around each center, samples test points from each "donut" slice, and measures local cluster densities.
  9. When a region is undersampled, points redirect their weight toward nearer centers along a directed acyclic graph. The resulting fractional assignment guarantees that while individual cluster weights may deviate, the aggregate weighted set forms a valid $O(1)$-approximate coreset.

Datasets

Not discussed in the paper. (This paper is purely theoretical, providing formal algorithms, complexity proofs, and approximation guarantees without empirical benchmark evaluations.)

Key Insights and Ideas

  • Limits of Relational Clustering: The paper shows that basic geometric operations in Euclidean space become computationally intractable when data is implicitly defined by relational joins. Counting points on one side of a hyperplane reduces from #P-hard Knapsack Counting, while distinguishing non-empty Voronoi regions for $k \ge 3$ centers reduces from Subset Sum.
  • Laminar Boxes as Join-Friendly Proxies: While Euclidean Voronoi partitions cut diagonally across features, axis-aligned hyper-rectangles factorize independently across columns. This allows complex spatial distances to be decomposed into standard database semi-ring aggregate operations (SumProd).
  • Exact Simulation via Rejection Sampling: Even though the true probability distribution cannot be computed globally, the ratio between the true distance and the laminar-box distance is bounded by $O(i^2 d)$. This enables efficient rejection sampling without getting stuck in infinite rejection loops.
  • Coresets Without True Cluster Sizes: Coreset theory typically requires knowing cluster cardinalities. The authors show that a flow-based fractional weight reassignment bounds the cumulative error to within a constant factor of the optimal clustering cost $\phi_{\text{opt}}$.

Key Results

  • Polynomial-Time $k$-Means (Theorem 1): For an acyclic relational database with maximum table size $n$, dimension $d$, and join size $N$, the algorithm computes an $O(1)$-approximation to $k$-means in time polynomial in $n, d,$ and $k$ with high probability—exponentially faster than materializing the full $N$-row matrix.
  • Hardness of Relational Cluster Counting (Theorem 2):
  • Computing $|J_i|$ (points closest to center $c_i$) is #P-hard for $k \ge 2$.
  • Approximating $|J_i|$ to any factor is NP-hard for $k \ge 3$.
  • Sampling Complexity (Theorem 10): Generating $k' = \Theta(k \log N)$ centers via relational rejection sampling takes expected time $O(k'^4 d m \Psi(n, d, m))$, where $\Psi(n, d, m) = O(m d^2 n^{\text{fhtw}} \log n)$ is the cost of an acyclic SumProd query ($\text{fhtw} = 1$).

Limitations and Future Work

  • High Polynomial Exponents: Although polynomial, the theoretical runtime exponents for sampling and coreset reweighting are relatively large (e.g., scaling with $k^2 m^{11} \log^8 n / \epsilon^4$). Practical implementations will require tighter algorithmic engineering and heuristic optimizations.
  • Restriction to Acyclic (or Low-Width) Joins: True polynomial time requires the database schema to be acyclic ($\text{fhtw} = 1$). For cyclic schemas, the runtime scales exponentially with the fractional hypertree width ($n^{\text{fhtw}}$).
  • Lack of Empirical Validation: The paper focuses strictly on theoretical guarantees; empirical comparisons against materialized baselines or prior relational heuristics (e.g., $Rk$-means) are left to future work.

Summary updated

BibTeX
@inproceedings{moseley2021relational,
  author = {Benjamin Moseley and Kirk Pruhs and Alireza Samadian and Yuyan Wang},
  title = {{Relational Algorithms for k-Means Clustering}},
  booktitle = {ICALP},
  year = {2021},
  url = {https://openreview.net/forum?id=oX3odeCDF6},
}
Figure