Dynamic Programming21 sections · 916 units
Open in Course

Knapsack 2 - Transition

Minimum weight

For each item ii and value vv, you have two choices:

1.1. Skip item ii: dp[i][v]=dp[i1][v]dp[i][v] = dp[i-1][v]

2.2.

Take item ii: dp[i][v]=dp[i1][vvi]+widp[i][v] = dp[i-1][v-v_i] + w_i (if vviv \ge v_i) Take the minimum: dp[i][v]=min(dp[i1][v],dp[i1][vvi]+wi)dp[i][v] = \min(dp[i-1][v], dp[i-1][v-v_i] + w_i) After filling the table, scan from VmaxV_{max} down to 00. The first vv where dp[n][v]Wdp[n][v] \le W is your answer. This formula captures the relationship between smaller problems.