最接近配对算法
我想了解最接近的配对算法。我理解将集合分成两半。但我无法理解如何递归计算最接近的对。我了解递归,但不了解如何通过递归计算最接近的对。如果你有 (1,2)(1,11)(7,8) ,递归将如何处理这些?
I am trying to understand the closest pair algorithm. I understand about dividing the set in half. But I am having trouble understanding how to recursively compute the closest pair. I understand recursion, but do not understand how to compute the closest pair by recursion. If you have (1,2)(1,11)(7,8) how would recursion work on these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该算法的基本思想是这样的。
您有一组点 P,并且您想要找到 P 中距离最短的两个点。
一个简单的强力方法会遍历 P 中的每一对,计算距离,然后选取距离最短的一对。这是一个 O(n²) 算法。
但是,通过您正在讨论的算法可以做得更好。这个想法是首先根据一个坐标(例如 x 坐标)对所有点进行排序。现在你的集合 P 实际上是一个排序的点列表,按它们的 x 坐标排序。该算法现在的输入不是一组点,而是一个排序的点列表。我们将该算法称为 ClosestPair(L),其中 L 是作为参数给出的点列表。
ClosestPair(L) 现在递归实现如下:
令δleft和δright得到对应的最短距离。
the basic idea of the algorithm is this.
You have a set of points P and you want to find the two points in P that have the shortest distance between them.
A simple brute-force approach would go through every pair in P, calculate the distance, and then take the one pair that has the shortest distance. This is an O(n²) algorithm.
However it is possible to better by the algorithm you are talking about. The idea is first to order all the points according to one of the coordinates, e.g. the x-coordinate. Now your set P is actually a sorted list of points, sorted by their x-coordinates. The algorithm takes now as its input not a set of points, but a sorted list of points. Let's call the algorithm ClosestPair(L), where L is the list of points given as the argument.
ClosestPair(L) is now implemented recursively as follows:
Let the corresponding shortest distances obtained by δleft and δright.
如果您指的是此算法,您可以执行以下操作:
递归由与上述相同的步骤组成。例如,使用 (1,2) 和 (1,11) 进行的调用会执行以下操作:
If you mean this algorithm you do the following:
The recursion consists of the same steps as above. E.g. the call with (1,2) and (1,11) does:
我想我知道你在说什么算法。我可以自己在这里重述一下,但是算法简介中给出的描述远远优于我可以生产。该章也可以在 Google 图书上找到:享受。 (其他人也可以在那里找到问题描述)
I think I know what algorithm you're talking about. I could recount it here myself, but the description given in Introduction to Algorithms is by far superior to what I can produce. And that chapter is also available on google books: enjoy. (Everybody else can find problem description there too)
也许线性时间随机最近对< /a> 算法会有所帮助。在那里你可以在预期时间 O(n) 内找到该对。
Maybe Linear-time Randomized Closest Pair algorithm will help. There you can find the pair in expected time O(n).