区间并集
我有一个代表间隔的类。 该类有两个可比较类型的属性“start”和“end”。 现在我正在寻找一种有效的算法来获取一组此类间隔的并集。
提前致谢。
I've got a class representing an interval. This class has two properties "start" and "end" of a comparable type. Now I'm searching for an efficient algorithm to take the union of a set of such intervals.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用扫描线算法。 基本上,您对列表中的所有值进行排序(同时保留它是与每个项目一起的间隔的开始还是结束)。 此操作的复杂度为 O(n log n)。 然后,沿着已排序的项目进行一次循环并计算间隔 O(n)。
O(n log n) + O(n) = O(n log n)
Use the sweep line algorithm. Basically, you sort all the values in a list (while keeping whether it's beginning or end of the interval along with each item). This operation is O(n log n). Then you loop in a single pass along the sorted items and compute the intervals O(n).
O(n log n) + O(n) = O(n log n)
事实证明,这个问题已经被解决了很多次——在不同程度的想象中,在命名法下:http://en.wikipedia.org/wiki/Interval_tree , http://en .wikipedia.org/wiki/Segment_tree ,以及“RangeTree”
(因为OP的问题涉及大量间隔,这些数据结构很重要)
就我自己选择的python库选择而言:
从测试中,我'我发现在功能齐全和 python 当前(非位腐烂)方面最能解决问题的是:SymPy 中的“Interval”和“Union”类,请参阅:http://sympystats.wordpress.com/2012/03/30/simplifying-sets/
另一个好东西寻找选择,性能更高但功能较少的选项(例如。 不适用于浮点范围删除): https://pypi.python.org/pypi/Banyan< /a>
最后:在 IntervalTree、SegmentTree、RangeTree 中的任何一个下搜索 SO 本身,你会找到答案/钩子更多
It turns out this problem has been solved, many times over -- at varying levels of fancy, going under nomenclature(s): http://en.wikipedia.org/wiki/Interval_tree , http://en.wikipedia.org/wiki/Segment_tree , and also 'RangeTree'
(as OP's question involves large counts of intervals these datastructures matter )
in terms of my own choice of python library selection:
From testing, I'm finding that what most nails it in terms of being full featured and python current ( non bit-rotted ) : the 'Interval' and 'Union' classes from SymPy, see : http://sympystats.wordpress.com/2012/03/30/simplifying-sets/
Another good looking choice, a higher performance but less feature rich option (eg. didn't work on floating point range removal) : https://pypi.python.org/pypi/Banyan
Finally: search around on SO itself, under any of IntervalTree, SegmentTree, RangeTree, and you'll find answers/hooks further galore
对所有点进行排序。 然后遍历列表,增加“开始”点的计数器,并减少“结束”点的计数器。 如果计数器达到 0,则它确实是并集中某个间隔的端点。
计数器永远不会变为负数,并且在列表末尾将达到 0。
Sort all the points. Then go through the list incrementing a counter for "start" points, and decrementing it for "end" points. If the counter reaches 0, then it really is an endpoint of one of the intervals in the union.
The counter will never go negative, and will reach 0 at the end of the list.
geocar 的算法在以下情况下失败:
我不太确定,但我认为这是正确的方法:
我还实现了它的减法:
The algorithm by geocar fails when:
I'm not very sure but I think this is the correct way:
I also implemented it for subtraction:
在 C++ 中求区间并集的总和
To find the total of the union of intervals in c++
按其中一个术语(例如,开始)对它们进行排序,然后在浏览列表时检查与其(右侧)邻居的重叠。
Sort them by one of the terms (start, for example), then check for overlaps with its (right-hand) neighbor as you move through the list.