[ 0/1 Knapsack] Book example 5.11
Consider the case in which n = 3, w1 = 2, w2 = 3, w3 = 4 and p1 = 1, p2 = 2, p3 = 5 and m = 6 General equation: gi(y) = max{gi+1(y), gi+1(y-wi+1) + pi+1}
g0(m) = g0(6) = the value of an optimal solution to KNAP(1,3,6) (from KNAP(1,n,m) )
- def. g0(m) = max{g1(m), g1(m-w1) + p1}
- def. g0(6) = max{g1(6), g1(6-24) + 1}
- def. g1(6) = max{g2(6), g2(6-w23) + 2}
- def. g2(6) = max{g3(6), g3(6-w32) + 5}
but n = 3 and hence g3 = gn. Given: gn(y)=0 for all y > 0
- so g2(6) = max{0, 0+5} = 5
- using 3,5 g1(6) = max{5, g2(3) + 2}
- def. g2(3) = g3(3), g3(3-w33-4) + 5}
but n = 3 and hence g3 = gn. Given: gn(y)= - for all y < 0 ( y < 0 indicates weight of negative value - can't do)
- so g2(3) = max{0,- } = 0
- 6 becomes g1(6) = max{5, 2} = 5
- 2 becomes g0(6) = max{5, g1(4) + 1}
- def. g1(4) = max{g2(4), g2(4 -w21) + 2}
- def. g2(4) = max{g3(4), g3(4 -w30) + 5} = max{0,5} = 5
- def. g2(1) = max{g3(1), g3(1 -w3-3) + 5} = max{0,-} = 0
- 11 becomes g1(4) = max{5, 2} = 5
- 2 (with 9 and 14) g0(6) = max{5, 5 + 1} = 6
All in all, I think mathematicians are magic. Rather beautiful, isn't it.