1 / 25

Dynamic Programming

Dynamic Programming. Dynamic Programming. Like divide-and-conquer algorithms: Combines solutions to subproblems Dynamic programming is useful when subproblems share subproblems Not necessary to recompute Save the answer in a table

Télécharger la présentation

Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Programming

  2. Dynamic Programming • Like divide-and-conquer algorithms: • Combines solutions to subproblems • Dynamic programming is useful when subproblems share subproblems • Not necessary to recompute • Save the answer in a table • Dynamic programming applies to optimization problems (finding min or max)

  3. Developement of DP • Characterize the structure of an optimal solution • Recursively define the value of an optimal solution • Compute the value of an optimal solution (bottom-up) • Construct an optimal solution from computed information (may be omitted)

  4. The Assembly Line • Imagine two assembly lines, with n stations • The ith station performs the same thing, but in different times (denoted ai,j) • There is a time to transfer from one line to another costs ti,j • The problem: determine the fastest way through the factory • How many possibilities are there?

  5. Example: Assembly Line Station S1,1 Station S1,2 Station S1,3 Station S1,n-1 Station S1,n a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 chassis enters completed auto exits t2,1 t2,2 t2,n-1 e2 x2 a2,1 a2,2 a3,3 a2,n-1 a2,n Station S2,1 Station S2,2 Station S2,3 Station S2,n-1 Station S2,n

  6. Step 1: the structure • Because there are  (2n) possibilities, this won’t work. • Consider the fastest possible way through station S1, j • If j = 1, then there is only one way • If j > 1, then there are two choices: • came from station S1, j -1 • came from station S2, j -1, incurring cost t2, j -1

  7. Key Observation • The chassis must have taken the fastest way from the start to station S1, j -1 • Why? If there were a faster way, we could substitute it to yield a faster way: contradiction! • In other words, for it to be the fastest way, the subproblem had to be optimal. • Optimal substructure: a hallmark of dynamic programming

  8. Thus... • The fastest way is either • the fastest way through station S1,j-1 and directly to S1, j • the fastest way through station S2,j-1 with a transfer, and then through S1, j • To find the fastest way through station j, we solve the subproblems

  9. Step 2: A recursive solution • Let fi[j] denote the fastest time from the start to station Si,j • Fastest way through the factory f* is therefore min(f1[n]+x1, f2[n]+x2) • f1[j] = min(f1[j-1]+a1,j, f2[j-1]+t2,j-1+a1,j) • f2[j] = min(f2[j-1]+a2,j, f1[j-1]+t1,j-1+a2,j)

  10. Step 3: Computing the fastest time • Top down yields a 2n solution. • As a note f1[1] is calculated 2n-1 times! • By computing the fi[j] values in increasing order j, time is reduced to (n)

  11. Station S1,1 Station S1,2 Station S1,3 Station S1,4 Station S1,5 Station S1,6 7 9 3 8 4 4 2 3 2 3 4 3 1 chassis enters completed auto exits 2 1 1 2 2 4 2 8 5 6 5 7 4 Station S2,1 Station S2,2 Station S2,3 Station S2,4 Station S2,5 Station S2,6 f1[ j ] 9 35 18 20 24 32 f * = 38 f2[ j ] 12 37 16 22 25 30

  12. Matrix-Chain Multiplication • Given a sequence of matrices, find the best way to parenthesize them. • For example: • (A1(A2(A3A4))) • (A1((A2A3)A4)) • ((A1A2)(A3A4)) • (A1(A2A3)A4) • (((A1A2)A3)A4) • There are actually (2n) different parenthesizations!

  13. Why This is Important • The way that we parenthesize them dramatically affects the amount of work to do: • For Example: • A1=10100, A2=1005, A3=550 • ((A1A2)A3) • 101005 = 5000 (for A1A2) leaving a 105 plus • 10550 = 2500 = 7500 total ops • (A1(A2A3)) • 100550 = 25,000 (for A2A3) leaving a 10050 plus • 1010050 = 50,000 = 75,000 total ops

  14. Notation • Let Ai..j denote the matrix results for AiAi+1...Aj • Must pick a split point i<k<j • for some k, we first compute Ai..k and Ak+1...j, and then multiply them together • How do you pick k? • Note the optimal substructure: • Ai..k must be optimal • If there was a less-costly way, we could cut-n-paste!

  15. Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4

  16. Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 15,750 2,625 750 1,000 5,000 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4

  17. Building from the Ground Up 1 6 2 5 3 4 3 4 ? 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 Note: Can be either (A1(A2A3)) or ((A1A2)A3) 15,750 + (30155) = 18,000 vs2,625 + (30355) = 7,875

  18. Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4

  19. One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4

  20. One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs

  21. One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs

  22. One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs 4,375 + 0 + (351020)

  23. One Last Example 1 6 2 5 3 4 3 4 7,125 35x20 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs 4,375 + 0 + (351020) =7,125

  24. Brain Teaser • Using this tabular method, can you: • create a dynamic merge-sort? • create a dynamic twelve days of Christmas? • *find the total number of ways to make change for n cents?

  25. In Summary • Dynamic Programming exhibits optimal substructure • Overlapping Subproblems • Tables for holding intermediate results

More Related