knncolle
Collection of KNN methods in C++
|
knncolle is a header-only C++ library that collects a variety of different k-nearest neighbor algorithms under a consistent interface. The aim is to enable downstream libraries to easily switch between different methods with a single runtime flag, or by just swapping out the relevant constructors at compile time.
The core library supports the following methods:
This framework is extended by various add-on libraries to more algorithms:
Most of the code in this library is derived from the BiocNeighbors R package.
Given a matrix with dimensions in the rows and observations in the columns, we can do:
Check out the reference documentation for more details.
We can perform the search manually by constructing a Searcher
instance and looping over the elements of interest. Continuing with the same variables defined in the previous section, we could replace the find_nearest_neighbors()
call with:
Similarly, we can query the prebuilt index for the neighbors of an arbitrary vector. The code below searches for the nearest 5 neighbors to a query vector at the origin:
To parallelize the loop, we just need to construct a separate Searcher
(and the result vector) for each thread. This is already implemented in find_nearest_neighbors()
but is also easy to do by hand, e.g., with OpenMP:
Either (or both) of indices
and distances
may be NULL
, in which case the corresponding values are not reported. This allows implementations to skip the extraction of distances when only the identities of the neighbors are of interest.
A related problem involves finding all neighbors within a certain distance of an observation. This can be achieved using the Searcher::search_all()
method:
This method is optional so developers of Searcher
subclasses may choose to not implement it. Applications should check Searcher::can_search_all()
before attempting a call, as shown above. Otherwise, the default method will raise an exception.
Some algorithms allow the user to modify the parameters of the search by passing options in the relevant Builder
constructor. For example, the KMKNN method has several options for the k-means clustering step. We could, say, specify which initialization algorithm to use:
Or modify the behavior of the refinement algorithm:
After which, we construct our KmknnBuilder
, build our KmknnPrebuilt
index, and proceed with the nearest-neighbor search.
Check out the reference documentation for the available options in each algorithm's Builder
.
All methods implement the Builder
, Prebuilt
and Searcher
interfaces via inheritance. This means that users can swap algorithms at run-time:
Each class is also heavily templated to enable compile-time polymorphism. We default to int
s for the indices and double
s for the distances. If precision is not a concern, one can often achieve greater speed by swapping all double
s with float
s. The choice of distance calculation is also a compile-time parameter for most subclasses, and users can define their own classes to use a custom distance.
The choice of input data is another compile-time paramter, as defined by the MockMatrix
interface. Advanced users can define their own inputs to, e.g., read from file-backed or sparse matrices. For example, we implement the L2NormalizedMatrix
class to apply on-the-fly L2 normalization of each observation's vector of coordinates. We then combine this with the L2NormalizedBuilder
class to transform an existing neighbor search method from Euclidean to cosine distances.
Check out the reference documentation for more details on these interfaces.
FetchContent
If you're using CMake, you just need to add something like this to your CMakeLists.txt
:
Then you can link to knncolle to make the headers available during compilation:
find_package()
To install the library, use:
By default, this will use FetchContent
to fetch all external dependencies. If you want to install them manually, use -DKNNCOLLE_FETCH_EXTERN=OFF
. See extern/CMakeLists.txt
to find compatible versions of each dependency.
If you're not using CMake, the simple approach is to just copy the files in include/
- either directly or with Git submodules - and include their path during compilation with, e.g., GCC's -I
. The external dependencies listed in extern/CMakeLists.txt
also need to be made available during compilation.
Wang X (2012). A fast exact k-nearest neighbors algorithm for high dimensional search using k-means clustering and triangle inequality. Proc Int Jt Conf Neural Netw, 43, 6:2351-2358.
Hanov S (2011). VP trees: A data structure for finding stuff fast. http://stevehanov.ca/blog/index.php?id=130
Yianilos PN (1993). Data structures and algorithms for nearest neighbor search in general metric spaces. Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, 311-321.