Parallel k nearest neighbor graph construction using tree-based data structures

Nazneen Fatema Rajani, Kate McArdle, Inderjit S. Dhillon2015

Abstract

Construction of a nearest neighbor graph is a critical yet computationally expensive step in many machine learning applications, particularly with high-dimensional data. This paper explores optimizing parallel execution of $k$-nearest neighbor ($k$NN) graph construction using tree-based data structures like $k$-d trees and ball trees. By leveraging the OpenMP and Galois frameworks, we present efficient and scalable parallel implementations. Our results demonstrate that while $k$-d trees excel in low-dimensional spaces, ball trees provide superior scalability in high-dimensional contexts. Notably, our Galois-based ball tree implementation achieves near-linear speedup across various datasets, significantly outperforming standard sequential libraries.

Introduction

The $k$-Nearest Neighbor ($k$NN) graph is a fundamental structure in machine learning, essential for tasks ranging from manifold learning to computer vision. However, as datasets grow in size and dimensionality, the computational cost of building these graphs becomes a significant bottleneck. The "curse of dimensionality" renders simple spatial partitions inefficient, often forcing algorithms to search nearly every branch of a tree.

While libraries like Scikit-learn offer tree-based search capabilities, they are frequently limited by sequential execution or poor scaling on large-scale data. This work addresses these challenges by developing a high-performance, parallel, and exact $k$NN graph construction tool capable of handling massive, high-dimensional datasets.

Methodology: Tree-Based Partitioning

The core of our approach lies in parallelizing the search phase of two primary data structures:

  1. $k$-d Trees: These partition space using axis-aligned hyperplanes. They are most effective when the number of points ($N$) is significantly larger than $2^d$, where $d$ is the number of dimensions.
  2. Ball Trees: These utilize nested hyperspheres to partition data, relying on the triangle inequality for pruning. This makes them generally more robust than $k$-d trees in high-dimensional spaces.
Hierarchical partitioning of a dataset into a ball tree structure.

As illustrated in Figure 1, the dataset is recursively divided into a hierarchy of nodes. While the initial tree construction is performed sequentially, the search phase—the primary computational bottleneck—is parallelized.

Parallel Frameworks

We implemented these algorithms using two distinct paradigms:

  • OpenMP: Utilizes parallel loops to search for neighbors for each point independently in a shared-memory environment.
  • Galois Framework: Employs a "graph-centric" operator formulation. By treating the search as a series of tasks on a dynamic worklist, Galois enables more efficient scheduling and resource utilization compared to traditional loop-based parallelism.

Experimental Evaluation

We evaluated our implementations on six datasets with varying characteristics:

  • MNIST: High-dimensional (784 features) handwritten digits.
  • Covtype: Moderate dimensions (54 features) for forest cover prediction.
  • Poker Hand: Large-scale (1 million points), low-dimensional (10 features).
  • RNA & Housing: Small, low-dimensional datasets.

Key Results and Insights

Our empirical analysis yielded several critical findings:

  • Dimensionality Thresholds: We confirmed the heuristic that $k$-d trees are superior only when $N \gg 2^d$. In high-dimensional scenarios like MNIST, ball trees are essential for performance.
  • Galois Scalability: The Galois-based ball tree implementation demonstrated remarkable scalability, achieving almost linear speedup up to 16 threads. This indicates that doubling processing power nearly halves execution time, regardless of data dimensionality.
  • Comparison with Scikit-learn: Our parallel implementations consistently outperformed Scikit-learn. On larger datasets like Poker and Covtype, Scikit-learn’s brute-force methods often failed due to memory constraints, whereas our parallel tree methods succeeded.
  • Exactness: Unlike approximate $k$NN libraries, our tool ensures exact graph construction, maintaining full accuracy for downstream machine learning tasks.

Conclusion and Future Work

This work presents the first implementation of $k$-d and ball trees using the Galois framework, providing a scalable solution for exact $k$NN graph construction. While the search phase is now highly optimized, future work could focus on parallelizing the initial tree construction to remove it as a potential bottleneck for even larger datasets. Additionally, extending these methods to distributed-memory systems would allow for scaling across multiple machines.

Summary updated

Abstract

Construction of a nearest neighbor graph is often a neces- sary step in many machine learning applications. However, constructing such a graph is computationally expensive, es- pecially when the data is high dimensional. Python's open source machine learning library Scikit-learn uses k-d trees and ball trees to implement nearest neighbor graph construc- tion. However, this implementation is ine cient for large datasets. In this work, we focus on exploiting these under- lying tree-based data structures to optimize parallel execu- tion of the nearest neighbor algorithm. We present parallel implementations of nearest neighbor graph construction us- ing such tree structures, with parallelism provided by the OpenMP and the Galois framework. We empirically show that our parallel and exact approach is e cient as well as scalable, compared to the Scikit-learn implementation. We present the rst implementation of k-d trees and ball trees using Galois. Our results show that k-d trees are faster when the number of dimensions is small (2d N); ball trees on the other hand scale well with the number of dimensions. Our implementation of ball trees in Galois has almost linear speedup on a number of datasets irrespective of the size and dimensionality of the data.
BibTeX
@misc{rajani2015parallel,
  author = {Nazneen Fatema Rajani and Kate McArdle and Inderjit S. Dhillon},
  title = {{Parallel k nearest neighbor graph construction using tree-based data structures}},
  year = {2015},
  doi = {10.5821/hpgm15.1},
}
Figure