Divide set of values into two sets of same or similar size with similar value sums

后端 未结 2 1616
再見小時候
再見小時候 2021-01-28 05:06

I have a set of floating point values that I want to divide into two sets whose size differs at most by one element. Additionally, the difference of value sums between the two s

2条回答
  •  无人及你
    2021-01-28 05:47

    My suggestion would be to sort the values, then consider each pair of values (v1, v2), (v3, v4) putting one element from each pair into one partition.

    The idea is to alternate putting the values into each set, so:

    s1 = {v1, v4, v5, v8, . . . }
    s2 = {v2, v3, v6, v7, . . . }
    

    If there are an odd number of elements, put the last value into the set that best meets your conditions.

    You have a relaxed definition of minimal, so a full search is unnecessary. The above should work quite well for many distributions of the values.

提交回复
热议问题