Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 knapsacks with same capacity - Why can't we just find the max-value twice

If you are given one set of items with values and weight: [(w1,v2),(w2,v2),...(wn,vn)], and two knapsacks Knap1 and Knap2 of equal capacity C, the goal is to determine what are the optimal subsets of items S1 and S2 that can go into Knap1 and Knap2 respectively and maximize the knapsacks' values and capacity.

An incorrect way to solve this would be to first fill Knap1 with a DP programming algorithm using all of the items as candidates, and then fill Knap2 by using the leftover items from Knap1.

I don't understand why this algorithm is incorrect if the two knapsacks have equal capacity. Could someone please explain or give an example?

like image 413
Joseph D Avatar asked Dec 11 '22 22:12

Joseph D


2 Answers

A counterexample:
Set S of items: (w_i, v_i)

   s_1=(1,2) , s_2=(2,1) , s_3=(3,10) , s_4=(4,7) 

Capacity of the Knapsacks: c_1 = c_2 = 5

Your first round of DP would take the items s_1 and s_3 which would result in value 12. Now for the second knapsack, there is s_4 and s_2 left. Thus your algorithm will choose s_4 which will result in value 7. s_2 will be left over.
Total value: 19

The optimal solutions would be s_1 and s_4 in one knapsack and s_2 and s_3 in the other. Optimal total value: 20

like image 184
bro Avatar answered Dec 13 '22 12:12

bro


Suppose the capacity of the knapsacks is 10, and we have these objects (weight, value):

(8, 200) (1, 10) (1, 10) (2, 15) (9, 100)

The greedy algorithm only looking at one knapsack would use the objects of weight 8, 1 and 1 to score 220 value, but when you consider both sacks clearly leaving the 1's and taking the 2 is better.

like image 26
Stef Avatar answered Dec 13 '22 10:12

Stef