1.204 Lecture 15
Dynamic programming:
Knapsack
Knapsack
When multistage graphs don’t work
•
If the resource has many levels:
–
Larg
e rang
e of ints
–
Floating point number
•
Then the multistage graph can’t be constructed
–
And label correction is not a sufficient implementation for pruning
•
We need a set representation instead
–
Different than our Set data structure, alas
•
We keep all the elements in the solution at any stage in a
set
–
W
d
i
t
d
l
We purge dominated elements
–
In a knapsack problem, for example, we purge any element
whose weight is same or higher and its profit is same or lower
than another element
–
This is how we implement pruning
•
We still need to structure the problem so that feasibility
constraints keep the size of the sets low
1

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
2
Knapsack problem
•
Problem is modeled as a series of decisions on
Problem is modeled as a series of decisions on
whether to include item 1, item 2, item 3, …
–
Each item has a profit (benefit) and a weight (cost)
–
The knapsack has a maximum weight (cost)
–
Each project is either in or out of the knapsack
•
No fractional values allowed, as were in the greedy version
•
Algorithm
Forward pass: builds sets instead of graph
–
•
Sets contain cumulative
(profit, weight) pairs
–
Backward pass: traces sets back from sink to source to
recover solution
–
Algorithm can produce solution for all
weights less than
or equal to maximum weight in a single run
First example
Item
Profit
Weight
•
Maximum weight= 9
0
0
0
1
1
2
2
2
3
3
5
4
•
Item 0 is a sentinel with 0 weight, 0 profit always

Forward pass: build sets
•
S(0)= (0,0)
S holds cumulative profit, weight
(1,2)
S’ is set of items to merge
•
S’= (1,2)
S is set of items to merge
•
S(1)= (0,0) (1,2)
S(n) is merged S(n-1) and S’
•
S’= (2,3) (3,5)
•
S(2)= (0,0) (1,2) (2,3) (3,5)
•
S’= (5,4) (6,6) (7,7) (8,9)
•
S(3)= (0,0) (1,2) (2,3) (5,4) (6,6) (7,7) (8,9)
–
Note that (3,5) is purged when S(3) is constructed
–
It is dominated by (5,4): higher profit, lower weight
•
If maximum weight were 7, (8,9) pair would not be built
–
Infeasibility
Backward pass: get solution
•
S(0)= (0,0)
Item
•
S(1)= (0,0) (1,2)
(1,2)
•
S(2)= (0,0) (1,2) (2,3) (3,5)
(2,3)
•
S(3)= (0,0) (1,2) (2,3) (5,4) (6,6) (7,7) (8,9)
(5,4)
•
Maximum weight:
4 5
6
7
8
9
6 7 8 9
Maximum weight:
4
–
Last pair is optimal (profit, weight) for entire problem
•
If pair exists in previous set, item not in solution
•
If pair not in previous set, item is in solution
–
Subtract item profit, weight and find that pair in previous set
–
Continue to trace back to source node
3

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*