Python: Dynamic Interval Data Structure
Solution 1:
banyan
supports deleting intervals from the tree. For example, to remove a minimal number of intervals from a list of intervals such that the intervals that are left do not overlap in O(n log n)
, banyan.SortedSet
(augmented red-black tree) could be used:
from banyan import SortedSet, OverlappingIntervalsUpdator # pip install banyandefmaximize_nonoverlapping_count(intervals):
# build "interval" tree sorted by the end-point O(n log n)
tree = SortedSet(intervals, key=lambda (start, end): (end, (end - start)),
updator=OverlappingIntervalsUpdator)
result = []
whiletree:# until there are intervals left to consider# pop the interval with the smallest end-point, keep it in the result
result.append(tree.pop()) # O(log n)# remove intervals that overlap with the popped interval
overlapping_intervals = tree.overlap(result[-1]) # O(m log n)
tree -= overlapping_intervals # O(m log n)return result
Example:
print maximize_nonoverlapping_count([[3, 4], [5, 8], [0, 6], [1, 2]])
# -> [[1, 2], [3, 4], [5, 8]]
See Python - Removing overlapping lists.
Solution 2:
Maybe storing of all intersection intervals can help.
You need:
- boundaries of union of all intervals,
- for each intersection left boundary and list of intervals from which intersection is made.
Intersection intervals can be stored in a tree, because they are represented only with left boundary. Methods insert and delete interval look like (simplified):
Insert: find intersection intervals for left and right boundary of new interval, split these intersection intervals in 2 or 3 new intersection intervals. For each intersection intervals between add pointer to new interval.
Delete: find intersection intervals for left and right boundary, merge them to intersection intervals before. For each intersection intervals between remove pointer to deleted interval.
Solution 3:
If you're looking for a Python library that handles intervals arithmetic, consider python-interval. Disclaimer: I'm the maintainer of that library.
This library has support to check for overlaps, and to automatically merge intervals. For example:
>>>import intervals as I>>>I.closed(1,2) | I.closed(2,3)
[1,3]
>>>I.closed(1,2).overlaps(I.closed(3,4))
False
If you want to specifically compute the overlap:
>>> I.closed(1,3) & I.closed(2, 4)
[2,3]
It supports open/closed intervals, finite or infinite. To remove intervals for a given one, just use the difference operator:
>>> I.closed(1, 4) - I.closed(2, 3)
[1,2) | (3,4]
I can help you if you can be a little bit more specific.
Post a Comment for "Python: Dynamic Interval Data Structure"