Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find pairwise overlaps of intervals (segments)

We are given two sets of intervals A and B. By an interval, I mean an ordered pair of integers such as c(2,5). I want to find all pairs of intervals - one from A and one from B - that have overlap.

For instance if A and B are as follows:

A=c(c(1,7), c(2,5), c(4, 16))
B=c(c(2,3), c(2,20))

Then FindOverlap(A, B) should return a matrix like below (the only zero element is because the 3rd interval of A does not overlap with the first interval of B):

1 1
1 1
0 1

Do you have any efficient idea?

like image 427
Ali Avatar asked Aug 15 '13 07:08

Ali


People also ask

How do you find overlapping intervals?

Following is complete algorithm. 1) Sort all intervals in increasing order of start time. This step takes O(nLogn) time. 2) In the sorted array, if start time of an interval is less than end of previous interval, then there is an overlap.

How do you find overlapping time intervals in Python?

The basic idea is 1) first take input_start to test_start (if both of them are not equal and input_start is min) 2) always take test_start and test_end 3) take test_end to input_end if test_end is less than input end (and end_input and end_test are not equal).

What is meant by overlapping intervals?

Let's take the following overlapping intervals example to explain the idea: If both ranges have at least one common point, then we say that they're overlapping. In other words, we say that two ranges and are overlapping if: On the other hand, non-overlapping ranges don't have any points in common.

How do you find non-overlapping intervals?

Traverse all the set of intervals and check whether the consecutive intervals overlaps or not. If the intervals(say interval a & interval b) doesn't overlap then the set of pairs form by [a. end, b. start] is the non-overlapping interval.


1 Answers

The intervals package seems to provide a solution here:

require("intervals")
A <- rbind(A1=c(1,7), A2=c(2,5), A3=c(4, 16))
B <- rbind(B1=c(2,3), B2=c(2,20))

# here you can also define if it is an closed or open interval
Aint <- Intervals(A)
Bint <- Intervals(B)

# that should be what you are looking for    
interval_overlap(Aint, Bint)

A nice demonstration

like image 182
holzben Avatar answered Oct 01 '22 08:10

holzben