0% found this document useful (0 votes)
113 views14 pages

Knapsack Algoritma Weakness

The document describes the knapsack problem and its solution using a dynamic programming approach. It involves finding the optimal combination of items with different weights and values to maximize the total value within a knapsack's weight capacity. A table is populated with the maximum value for each possible total weight using items up to that point. The optimal solution is then the combination of items that generates the highest value in the last row, within the knapsack weight limit.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views14 pages

Knapsack Algoritma Weakness

The document describes the knapsack problem and its solution using a dynamic programming approach. It involves finding the optimal combination of items with different weights and values to maximize the total value within a knapsack's weight capacity. A table is populated with the maximum value for each possible total weight using items up to that point. The optimal solution is then the combination of items that generates the highest value in the last row, within the knapsack weight limit.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Knapsack Problem

Truck 10t capacity

Optimum cargo combination:


Item 1: $5 (3t) Item 2: $7 (4t) Item 3: $8 (5t)

Knapsack Problem
Output function f(i,w) Optimum output of a combination of items 1 to i with a cumulated weight of w or less. Item 1: x1=$5 ; w1=3t Item 2: x2=$7 ; w2=4t Item 3: x3=$8 ; w3=5t

Knapsack Problem
Output function f(i,w)

f(i,w)=Max[ xi + f(i,wf(i,w-wi) ; f(if(i-1,w) ]

ONE Item i + optimum combination of weight w-wi

NO Item i + optimum combination items 1 to i-1

Knapsack Problem
Table
1 1 2 3 f(i,w) i 2 3 4 5 6 7 8 9 10 W

Knapsack Problem
Table
1 1 2 3 2 3 4 5 6 7 8 9 10 W

Using only item 1

Knapsack Problem
Table
1 1 2 3 Using only item 1 & 2 2 3 4 5 6 7 8 9 10 W

Knapsack Problem
Table
1 1 2 3 Using items 1, 2 & 3 2 3 4 5 6 7 8 9 10 W

Knapsack Problem
Table
1 1 2 3
0 items n1
1 items n1 w1 = 3 2 items n1 2 w1 = 6

2 0

3 5

4 5

9 10

5 10

Knapsack Problem
Table
1 1 2 3
+ x2 (= 7)

2 0 0

3 5 5

4 5 7

9 10

0 0

5 10 10 10 15 15

w w2 = 54=1

f(i,w)=Max[ f(i,w) =Max[ xi + f(i,wf(i,w-wi) ; f(i f(i-1,w) ]

Knapsack Problem
Table
1 1 2 3
+ x2 (= 7)

2 0 0

3 5 5

4 5 7

5 7

9 10

0 0

5 10 10 10 15 15

f(i,w)=Max[ f(i,w) =Max[ xi + f(i,wf(i,w-wi) ; f(i f(i-1,w) ]

Knapsack Problem
Table
1 1 2 3
+ x2 (= 7)

2 0 0

3 5 5

4 5 7

5 7

9 10

0 0

5 10 10 10 15 15

w w2 = 64=2

f(i,w)=Max[ f(i,w) =Max[ xi + f(i,wf(i,w-wi) ; f(i f(i-1,w) ]

Knapsack Problem
Table
1 1 2 3
+ x2 (= 7)

2 0 0

3 5 5

4 5 7

9 10

0 0

5 10 10 10 15 15 7 10

f(i,w)=Max[ f(i,w) =Max[ xi + f(i,wf(i,w-wi) ; f(i f(i-1,w) ]

Knapsack Problem
COMPLETED TABLE
1
1 2 3 0 0 0

2
0 0 0

3
5 5 5

4
5 7 7

5
5 7 8

10

10 10 10 15 15 10 12 14 15 17 10 12 14 15 17

Knapsack Problem
Path
1 1 2 3 0 0 0
Item 1

2 0 0 0

3 5 5 5

4 5 7 7

5 5 7 8
Item 1

10

10 10 10 15 15 10 12 14 15 17 10 12 14 15 17
Item 2

Optimal: 2 x Item 1 + 1 x Item 2

You might also like