0

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])

newbie_old
  • 500
  • 5
  • 19

1 Answers1

0

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])

ysrhung
  • 554
  • 2
  • 6
  • 11
  • the question is not to rewrite the equation but let me know if the below is wrong? 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]) – newbie_old Mar 19 '15 at 19:06
  • Your equation is very probably wrong. Please have a look at http://en.wikipedia.org/wiki/Knapsack_problem to understand how to deduce the recurrence equation for 0/1 knapsack problem with one knapsack. Once you understand the principle of making recurrence equation for this base problem, then you could deduce the correct equation for the knapsack problem with 2 knapsacks. – ysrhung Mar 19 '15 at 23:36
  • 1
    There should be 3 separate cases: (1) item i is not put into both knapsacks, (2) item i is put in knapsack 1 and (3) item i is put in knapsack 2. So there should be 3 cases. Let's take case (3) as an example, the value of case (3) should be DP[i-1][j][k-W[i]] + C[i] since knapsack 2 capacity decreases by W[i] and value increases by c[i]. Note: item i is considered and thus it should be something like "DP[i-1]....." That's why your equation looks wrong. – ysrhung Mar 19 '15 at 23:56