Faster cover trees

Mike Izbicki, Christian R. SheltonICML 2015

TL;DR

This paper introduces four key optimizations to the cover tree data structure, significantly speeding up exact nearest neighbor searches in arbitrary metric spaces. By simplifying the tree definition, adding a "nearest ancestor" invariant, parallelizing construction, and optimizing memory layout, the authors achieve up to a 71% reduction in distance computations and turn hours of processing into minutes.

What Problem Does This Paper Address?

Nearest neighbor (NN) search is a cornerstone of machine learning, used in everything from $k$-NN classification to dimensionality reduction. While $kd$-trees are the gold standard for Euclidean spaces, they fail in "metric spaces"—where we only know the distance between points but don't have coordinates (like comparing protein structures or text documents).

The cover tree was designed to solve this, offering strong theoretical runtime guarantees for any metric space. However, the original implementation was notoriously complex, involving an "implicit" tree with infinite nodes and a "nesting invariant" that added significant overhead. In practice, these theoretical beauties were often slower than simpler, less-guaranteed structures like ball trees. This paper aims to close the gap between theory and practice.

Methods

The authors propose four distinct improvements:

  1. Simplified Cover Tree: They discard the "nesting invariant" (which required a node to be its own child). This reduces the number of nodes from $O(n)$ to exactly $n$, cutting down on traversal overhead.
  2. Nearest Ancestor Invariant: They introduce a new rule: every point must be attached to its closest possible ancestor at the level above. While this makes the tree slightly more expensive to build, it significantly improves "pruning"—the ability of the search algorithm to skip large branches of the tree that can't possibly contain the nearest neighbor.
  3. Parallel Construction: They provide an algorithm to build separate cover trees on multiple CPU cores and then merge them. This is particularly effective for expensive distance metrics where the cost of a single comparison is high.
  4. Cache-Efficient Layout: They implement the van Emde Boas tree layout, a "cache-oblivious" strategy that arranges nodes in memory according to a depth-first traversal. This ensures that nodes likely to be accessed together are physically close in memory, reducing expensive "cache misses."

Datasets

The authors evaluated their work on a diverse range of datasets:

  • MLPack Benchmarks: Eight standard datasets including yearpredict, twitter, mnist, and covtype, ranging from 10k to 580k points.
  • Bioinformatics (Protein Data Bank): 250,000 protein graphs compared using a computationally expensive random walk graph kernel.
  • Computer Vision (Yahoo! Flickr): 1.5 million images compared using the Earth Mover’s Distance (EMD), a notoriously slow but accurate metric for image similarity.

Key Insights and Ideas

  • The "Nesting" Tax: The original cover tree's requirement that a node be its own child was a theoretical convenience that acted as a "tax" on practical performance. Removing it doesn't break the theoretical guarantees but makes the implementation much leaner.
  • Build Time vs. Query Time: The "Nearest Ancestor" tree is a classic trade-off. It takes longer to construct because it rebalances nodes to find the best parents, but the resulting tree is much higher quality for searching.
  • Parallelism and Metric Cost: Parallel speedup isn't just about the number of cores; it's about the "weight" of the distance metric. For cheap metrics (like L2 distance), the overhead of merging trees can limit speedup. For expensive metrics (like EMD), the speedup becomes nearly linear (or even super-linear).
  • Memory Matters: Even with a perfect algorithm, modern CPUs are often bottlenecked by waiting for data from RAM. The van Emde Boas layout proves that "cache-oblivious" structures—often ignored in ML libraries—provide a "free" performance boost of 5-20%.

Key Results

Distance comparisons across tree variants
  • Distance Computations: On standard benchmarks, the new tree reduced distance checks by 10–50%. On the bioinformatics dataset, the reduction was a massive 71%. The nearest ancestor invariant significantly cuts down the number of distance comparisons required during both construction and querying.
  • Wall-Clock Speedup: On the large-scale image dataset (1.5M images), the parallel algorithm using 16 cores reduced tree construction time from 3.5 hours to just 12 minutes.
Stalled CPU cycle rates
  • Hardware Efficiency: The cache-efficient layout reduced stalled CPU cycles by 2–15%, demonstrating that software-level memory management has a tangible impact on hardware performance. The packed layout consistently outperforms unpacked versions and reference implementations.

Limitations and Future Work

The authors note that their parallel merging algorithm does not perfectly maintain the "nearest ancestor" invariant to save time during the merge. While this doesn't seem to hurt performance much in practice, a "perfect" parallel merge remains an open challenge. Additionally, while they focused on single-tree nearest neighbor queries, they suggest their optimizations would likely benefit dual-tree algorithms (used for all-nearest-neighbor searches) as well.

The paper concludes by suggesting that other invariants might exist that could further balance the tree, hinting that the evolution of the cover tree is far from over.

Summary updated

BibTeX
@inproceedings{izbicki2015faster,
  author = {Mike Izbicki and Christian R. Shelton},
  title = {{Faster cover trees}},
  booktitle = {International Conference on Machine Learning},
  year = {2015},
  url = {https://openreview.net/forum?id=ByWtcj-dZB},
}
Figure