6

I'm having a problem with understanding knapsack problem when there's more than 1 property. When there's 1 property,

I have to write a program that uses knapsack algorithm with a 2 properties. Teacher told us, It has to be done in a 3d array. Wrong implementation will lead to O(2^n) processing time. I can't imagine how would such array look like.

Let's say here's my input:

4 3 4 // number of records below, 1st property of backpack, 2nd property  of backpack
1 1 1 // 1st property, 2nd property, cost
1 2 2 // 1st property, 2nd property, cost
2 3 3 // 1st property, 2nd property, cost
3 4 5 // 1st property, 2nd property, cost

And the output would look like that:

4    // the cheapest sum of costs of 2 records
1 3  // numbers of these 2 records

The explanation of output: 2 sets of records fit's into 1'st line of input:

(1) - record number 1 and record number 3

  1 1 1
+ 2 3 3
-------
  3 4 4

(2) - record number 4

  3 4 5

Because 1st set of the records is the cheapest (4 < 5), we chose it. Not only I'll have to find out whether such set of records exists, I'll also have to find records I've summed.

But for now, I only need to understand, how will 3d array look like. Could some of You help me out with that and show, layer by layer, just like in my image, how would this look like? Thanks.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Paulina
  • 483
  • 3
  • 13
  • 1
    Your question is very vague. What do you mean by "looks like"? Do you mean a visual representation? Do you mean code that models a 3d array? – Daniel Mann Nov 18 '12 at 22:51
  • Oh sorry. I ment visual representation. I'll implement it myself as soon as I understand how does it work. – Paulina Nov 18 '12 at 22:59
  • Please post some code for the easier problem (with 1 property). Also, what do the numbers inside the array in the first picture represent? – anatolyg Nov 18 '12 at 23:13
  • duplicate of http://stackoverflow.com/questions/13449069/knapsack-algorithm-with-an-additional-propertie – Handcraftsman Mar 24 '13 at 18:39

2 Answers2

3

To convert from the algorithm to solve the 1-constraint knapsack problem with dynamic programming to solving the 2-constraint problem is very easy. For someone to draw a 3d image that shows you what's happening there I imagine is somewhat difficult. The algorithm, on the other hand, is quite easy:

I'll assume you want an exact-fit solution, and that you want to minimize cost, not maximize value (which I derived from your example). I've done neither of these variations before, so I can't guarantee there isn't an error.

1-constraint

The matrix for the 1-constraint knapsack problem is (item x weight) storing the value at each position.

The algorithm is basically:

// WL = backpack weight limit
A[0, 0] = 0
for w = 1, 2, ..., WL // weight
  A[0, w] = INFINITY
for i = 1, 2, ..., n // items
  for w = 0, 1, ..., WL // weight
    // wi and ci are the weight and cost of the item respectively
    // A[i-1, w-wi] + ci = 0 when wi > w
    A[i, w] = min(A[i-1, w], A[i-1, w-wi] + ci)

2-constraint

Now to extend this to the include another constraint is just: (let's say size is the other constraint)

The matrix would be (item x weight x size).

// WL = backpack weight limit
// SL = backpack size limit
for w = 0, 1, ..., WL // weight
  for s = 0, 1, ..., SL // size
    A[0, w, s] = INFINITY
A[0, 0, 0] = 0
for i = 1, 2, ..., n // items
  for w = 0, 1, ..., WL // weight
    for s = 0, 1, ..., SL // size
      // wi, si and ci are the weight, size and cost of the item respectively
      // A[i-1, w-wi, s-si] + ci = 0 when wi > w or si > s
      A[i, w, s] = min(A[i-1, w, s], A[i-1, w-wi, s-si] + ci)
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • If we have a question where we have a contraint on the number of elements in the knapsack.It has to be exactly equal to k and maximise capacity.I would assume this algorithm can be used in that case too.Except in the last stage A[i, w, s] = max(A[i-1, w, s], A[i-1, w-wi,s] + ci) where s=1 to k.Also if the value of A[n,WL,k] return infinity, that means no solution could be found for the problem.Do you think It's right? – SteveIrwin Jun 26 '14 at 21:55
  • @SteveIrwin I could be wrong, but I think a constraint on the number of elements would require an additional dimension (so you'd need a 4D array). If you'd like a more definitive, peer reviewed, answer or perhaps just some more information, you may want to ask a separate question. – Bernhard Barker Jun 27 '14 at 16:44
-3

You are trying to do something impossible - that's your problem.

When you are adding to the number of dimensions, your items are getting additional properties. So, instead of a left, column side of a table(prop1), you have rectangle side(prop1 x prop2) or block side (prop1 x prop2 x prop3) and so on. But the existing constraints, that define the upper, row side of the table, should have the same number of dimensions. Not only one dimension!. So, you will never be able to put two-property problem into a 3-dimensional block, you need 4D block instead. 6D block for 3 properties and so on.

Gangnus
  • 24,044
  • 16
  • 90
  • 149