c# - 一系列xy坐标中的最短距离

标签 c# algorithm brute-force divide-and-conquer

我有一个作业,比较解决一个问题的 2 种不同算法。问题是:

假设我有一系列这样的 xy 坐标:

A(2,3), B(5,6), C(7,8), D(6,2), E(5,5)等。

我想找到 2 个坐标,它们之间的距离最短。一种解决方案是使用蛮力(将它们一一匹配),但还有另一种解决方案使用“分而治之”的方法..

你能帮我用“分而治之”的方法吗?

最佳答案

递归分治法的工作原理如下:

1) Sort points according to their x-coordinates.
2) Split the set of points into two equal-sized subsets by a vertical line x=xmid.

3) Solve the problem recursively in the left and right subsets. This
yields the left-side and right-side minimum distances dLmin and dRmin, respectively.

4) Find the minimal distance dLRmin among the pair of points in which one point lies on the left of the dividing vertical and the second point lies to the right.

5) The final answer is the minimum among dLmin, dRmin, and dLRmin. (source)

它的复杂度为 O(n log n)。还有一个伪代码实现 here以及方法的可视化描述 here .

关于c# - 一系列xy坐标中的最短距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666230/

相关文章:

c# - 获取硬盘序列号

c# - 如何使用 PropertyBuilder 创建私有(private)属性

php - 按日期划分的集群时间戳

python - key 暴力破解

c# - 在 Microsoft LUIS 中定义实体/意图以从 MySQL 数据库中获取正确的数据

c# - 作为.NET初学者,我应该学习什么,在哪里可以找到开源项目?

algorithm - 我怎样才能找到所有连续的子矩阵?

javascript - 如何对D3中的节点进行排序,以使连接路径清晰明了?

python - 将 4 色定理应用于图形数组中存储的相邻多边形列表

python - 如何构建这个 OpenCL 暴力破解代码