最接近配对算法

发布于 2024-11-03 07:48:00 字数 106 浏览 0 评论 0原文

我想了解最接近的配对算法。我理解将集合分成两半。但我无法理解如何递归计算最接近的对。我了解递归,但不了解如何通过递归计算最接近的对。如果你有 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生面具三千个 2024-11-10 07:48:00

该算法的基本思想是这样的。

您有一组点 P,并且您想要找到 P 中距离最短的两个点。

一个简单的强力方法会遍历 P 中的每一对,计算距离,然后选取距离最短的一对。这是一个 O(n²) 算法。

但是,通过您正在讨论的算法可以做得更好。这个想法是首先根据一个坐标(例如 x 坐标)对所有点进行排序。现在你的集合 P 实际上是一个排序的点列表,按它们的 x 坐标排序。该算法现在的输入不是一组点,而是一个排序的点列表。我们将该算法称为 ClosestPair(L),其中 L 是作为参数给出的点列表。

ClosestPair(L) 现在递归实现如下:

  1. 在中间分割列表 L,获得 Lleft 和 Lright
  2. 递归求解 ClosestPair(Lleft) 和 ClosestPair(Lright)。
    令δleft和δright得到对应的最短距离。
  3. 现在我们知道原始集合中的最短距离(用 L 表示)要么是两个 δ 之一,要么是 Lleft 中的点与 L 中的点之间的距离>对。
  4. 我们仍然需要检查左右细分的两点之间的距离是否较短。诀窍在于,因为我们知道距离必须小于 δleft 和 δright,所以从两个细分点考虑不远于 min(δ left, δright) 距分割线(用于分割原始列表 L 的 x 坐标)。这种优化使得该过程比暴力方法更快,实际上是 O(n log n)。

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:

  1. Split the list L at its middle, obtaining Lleft and Lright.
  2. Recursively solve ClosestPair(Lleft) and ClosestPair(Lright).
    Let the corresponding shortest distances obtained by δleft and δright.
  3. Now we know that the shortest distance in the original set (represented by L) is either one of the two δs, or then it is a distance between a point in Lleft and a point in Lright.
  4. Se we need still to check if there is a shorter distance between two points from the left and right subdivision. The trick is that because we know the distance must be smaller than δleft and δright, it is enough to consider from both subdivisions points that are not farther than min(δleft, δright) from the dividing line (the x-coordinate you used to split the original list L). This optimization makes the procedure faster than the brute-force approach, in practice O(n log n).
蓝眼睛不忧郁 2024-11-10 07:48:00

如果您指的是此算法,您可以执行以下操作:

  1. 对点进行排序:(1,2) ( 1,11) (7,8)
  2. 构建两个子集:(1,2) (1,11) 和 (7,8)
  3. 在 (1,2) (1,11) 和 (1,2) 上运行算法(7,8) 分别 <= 这就是递归出现的地方。结果是 dLmin = 9 且 dRmin = 无穷大(右侧没有第二个点)
  4. dLRmin = sqrt(45)
  5. result = min(dLmin, dRmin, dLRmin) = sqrt(45)

递归由与上述相同的步骤组成。例如,使用 (1,2) 和 (1,11) 进行的调用会执行以下操作:

  1. 排序点:(1,2) (1,11)
  2. 构建两个子集:(1,2) 和 (1,11)
  3. 在 ( 1,2) 和 (1,11) 分别<=再次递归调用。结果是 dLmin = 无穷大 dRmin = 无穷大
  4. dLRmin = 9
  5. 结果 = min(dLmin, dRmin, dLRmin) = 9

If you mean this algorithm you do the following:

  1. Sort points: (1,2) (1,11) (7,8)
  2. Build two subsets: (1,2) (1,11) and (7,8)
  3. Run the algorithm on (1,2) (1,11) and on (7,8) separately <= this is where the recursion comes. The result is dLmin = 9 and dRmin = infinity (there is no second point on the right)
  4. dLRmin = sqrt(45)
  5. result = min(dLmin, dRmin, dLRmin) = sqrt(45)

The recursion consists of the same steps as above. E.g. the call with (1,2) and (1,11) does:

  1. Sort points: (1,2) (1,11)
  2. Build two subsets: (1,2) and (1,11)
  3. Run the algorithm on (1,2) and on (1,11) separately <= again recursion calls. The result is dLmin = infinity and dRmin = infinity
  4. dLRmin = 9
  5. result = min(dLmin, dRmin, dLRmin) = 9
黯然 2024-11-10 07:48:00

我想我知道你在说什么算法。我可以自己在这里重述一下,但是算法简介中给出的描述远远优于我可以生产。该章也可以在 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文