Knapsack Problem & Multistage Graph: Jeremy Ardhito Sulle/ 2440031250 Jason Oei / Joshua Immanuel

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

KNAPSACK PROBLEM &

MULTISTAGE GRAPH
Jeremy Ardhito Sulle/ 2440031250
Jason Oei /
Joshua Immanuel /
KNAPSACK
PROBLEM
PROBLEM
A theif enters a house. He carries a bag •n=4
that can ONLY fit 6 kg of goods. In the
house there are item A, B, C, and item D. • W = 6; (b1, b2, b3, b4) = (6, 5, 9, 8)
Item A weight 3 kg, value $6 • (w1, w2, w3, w4) = (3, 2, 5, 4)
Item B weight 2 kg, value $5
Item C weight 5 kg, value $9
Item D weight 4 kg, value $8 • n is number of items
Each item is only one piece, and • W is the maximum weight that fit the bag
can not be taken in apart. The thief • bn is benefit of item n
has only a choice, take it or leave
• wn is weight of item n
it, can not bring a half. The thief
has to bring any items to get the
maximum benefit?
for w=0 to Wmax do
K[0,w]=0 Inisialisasi array ke 0
end for dengan nilai 0
for i=1 to n do
K[i,0]=0
end for
for i=1 to n do
for w=0 to Wmax do
if w[i]<=w then
Jika w barang lebih kecil
if b[i]+K[i-1,w-w[i]]>K[i-1,w] then
dari w maksimal
K[i,w]=b[i]+K[i-1,w-w[i]]
else
K[i,w]=K[i-1,w]
end if
else Jika w barang lebih besar
K[i,w]=K[i-1,w] dari w maksimal
end if
end for
end for
n = 4;
W = 6;
(b1, b2, b3, b4) = (6, 5, 9, 8);
(w1, w2, w3, w4) = (3, 2, 5, 4)

for w = 0 to W do K[0,w]=0
for i = 1 to n do K[i,0]=0

i\w 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
n = 4; W = 6; (b1, b2, b3, b4) = (6, 5, 9, 8); (w1, w2, w3, w4) = (3, 2, 5,
4)

if w[i]<=w then
if b[i]+K[i-1,w-w[i]]>K[i-1,w] then
K[i,w]=b[i]+K[i-1,w-w[i]]
else
K[i,w]=K[i-1,w]
end if
else
K[i,w]=K[i-1,w] B[1] + K[0, (3-3)]
end if 6+0
6
i\w 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

i=1 b[1]=6 w[1]=3 1 0 0 0 6 6 6 6

2 0

3 0

4 0
n = 4; W = 6; (b1, b2, b3, b4) = (6, 5, 9, 8); (w1, w2, w3, w4) = (3, 2, 5, 4)

if w[i]<=w then
if b[i]+K[i-1,w-w[i]]>K[i-1,w] then
K[i,w]=b[i]+K[i-1,w-w[i]]
else
K[i,w]=K[i-1,w]
end if
else
K[i,w]=K[i-1,w]
end if B[2] + K[1, 0] B[2] + K[1, (5-2)]
5+0 5+6
5 11
i\w 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 0 0 6 6 6 6

i=2 b[2]=5 w[2]=2 2 0 0 5 6 6 11 11

3 0

4 0
n = 4; W = 6; (b1, b2, b3, b4) = (6, 5, 9, 8); (w1, w2, w3, w4) = (3, 2, 5, 4)

if w[i]<=w then
if b[i]+K[i-1,w-w[i]]>K[i-1,w] then
K[i,w]=b[i]+K[i-1,w-w[i]]
else
K[i,w]=K[i-1,w]
end if
else
K[i,w]=K[i-1,w]
B[3] + K[2, (5-5)]
end if
5+0
5

i\w 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 0 0 6 6 6 6

2 0 0 5 6 6 11 11

i=3 b[3]=9 w[3]=5 3 0 0 5 6 6 11 11

4 0
n = 4; W = 6; (b1, b2, b3, b4) = (6, 5, 9, 8); (w1, w2, w3, w4) = (3, 2, 5, 4)

if w[i]<=w then
if b[i]+K[i-1,w-w[i]]>K[i-1,w] then
K[i,w]=b[i]+K[i-1,w-w[i]]
else MAKA, NILAI MAKSIMAL YANG
K[i,w]=K[i-1,w] BISA DIDAPATKAN ADALAH 13
end if
else
K[i,w]=K[i-1,w]
end if B[4] + K[3, 4-4] B[4] + K[3, 6-4]
8+0 8+5
8 13
i\w 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 0 0 6 6 6 6

2 0 0 5 6 6 11 11

3 0 0 5 6 6 11 11

i=4 b[4]=8 w[4]=4 4 0 0 5 6 8 11 13

You might also like