Recursion Formula
Recursion Formula
Value vi
15
10
9
5
Weight wi
1
5
3
4
f(0,g) = 0, f(k,0) = 0
Recursion formula:
f(k,g) =
f(k-1,g)
if wk > g
9 max {vk + f(k-1,g-wk), f(k-1,g)} if wk # g and k>0
Solution tabulated:
Capacity remaining
g=0
g=1
g=2
g=3
g=4
g=5
g=6
g=7
g=8
k=0
f(0,g) =
k=1
f(1,g) =
15
15
15
15
15
15
15
15
k=2
f(2,g) =
15
15
15
15
15
25
25
25
k=3
f(3,g) =
15
15
15
24
24
25
25
25
k=4
f(4, g) =
15
15
15
24
24
25
25
29
f(4,8) f(3,8) Y x4 = 1
g = g - w4 = 8-4 = 4
k=3
f(3,4) f(2,4)Y x3 = 1
g = g - w3 = 4-3 = 1
k=2, :
f(2,1) = f(1,1) Y x2 = 0
g=g -0=1
k=1
f(1,1) f(0,1)Y x1 = 1
g = g - w1 = 1-1 = 0
The solution is x = (1,0,1,1) i.e. items 1,3, and 4 are selected. value of the knapsack is 29.