【发布时间】:2017-08-19 15:12:05
【问题描述】:
这里是来自 Goodrich 的算法教科书的伪代码算法,用于在一组点中找到主要的 2D 点,称为找到最大值集:
Algorithm MaximaSet(S):
Input: A set,S,of n points in the plane
Output: The set, M, of maxima points in S
if n ≤ 1
then return S
Let p be the median point in S, by lexicographic (x,y)-coordinates
Let L be the set of points lexicographically less than p in S
Let G be the set of points lexicographically greater than or equal to p in S
M1 ←MaximaSet(L)
M2 ←MaximaSet(G)
Let q be the lexicographically smallest point in M2
for each point, r, in M1 do
if x(r) ≤ x(q) and y(r) ≤ y(q)
then Remove r from M1
return M1 ∪ M2
我的问题在于第 10,11 行的两个递归调用。我的理解是,第 10 行对 M1 的第一次调用会将 L 侧分成另一个 L 和 G 组,然后将下一个 L 分成另一个 L 和 G 组,直到剩下一组,下一行将运行并执行M2 的 G 侧也是如此,但现在它如何与 q 进行比较?有人可以帮我追踪这个算法的征服部分吗?
给定
(1,4) (2,6), ( 3,1) (4,5) (5,7) (6,9) (7,2), (8,6) (9,3)
我知道最大值集是 (6,9) (8,6) (9,3),但我不知道使用这个算法究竟是如何发现的。我通过蛮力解决了它。这开始是一个硬件问题,从技术上讲,我可以继续前进,因为我确实找到了答案,但我真的需要了解这一点,我已经尝试了 2 本书和谷歌,但我认为这次我需要一个人。提前谢谢你,因为这是我第一次在堆栈上,请不要犹豫(请)给我任何更正我如何提出我的问题或提出我的 p 代码
【问题讨论】:
标签: algorithm recursion divide-and-conquer