Overlapping intervals and the amount of overlaps

怎甘沉沦 提交于 2019-12-13 19:24:52

问题


This question might be similar to others but is a bit different. Let's say we have a set of intervals that goes like this let's say A-9 are numbers but for the sake of formatting I used letters):

            <-a---> <------c--->
        <------------MAIN>
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
         <--d-> <--b------->

So we have the Main interval which goes from I to Z, the interval a that goes from M to S and so on.

Now I want to have the intervals where I have the most overlaps WITHIN the main one (which is essentially the basic constraint) that would be M-O with (a and d) and Q-S (a and b) and U-Z (b and c) with 2 overlaps each (everything beyond Z is out of the question because it is outside the main interval

I essentially want a list (aka array) of the intervals and the number of overlaps inside the Main, while not counting the main into that number (sorting not needed there are enough ways to do this) in PHP.

I thought of making a picture of the intervals and then counting the colors for each pixel column but that's

  1. averting the problem
  2. inefficient
  3. probably pretty slow

so, TLDR, I need a solution that

  • works in PHP
  • is relatively fast
  • is reliable
  • gives you the overlapping intervals with their overlap count
  • has a main interval used as base constraint which is not counted to the overlap.

I know it's a very specific question but I am not that well versed in Math and algorithms.


回答1:


  • Build 2 vectors. One contains the start points and one contains the end points of each interval.

  • Sort the 2 vectors.

  • While you have something in any of the two vectors:

    • pick the minimum between the 2.
    • If you picked it from the vector with start points increment the number of overlapping intervals by 1, else decrease it by 1.
    • If your point is also the starting point of the main interval set a flag that you results are valid now.
    • If your point is the end point of the main interval you can stop.
    • If the flag is set then you can compare the current number of overlapping intervals with the max so far and update accordingly.

You can decrease 1 from the max to ignore the main interval. You can keep a list of sections where you have the max count (Just make sure you clear it when you getter a better result).

This is O(NlogN) with the number of intervals you have.




回答2:


The basic algorithm for this is go over the whole length with a counter that increases when you meet a start point and decreases when you meet an end point.

Keep track of the maximum this counter gets to.

Go over the whole length again, this time add locations to your result array when:
1. You are within the MAIN part borders.
2. Your counter (which is maintained the same way) is equal to the maximum you calculated before.



来源:https://stackoverflow.com/questions/32989461/overlapping-intervals-and-the-amount-of-overlaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!