+ All Categories
Home > Documents > CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington...

CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington...

Date post: 15-Dec-2015
Category:
Upload: audrey-offord
View: 219 times
Download: 2 times
Share this document with a friend
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 taken the following prerequisite. Sometimes enthusiasm alone is not enough. CSci 1311: Discrete Structures I (3) CSci 1112: Algorithms and Data Structures (3)
Transcript
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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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


Recommended