1

This might sound like a simple problem but I am not able to get a good solution. The problem is similar to a knapsack problem but slightly modified.

I have a bag which has a fixed capacity, say C. We have a list of items and their weights. The total weight of all the items is greater than C. How can I fit the maximum number of items in the bag (Also trying to best fill the bag) ?

I thought of sorting the list and select items until the bag is fully filled but the below example disproves the idea

C = 100 and L = 50, 40, 20, 30.

When I sort I get 20, 30, 40, 50 hence my allocation will be (20+30+40) = 90. But we can get a better combination (20+30+50) = 100.

The problem can be solved by transforming this problem into knapsack by giving the weights for each item equivalent to its weight. Is there any other algorithm ?

Neo
  • 141
  • 5
  • 16
  • 1
    Do you want to maximize the number of items, or do you want to make the bag as full as possible? – Abhishek Bansal Nov 23 '13 at 04:55
  • @AbhishekBansal: I want to make the bag as full as possible. – Neo Nov 23 '13 at 05:40
  • There is the [simplex method](http://en.wikipedia.org/wiki/Simplex_algorithm). – neeKo Nov 23 '13 at 06:23
  • @NikoDrašković Plain Simplex method cannot be used for Integer Linear programming models. It has to be combined with sophisticated techniques like branch and bound etc to hope to get a solution in reasonable time. – Abhishek Bansal Nov 23 '13 at 06:27
  • 1
    How many items do you have? What is the range of weights? What is the maximum capacity? Beyond certain limits, it's very difficult for the unbounded problem to be solved efficiently. With nice bounds placed on the problem, there's potential to reduce it. How much does inaccuracy cost? If you took the case with 90 instead of 100, how big of a loss does that actually represent? If the ongoing cost is sufficiently large, you may be justified paying professionals to solve it. There is a fairly large field called operations research that deals with these types of problems. – bdean20 Nov 23 '13 at 09:25
  • @bdean20 You have asked valid questions, I got your point. I found this problem when I was solving another problem and was wondering if there is optimal efficient algorithm for this. In my case, the inaccuracy does not cost much, so I can sort the list and pick items until my capacity is exhausted. – Neo Nov 23 '13 at 21:15
  • To get an idea of how deep the research goes into the knapsack problem, and the different approaches, take a look at this table of contents for a book on knapsack problems: http://www.diku.dk/hjemmesider/ansatte/pisinger/knapsack/toc.pdf – bdean20 Nov 23 '13 at 21:31

2 Answers2

1

DISCLAIMER: This is not the most efficient solution; however, this is a solution.

I would -

  • Generate all possible sums
  • Filter sums by max capacity (bagSize)
  • Get the max sum from the generated sums
  • Filter sums by the max sum
  • Find and filter by the maximum number of items left

Here's an example in everyone's favorite language - Haskell!

import Data.List

knappsack bagSize items = answers
  where
    sums = [(xs, sum xs) | xs <- subsequences items]
    sumFilter = filter ((<= bagSize) . snd) sums
    maxSum = foldl max 0 . map (sum . fst) $ sumFilter
    maxFilter = filter ((== maxSum) . snd) sumFilter
    maxLen = foldl max 0 . map (length . fst) $ maxFilter
    lenFilter = filter ((== maxLen) . length . fst) maxFilter
    answers = lenFilter
pyrospade
  • 7,870
  • 4
  • 36
  • 52
  • Generating all possible sums is a O(2^n) complexity, making this solution extremely inefficient. – neeKo Nov 23 '13 at 06:21
  • Since this is one of the solutions apart from knapsack, I am accepting it in spite of its high complexity. – Neo Nov 24 '13 at 07:39
  • Wow, didn't see that coming! Note that Haskell performs lazy evaluation by default, so there is probably a good way to leverage that for a more efficient algorithm. Unfortunately, my solution provides *all* of the best answers, so it will evaluate everything. – pyrospade Nov 24 '13 at 16:21
1

As per you comment, if the objective is to make the bag as full as possible, then the problem is just knapsack problem with the values being equal to their weights.

Solve it using dynamic programming technique given in Wikipedia.

Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • I know we can solve the problem by transferring it to a knapsack problem (I have mentioned it in the problem statement). But Knapsack is NP hard, I wanted to know if there a better algorithm. – Neo Nov 23 '13 at 06:01
  • @Neo When you know that the problem is NP hard, it means that there is no known way to solve this problem in reasonable time. Dynamic Programming algorithm is Psuedo-polynomial in time complexity and that is the best known algorithm so far for solving such problems. – Abhishek Bansal Nov 23 '13 at 06:04
  • I didn't know that the current problem was NP hard in the beginning, I thought since I was transforming the problem to knapsack, it was NP hard. I guess bdean20 has provided valuable comments on the problem and the nature of solution required. – Neo Nov 23 '13 at 21:19