The algorithm for shifting the intervals of overlap until there is no overlap

I have a sorted list of overlapping intervals, the intervals are never contained in each other, like

[(7, 11), (9, 14), (12, 17)] 

The limitation for output is to keep each element as close as possible to its origin (middle of the interval), keep the input order and remove all overlaps. Only an approximate solution is needed. Expected result for entering an example:

 [(5,9), (9, 14), (14, 19)] 

I only know about the solutions that arise in some style simulators: shifting each element by a certain value in the free direction and iterating until all overlaps are removed.

Is there an existing algorithm to solve this problem?

+4
source share
2 answers

find the average value:

in our example:

 (7 + 11 + 9 + 14 + 12 + 17)/6 = 11.667 

find the total length:

 (11-7) + (14-9) + (17-12) = 4 + 5 + 5 = 14; 

find the new min / max;

 14/2 = 7 11.667 - 7 = 4.667 11.667 + 7 = 18.667 

you can combine them

 4.667 ~ 5 18.667 ~ 19 

start with mines by creating partitions with intervals

 (5, (11-7)+5) = (5,9) (9, (14-9)+9) = (9,14) (14, (17-12)+14) = (14,19) 

Note:

this method will not match the original elements as much as possible, but will keep them as close to the original as possible, given their relative values โ€‹โ€‹(keeping the center)

EDIT:

if you want to keep the average values โ€‹โ€‹of all intervals as close as possible to the original, you can implement a mathematical solution.

our input problems:

a 1 = (a 1,1 , a 1,2 ), ..., a nsub> = (a <sub> n, 1sub>, and <sub> n, 2sub>)

define:

ai 1 = a 1,2 -a 1,1 // define the intervals

b 1 = (d, d + ai 1 )

b n = (d + sum (ai 1 .. ai n-1 )), d + sum (ai 1 .. ai n ))

bi 1 = b 1,2 -b 1,1 // set the intervals

we need to find "d", for example:

s = sum (abs ((a 1,1 + a 1,2 ) / 2 - (b 1,1 + b 1,2 ) / 2))

min (s) is what we want

in our example:

a 1 = (7.11), ai 1 = 4, A avg1 = 9

a 2 = (9.14), ai 2 = 5, A avg2 = 11.5

a 3 = (12.7), ai 3 = 5, A avg3 = 14.5

b 1 = (d, d + 4) B avg1 = d + 2

b 2 = (d + 4, d + 9) B avg2 = d + 6.5

b 3 = (d + 9, d + 14) B avg3 = d + 11.5

s = abs (9- (d + 2)) + abs (11.5- (d + 6.5)) + abs (14.5- (d + 11.5)) = abs (7-d) + abs (5-d) + abs (3-d)

Now calculate the derivative to find the min / max OR iterations over d to get the result. in our case, you will need to iterate from 3 to 7

which should do the trick

+2
source

Given that the solution should be kept in order, we can formulate this problem as a linear program. Let [a i , b i ] be the ith interval. Let the variables x i be the left shift of the i-th interval, and let y i be the right shift of the i-th interval.

minimize the amount i (x i + y i )
taking into account (*) for all i: b i - x i + y i โ‰ค a i + 1 - x i + 1 + y i + 1
for all i: x i , y i โ‰ฅ 0

Replace the constraint (*) by introducing the variable z i .

for all i: x i - y i - x i + 1 + y i + 1 - z i = 0
for all i: z i โ‰ฅ b i - a i + 1

Now the problem boils down to calculating the minimum cost that can be done in a lot of time. However, I have a feeling that there is a more direct solution to this problem.

The graph looks something like this:

  (*) ---- | ---- / z| \ / i| \ / xi | xi+1 \ |/ <---- v <---- \| ... (*) ... ----> ----> yi yi+1 
0
source

Source: https://habr.com/ru/post/1382193/


All Articles