![Page 1: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/1.jpg)
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)
![Page 2: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/2.jpg)
Example: Rod Cuttingn=4
![Page 3: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/3.jpg)
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
![Page 4: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/4.jpg)
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
![Page 5: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/5.jpg)
Notation$5
$5
$10$10
rod into 2 pieces
4-inch
Decomposition:
4 = 2 + 2r4 = $5 + $5r4 = $5 + $5
Maximum Revenue:
![Page 6: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/6.jpg)
Notation
rnrn
rod into k pieces
n-inch
Decomposition:
n = i1 + i2 + … + ik
Maximum Revenue:
![Page 7: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/7.jpg)
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
![Page 8: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/8.jpg)
General Procedure to Find Optimal Rod Cutting
![Page 9: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/9.jpg)
General Procedure to Find Optimal Rod Cutting
![Page 10: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/10.jpg)
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
![Page 11: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/11.jpg)
vs Divide-and-conquer
Similarity
to divide problems into subproblems
Difference
subproblems overlap
![Page 12: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/12.jpg)
Can we do better ?
![Page 13: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/13.jpg)
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)
![Page 14: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/14.jpg)
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
![Page 15: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/15.jpg)
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]
![Page 16: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/16.jpg)
Running Time
![Page 17: CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cae5503460f9497107a/html5/thumbnails/17.jpg)
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