3

I'm wondering if anyone has implemented/knows of an (preferably javascript) interval-tree algorithm that will handle circular intervals. By circular, I mean intervals with a start > end. Note this also necessitates a cap to how large the intervals can be.

Is this just a subcase of the common interval tree problem?

In response to the questions posed in the comments: Here's an image (thanks G. Bach and wikipedia) of what I mean by a circular subrange: enter image description here

And (unrelated to the above image) here's an example json representation of the ranges: [{id: 'range1', start: 3, end: 34}, {id: 'range2circular', start: 30, end:6}]

Hope

Thanks!

OmG
  • 18,337
  • 10
  • 57
  • 90
tnrich
  • 8,006
  • 8
  • 38
  • 59
  • 1
    please supply some example. – Nina Scholz Nov 20 '15 at 08:35
  • You could simply reverse each of those intervals with negative length so if an interval is `(start, end)` and `start > end` then store `(end, start, R)` where R notes that this interval is reversed. The you can use a normal interval tree and then know from the `R` that a given result has its start and end reversed. – Dan D. Nov 20 '15 at 08:36
  • From what domain are your values? How do they wrap around? – Bergi Nov 20 '15 at 10:47
  • @DanD.: Not exactly, because you'd have to invert your search as well. – Bergi Nov 20 '15 at 10:51
  • I don't see any logical connection between the qualifier "circular" and bounds reversal "start >end". I don't think that you are telling us the right thing. –  Nov 20 '15 at 11:08

1 Answers1

1

Sounds related to the idea behind circular arc graphs (but not the graphs themselves, since you start out with the intervals and don't care about a circular arc graph representation of them).

Assuming that's what it is, that means the domain can be represented by a period akin to the degrees of a circle. Then you have a minimum possible value min and a maximum possible value max = min + 1*period, and the first thing you do is find the smallest s such that start = min + s + k*period for integer k (basically, this is a modulo operation), and similarly you find the smallest e such that end = min + e + j*period.

Now you can represent your interval as (s,e) still with s > e. Split it up into two intervals (s, max) and (min, e), throw those into your interval tree, and give both of them a reference to your original interval (start, end). If you start with n intervals that possibly overlap a period, you end up with 2n intervals in the tree, and the asymptotic bounds hold.

G. Bach
  • 3,869
  • 2
  • 25
  • 46