Sub topic 1
What the Knapsack Problem Asks
You have a bag that can carry a limited weight and a set of items. Each item has a weight and a value. You want to pick items so that the total value is as large as possible and the total weight stays within the limit. This chapter sets out that problem precisely and names the variants used in the rest of the tutorial.
Prerequisites
This chapter assumes you know:
- Basic Python: functions, loops, lists and tuples
- Recursion, which later chapters use
- Big-O notation for time and space
- Two-dimensional arrays or nested lists
The Formal Statement
An instance has:
nitems, numbered0ton - 1- a value
v[i] > 0and a weightw[i] > 0for each item - a capacity
W, the largest total weight the knapsack can hold
A solution assigns an amount x[i] to each item. A solution is feasible if its total weight sum(w[i] * x[i]) is at most W. The goal is to maximize the total value sum(v[i] * x[i]) over all feasible solutions.
The variants differ only in which values x[i] may take.
| Variant | Allowed `x[i]` | Plain meaning |
|---|---|---|
| 0/1 | 0 or 1 | Take each item whole, or leave it |
| Fractional | Any real number from 0 to 1 | You may take part of an item |
| Unbounded | Any whole number ≥ 0 | Unlimited copies of each item |
| Bounded | Whole number from 0 to `k[i]` | At most `k[i]` copies of item `i` |
When people say "the knapsack problem" without a qualifier, they usually mean the 0/1 version.
A Running Example
The tutorial uses this instance throughout:
| Item | Value | Weight |
|---|---|---|
| 0 | 60 | 10 |
| 1 | 100 | 20 |
| 2 | 120 | 30 |
The capacity is W = 50.
The function below checks whether a selection is feasible and returns its value. It uses whole-number counts, so it covers the 0/1, unbounded and bounded variants.
def evaluate(values, weights, capacity, counts):
# counts[i] is how many copies of item i are taken
total_weight = sum(w * k for w, k in zip(weights, counts))
if total_weight > capacity:
return None # infeasible
return sum(v * k for v, k in zip(values, counts))
values, weights, capacity = [60, 100, 120], [10, 20, 30], 50
print(evaluate(values, weights, capacity, [0, 1, 1])) # 220
print(evaluate(values, weights, capacity, [1, 1, 1])) # None, weight 60 > 50
print(evaluate(values, weights, capacity, [5, 0, 0])) # 300, valid only if reuse is allowedThe selection [5, 0, 0] shows why you must fix the variant first. It is the best answer for the unbounded problem, but it is not allowed in the 0/1 problem.
Common Errors in Stating the Problem
- Maximizing weight instead of value. Weight is the constraint. Value is the objective.
- Requiring the bag to be exactly full. The standard problem allows unused capacity. The "exactly full" version is a different problem and needs different base cases.
- Allowing zero or negative weights. Most algorithms in this tutorial assume
w[i] > 0. An item with zero weight and positive value should simply always be taken. Handle that case separately.
Exercise
For the running example (values 60, 100, 120; weights 10, 20, 30; capacity 50), is the selection "item 0 twice and item 2 once" feasible, and what is its value? Which variants allow it?
Show answer
Its weight is 2·10 + 30 = 50, which is within the capacity, and its value is 2·60 + 120 = 240. The selection takes item 0 twice. The 0/1 and fractional variants do not allow that. The unbounded variant allows it, and so does the bounded variant if item 0 has a limit of at least 2 copies.
Exercise
State the bounded knapsack problem as a constraint on x[i], and explain how the 0/1 problem is a special case of it.
Show answer
In the bounded problem, each x[i] is a whole number with 0 ≤ x[i] ≤ k[i], the total weight must be at most W, and the goal is to maximize total value. The 0/1 problem is the bounded problem with every k[i] = 1.