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

后端 未结 2 1614
再見小時候
再見小時候 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.

    0 讨论(0)
  • 2021-01-28 05:50

    It easy to prove that the partition problem reduces to this problem in polynomial time.

    Imagine you want to solve partition for some array A, but you only know how to solve your problem. You just have to double the array length, filling it with zeros. If you can solve it with your algorithm, then you have solved the partition problem. This proves your problem to be NP-hard.

    But you'll see you can't reduce this problem to partition (i.e. it isn't NP-complete), unless you limit the precision of your floats. In that case the same algorithm would solve both.

    In the general case, the best you can do is backtrack.

    0 讨论(0)
提交回复
热议问题