[英]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? 找到这样的
i
和j
的最佳方法是什么,从而使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
中的每个点到b
中a
每个点的距离,并使该对保持最短的距离。 But this algorithm is O(n^2)
, which is not good. 但是此算法为
O(n^2)
,效果不好。 Is there some better approach? 有更好的方法吗?
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.