Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all intervals (overlapping and nonoverlapping) in overlapping intervals

My question is very similar to the one asked here: Finding elementary intervals in overlapping intervals

I like the top solution given there, but I need some tweaking/further explanation of the algorithm due to an extra key piece of information I need to retain. Just as background, these numbers are age ranges of participants in a database. I need to figure the overlaps so that I can split the participants evenly between the overlapping research labs, and if there are no overlaps, I can give all of the participants to that one lab.

This is what I get:

Interval    Lab
{75, 105}    A
{100, 120}   B
{100, 130}   C

This is what I want to get from the input(so I know what to query):

Interval    Lab(s)
{75, 100}    A
{100, 105}   A, B, C
{105, 120}   B, C
{120, 130}   C

Using the top algorithm given in the previous question, I can easily get the nesting: {75, 100, 105, 120, 130} This results in the intervals: {75, 100} {100, 105} {105, 120} {120, 130}. This is great, but now I don't know which intervals correspond to which labs (without making another pass through the list, checking each lab one by one, which can be inefficient).

Can anyone explain to me how to do this easily? Thank you for your help!

like image 404
LeoPardus Avatar asked Jun 12 '12 02:06

LeoPardus


People also ask

How do you find the overlapping interval?

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.

What is an overlapping interval?

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 overlapping intervals in Java?

Java Program to find overlapping intervals among a given set of intervals. In this approach, we are going to take a count array and for each given interval start time we will do count[startTime]++ and for end time, do count[endTime]--. sum > 2 it means there is overlapping intervals.

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).


1 Answers

Create a second array with the associated lab(s)

{A, [B,C], A, B, C}
{75, 100, 105, 120, 130}

As you create your intervals keep a running set of labs. As you hit index i, add the ith lab if it doesn't exist, if it does, remove it. Associate each new interval i to i+1 with the items in the set.

e.g.,

i = 0; set = {A}; interval = 75-100; 
i = 1; set = {A,B,C}; interval = 100-105; 
i = 2; set = {B,C}; interval = 105-120; 
i = 3; set = {C}; interval = 120-130; 
like image 148
dfb Avatar answered Sep 24 '22 16:09

dfb