Sub topic 17
Beyond One Constraint: Multidimensional and Related Variants
This chapter assumes you know the one-array 0/1 DP and why its capacity loop runs backwards. It also assumes you know the pseudo-polynomial running time O(nW). Many real problems look like knapsack but add a second limit, group the items, or change the objective. Most of these variants reuse the same take-or-skip idea, and each one changes the cost in a predictable way.
Two Constraints: Weight and Volume
In the multidimensional knapsack, each item has a value and several resource costs. Every resource has its own capacity. With two resources, say weight w and volume u, the state needs both remaining capacities. The recurrence becomes:
best(i, c, d) = max(best(i+1, c, d), vi + best(i+1, c − wi, d − u_i))
The second option is allowed only when wi ≤ c and ui ≤ d.
The one-array trick still works with a two-dimensional array. Both loops must run backwards so that each item is used at most once.
def knapsack_2d(values, weights, volumes, max_weight, max_volume):
dp = [[0] * (max_volume + 1) for _ in range(max_weight + 1)]
for v, w, u in zip(values, weights, volumes):
for c in range(max_weight, w - 1, -1):
for d in range(max_volume, u - 1, -1):
dp[c][d] = max(dp[c][d], dp[c - w][d - u] + v)
return dp[max_weight][max_volume]
print(knapsack_2d([60, 100, 120], [10, 20, 30], [2, 3, 4], 50, 5)) # 160The weight limit alone would allow items 1 and 2 for 220. Their total volume is 7, which exceeds the volume limit of 5, so the best feasible choice is items 0 and 1 for 160.
Related Variants
| Variant | What changes | How the DP adapts |
|---|---|---|
| Subset sum | Value equals weight. The question is whether some subset hits exactly W | Boolean array: `dp[c] = dp[c] or dp[c - w]` |
| Multiple-choice | Items come in groups and you pick at most one item per group | Loop over groups; for each capacity, take the best item in the group |
| Multiple knapsacks | Several bins, each with its own capacity | State grows per bin; usually solved with ILP or heuristics |
| Minimum-cost cover | Reach a value of at least T at the lowest weight | Value-indexed DP, read the cheapest weight at any t ≥ T |
Common Errors
- Running the inner volume loop forward makes items reusable along that axis, which silently turns the problem into a partly unbounded one.
- Adding the second resource with a greedy ratio such as value / (weight + volume) gives no optimality guarantee.
- Allocating a table for large capacities without first checking its size, for example 10⁴ × 10⁴ cells per item pass.
Exercise
In the multiple-choice knapsack, each group holds several (value, weight) items and you may take at most one item per group. Using one array dp indexed by capacity, how should the loops be arranged, and why does the capacity loop still run backwards?
Show answer
The outer loop goes over groups and the middle loop runs capacity c from W down to 0. The inner loop goes over items in the group and sets dp[c] = max(dp[c], dp[c - w] + v) whenever w ≤ c. Because c descends, every dp[c - w] that is read still holds the value from before this group, so at most one item from the group is added. If the item loop were outside the capacity loop, two items from the same group could be combined.
Exercise
A 2D knapsack has n = 100 items, weight capacity 1,000 and volume capacity 500. Roughly how many cell updates does the DP perform, and how much memory does the array need?
Show answer
About 100 × 1,000 × 500 = 5 × 10^7 updates. The array has (1,001 × 501) ≈ 5 × 10^5 cells, which is manageable.