Algorithm help: how to divide array into N segments with least possible largest segment (balanced segmenting) -


I have come across one of the Russian programming forums on this problem, but have not come up with an elegant solution. N positive integer , you need to split it into M adjacent segments

Problem:

/ strong>, so that the largest segment is the smallest possible value. By the total of the block, I mean the sum of all its integers in other words, I want to divide a well balanced array, where you do not want a segment big enough.

Example:

    Array: [4, 7, 12, 5, 3, 16]

  • < P> M = 3, which means that I need to divide my array into 3 horoscopes. The solution will be: [4,7] [12, 5] [3, 16] so that the largest segment [3, 16] = 1 and any other segmentation variant has a small total Can produce the largest segment. Another example:

    • Array [3, 13, 5, 7, 18, 8, Solution: [3, 13, 5] [7, 18] [8] [20, 1], "Fat: 20, 1]
    • M = 4
    "Section is [7, 18] = 25 (correct me if I'm wrong, I have made this example)

    I have thought that this is something of the classic CS / Math problem, maybe its With the name of some famous person associated with, problem of Dizxra - is there any known solution for this? - If not, can you come with a solution other than forcibly compulsions, which, as long as I understand the complexity of time, exponential. (N ^ M, to be more specific).

    Thank you in advance, StackwareFlowers.

I like here another method that O (Mn ^ 2) time , which will be sharp if N and M are small, but the numbers in the array are very large (especially, if log (yoga) >> mn, which is possible but of course this is not very realistic). It uses dynamic programming.

Let's just consider dividing the subrerers in which I & lt; = N entries; J and lieutenant; = M is included in the segment. For the best solution for this subprobe, let's move the largest segment F (i, j) - that is the weight of the largest segment in the first part of J-Partition, whose largest segment is the smallest of all such divisions I am F (n, M) as well as one (can be more than one) division, which matches it.

It is easy to calculate f (i, 1) - this is just the sum of the first elements:

  f (i, 1) = x [1] + .. For calculation (f, i, j) for j = = 2, for x, [i]  

, be sure to see that element, let me be the last element of some section Which should start at some position 1 & lt; = K & lt; In the optimal solution for J-1 segment - and parameter (i, j), those J-1 preceding segments are self-parameters (K-1, J-1) . So if we consider the situation of every possible start for the final segment and take the best, then we will calculate the best j-division of the first elements:

[3 / Edit 2/2015: We need to pick new segments and the largest balance, instead of adding them!]

  f (i, j> = 2 ) = Minimum (all 1  

at max (f (k -1, j -1), x [k] + ... + X [i])) If we decrease the value of Kashmir Try not, then we can easily create a value price per pound, so a single F (i, j) values ​​calculated O (N) takes time. We have MN of these values ​​to calculate, therefore O time required (O MN ^ 2).

In order to try to divide more than the elements, one more boundary is needed:


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -