What is an algorithm to split N line segments into M segments (N <= M) in a balanced way? -


Probably a trivial question, but I'm looking for an algorithm that can break the N segment segment into M sections (M> = N) in a balanced way (for example that R ^ 2 R-Square Max).

Edit (An example added as requested by the commenter):

For example, N = 5 Length segment: {1, 10, 7, 15, 1} which we want to divide into the M = 7 parts.

  • will be a good solution: {1, 1, 5, 5, 7, 7, 8} (split 15 and 10)
  • One bad solution would be: {1, 1, 5, 5, 5, 7, 10} (split between 15 to 3)

    I think, A greedy algorithm with distance from code> can do as well as Avg , but it was not certain that there are some corner cases with it.

    Thank you,

    In fact this problem not NP-hard, because it is not possible to reconstruct the pieces. As a result, there is an O (M log n) -time algorithm for the problem of determining the reduction in the sum of the squares of the length of the pieces. I will start dividing the count on each segment and put them in the priority queue. (I will soon prioritize priorities). Repeat the following actions - n times: drag the top section (maximum priority), increase its dividing calculation and return it to the queue.

    The priority of each segment is the sum of its squares, the sum of the squares of their imaginary division in another piece by subtracting from the current partition. For example, if 15 is currently divided into two pieces, 7 and 8, and we can divide it into three, 5, 5 and 5, then priority 7 ^ 2 + 8 ^ 2 - 5 ^ 2 - 5 ^ 2 is - 5 ^ 2 = 38. For your example, the initial priority queue

      15 (1 cut), priority 15 ^ 2 - 7 ^ 2 - 8 ^ 2 = 112 10 (1 Deduction), priority 10 ^ 2 - 5 ^ 2 - 5 ^ 2 = 50 7 (1 cut), priority 7 ^ 2 - 3 ^ 2 - 4 ^ 2 = 24 1 (1 cut), priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0 1 (1 cut), priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0.   

    We have divided once, once.

      10 (1 cut), priority 10 ^ 2 - 5 ^ 2 - 5 ^ 2 = 50 15 (2 deductible), priority 7 ^ 2 + 8 ^ 2 - 5 ^ 2 - 5 ^ 2 - 5 ^ 2 = 38 7 (1 cut), priority 7 ^ 2 - 3 ^ 2 - 4 ^ 2 = 24 1 (1 cut), priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0 1 ( 1 kata), Priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0.   

    We have divided once 10 times.

      15 (2 cut), priority 7 ^ 2 + 8 ^ 2 - 5 ^ 2 - 5 ^ 2 - 5 ^ 2 = 38 10 (2 cut), priority 5 ^ 2 + 5 ^ 2 - 3 ^ 2 - 3 ^ 2 - 4 ^ 2 = 16 7 (1 cut), priority 7 ^ 2 - 3 ^ 2 - 4 ^ 2 = 24 1 (1 cut), priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0 1 (1 cut), priority 1 ^ 2 - 0 ^ 2 - 1 ^ 2 = 0.   

    We stop here; 15, the next will be for 1, 1, 5, 5, 5, 5, 5, 7.

    The reason for this is that the "greedy" algorithm is optimal because, because of dividing more than one segment, the return pieces are free and reduced in an accurate technical sense (supermodulary).

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -