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
If you allow for backtracking (that is, the ability to go back and undo the previous decision), then you can search the whole space of possibilities and be sure to find your solution. Not the most efficient, but it works.
Or you could build on @Druid's comment: find all of the possible subsets, and search through those to find your partition, allowing for subsets to be duplicated. You could add the heuristic that you want to try smaller subsets before larger ones (leaving more flexibility for future choices).