简体   繁体   English

在两组中找到一对最接近的点的智能,快速方法是什么?

[英]What would be smart and fast way to find a pair of closest points in two sets?

For example, I have two lists of points: 例如,我有两个要点列表:

List<Point2D> a;
List<Point2D> b;

What would be the best way to find such i and j , so that a.get(i).distance(b.get(j)) is minimal? 找到这样的ij的最佳方法是什么,从而使a.get(i).distance(b.get(j))最小?

The obvious solution is brute-force - calculate distance from each point in a to each point in b , keep the pair with shortest distance. 显而易见的解决方案是蛮力-计算从a中的每个点到ba每个点的距离,并使该对保持最短的距离。 But this algorithm is O(n^2) , which is not good. 但是此算法为O(n^2) ,效果不好。 Is there some better approach? 有更好的方法吗?

You can put one of the lists in a quad tree or some other spatial index to make each lookup fast. 您可以将列表之一放在四叉树或其他空间索引中,以快速进行每次查找。

As an alternative you could put all your data in a database with spatial index capabilites. 或者,您可以将所有数据放入具有空间索引功能的数据库中。

For every point of list a you can find the nearest point from list b as described in this answer . 对于列表a每个点,您可以按照此答案中的描述从列表b找到最近的点。 Time complexity is O((M+N) log M). 时间复杂度为O((M + N)log M)。 N = |A|, M = |B|. N = | A |,M = | B |。

Then you just search the point in a , having the nearest neighbor. 然后你只需在搜索点a ,具有近邻。 Time complexity is O(N). 时间复杂度为O(N)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM