1

What is the best approach to query a list of date intervals for overlapping intervals, given granularity to the second (or even millisecond)?

From the exhaustive list of other questions regarding overlapping integer intervals, I was pointed to the Interval Tree. After reading it, I modified this implementation, which handles integer intervals, for dates.

His search implementation loops from the starting search interval to the ending search interval, recursively appending from the left node or right node as appropriate. This works great if your intervals are close integers (or if your date intervals have granularity down to the hour and are between a day).

I simply decided to loop over the starting search date (converted to seconds) to the ending search interval (converted to seconds via int(time - epoch)), which of course results in a loop of 3600 for searching over a one hour interval. Converting to milliseconds, an hour loop would be 3,600,000!

When searching for overlapping date intervals with granularity down to the second, or millisecond, what is the appropriate algorithmic data structure to use? Is there a better way to implement the Interval Tree than looping over every second?

Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
  • I'm not sure I fully got your question. You have a set of intervals. Do you want to find all the intervals in this set that intersect with another given interval, or do you want to find all pairs of intersecting intervals in your set? – Francis Colas Mar 15 '15 at 21:44
  • In the first case, you don't have to query every point in your query interval; there are three cases for interval `I` to consider: the start of `I` is in the query interval (you can find that by sorting the starting dates and binary search), or the end of `I` is in the query (same as with start), or `I` fully encloses the query (use your interval tree with a random point in the query). – Francis Colas Mar 15 '15 at 21:45

0 Answers0