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

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

提交回复
热议问题