1 / 42

Dynamic Programming

Dynamic Programming. Dynamic Programming. Usually involves optimization problems Optimal solution exists Sub-problems arise more than once Could use brute force, but… Main idea: If you’ve already solved the sub-problem, leave yourself a note!. To Get You Thinking…. Fibonacci.

fionn
Download Presentation

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 Jeff Chastine

  2. Dynamic Programming • Usually involves optimization problems • Optimal solution exists • Sub-problems arise more than once • Could use brute force, but… • Main idea: If you’ve already solved the sub-problem, leave yourself a note! Jeff Chastine

  3. To Get You Thinking… • Fibonacci How would you calculate the 100th Fibonacci number? Fib (100) = Fib(99) + Fib(98) Fib (99) = Fib (98) + Fib (97) Jeff Chastine

  4. Assembly Line Scheduling line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 Jeff Chastine

  5. Assembly Line Scheduling line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 Entry Time – time to get the chassis on the line Jeff Chastine

  6. Assembly Line Scheduling line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 Station Time – time in each station. Note a1,x != a2,x Jeff Chastine

  7. Assembly Line Scheduling line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 Transfer Time – time to move to the other assembly line Jeff Chastine

  8. Assembly Line Scheduling line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 Exit Time – time to get the chassis off the line Jeff Chastine

  9. What’s the fastest way through? line 1 a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 … finishedcar chassis t2,1 t2,2 t2,n-1 e1 x2 a2,1 a2,2 a2,3 a2,n-1 a2,n line 2 We have a rush order! Jeff Chastine

  10. What about Brute Force? • Try all paths • How many? 2n • This is the lower bound! • Example: 100 stations? • You’ll be dead before it finishes calculating • Good gift for your grandchildren Jeff Chastine

  11. Giving the problem structure! • There’s only 1 path to the first station • There are 2 paths to the subsequent stations • For station S1, j: • S1, j-1 OR • S2, j-1 with transfer time t2, j-1 Jeff Chastine

  12. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 Jeff Chastine

  13. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 f1[j] f1[2] = min ((9+9), (12+2+9)) = 18 from line 1 f2[j] f2[2] = min ((12+5), (9+2+5)) = 16 from line 1 Jeff Chastine

  14. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 l1[j] f1[j] l2[j] f2[j] Jeff Chastine

  15. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 f1[j] f1[3] = min ((18+3), (16+1+3)) = 20 from line 2 f2[j] f2[3] = min ((16+6), (18+3+6)) = 22 from line 2 Jeff Chastine

  16. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 l1[j] f1[j] l2[j] f2[j] Jeff Chastine

  17. What’s the fastest way through? S1,1 S1,2 S1,3 S1,4 S1,5 S1,6 line 1 4 8 4 7 3 9 3 2 1 3 2 3 3 4 finishedcar chassis 1 2 2 1 2 2 4 8 6 5 4 5 7 line 2 S2,1 S2,2 S2,3 S2,4 S2,5 S2,6 l1[j] f1[j] f*=1 f*=38 l2[j] f2[j] Jeff Chastine

  18. Matrix Multiplication • [p×q][q×r] →[p×r] for pqr scalar operations • Most efficient way to multiply?<A1, A2, A3, A4>(A1, (A2, (A3, A4)))(A1, ((A2, A3), A4))((A1, A2), (A3, A4))((A1, (A2, A3)), A4)(((A1, A2), A3), A4) Example:[10×100][100×5][5×50](([10×100][100×5])[5×50]) // 5000 (([10×5])[5×50]) // 2500([10×100]([100×5][5×50])) // 25000 ([10×100]([100×50])) // 50000 Jeff Chastine

  19. How many Parenthesizations?(say that 5 times fast) For any sequence of matrices, we can split at k M1M2M3M4 … MkMk+1Mk+2 …Mn Jeff Chastine

  20. Optimal Substructure For any sequence of matrices, we can split at k M1M2M3M4 … MkMk+1Mk+2 …Mn M1M2…Mk must be optimal Mk+1Mk+2…Mnmust be optimal as well // Note the recursion! Jeff Chastine

  21. Recursive Solution Let m[i, j] be the min # of multiplications m[1, n] is cheapest way to compute solution Then, m[i, j] = m[i, k] + m[k+1, j] + pi-1 pkpj Cost to multiply matrices together Big problem. We don’t know k! Jeff Chastine

  22. Recursive Solution Let m[i, j] be the min # of multiplications m[1, n] is cheapest way to compute solution Then, m[i, j] = min {m[i, k] + m[k+1, j] + pi-1 pkpj} Jeff Chastine

  23. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 3 4 ? 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  24. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 3 4 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  25. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 ? 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  26. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 ? 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  27. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 ? 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  28. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 5 2 4 3 7125 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  29. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  30. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  31. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  32. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  33. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  34. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 ? 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  35. 6 1 Matrix M1 M2M3 M4M5M6 Dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25 15125(30x25) 5 2 11875(30x20) 10500(35x25) 4 3 9375(30x10) 7125(35x20) 5375(15x25) 3 4 2500(15x20) 3500(5x25) 4375(35x10) 7875(30x5) 2 5 2,625(35x5) 750(15x10) 1,000(5x20) 5,000(10x25) 15,750(30x15) 6 1 0 0 0 0 0 0 M1 M4 M2 M3 M5 M6 Jeff Chastine

  36. Summary • Elements of Dynamic Programming • Bottom-up approach • Optimal substructure • Fastest way through station j contained fastest way through station (j-1) • Determining correct split contained optimal solutions to subproblems • Overlapping subproblems • Doesn’t solve the same subproblems over and over Jeff Chastine

  37. A Note about Memoization • Top-down approach • Leave a note about solutions to sub-problems • Example: Fibonacci numbers 1, 1, 2, 3, 5, 8, 13… Calculate the nth Fibonacci number Jeff Chastine

  38. Why this could be bad F(10) + F(8) F(9) Jeff Chastine

  39. Why this could be bad F(10) F(8) F(9) F(6) F(7) F(7) F(8) Jeff Chastine

  40. Why this could be bad F(10) height = ~10 F(8) F(9) F(6) F(7) F(7) F(8) F(4) F(5) F(5) F(6) F(5) F(6) F(6) F(7) Ballpark: 210 nodes! Jeff Chastine

  41. What to notice F(10) F(8) F(9) F(6) F(7) F(7) F(8) F(4) F(5) F(5) F(6) F(5) F(6) F(6) F(7) Look at the redundancy! Jeff Chastine

  42. Memoization • Leave a note • Once you solve a problem • Write it in a table • Instead of calculating/branching again, look it up! Jeff Chastine

More Related