I have a sorted list of overlapping intervals, intervals are never contained in each other, e.g.,
[(7, 11), (9, 14), (12, 17)]
The constraint for the output is to keep every element as close as possible to its
origin (the middle of the interval), preserve the order of the input, and remove all overlap. Only an
approximate solution is necessary. The expected result for the example input would be:
[(5,9), (9, 14), (14, 19)]
I’m only aware of solutions that go about this in some simulation
style: shift each element by some value in a free direction and
iterate until all overlap has been removed.
Is there an existing algorithm to solve this?
find the overall average:
in our example:
find the total length:
find the new min/max;
you can round ’em
start from the min, creating the sections by the intervals
NOTE:
this method will not keep the elements as equal as possible to the originals, but will keep them as close as possible to the original considering their relative values (preserving the center)
EDIT:
if you want to keep the averages of all intervals as close as possible to the original, you can implement a mathematical solution.
our problem’s input is:
we will define:
we need to find a ‘d’ such as:
min(s) is what we want
in our example:
now calculcate the derivative to find min/max OR iterate over d to get a result. in our case you will need to iterate from 3 to 7
that should do the trick