Skip to main content
QUICK REVIEW

[Paper Review] KDTREE 2: Fortran 95 and C++ software to efficiently search for near neighbors in a multi-dimensional Euclidean space

Matthew B. Kennel|ArXiv.org|Aug 14, 2004
Robotics and Automated SystemsEngineering66 citations
TL;DR

KDTREE2 is a high-performance C++ and Fortran 95 implementation of k-d trees for efficient nearest neighbor searches in multi-dimensional Euclidean space. It achieves up to 10× faster search speeds than its predecessor through cache-friendly memory access, heap-based priority queues, and enhanced pruning, especially effective in high dimensions and large datasets.

ABSTRACT

Many data-based statistical algorithms require that one find extit{near or nearest neighbors} to a given vector among a set of points in that vector space, usually with Euclidean topology. The k-d data structure and search algorithms are the generalization of classical binary search trees to higher dimensional spaces, so that one may locate near neighbors to an example vector in $O(\log N)$ time instead of the brute-force O(N) time, with $N$ being the size of the data base. KDTREE2 is a Fortran 95 module, and a parallel set of C++ classes which implement tree construction and search routines to find either a set of $m$ nearest neighbors to an example, or all the neighbors within some Euclidean distance $r.$ The two versions are independent and function fully on their own. Considerable care has been taken in the implementation of the search methods, resulting in substantially higher computational efficiency (up to an order of magnitude faster) than the author's previous Internet-distributed version. Architectural improvements include rearrangement for memory cache-friendly performance, heap-based priority queues for large $m$searches, and more effective pruning of search paths by geometrical constraints to avoid wasted effort. The improvements are the most potent in the more difficult and slowest cases: larger data base sizes, higher dimensionality manifolds containing the data set, and larger numbers of neighbors to search for. The C++ implementation requires the Standard Template Library as well as the BOOST C++ library be installed.

Motivation & Objective

  • To develop a high-performance, portable implementation of k-d tree algorithms for nearest neighbor searches in multi-dimensional Euclidean spaces.
  • To overcome performance bottlenecks in existing k-d tree software, particularly in high-dimensional and large-scale data scenarios.
  • To improve computational efficiency through architectural and algorithmic optimizations, including memory access patterns and pruning heuristics.
  • To provide both Fortran 95 and C++ interfaces with full independence and compatibility with standard libraries (STL, Boost).
  • To make the software open source and freely available for research and production use, with clear citation and licensing terms.

Proposed method

  • Uses a k-d tree data structure that recursively partitions the data space along the dimension with the largest extent, creating bounding hyper-rectangles (bounding boxes) for each node.
  • Employs a top-down, one-pass algorithm to build the tree, using the arithmetic mean of the split dimension as the cut value, with efficient index reassignment for children.
  • Implements search using a priority queue (min-heap) for large m-neighbor searches, ensuring only the most promising candidates are retained.
  • Applies geometric pruning by checking whether the query point’s distance to a node’s bounding box could possibly yield a closer neighbor than the current best.
  • Optimizes memory access by reordering data and using cache-friendly layouts to reduce CPU cache misses during traversal.
  • Supports both fixed-number (n-nearest) and fixed-radius (r-nearest) search modes, with additional decorrelation filtering to exclude nearby points in time-series contexts.

Experimental results

Research questions

  • RQ1How can k-d tree search performance be significantly improved in high-dimensional and large-scale datasets?
  • RQ2What architectural and algorithmic changes lead to substantial speedups in nearest neighbor search without sacrificing correctness?
  • RQ3To what extent do cache-aware memory access and heap-based priority queues improve performance in k-d tree searches?
  • RQ4How effective is geometric pruning in reducing unnecessary node traversals during search?
  • RQ5Can a dual-language (C++ and Fortran 95) implementation maintain feature parity while supporting diverse scientific computing environments?

Key findings

  • KDTREE2 achieves up to 10× higher search throughput than the previous version, with the largest gains observed in high-dimensional (8D) and large-dataset (50,000 points) scenarios.
  • For 50,000 points in 8D space, KDTREE2 performs 396 searches per second for m=500, compared to only 49 for the old version, a nearly 8× improvement.
  • In 3D with 200,000 points, the new implementation achieves 3,001 searches per second for m=500, compared to 261 for the old version, a 12× speedup.
  • The performance gain is most pronounced in the most computationally challenging cases: high dimensionality, large data size, and large m.
  • Cache-friendly memory layout and heap-based priority queues significantly reduce overhead in large m searches, improving efficiency by up to an order of magnitude.
  • The software maintains feature completeness across both C++ and Fortran 95 versions, with identical functionality and performance characteristics.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.