Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fewest subsets with sum less than N

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.

like image 775
cr1msonB1ade Avatar asked Oct 19 '22 17:10

cr1msonB1ade


1 Answers

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.

like image 52
beoliver Avatar answered Oct 22 '22 00:10

beoliver