Algorithms · Branch and Bound, Puzzles and Comparison of Design Paradigms
0/1 Knapsack by Branch and Bound
3.1 The bound
Items sorted by decreasing . At a node with profit , weight , next item index :
The fractional (greedy) knapsack is an upper bound on the 0/1 optimum of the remainder because allowing fractions can only increase profit. This is the same greedy from the Fractional Knapsack chapter, applied to a suffix.
3.2 Worked example (4 items, )
| Item | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Profit | 10 | 10 | 12 | 18 |
| Weight | 2 | 4 | 6 | 9 |
| 5 | 2.5 | 2 | 2 |
Sorted by descending: order is 1, 2, 3, 4 (already sorted). Capacity .
Root: . Fractional bound: take item 1 (w 2, p 10) → ; item 2 (w 4, p 10) → ; item 3 (w 6) — only 4 units of capacity left, so take fraction : profit . Total . (Item 4 not reached.)
We use LC search (expand largest first). Node notation: = (profit, weight, UB).
Expand root → two children:
- A = include item 1: . Bound: remaining cap ; item 2 (w4,p10) → ; item 3 (w6) fraction : . .
- B = exclude item 1: . Bound: item 2 (w4,p10) → ; item 3 (w6,p12) → ; item 4 no capacity. .
Live: A(24), B(22). Expand A (larger UB).
Expand A (item 2):
- C = include item 2: . Remaining cap 4; item 3 (w6) fraction : . .
- D = exclude item 2: . Remaining cap 8; item 3 (w6,p12) → ; item 4 (w9) fraction : . .
Live: C(28), D(26), B(22). Expand C.
Expand C (item 3):
- E = include item 3: → infeasible, killed.
- F = exclude item 3: . Remaining cap 4; item 4 (w9) fraction : . .
Live: F(28), D(26), B(22). Expand F.
Expand F (item 4):
- G = include item 4: → infeasible, killed.
- H = exclude item 4: leaf, . Incumbent .
Live: D(26), B(22). Expand D (26 > 20).
Expand D (item 3):
- I = include item 3: . Remaining cap 2; item 4 (w9) fraction : . .
- J = exclude item 3: . Remaining cap 8; item 4 (w9) fraction : . .
Live: I(26), J(26), B(22). Expand I.
Expand I (item 4):
- K = include item 4: → infeasible, killed.
- L = exclude item 4: leaf, . New incumbent .
Live: J(26), B(22). Expand J (26 > 22).
Expand J (item 4):
- M = include item 4: → infeasible, killed.
- N = exclude item 4: leaf, . → not better.
Live: B(22). Prune B: → killed by bound (cannot strictly improve).
Answer: , items {1, 3} (profit , weight ).
Nodes killed: E, G, K, M (infeasible — 4 nodes); B (bound — 1 node). Total nodes generated: root + A,B + C,D + E,F + G,H + I,J + K,L + M,N = 15. Only 15 of the possible nodes were needed.
Check by brute force: {1,2}=20; {1,3}=22; {1,4}=28 but weight 11 > 10 infeasible; {2,3}=22 weight 10; {2,4}=28 weight 13 infeasible; {3,4}=30 weight 15 infeasible; {1,2,3}=32 weight 12 infeasible. Max feasible = 22. ✔