Sub topic 11
Bounded Knapsack: Limited Copies and Binary Splitting
This chapter assumes you already know the one-dimensional 0/1 DP with its backward capacity loop and the unbounded variant with its forward loop. You should also be comfortable with Python lists and Big-O notation.
The Problem
In the bounded knapsack, item i has value v[i], weight w[i], and a count k[i]. You may take anywhere from 0 to k[i] copies of it, and the total weight must stay within capacity W. The 0/1 knapsack is the special case where every k[i] = 1. The unbounded knapsack is the case where every k[i] is effectively infinite.
Neither earlier loop fits this problem directly. The backward loop allows at most one copy of each item. The forward loop allows unlimited copies.
The Naive Approach: Expand Every Copy
The simplest fix is to list each item k[i] times and run the 0/1 DP on the expanded list. This gives correct answers, but it costs O(W · Σ k[i]). An item with 1,000 copies adds 1,000 passes over the array.
Binary Splitting
You don't need every copy as its own item. You only need a set of bundles whose subsets can add up to every count from 0 to k. Bundles of 1, 2, 4, ... copies, plus one final bundle holding the remainder, achieve this:
- Bundles of sizes 1, 2, ..., 2^(j-1) sum to 2^j − 1, and their subsets produce every count in 0..2^j − 1.
- Adding a remainder bundle of size
r = k − (2^j − 1)extends the reachable range to 0..k.
Each bundle becomes a single 0/1 item, with its value and weight scaled by the bundle size. An item with k copies now becomes O(log k) items instead of k.
Putting It Together
The code below splits each item into bundles, then runs the ordinary backward-loop 0/1 DP over the bundles.
def split_bounded(values, weights, counts):
items = []
for v, w, k in zip(values, weights, counts):
p = 1
while k > 0:
take = min(p, k)
items.append((v * take, w * take))
k -= take
p *= 2
return items
def knapsack_bounded(values, weights, counts, capacity):
dp = [0] * (capacity + 1)
for v, w in split_bounded(values, weights, counts):
for c in range(capacity, w - 1, -1): # backwards: each bundle at most once
dp[c] = max(dp[c], dp[c - w] + v)
return dp[capacity]
print(split_bounded([10], [3], [5])) # [(10, 3), (20, 6), (20, 6)]
print(knapsack_bounded([60, 100, 120], [10, 20, 30], [2, 1, 1], 50)) # 240On the running example, unbounded knapsack reached 300 by taking five copies of the first item. With only two copies allowed, the best answer is 240: both copies of item 0 (weight 20) plus item 2 (weight 30).
| Method | Time | Items fed to the DP |
|---|---|---|
| Expand every copy | O(W · Σ k[i]) | Σ k[i] |
| Binary splitting | O(W · Σ log k[i]) | Σ ⌈log₂(k[i]+1)⌉ |
Common Errors
- Running the capacity loop forwards. A forward loop lets a bundle be reused, which turns the problem back into unbounded knapsack and allows more than
kcopies. - Scaling only the value. A bundle of
tcopies weighsw * tas well as being worthv * t. - Splitting into equal chunks. For example, splitting 5 copies as 2 + 2 + 1 happens to work, but equal chunks in general leave gaps. Powers of two plus a remainder never leave gaps.
Exercise
What bundles does split_bounded produce for an item with k = 10 copies, and which bundles form 6 copies and 9 copies?
Show answer
The bundle sizes are 1, 2, 4 and a remainder of 3. Six copies is 2 + 4, and nine copies is 2 + 4 + 3. (Other combinations also work, such as 6 = 1 + 2 + 3.)
Exercise
If you change the inner loop of knapsack_bounded to run forwards, what does it return for values [60, 100, 120], weights [10, 20, 30], counts [2, 1, 1], capacity 50, and why?
Show answer
It returns 300. With a forward loop, each bundle can be reused, so the (60, 10) bundle is taken five times. That breaks the limit of 2 copies and gives the unbounded answer.