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

我的未来我决定 提交于 2019-12-10 16:39:06

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/31696694/2-knapsacks-with-same-capacity-why-cant-we-just-find-the-max-value-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!