5

I have a specific sub-problem for which I am having trouble coming up with an optimal solution. This problem is similar to the subset sum group of problems as well as space filling problems, but I have not seen this specific problem posed anywhere. I don't necessarily need the optimal solution (as I am relatively certain it is NP-hard), but an effective and fast approximation would certainly suffice.

Problem: Given a list of positive valued integers find the fewest number of disjoint subsets containing the entire list of integers where each subset sums to less than N. Obviously no integer in the original list can be greater than N.

In my application I have many lists and I can concatenate them into columns of a matrix as long as they fit in the matrix together. For downstream purposes I would like to have as little "wasted" space in the resulting ragged matrix, hence the space filling similarity.

Thus far I am employing a greedy-like approach, processing from the largest integers down and finding the largest integer that fits into the current subset under the limit N. Once the smallest integer no longer fits into the current subset I proceed to the next subset similarly until all numbers are exhausted. This almost certainly does not find the optimal solution, but was the best I could come up with quickly.

BONUS: My application actually requires batches, where there is a limit on the number of subsets in each batch (M). Thus the larger problem is to find the fewest batches where each batch contains M subsets and each subset sums to less than N.

cr1msonB1ade
  • 1,716
  • 9
  • 14
  • Could you just think of this as a knapsack problem where the weight of each item is its value, each item is worth 1 or the variance in the current knapsack (some heuristic to account for big numbers paired with smaller ones), and the capacity of the knapsack is N, and keep iteratively filling knapsacks until no numbers remain? – Adam Evans Jan 19 '16 at 22:02
  • I just recognize, that if you have y wholes in your lists - let's say N is 30, and you have 3 lists, and one has a sum of 27, one of 25 and one of 10. Then you have an optimal solution, because there is no way to divide 10 elements to the 27 and 25 list, which only lack a sum of 8 to be filled, but you would have to divide a weigth of 10. – user unknown Jan 19 '16 at 22:03
  • 4
    Isn't this just the bin-packing problem? – John Coleman Jan 19 '16 at 22:07
  • And I guess I would try to build filled lists from the beginning, and to build them from big elements, so for example for a list of 20 (N=30), I would prefer to fill it with 2x5 instead of 8+1, because the latter is incomplete. Some demo values would be fine. – user unknown Jan 19 '16 at 22:07

1 Answers1

3

Straight from Wikipedia (with some bold amendments):

In the bin packing problem, objects [Integers] of different volumes [values] must be packed into a finite number of bins [sets] or containers each of volume V [summation of the subset < V] in a way that minimizes the number of bins [sets] used. In computational complexity theory, it is a combinatorial NP-hard problem.

https://en.wikipedia.org/wiki/Bin_packing_problem

As far as I can tell, this is exactly what you are looking for.

beoliver
  • 5,579
  • 5
  • 36
  • 72
  • Yes! I knew the problem had to have a name, but for some reason my googling did not turn up the "Bin packing problem". I will employ the first-fit algorithm found on the wiki page. Thanks for pointing me in the right direction! – cr1msonB1ade Jan 19 '16 at 22:29