Sub topic 14
Branch and Bound with a Fractional Bound
This chapter assumes you know the 0/1 knapsack problem, the greedy algorithm for fractional knapsack, and exhaustive search over subsets. You should also be comfortable reading recursive Python.
The weight-indexed DP takes O(nW) time. If the capacity W is in the billions, that table cannot be built. Branch and bound avoids it. It runs the exhaustive search over subsets, but it skips any part of the search tree that cannot beat the best answer found so far.
The working model
Each node in the search tree is a partial decision. Items 0 to i−1 have been decided, and the node knows the value taken and the capacity left. From a node, the search branches two ways: take item i, or skip it.
At every node you compute an upper bound, which is a number the best completion of this node cannot exceed. If the bound is no better than the incumbent (the best complete solution found so far), you prune the node and never visit its subtree.
The fractional knapsack gives a cheap upper bound with no gaps. Fill the remaining capacity greedily by value-to-weight ratio, and let the last item go in partly. Any 0/1 completion is also a valid fractional solution, so it can never be worth more than the fractional optimum.
Implementation
def knapsack_bb(values, weights, capacity):
items = sorted(zip(values, weights), key=lambda it: it[0] / it[1], reverse=True)
n = len(items)
best = 0
def bound(i, value, room):
# Fractional greedy over items i..n-1
for v, w in items[i:]:
if w <= room:
room -= w
value += v
else:
return value + v * room / w
return value
def search(i, value, room):
nonlocal best
if value > best:
best = value
if i == n or bound(i, value, room) <= best:
return
v, w = items[i]
if w <= room:
search(i + 1, value + v, room - w) # take branch first
search(i + 1, value, room) # skip branch
search(0, 0, capacity)
return best
print(knapsack_bb([60, 100, 120], [10, 20, 30], 50)) # 220The order of the work affects how fast this runs:
- The items are sorted by ratio once. This lets the bound be computed with one linear scan, and it makes the take-first branch reach good solutions early.
- A good incumbent found early makes later pruning much stronger.
Common errors
| Mistake | Effect |
|---|---|
| Forgetting to sort by ratio | The bound is no longer the fractional optimum. It may be too low, so the search prunes the true answer and returns a wrong result. |
| Pruning with `<` where `<=` is safe | Still correct, but the search explores ties for no gain. |
| Updating the incumbent only at leaves | Pruning is weaker, because every partial node already holds a feasible solution. |
| Expecting polynomial time | The worst case is still O(2^n). Branch and bound is a heuristic speedup, not a complexity improvement. |
Exercise
Why does skipping the subtree when bound(i, value, room) <= best never lose the optimal 0/1 solution?
Show answer
Every 0/1 completion of the node is also a feasible fractional solution for the remaining items. The sorted greedy fill gives the maximum fractional value, so no completion can beat the bound. If the bound is at most the incumbent, nothing in the subtree can improve on the incumbent.
Exercise
Suppose the bound is replaced by value plus the sum of all remaining item values, with capacity ignored. Is the algorithm still correct, and how does its speed change?
Show answer
The algorithm is still correct, because that sum is also a valid upper bound. The bound is much looser, though, so far fewer nodes are pruned and the search moves closer to full exhaustive search.