Sub topic 2
Trying Every Subset and Why It Does Not Scale
This chapter assumes you can read basic Python, understand bit operations on integers, and know Big-O notation. It uses the 0/1 knapsack problem from the previous chapter. You have n items, each with a value and a weight. You may take each item at most once. The goal is the largest total value whose total weight fits within the capacity W.
The idea: check every possible choice
A 0/1 solution is a subset of the items: each item is either in the knapsack or not. Exhaustive search, also called brute force, lists every subset, discards the ones that are too heavy, and keeps the best of the rest. It needs no cleverness, and it always finds the optimum because it examines every candidate.
Each item has two options, take or skip, so n items produce 2^n subsets. The diagram shows the full decision tree for three items.
An implementation with bitmasks
An integer from 0 to 2^n - 1 can stand for a subset: if bit i is set, item i is in the subset. Counting through every integer in that range visits every subset exactly once.
def knapsack_brute(values, weights, capacity):
n = len(values)
best_value, best_set = 0, ()
for mask in range(1 << n):
total_w = total_v = 0
for i in range(n):
if mask >> i & 1:
total_w += weights[i]
total_v += values[i]
if total_w <= capacity and total_v > best_value:
best_value = total_v
best_set = tuple(i for i in range(n) if mask >> i & 1)
return best_value, best_set
print(knapsack_brute([60, 100, 120], [10, 20, 30], 50)) # (220, (1, 2))The program checks all eight subsets. The best one is items 1 and 2, with weight 50 and value 220. Adding item 0 as well brings the weight to 60, which goes over the capacity.
Cost
The outer loop runs 2^n times. The inner loop does n steps each time, so the running time is O(n · 2^n). Extra memory is O(n). The running time depends only on the number of items. Large weights or a large capacity do not slow it down. That property comes back in the meet-in-the-middle chapter.
The problem is how fast 2^n grows:
| Items `n` | Subsets `2^n` | Time at 10^9 subsets per second |
|---|---|---|
| 20 | about 1.0 × 10^6 | about 1 millisecond |
| 40 | about 1.1 × 10^12 | about 18 minutes |
| 60 | about 1.2 × 10^18 | about 37 years |
Each extra item doubles the work. Pure Python checks far fewer than 10^9 subsets per second, so in practice the cutoff comes even earlier, around 25 items.
Common errors
- Writing
1 << n - 1when you mean(1 << n) - 1. In Python,-binds more tightly than<<, so1 << n - 1means1 << (n - 1). - Starting
best_valueat a negative number or skipping the empty subset. The empty subset is always feasible and is worth 0. - Updating
best_valuewithout first checkingtotal_w <= capacity.
Exercise
A brute-force solver checks 10^9 subsets per second. Roughly how long does it take for n = 30 and for n = 50?
Show answer
For n = 30 there are 2^30 ≈ 1.07 × 10^9 subsets, so it takes about 1 second. For n = 50 there are 2^50 ≈ 1.13 × 10^15 subsets, which is about 1.13 × 10^6 seconds, or roughly 13 days. Twenty more items make the search about a million times slower.
Exercise
Why does the brute-force running time not depend on the capacity W, and why might that matter?
Show answer
The search enumerates subsets of items, and the number of subsets is 2^n whatever the weights are. When the capacity or the weights are very large but n is small, this makes exhaustive search (or meet in the middle) a better fit than a table indexed by capacity.