K-nearest neighbour C/C++ implementation
Asked Answered
W

3

9

Where can I find an serial C/C++ implementation of the k-nearest neighbour algorithm?
Do you know of any library that has this?
I have found openCV but the implementation is already parallel.
I want to start from a serial implementation and parallelize it with pthreads openMP and MPI.

Thanks,
Alex

Wartime answered 21/11, 2012 at 7:45 Comment(1)
For what problems are going to apply this algorithm? KNN is really simple and you can try to implement your own approach.Vocabulary
M
5

How about ANN? http://www.cs.umd.edu/~mount/ANN/. I have once used the kdtree implementation, but there are other options.

Quoting from the website: "ANN is a library written in C++, which supports data structures and algorithms for both exact and approximate nearest neighbor searching in arbitrarily high dimensions."

Mahaffey answered 21/11, 2012 at 11:3 Comment(0)
F
3

I wrote a C++ implementation for a KD-tree with nearest neighbor search. You can easily extend it for K-nearest neighbors by adding a priority queue.

Update: I added support for k-nearest neighbor search in N dimensions

Faustina answered 10/12, 2012 at 21:37 Comment(0)
H
1

The simplest way to implement this is to loop through all elements and store K nearest. (just comparing). Complexity of this is O(n) which is not so good but no preprocessing is needed. So now really depends on your application. You should use some spatial index to partition area where you search for knn. For some application grid based spatial structure is just fine (just divide your world into fixed block and search only within closes blocks first). This is good when your entities are evenly distributed. Better approach is to use some hierarchical structure like kd-tree... It really all depends on what you need

for more information including pseudocode look in these presentations:

http://www.ulozto.net/xCTidts/dpg06-pdf

http://www.ulozto.net/xoh6TSD/dpg07-pdf

Houseclean answered 21/11, 2012 at 9:47 Comment(1)
Thanks for the reply. Actually I have to start from an already implemented version.Wartime

© 2022 - 2024 — McMap. All rights reserved.