【问题标题】:Searching in KD-tree slow在 KD-tree 中搜索缓慢
【发布时间】:2011-07-05 07:47:16
【问题描述】:

我正在实现一个 KD-tree 来将地图点聚类成组。我一直使用Wikipedia's KD-tree article 作为参考。搜索返回正确的最近邻点,但它比我预期的要慢。这是我的代码:

- (FDRKDTree *)nnSearchForPoint:(id <MKAnnotation>)point best:(FDRKDTree *)best {
// consider the current node
distToPoint = [self distanceBetweenAnnotations:self.location and:point];
if (distToPoint < best.distToPoint) {
    best = self;
}
// search near branch
int axis = depth % 2;
FDRKDTree *child = nil;
if (axis) {
    if (point.coordinate.latitude > location.coordinate.latitude)
        child = rightChild;
    else
        child = leftChild;
} else {
    if (point.coordinate.longitude > location.coordinate.longitude)
        child = rightChild;
    else
        child = leftChild;
}
if (child != nil)
    best = [child nnSearchForPoint:point best:best];

child = nil;
//search the away branch - maybe
if (axis) {
    if (fabs(point.coordinate.latitude - self.location.coordinate.latitude) <
        best.distToPoint) {
        if (point.coordinate.latitude > location.coordinate.latitude)
            child = leftChild;
        else
            child = rightChild;
    }
} else {
    if (fabs(point.coordinate.longitude - self.location.coordinate.longitude) <
        best.distToPoint) {
        if (point.coordinate.longitude > location.coordinate.longitude)
            child = leftChild;
        else
            child = rightChild;
    } 
}


if (child != nil) {
    best = [child nnSearchForPoint:point best:best];
}

return best;
}

我的问题是我对“的解释是不是简单的比较一下,看看搜索点和当前节点的分裂坐标之差是否小于搜索点到当前最佳点的距离(整体坐标) .”是正确的。我将其解释为:

if (fabs(point.coordinate.latitude - self.location.coordinate.latitude) < best.distToPoint)

if (fabs(point.coordinate.longitude - self.location.coordinate.longitude) < best.distToPoint)

分别。也欢迎任何其他建议。

谢谢。

【问题讨论】:

    标签: ios nearest-neighbor kdtree recursive-datastructures


    【解决方案1】:

    假设你的distToPointsqrt((x1-x0)**2+(y1-y0)**2),你所做的对我来说看起来不错。我在 Python 中实现了该算法,这可能有助于交叉检查您的版本并澄清一些 Wikipedia 文章要点: https://gist.github.com/863301

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 2013-08-02
      • 2011-03-22
      • 2014-01-27
      • 1970-01-01
      • 2013-02-14
      • 2014-06-10
      • 2021-10-10
      相关资源
      最近更新 更多