際際滷

際際滷Share a Scribd company logo
Dynamic Programming-Knapsack Problem
Dynamic Programming
It is a multistage optimizing decision
making problem closely related to divide
& conquer technique.
It solves the sub-problem only once &
stores the result in a table instead of
solving it recursively.
0/1 Knapsack Problem
 Given a set of n items and a knapsack having
capacity w, each item has weight wi and
value vi.
 The problem is to pack the knapsack in such
a way so that maximum total value is
achieved and the total weight should not be
more than the fixed weight.
 The problem is called 0/1 knapsack because
the items are either accepted or rejected.
Algorithm
 Loop, for w0 to W(total capacity)
Set Kp[0,w] 0.
 Loop, for i 1 to n(set of items)
Set Kp[i,0] 0.
 Check whether K is a part of solution or not-
if (wi<=w)(can be part of the solution)
if (vi+kp[i-1,w-wi])>Kp(i-1,w)
Set kp[i,w] vi
+kp[i-1,w-wi]
Else
Set Kp[i,w] Kp[i-1,w]
 Exit
Consider a knapsack with weight capacity
W=3 and total items 3 such that
S=3
wi={1,2,3}
vi={2,3,4}
On applying knapsack algorithm,
Item wi
vi
1 1 2
2 2 3
3 3 4
i/w 0 1 2 3
0
1
2
3
for w 0 to W, for i1 to n
Set Kp[0,w] 0, Set Kp[i,0]0
i/w 0 1 2 3
0 0 0 0 0
1 0
2 0
3 0
i=1,vi=2,wi=1,w=1,w-wi=0
if (wi<=w), set kp[i,w] vi
+kp[i-1,w-wi]
Kp[1,1]2+Kp[0,0]=2
i/w 0 1 2 3
0 0 0 0 0
1 0
2 0
3 0
i/w 0 1 2 3
0 0 0 0 0
1 0 2
2 0
3 0
i=1,vi=2,wi=1,w=2,w-wi=1
if (wi<=w), set kp[i,w] vi
+kp[i-1,w-wi]
Kp[1,1]2+Kp[1,1]=2
i/w 0 1 2 3
0 0 0 0

0
1 0 2 2
2 0
3 0
i=1,vi=2,wi=1,w=3,w-wi=2
(wi<=w), Kp[1,3]2+Kp[0,2]=2
i=2,vi=3,wi=2,w=1,w-wi=-1
if (wi>w), Set Kp[i,w] Kp[i-1,w]
kp[2,1]Kp[1,1]=2
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2
3 0
i=2,vi=3,wi=2,w=2,w-wi=0
(wi<=w), Kp[2,2]3+Kp[1,0]=3
i=2,vi=3,wi=2,w=3,w-wi=1
(wi<=w), Kp[2,3]3+Kp[1,1]=3+2=5
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0
i=3,vi=4,wi=3,w=1,w-wi=-2
if (wi>w), Set Kp[i,w] Kp[i-1,w]=2
i=3,vi=4,wi=3,w=2,w-wi=-1
if (wi>w), Set Kp[i,w] Kp[i-1,w]=3
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3
i=3,vi=4,wi=3,w=3,w-wi=0 (wi<=w),
if (vi+kp[i-1,w-wi])>Kp(i-1,w)=4<5
Kp[3,3]5
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3 5
Now we have computed the maximum
possible values that can be taken in a
knapsack i.e Kp[n,W]
In order to select items that can take part
in making this maximum value let
i=n,k=W.
Algorithm
 Set in
set kW
 Loop to check K is a part of knapsack
while (i>0 and k>0)
if Kp[i,k]Kp[i-1,k] then
mark the ith item in knapsack
set i i-1
set k k-wi
Else
set i i-1
 Exit
i=3,k=3,vi=4,wi=3,Kp[i,k]=5,Kp[i-1,k]=5
ii-1
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3 5
i=2,k=3,vi=3,wi=2,Kp[i,k]=5,Kp[i-1,k]=2
Set kk-wi=1
Set ii-1
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3 5
i=1,k=1,vi=2,wi=1,Kp[i,k]=2,Kp[i-1,k]=0
Set kk-wi=0
Set ii-1
i=0
K=0
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3 5
The optimal knapsack contains
S= {1,2}
wi= {1,2}={3} <= W
vi= {2,3}= {5}損 Optimum Value
THANKS!!!!

More Related Content

Dynamic Programming-Knapsack Problem