~/ learn/ comp-372/ cards/ When greedy works vs when it fails: knapsack
1 of 5

Type the 0/1 knapsack DP recurrence

Type the 0/1 knapsack DP recurrence

Answer

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

dp[i][w] is the best value using the first i items within weight w. Either skip item i (dp[i-1][w]) or take it (value v_i plus the best for the remaining capacity w - w_i). Fill the table in O(nW); pseudo-polynomial because W is a numeric value, not a count.

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