7

Assume that you are given a set of intervals (not necessarily integral in length). How do you determine if there is an overlap between any two intervals in the given set? I am wondering if there is a linear solution in the number of intervals.

P.S: Not a HW problem. This was asked in one of my interviews with a company.

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
Hari
  • 5,057
  • 9
  • 41
  • 51
  • 1
    Take a look at this question http://stackoverflow.com/questions/2593774/interval-tree-algorithm-that-supports-merging-of-intervals-with-no-overlap – Dr. belisarius Apr 22 '11 at 06:59

2 Answers2

12

If all the intervals are sorted by start point, then it's easy to check whether there is two of them overlap. Just scan through all the intervals, keep the maximum end point we got from previous intervals, and if the max end point > current start point, then we got a overlap. If we haven't get overlap for all intervals, then there is no overlap.

If the intervals are not sorted. Then in general the lower bound to detect overlap is O(n logn). Because when all intervals have start_point = end_point, then the problem is reduced to distinctness problem. http://en.wikipedia.org/wiki/Element_distinctness_problem. All comparison based algorithm need O(n log n) time.

However, if the points of all intervals are discrete and in some certain range [0,L), e.g. the seconds in a day is from 0 to 60*60*24, then it can be solved in O(n+L) linear time and O(L) space.

The idea is that, each interval i has a start point si and end point ei. We create an array a = new int[L]. For each interval i, we add 1 for a[si] to a[ei]. Then we check whether there is an a[j] > 1, if so we get an overlap.

The most naive method is using 2 for loops and the time complexity is O(n*L). In Programming Peals there is a clever algorithm which could reduce the running time to O(n+L). If you are interested and this is what your interviewer wants, I can show you the detail.

So, this is the 3 situations I know. Which one do you think fits your problem.

Nova2358
  • 199
  • 1
  • 5
  • 1
    Hi , Would you please kindly let us know the clever algorithm you mentioned from Programming Peals ? – ARH Nov 25 '12 at 19:26
1

Look into the data structure called Interval Tree. This is used to find overlapping intervals.

If the intervals are sorted by their starting values, this is a simple problem in O(n).

An alternative would be to mark each interval in an array of size m and progressively check if they overlap. The size of the array (say m) can be determined in O(n), but the space and time requirement would be O(m).

BiGYaN
  • 6,974
  • 5
  • 30
  • 43
  • I don't think you understood my question properly. You are just given a set of intervals. It is not - you are given a set of intervals and asked if it overlaps with a given interval. Using an interval tree it would then be O(NlogN). However the interviewer, in the end hinted at using parallelism. – Hari Jul 16 '11 at 14:43
  • FYI, AFAIK, there is no non-linear algorithm that can be made linear merely by the application of parallelism over a finite number of processors. – RBarryYoung Aug 23 '11 at 02:37