For a defragmentation algorithm I need to solve the following problem: given a collection of positive integers, extract as many subsets as possible that sum to a given value. Each item of the collection must only appear in one subset.
A greedy algorithm (iterative normal subset sum) does not work, counter example:
collection: 3 5 8 4 5 1 1 1 1 1, targeted sum: 10
1. subset: 5 1 1 1 1 1
there is no other subset
but:
1. subset: 8 1 1
2. subset: 5 4 1
3. subset: 3 5 1 1
As you can see, if I pick previous subsets poorly, the solution is not optimal. How do I solve this? I have implemented "normal" subset sum already.
Edit: Could the solution possibly be to use small subsets first? Also, this question is no duplicate to the question on how to find the number of possible subsets. I want to find as many disjoint subsets as possible.
Thanks, Phil
This problem is
probablystrongly NP-hard*, so let me sketch an O(2^n * n)-time algorithm.Let the input be a multiset U and let the target sum be s. Imagine a graph on 2^n vertices corresponding to subsets of U. There is an arc from a subset X to a subset Y if and only if (1) Y = X - {x} for some x in X, and (2) (sum(U - X) mod s) + x ≤ s, i.e., we don't overflow the current partition. Traverse this graph starting from U and report the path to the subset with the smallest sum.
*My reduction showing hardness is from a problem called 3-dimensional matching. Given that the matching instance has n vertices, find n numbers such that all triples have distinct sums. Applying the probabilistic method, if we choose all n uniformly at random from 1..n^6, that succeeds with probability greater than 11/12 (one minus (n choose 3) choose 2 triples times < 1/n^6 failure probability for each), and verification requires time O(n^3 log n). Technically we have to derandomize; see my question on math.SE.
To prepare the input to this problem, for each vertex associated with a number x, output n^12 + x. For each triple x, y, z that can be matched, output n^9 - x - y - z as a number. The target is s = 3 n^12 + n^9. Some tedious math should show that the only subsets with the target sum correspond to 3-dimensional matched triples.