Escolar Documentos
Profissional Documentos
Cultura Documentos
Goal
Output a set of items S such that
the sum of weights of items in S is at most K
and the sum of values of items in S is maximized
Example
The thief breaks into a gold refinery; he can steal from a
selection of raw gold nuggets, each of the same value per
pound. If he can carry 50 pounds, what selection would
maximize the amount he carries out?
47.3 pounds
36.7 pounds
25.5 pounds
16.7 pounds
8.8 pounds
6.0 pounds
5.6 pounds
5.6 pounds
5.4 pounds
5.3 pounds
5.2 pounds
5.2 pounds
5.0 pounds
3.2 pounds
0.25 pounds
An Easier Version...
What if all of the sizes we are working with are relatively
small integers? For example, if we could fit 10 pounds
and:
Object A is 2 pounds and worth $40
Object B is 3 pounds and worth $50
Object C is 1 pound and worth $100
Object D is 5 pounds and worth $95
Object E is 3 pounds and worth $30
We can use dynamic programming!
Definining subproblems
Define P(i,w) to be the problem of choosing
a set of objects from the first i objects that
maximizes value subject to weight
constraint of w.
V(i,w) is the value of this set of items
Original problem corresponds to V(n, K)
Recurrence Relation
V(i,w) = max (V(i-1,w-wi) + vi, V(i-1, w))
A maximal solution for P(i,w) either
uses item i (first term in max)
or does NOT use item i (second term in max)
The solution...
Items
wA = 2 vA = $40
wC = 1 vC = $100
wD = 5 vD = $95
wE = 3 vE = $30
Weight
wB = 3 vB = $50
1
2
3
4
5
6
7
8
9
10
A
$0
$40
$40
$40
$40
$40
$40
$40
$40
$40
B
$0
$40
$50
$50
$90
$90
$90
$90
$90
$90
C
$100
$100
$140
$150
$150
$190
$190
$190
$190
$190
D
$100
$100
$140
$150
$150
$195
$195
$235
$245
$245
E
$100
$100
$140
$150
$150
$195
$195
$235
$245
$245
Define P(i,k) to be