1

I'm working with a list of tuples in Python, where each tuple contains two integers representing the start and end points of intervals. I'm trying to figure out a way to efficiently identify non-overlapping intervals from this list. My current method, which involves nested loops to compare each interval with every other interval, isn't efficient, especially when the list is large.

Can anyone point me in the direction of relevant algorithms or libraries that can help me with this? :) Thank you!

7
  • Hi. Interesting problem. Do you expect most intervals to overlap? Commented Aug 10, 2023 at 19:50
  • 3
    Just a suggestion, I would provide example inputs and outputs as well as your current approach to help your question gain more traction. It is an interesting problem, but prose-only questions tend to be poorly received. Also, I suggest trying out Computer Science as well. Here are their guidelines Commented Aug 10, 2023 at 19:51
  • 1
    What is the range of the integers involved? Commented Aug 10, 2023 at 20:10
  • I think interval trees is what you want. I guess there are existing Python modules for that (otherwise you can reimplement yourself in Python). Note that "Seeking recommendations for books, tools, software libraries, and more" is off-topic on Stack-Overflow so you question can be closed because of this. Commented Aug 10, 2023 at 21:48
  • Please provide enough code so others can better understand or reproduce the problem. Commented Aug 10, 2023 at 23:07

1 Answer 1

1

Sort the start & end points of every interval, where each endpoint is associated with an interval id (say the index of the interval in a list, or whatever makes sense for how they're stored).

Now parse your sorted array of points, maintaining a set of currently active points, initially empty. As you encounter a start point, add the associated interval to your set, and associate it with everything else already in the set. When you encounter an end point, remove that from the set.

Once done, all overlapping intervals are in associated pairs, so the unassociated pairs are your nonoverlapping intervals.

E.g.

0 [1,5]
1 [3,7]
2 [6,9]

sorted array of points associated with intevals: [1=>0, 3=>1, 5=>0, 6=>2, 7=>1, 9=>2]

parse point 1: set = {0}, pairs = {}
parse point 3: set = {0,1}, pairs = {(0,1}}
parse point 5: set = {1}, pairs = {(0,1)}
parse point 6: set = {1,2}, pairs = {(0,1), (1,2)}
parse point 7: set = {2}, pairs = {(0,1), (1,2)}
parse point 9: set = {}, pairs = {(0,1), (1,2)}

We have that intervals 0 & 1 overlap, as do 1 & 2, so our only non-overlap is 0 & 2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.