3

There are multiple recurring intervals of time, starting from a startTime to an endTime. Each interval is defined by the start time of the recurrence, the end time (till the point where the recurrence is going to continue), the onDuration (when it is active, and can overlap), and the offDuration.

Sample Interval:

startTime: 3 secs  
endTime: 30 secs  
onDuration: 3 secs (represented by x)   
offDuration: 5 secs (represented by -)  

|--[xxx]-----[xxx]-----[xxx]-----[xxx]-|

Overlapping Intervals: Two recurring series are said to overlap if they have overlapping on-times (x) within the start and end time ranges of each of them.

Question: There are tens of such intervals. A new recurring interval, defined by the same parameters (startTime, endTime, onDuration, offDuration) is provided. Does this new recurring interval overlap with any of the existing intervals, within the time ranges of startTime and endTime?

PotentialInterval:

startTime: 6 secs  
endTime: 15 secs  
onDuration: 3 secs  
offDuration: 6 secs  

PotentialInterval does not conflict with SampleInterval because it ends before it would have overlapped.

Notes:

  1. This is very similar to this question, but I couldn't completely understand the correctness of the solution. Also, I'm interested in only determining whether they conflict (boolean true or false), rather than the actual conflicting interval.

  2. Both the end time and start time of each interval form an arithmetic progression. startTimen = startTime + (n-1) (onDuration + offDuration) where startTimen < endTime. Thus, maybe this question might point in the right direction, though I couldn't find a way to incorporate durations in this.

  3. The samples are much smaller. In reality, the number of individual intervals in each recurrence would be several thousands (for eg, from now till next 10 years every day from 3PM to 4PM). Also, the number of recurrences could be hundreds. Thus, denormalizing the data (making a list of each occurence) may not be practical.

  4. This data is stored in a NoSQL database, so date-time manipulations within the database are not possible. This needs to be done in memory, and preferably in the order of ~500 milliseconds

user1271286
  • 333
  • 5
  • 14
  • Smells like homework... – dat3450 May 10 '17 at 03:30
  • What is your question? – Dawood ibn Kareem May 10 '17 at 03:37
  • @dat3450: It's not homework, it's a problem I'm currently working on for scheduling resources. Though I wish I had done this kind of homework, so that I wouldn't be stuck now :) – user1271286 May 10 '17 at 08:42
  • @DawoodibnKareem: I've edited it to rename the "Task" as "Question". My question is to find if a given new recurrence overlaps with any of the existing recurrence intervals in the given time range. (Note #1 also talks more or less about the same, except that my problem requires a boolean overlaps or not, whereas the OP there expects the actual overlap) – user1271286 May 10 '17 at 08:43
  • Well, yes, I understand what your **task** is. It's a lot of work, and I don't think anyone is going to do it for you for free. But if you have a **question** about your task, I imagine somebody might answer it for you. You still haven't asked a question. – Dawood ibn Kareem May 10 '17 at 09:06
  • @DawoodibnKareem: I understand. The question is whether is exists an algorithm for this task? Or if it's possible to give me any pointers towards resources/algorithms and I can come up with the solution. – user1271286 May 10 '17 at 19:29
  • If I had to do this, I'd try the "brute force" way - just iterate through time, for the least common multiple of the two time periods you're comparing, looking for the overlap. Once you get that working correctly, you may or may not want to think about optimisations. You may find that the brute force approach fits within your time constraint. – Dawood ibn Kareem May 10 '17 at 19:47
  • @user1271286 Can you bound the problem with a smallest time resolution (perhaps 1 second or hopefully larger) and a largest overall time that you care about (perhaps 20 years or less). – Phill Treddenick May 10 '17 at 20:02
  • 3
    Can you tackle it step by step? 1. Discard all the composite intervals that are completely before or after. 2. Among the remaining ones, check which have the same period (on + off duration). If the period is the same, you only need to check how they are offset relative to one another. 3. Now the composite intervals you have left have a different period, that means they intersect eventually. You can compute the least common multiple of the periods. If that one is smaller than the general overlap, there will be an overlap of on durations. 4. What is left might be fine for denormalization. – Andrei May 10 '17 at 21:04
  • Andrei offers good advice. Also, you can simplify things by looking at the "on" intervals as a center point with a certain "radius"; then finding overlaps is finding two points whose distance is less than r1+r2. – m69's been on strike for years May 11 '17 at 01:14
  • I'm currently using the approach that PhillTreddenick mentioned, but with much tighter constraints (overall time of ~6months, and smallest resolution of 5mins). Thanks a lot, Andrei, Phill and m69. I'd probably go with a combination of @Andrei's approach and Phill's constraints. That should be good enough for the constrained problem (for now). Thanks! :) But I'd still like to know if there are better algorithms, rather than such heuristics. – user1271286 May 11 '17 at 18:49
  • @m69 : I don't think so. Two points having a distance less than r1+r2 wouldn't guarantee an overlap. For example, one interval is from 2AM to 2PM, the other one from 2.30 PM to 1.30 AM. The points are close by, but still not overlapping. Or did I misunderstand you? And I feel that it should be possible to come up with some closed form formula to check this, since it's like finding whether two square waves (with different periods) interfere and when. – user1271286 May 11 '17 at 18:52
  • I meant representing the intervals in your example as "8AM +/-6h" and "8PM +/- 5.5h" and r1+r2 would be 11.5h, so indeed no overlap because their distance is 12h. That way you can check the overlap between 2 intervals with only 1 comparison. – m69's been on strike for years May 11 '17 at 23:16
  • How many units of measure is the longest (onDuration + off duration) interval (meaning the x in onDuration + off duration = x)? – גלעד ברקן May 12 '17 at 13:43

1 Answers1

1

I do not think there is a simple formula / algorithm to determine whether two series overlap. I will elaborate a bit. Let s1 = startTime of series 1, a1 = onDuration, b1 = offDuration, e1 = endTime, and c1 = a1 + b1. Let us also have s2, a2, b2, c2, and e2 similarly for series2. The question is, do the on periods of the series overlap?

Let i1 denote a particular period of series 1, i1 >= 0, i1 <= floor((e1-k1)/c1) = m1. (I ignore the right tails of the series, but those can be handled separately.) Similarly for i2.

The question then is, can we find integer i1 and i2 such that the intervals [s1+i1*c1, s1+i1*c1+a1] and [s2+i2*c2, s2+i2*c2+a2] overlap? As m69 suggests, that is equivalent to checking whether

abs((s1+2*i1*c1+a1)/2 - (s2+2*i2*c2+a2)/2) < (a1+a2)/2

We have two possibilities, either the expression under the modulus is positive or it is negative. Suppose it is positive, then we have

0 <= i1 <= m1
0 <= i2 <= m2
s1+2*i1*c1+a1 >= s2+2*i2*c2+a2,
s1+2*i1*c1+a1 - (s2+2*i2*c2+a2) < a1 + a2

Or,

0 <= i1 <= m1
0 <= i2 <= m2
i1 >= c2/c1*i2 + (s2-s1+a2-a1)/(2*c1)
i1 < c2/c1*i2 + (2*a2+s2-s1)/(2*c1)

(Hopefully, I have not done any silly mistakes in algebra). We get another system assuming that the expression under the modulus is negative.

This is a possibly non-empty polygon with at most six sides. The question is, do any integer values fall inside? That is a diophantine linear inequalities problem. If you google it, you come back to a sister site :)

https://mathoverflow.net/questions/69966/algorithm-for-solving-systems-of-linear-diophantine-inequalities

Andrei
  • 2,585
  • 1
  • 14
  • 17