Dynamic Programming21 sections · 915 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[i−1][v]dp[i][v] = dp[i-1][v]

2.2.

Take item ii: dp[i][v]=dp[i−1][v−vi]+widp[i][v] = dp[i-1][v-v_i] + w_i (if v≥viv \ge v_i) Take the minimum: dp[i][v]=min⁡(dp[i−1][v],dp[i−1][v−vi]+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.