~/ learn/ comp-372/ cards/ 0/1 knapsack DP — the OilKnapsack bridge
1 of 6

The 0/1 knapsack recurrence (type it)

The 0/1 knapsack recurrence (type it)

Answer

dp[0][w] = 0 dp[i][w] = dp[i-1][w] if w_i > w dp[i][w] = max(dp[i-1][w], dp[i-1][w - w_i] + v_i) if w_i <= w

Two choices per item: skip it (carry the row above) or take it (value v_i plus the best of the remaining capacity in the row above). The whole-or-nothing rule is why we read row i-1, never row i.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/0f68ef17-ec20-42df-8554-d78c2a757f27/flashcard utf-8 LF