algorithm for the 0-1 Knapsack with 2 sacks?

血红的双手。 提交于 2019-12-25 04:26:53

问题


formally, say, we have 2 sacks with capacities c1 and c2. There are N items with profits pi and weights wi. As in 0-1 Knapsack problem, we need to fill in c1 and c2 with these items in such a way the overall profit is maximized. Assume pi and wi are positive integers!

For the 2 knapsack problem does below recurrence relation hold good?

DP[I][J][K] is maximum profit we could achieve from the first i items such that the weight of exactly j was used in knapsack #1 and a weight of exactly k was used in knapsack #2

DP[i][j][k] = max(DP[i-1][j][k], DP[i][j-1][k], DP[i][j][k-1], DP[i][j-W[j]][k] + C[i], DP[i][j][k-W[k]] + C[i])


回答1:


Suppose C[i] and W[i] are the value and weight of item respectively.

Given that j-W[i] >0, k-W[i] > 0 (for the ease of writing the formula. We could still write a formula without this assumption by adding two more lines), the equation should be

DP[i][j][k] = max(DP[i-1][j][k], DP[i-1][j-W[i]][k]+C[i],DP[i-1][j][k-W[i]]+C[i])



来源:https://stackoverflow.com/questions/29136342/algorithm-for-the-0-1-knapsack-with-2-sacks

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