Transcript Lecture 12

The Knapsack Problem
• Input
– Capacity K
– n items with weights wi and values vi
• Goal
– Output a set of items S such that
• the sum of weights of items in S is at most K
• the sum of values of items in S is maximized
Variations
• Fractional
– You may take a fraction of the entire item
• All or nothing
– You must take all of an item or none of it
• Special cases of All or nothing
– All items have same weight (or same value)
– All items have same density (value/weight)
• For which of the above variations does a greedy strategy
work?
• For which of the above variations does a greedy strategy
NOT work?
Dynamic Programming
• Consider the all or nothing version
• Assume that all weights/values are
reasonably sized integers
• We can use dynamic programming to solve
this problem (even though there is no
explicit ordering among the items)
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.
– Impose an arbitrary ordering on the items
• V(i,w) is the value of this set of items
• Original problem corresponds to V(n, K)
Recurrence Relation/Running Time
• 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)
• V(0,w) = 0 (no items to choose from)
• V(i,0) = 0 (no weight allowed)
• What is the running time of this solution?
– Number of table entries:
– Time to fill each entry:
Example
Items
wA = 2 vA = $40
wB = 3 vB = $50
wC = 1 vC = $100
wD = 5 vD = $95
wE = 3 vE = $30
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 Subproblems Based on Value
• 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, W)
• Define P(i,k) to be …