Date post: | 15-Dec-2015 |
Category: |
Documents |
Upload: | audrey-offord |
View: | 219 times |
Download: | 2 times |
CSCI 6212 Design and Analysis of Algorithms
Dynamic Programming
Dr. Juman ByunThe George Washington University
Please drop this course if you have not taken the following prerequisite.
Sometimes enthusiasm alone is not enough.
• CSci 1311: Discrete Structures I (3)• CSci 1112: Algorithms and Data Structures
(3)
Example: Rod Cuttingn=4
Example: Rod Cuttinglength i
price pi
1 $1
2 $5
3 $8
4$9
$105 $10
6 $17
7 $17
8 $20
9 $24
10 $30
Maximum Revenue, r4 ?
n=4
rn when n=4 ?i p
[i]
1 $1
2 $5
3 $8
4
$9
$10
5 $10
6 $17
7 $17
8 $20
9 $24
10
$30
$9
$1
$8
$5
$5
$8
$1
$1
$1
$5
$1
$5
$1
$5
$1
$1
$1
$1
$1
$1
$10$10
Notation$5
$5
$10$10
rod into 2 pieces
4-inch
Decomposition:
4 = 2 + 2r4 = $5 + $5r4 = $5 + $5
Maximum Revenue:
Notation
rnrn
rod into k pieces
n-inch
Decomposition:
n = i1 + i2 + … + ik
Maximum Revenue:
General Procedure to Find Optimal Rod Cutting
Uncut Rod of length n
pn
r1 + rn-1
r2 + rn-2
rn-2 + r2
rn-1 + r1
Cut
Revenue
Pick the largest
General Procedure to Find Optimal Rod Cutting
General Procedure to Find Optimal Rod Cutting
Recursive Top-DownCut-Rod(p,n)1. if n == 02. return 03. q = -∞4. for i = 1 to n5. q = max(q,p[i] + Cut-Rod(p, n - i ) )6. return q
vs Divide-and-conquer
Similarity
to divide problems into subproblems
Difference
subproblems overlap
Can we do better ?
Momoized-Cut-RodMemoized-Cut-Rod(p,n)1.let r[0..n] be a new array2.for i = 0 to n3. r[i] = -∞4.return Memoized-Cut-Rod-Aux(p,n,r)
Momoized-Cut-Rod-Aux
Momoized-Cut-Rod-Aux(p,n,r)1. if r[n] >= 02. return r[n]3. if n == 04. q = 05. else q = -∞6. for i = 1 to n7. q = max(q,p[i]+Memoized-Cut-Rod-Aux(p,n-
i,r))• r[n] = q1. return q
Bottom-Up-Cut-RodBottom-Up-Cut-Rod(p,n)1. let r[0..n] be a new array2. r[0] = 03. for j = 1 to n4. q = -∞5. for i = 1 to j6. q = max(q, p[i] + r[j-i])7. r[j] = q• return r[n]
Running Time
Extended-Bottom-Up-Cut-Rod
Extended-Bottom-Up-Cut-Rod(p,n)1. let r[0..n] and s[0..n] be new arrays2. r[0] = 03. for j = 1 to n4. q = -∞5. for i = 1 to j6. if q < p[i] + r[j-i]7. q = p[i] + r[j-i]8. s[j] = i9. r[j] = q10.return r and s