1. Gyanastra
  2. Algorithms
  3. Branch and Bound, Puzzles and Comparison of Design Paradigms
  4. 0/1 Knapsack by Branch and Bound
Free lesson, no account needed 4 min read

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 pi/wip_i/w_i. At a node with profit PP, weight WW, next item index ii:

UB=P+fractional knapsack value of items i,i+1,…,n with capacity m−W.UB = P + \text{fractional knapsack value of items } i, i+1, \dots, n \text{ with capacity } m - W.

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, m=10m = 10)

Item1234
Profit pp10101218
Weight ww2469
p/wp/w52.522

Sorted by p/wp/w descending: order is 1, 2, 3, 4 (already sorted). Capacity m=10m = 10.

Root: P=0,W=0P=0, W=0. Fractional bound: take item 1 (w 2, p 10) → W=2,P=10W=2, P=10; item 2 (w 4, p 10) → W=6,P=20W=6, P=20; item 3 (w 6) — only 4 units of capacity left, so take fraction 4/64/6: profit 12×4/6=812 \times 4/6 = 8. Total UB=20+8=28UB = 20 + 8 = 28. (Item 4 not reached.)

We use LC search (expand largest UBUB first). Node notation: xx = (profit, weight, UB).

Expand root → two children:

  • A = include item 1: P=10,W=2P=10, W=2. Bound: remaining cap 88; item 2 (w4,p10) → W=6,P=20W=6,P=20; item 3 (w6) fraction 2/62/6: 12×2/6=412\times 2/6=4. UB=20+4=24UB = 20+4 = 24.
  • B = exclude item 1: P=0,W=0P=0,W=0. Bound: item 2 (w4,p10) → W=4,P=10W=4,P=10; item 3 (w6,p12) → W=10,P=22W=10,P=22; item 4 no capacity. UB=22UB = 22.

Live: A(24), B(22). Expand A (larger UB).

Expand A (item 2):

  • C = include item 2: P=20,W=6P=20, W=6. Remaining cap 4; item 3 (w6) fraction 4/64/6: 12×4/6=812\times4/6=8. UB=20+8=28UB = 20+8 = 28.
  • D = exclude item 2: P=10,W=2P=10, W=2. Remaining cap 8; item 3 (w6,p12) → W=8,P=22W=8,P=22; item 4 (w9) fraction 2/92/9: 18×2/9=418\times2/9=4. UB=22+4=26UB = 22+4 = 26.

Live: C(28), D(26), B(22). Expand C.

Expand C (item 3):

  • E = include item 3: P=32,W=12>10P=32, W=12 > 10 → infeasible, killed.
  • F = exclude item 3: P=20,W=6P=20, W=6. Remaining cap 4; item 4 (w9) fraction 4/94/9: 18×4/9=818\times4/9=8. UB=20+8=28UB = 20+8 = 28.

Live: F(28), D(26), B(22). Expand F.

Expand F (item 4):

  • G = include item 4: W=6+9=15>10W = 6+9 = 15 > 10 → infeasible, killed.
  • H = exclude item 4: leaf, P=20,W=6P=20, W=6. Incumbent z∗=20z^* = 20.

Live: D(26), B(22). Expand D (26 > 20).

Expand D (item 3):

  • I = include item 3: P=10+12=22,W=2+6=8≤10P=10+12=22, W=2+6=8 \le 10. Remaining cap 2; item 4 (w9) fraction 2/92/9: 18×2/9=418\times2/9=4. UB=22+4=26UB = 22+4 = 26.
  • J = exclude item 3: P=10,W=2P=10, W=2. Remaining cap 8; item 4 (w9) fraction 8/98/9: 18×8/9=1618\times8/9=16. UB=10+16=26UB = 10+16 = 26.

Live: I(26), J(26), B(22). Expand I.

Expand I (item 4):

  • K = include item 4: W=8+9=17>10W = 8+9 = 17 > 10 → infeasible, killed.
  • L = exclude item 4: leaf, P=22,W=8P=22, W=8. New incumbent z∗=22z^* = 22.

Live: J(26), B(22). Expand J (26 > 22).

Expand J (item 4):

  • M = include item 4: W=2+9=11>10W = 2+9 = 11 > 10 → infeasible, killed.
  • N = exclude item 4: leaf, P=10,W=2P=10, W=2. 10<2210 < 22 → not better.

Live: B(22). Prune B: UB(B)=22≤z∗=22UB(B) = 22 \le z^* = 22 → killed by bound (cannot strictly improve).

Answer: z∗=22z^* = 22, items {1, 3} (profit 10+12=2210+12=22, weight 2+6=82+6=8).

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 25−1=312^5-1=31 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. ✔


Pages 1–2 of 2