点间最短距离算法
给定平面上的一组点,找到由这些点中的任意两个点形成的最短线段。
我该怎么做?最简单的方法显然是计算每个距离,但我需要另一种算法来比较。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
给定平面上的一组点,找到由这些点中的任意两个点形成的最短线段。
我该怎么做?最简单的方法显然是计算每个距离,但我需要另一种算法来比较。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
http://en.wikipedia.org/wiki/Closest_pair_of_points
这个问题可以用 O( n log n) 使用递归分治方法的时间,例如,如下所示:
http://en.wikipedia.org/wiki/Closest_pair_of_points
The problem can be solved in O(n log n) time using the recursive divide and conquer approach, e.g., as follows:
我无法立即想到比暴力技术更快的替代方法(尽管一定有很多),但无论您选择什么算法都不会计算每个点之间的距离。如果您需要比较距离,只需比较距离的平方即可,以避免昂贵且完全多余的平方根。
I can't immediately think of a quicker alternative than the brute force technique (although there must be plenty) but whatever algorithm you choose don't calculate the distance between each point. If you need to compare distances just compare the squares of the distances to avoid the expensive and entirely redundant square root.
一种可能性是按 X 坐标(或 Y 坐标——哪个并不重要,只要保持一致)对点进行排序。然后,您可以使用它来消除与许多其他点的比较。当您查看点[i]和点[j]之间的距离时,如果单独的X距离大于当前的最短距离,则点[j+1]...点[N]可以被消除为好吧(假设
i——如果
j,那么点[0]...point[i]被消除)。
如果您的点以极坐标开始,您可以使用同一事物的变体 - 按距原点的距离排序,如果距原点的距离差大于当前的最短距离,您可以消除该点,以及所有其他比您当前正在考虑的距离原点更远(或更近)的位置。
One possibility would be to sort the points by their X coordinates (or the Y -- doesn't really matter which, just be consistent). You can then use that to eliminate comparisons to many of the other points. When you're looking at the distance between point[i] and point[j], if the X distance alone is greater than your current shortest distance, then point[j+1]...point[N] can be eliminated as well (assuming
i<j
-- ifj<i
, then it's point[0]...point[i] that are eliminated).If your points start out as polar coordinates, you can use a variation of the same thing -- sort by distance from the origin, and if the difference in distance from the origin is greater than your current shortest distance, you can eliminate that point, and all the others that are farther from (or closer to) the origin than the one you're currently considering.
您可以从 Delaunay 三角剖分 中提取线性时间内最接近的对,反之亦然从 Voronoi 图。
You can extract the closest pair in linear time from the Delaunay triangulation and conversly from Voronoi diagram.
这个问题有一个标准算法,在这里你可以找到它:
http://www.cs.mcgill.ca/~cs251/ClosestPair/ ClosestPairPS.html
这是我对该算法的实现,抱歉没有注释:
There is a standard algorithm for this problem, here you can find it:
http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairPS.html
And here is my implementation of this algo, sorry it's without comments:
从您的问题来看,不清楚您是在寻找线段的距离,还是线段本身。假设你正在寻找距离(然后简单修改中的段,一旦你知道哪两个点的距离最小),给定5个点,编号从1到5,你需要
如果我没记错的话,给定距离的交换律不需要执行其他比较。
在Python中,可能听起来像是
可以用下面的代码进行测试:
如果两个以上的点可以具有相同的最小距离,并且您正在寻找线段,则需要再次修改建议的代码,并且输出将是距离最小的点(或几个点)的列表。希望有帮助!
From your question it is not clear if you are looking for the distance of the segment, or the segment itself. Assuming you are looking for the distance (the segment in then a simple modification, once you know which are the two points whose distance is minimal), given 5 points, numbered from 1 to 5, you need to
If I am not wrong, given the commutativity of the distance you do not need to perform other comparisons.
In python, may sound like something
That can be tested with something like the following code:
If more than two points can have the same minimal distance, and you are looking for the segments, you need again to modify the proposed code, and the output will be the list of points whose distance is minimal (or couple of points). Hope it helps!
这是一个代码示例,演示如何实现分而治之算法。为了使算法正常工作,点的 x 值必须是唯一的。该算法的不明显部分是您必须沿 x 轴和 y 轴排序。否则,您无法在线性时间内找到裂缝上的最小距离。
Here is a code example demonstrating how to implement the divide and conquer algorithm. For the algorithm to work, the points x-values must be unique. The non-obvious part of the algorithm is that you must sort both along the x and the y-axis. Otherwise you can't find minimum distances over the split seam in linear time.