Search code examples
algorithmknapsack-problembinpacking

Bin Packing or Knapsack?


I'm having some issues with an assignment I have. I've been searching stackoverflow and other websites to see which kind of problem I'm dealing with, and turns out I'm not sure if it's a knapsack problem or a bin packing problem. Here's the problem:

An old lady bought N products, each product with a different weight (kg), and she wants to fit it all into a bag that can hold K kg. Find the set of objects that the sum of the weights gets as close as it can to K.


Solution

  • This is a special case of the knapsack problem where each item's value is equal to its weight. (In general knapsack problems, you may be maximizing the total "value" of all the objects as defined by the problem -- perhaps their monetary value in a physical problem, or desirability to the user when scheduling programs or tasks.)

    From Wikipedia,

    When the number of bins is restricted to 1 and each item is characterised by both a volume and a value, the problem of maximising the value of items that can fit in the bin is known as the knapsack problem.

    So you could consider it a special case of bin-packing, as well ("volume" being the item's weight).