如何遍历KDTree以找到k个最近邻居?

时间:2016-01-09 02:11:58

标签: nearest-neighbor knn kdtree

这个问题涉及KNN搜索KDTrees的实施。遍历KDTree以找到单个最佳匹配(最近邻居)非常简单,类似于修改后的二进制搜索。

如何将遍历修改为详尽而有效地找到k-best匹配(KNN)?

编辑以澄清: 在找到输入查询I的最近节点M之后,遍历算法如何继续找到与查询最接近的K-1匹配?是否存在遍历模式,以确保按查询的最佳匹配顺序访问节点?

3 个答案:

答案 0 :(得分:3)

你可以维持一个大小为k的最大堆(k是我们想要找到的最近邻居的数量)。

从根节点开始,并在最大堆节点中插入距离值。 继续使用维度分割,标准在k-d树中搜索并不断更新Max Heap树。

https://gopalcdas.wordpress.com/2017/05/24/construction-of-k-d-tree-and-using-it-for-nearest-neighbour-search/

〜阿希什

答案 1 :(得分:1)

除了@Ashish的答案外,您还可以通过以下方式使用max-heap:

1) Build a max-heap of the first k elements (arr[0] to arr[k-1]) of the given array. 

此步骤为 O(k)。然后

2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with 
   root of the max-heap.
    a) If the element is smaller than the root then make it root 
       and call heapify for max-heap.
    b) Else ignore it.

步骤2是 O((n-k)* log(k))

3) Finally, the max-heap has k smallest elements and root of the heap 
   is the kth smallest element.

时间复杂度: O(k +(n-k)* log(k)),但没有排序的输出。如果需要排序的输出,则 O(k +(n-k)* log(k)+ k * log(k))

答案 2 :(得分:0)

目前,我已经确定了执行一系列逐步扩大范围搜索的次优解决方案,直到找到K个节点。