Overview
0/1 knapsack dynamic programming considers each item once and stores the best achievable value at each capacity. Each table entry chooses between skipping the item and taking it if capacity allows.
Solves 0/1 knapsack by tabulating the best value for each item prefix and capacity.
Compact metadata for implementation and discovery.
0/1 knapsack dynamic programming considers each item once and stores the best achievable value at each capacity. Each table entry chooses between skipping the item and taking it if capacity allows.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n)
Average
O(n log n)
Worst
O(n^2)
Space
O(n)
dp[i][w] = max(dp[i-1][w], value_i + dp[i-1][w - weight_i])Chooses whether to include item i when it fits in the remaining capacity.
Show filling a value table over items and capacity on a small knapsack optimization example.
The common one-dimensional implementation is correct for 0/1 knapsack only when capacities are scanned downward.
What each reference supports for this entry.
Textbook support for 0/1 Knapsack Dynamic Programming's standard algorithm design, invariants, and complexity analysis.
Open sourceKnapsack reference for 0/1 Knapsack Dynamic Programming's exact, approximate, branch-and-bound, or split-enumeration formulation.
Good next jumps from the relationship graph, ranked for useful discovery.
Optimization · Core
Both use dynamic programming ideas for related knapsack optimization problems.
Compare with thisBoth use dynamic programming ideas for related knapsack optimization problems.
Both use dynamic programming ideas for related knapsack optimization problems.
Both use dynamic programming ideas for related knapsack optimization problems.