This is related to finding overlapping intervals. I know how to do so given a list of intervals (interval trees). What I have is a list of list of intervals. For example,
[2,6], [7,11] [1,3], [5,10], [11,13] [2,5], [6,8]
The result for this should be
[2,3], [7,8]
What I need to do is to find a list of intervals that are common in all the lists.
I see this problem as similar to merging n
lists. The problem is I cannot apply pairwise merging of lists. Applying this method can cause a loss of overlapping intervals. So I need to merge all the lists together considering all of them at a time (instead of pairwise).
I can use interval trees. Inserting the first intervals from each list to the interval tree and finding the overlap. Removing the weakest interval from the tree and inserting next interval from one of the lists. I haven't yet completely figured out how I can use this method but it seems It'll get too expensive.
Is there any efficient algorithm for finding overlapping intervals from a list of list of intervals.?
Additional Info: The intervals within a list are sorted. They don't overlap and form a sequence.
Create a single, sorted array of transitions. Each transition has a position, and a cumulative number based on how many intervals you're joining or leaving. As you pass through the list, keep track of how many intervals you are in. When you're in as many intervals as series, that's when you're in a common interval.
For your example the transitions would be:
which after sorting by position and merging collapses to:
which gives you transitions for running totals of:
And then we can read off the intervals where we're at 3 as one starting at
2
and going to3
, and another starting at7
and going to8
. Which is the answer.The idea of creating one long list and sorting is admittedly extra work. You can instead create those lists and merge them on the fly. The savings is a factor of the log of the number of series rather than the log of the number of events.