Professional Documents
Culture Documents
Optimization: Dynamic Programming
Optimization: Dynamic Programming
Optimization: Dynamic Programming
Dynamic Programming
Dynamic Programming Intro
2
Example: Interval Scheduling
3
Interval Scheduling
4
Example: Interval Scheduling
Notation. Label jobs by finishing time: f1 £ f2 £ . . . £ fn .
5
Example: Interval Scheduling
Notation. OPT(j) = value of optimal solution to the problem consisting of job
requests 1, 2, ..., j.
# 0 if j = 0
OPT( j) = $
%max { v j + OPT( p( j)), OPT( j −1) } otherwise
6
€
Brute Force Approach
7
Brute Force Approach
8
Memoization
9
Dynamic Programming Overview
10
States
11
Decisions
12
Optimal Policy and Principal of Optimality
15
Example: Coins
• To make K cents:
– If there is a K-cent coin, then that one coin is the minimum
– Otherwise, for each value i < K,
• Find the minimum number of coins needed to make
i cents
• Find the minimum number of coins needed to make
K - i cents
– Choose the i that minimizes this sum
• This algorithm can be viewed as brute force
– This solution is very recursive
– It requires exponential sub-problems
17
Example: Coins
• Use Memoization: Solve first for one cent, then two cents, then
three cents, etc., up to the desired amount
– Save each answer
19
In-Class Problem
21
Motivating Example
7 t
44
22
Breaking into SubProblems
23
Breaking into Subproblems
ì 0 if v = t , ¥ otherwise if i = 0
ï
OPT (i,v) = í ì ü
miníOPT (i - 1,v), min {OPT (i - 1,w) + cvw }ý otherwise
ïî î ( v ,w )ÎE þ
24
Knapsack Problem
26
Define Recusviely
• OPT(i, W’)
27
Define Recusviely
28
Example Knapsack
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3: (4,5)
2 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3: (4,5)
2 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3: (4,5)
2 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3: (4,5)
2 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5 7
4 0
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5 7
4 0 0 3 4 5
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Done!
How do we find items?
Backtracking
Items
(weight,profit):
i/w 0 1 2 3 4 5
1: (2,3)
0 0 0 0 0 0 0
2: (3,4)
1 0 0 3 3 3 3 3: (4,5)
2 0 0 3 4 4 7 4: (5,6)
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Solving the Problem
Item # wi pi W=20
1 2 3
2 4 5
3 5 8
4 3 4
5 9 10 47
Solving the Problem
Item # wi pi W=20
1 2 3
2 4 5 Solution for first
3 5 8 four items not
4 3 4 subset of the
solution for all 5!
5 9 10 48
In-Class Problem
• You are driving from Pittsburgh to San Francisco. You fixed your
driving route. Along the way you have to stop for gas. There
are n gas stations x1, x2, …, xn. It takes costs ci to fill up at
station xi regardless of how much gas you need.
• Assume the stations are ordered according to the order you see
them on your route.
• Your car drives 300 miles before you need to gas and you start
at gas station x1.
• di is the distance between stations xi and xi+1
• Design a dynamic program to decide which stations to fill at to
minimize the cost.
49