algorithm - 使用动态规划寻找最大区间

标签 algorithm dynamic-programming

http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/notes/05-dynprog.pdf 我在练习这些问题时遇到了一个难倒我的问题。

7.(a) Suppose we are given a set L of n line segments in the plane, where each segment has one endpoint on the line y = 0 and one endpoint on the line y = 1, and all 2n endpoints are distinct. Describe and analyze an algorithm to compute the largest subset of L in which no pair of segments intersects.

(b) Suppose we are given a set L of n line segments in the plane, where the endpoints of each segment lie on the unit circle x 2 + y 2 = 1, and all 2n endpoints are distinct. Describe and analyze an algorithm to compute the largest subset of L in which no pair of segments intersects.

我想出了如何在 O(n log n) 时间内完成 7a,(这个问题是一个变相的问题,寻找递增数字的最大子集)。我几乎要放弃 7b,因为我想不出办法。

但是,有没有办法将 7b 的前提转换为更像 7a 的前提?我觉得这是解决问题的正确方法,非常感谢任何帮助解决这个问题的方法。

最佳答案

我想不出一个 O(n*log(n)) 的算法,但这是一个 O(n2) 的算法。

我们的想法是构建一个有向图,其中顶点代表给定集合中的片段,边代表“位于右侧”的关系。

令 L 为段列表:{(a1, b1), (a2, b2 ), ..., (an, bn)},其中 ak 和 bk 是第 k 个段的端点。

令 L' 为片段列表:{(a1, b1), (b1, a 1), (a2, b2), (b2, a2) , ..., (an, bn), (bn, an)}。

设图的顶点有从 1 到 2*n 的索引,每个索引 k 代表线段 L'[k],即 (ak/2, bk/2) 如果 k 是奇数,并且 (bk/2, ak/2) 如果 k 是偶数。

线段 (a1, b1) 位于线段 (a2, b 的右侧>2) 当点a1, a2, b2, b1是在单位圆上按顺时针方向排列。

请注意 1) 如果一条线段位于另一条线段的右侧,则它们不会相交; 2) 如果来自 L 的两条线段不相交,则来自 L' 的四个对应线段中的两条必然位于另一条线段的右侧; 3) 来自 L 的任何一组非相交线段由 L' 的一系列线段定义,每个线段位于前一个线段的右侧。

Four non-intersecting segments from L; Eight corresponding segments from L'; Four segments from L' found by the algorithm, ordered from left to right

算法概要:

for every k1 from 1 to 2*n:
    for every k2 from 1 to 2*n:
        if (L'[k1].a, L'[k1].b) lies to the right of (L'[k2].a, L'[k2].b):
            add a directed edge (k1, k2) to the graph 
Find the longest path in the graph: (k1, k2, ..., km). 
The answer to the problem is: (k1/2, k2/2, ..., km/2).

关于algorithm - 使用动态规划寻找最大区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798906/

相关文章:

python - 如何检查 IP 地址是否在 python 网络列表中的任何网络中?

java - 选择 0/1 背包中的元素,其中两个元素具有相同的 yield |最大化值(value)和最小化重量

algorithm - 动态规划Matrix-Chain-Order分析

string - 仅交换的最小成本字符串对齐

algorithm - 子集求和算法

java - 当我实现快速排序方法时堆栈溢出

algorithm - 模拟退火是一种蒙特卡洛方法吗?

python - 为什么我尝试查找最大子数组的算法不起作用?

javascript - 如何使用 jQuery 读取外部 html 文件并将其存储到字符串变量?显示错误

c# - 在 map/2d 数组中查找最近的(几何上)非零值