1 / 56

Dynamic Programming

Dynamic Programming. Dynamic Programming. Dividing a problem into subproblems Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped - Divide and conquer : subproblems are independent Used for finding optimized solutions.

dalit
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

  2. Dynamic Programming • Dividing a problem into subproblems • Dynamic programming vs divide and conquer - Dynamic programming : subproblems are overlapped - Divide and conquer : subproblems are independent • Used for finding optimized solutions

  3. Assembly-Line Scheduling station 3 station 1 station 2 station 4 station 5 station 6 n = 6 Assembly Line 1 7 9 3 4 8 4 enters exits 8 5 6 4 5 7 Assembly Line 2

  4. Assembly-Line Scheduling Assembly Line 1 7 9 3 4 8 4 3 2 enters exits 4 2 8 5 6 4 5 7 Assembly Line 2

  5. Assembly-Line Scheduling Assembly Line 1 7 9 3 4 8 4 3 2 2 3 1 3 4 enters exits 2 1 2 2 1 4 2 8 5 6 4 5 7 Assembly Line 2

  6. Assembly-Line Scheduling Assembly Line 1 7 9 3 4 8 4 40 3 2 2 3 1 3 4 enters exits 2 1 2 2 1 4 2 41 8 5 6 4 5 7 Assembly Line 2

  7. Assembly-Line Scheduling Assembly Line 1 7 9 3 4 8 4 39 3 2 2 3 1 3 4 enters exits 2 1 2 2 1 4 2 8 5 6 4 5 7 Assembly Line 2

  8. Assembly-Line Scheduling station 3 number of cases = 2n station 1 station 2 station 4 station 5 station 6 Assembly Line 1 7 9 3 4 8 4 enters exits 8 5 6 4 5 7 Assembly Line 2

  9. Assembly-Line Scheduling T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 Assembly Line 1 T enters exits Assembly Line 2 T2,1 T2,2 T2,3 T2,4 T2,5 T2,6

  10. Assembly-Line Scheduling T1,5 T1,6 T = min( T1,6 + 3, T2,6 + 2) 8 4 Divide and Conquer? 3 T 4 T1,6 = min( T1,5 + 4, T2,5 + 1 + 4) exits 1 2 T2,6 = min( T1,5 + 4 + 7, T2,5 + 7) 5 7 T2,5 T2,6

  11. Assembly-Line Scheduling T1,5 T1,6 T = min( T1,6 + 3, T2,6 + 2) 8 4 Divide and Conquer? 3 T 4 T1,6 = min( T1,5 + 4, T2,5 + 1 + 4) exits 1 2 T2,6 = min( T1,5 + 4 + 7, T2,5 + 7) 5 7 T2,5 T2,6

  12. Assembly-Line Scheduling 9 T1,1 T1,2 T1,3 Assembly Line 1 T1,1 = 9 7 9 3 T2,1 = 12 2 2 3 enters 2 1 4 8 5 6 Assembly Line 2 T2,1 T2,2 T2,3 12

  13. Assembly-Line Scheduling 9 18 T1,1 T1,2 T1,3 Assembly Line 1 T1,1 = 9 7 9 3 T2,1 = 12 2 2 3 T1,2 = min(T1,1 + 9, T2,1 + 2 + 9) enters 2 1 4 8 5 6 Assembly Line 2 T2,1 T2,2 T2,3 12

  14. Assembly-Line Scheduling 9 18 T1,1 T1,2 T1,3 Assembly Line 1 T1,1 = 9 7 9 3 T2,1 = 12 2 T1,2 = 18 2 3 enters T2,2 = min(T1,1 + 2 + 5, T2,1 + 5) 2 1 4 8 5 6 Assembly Line 2 T2,1 T2,2 T2,3 12 16

  15. Assembly-Line Scheduling 9 20 24 35 32 18 T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 Assembly Line 1 7 9 3 4 8 4 38 3 2 T 2 3 1 3 4 enters exits 2 1 2 2 1 4 2 8 5 6 4 5 7 Assembly Line 2 T2,1 T2,2 T2,3 T2,4 T2,5 T2,6 22 25 37 30 12 16

  16. Assembly-Line Scheduling 35 32 T1,5 T1,6 T = T1,6 + 3 ? or T2,6 + 2 ? 8 4 38 3 T1,6 + 3 = T T 4 T2,6 + 2 > T exits 1 2 5 7 T2,5 T2,6 30 37

  17. Assembly-Line Scheduling 35 32 T1,5 T1,6 T = T1,6 + 3 ? or T2,6 + 2 ? 8 4 38 3 T1,6 + 3 = T T 4 T2,6 + 2 > T exits 1 2 5 7 T2,5 T2,6 30 37

  18. Assembly-Line Scheduling 35 32 T1,5 T1,6 T1,6 = T1,5 + 4 ? or T2,5 + 1 + 4 ? 8 4 38 3 T 4 T1,5 + 4 > T1,6 exits T2,5 + 1 + 4 = T1,6 1 2 5 7 T2,5 T2,6 30 37

  19. Assembly-Line Scheduling 35 32 T1,5 T1,6 T1,6 = T1,5 + 4 ? or T2,5 + 1 + 4 ? 8 4 38 3 T 4 T1,5 + 4 > T1,6 exits T2,5 + 1 + 4 = T1,6 1 2 5 7 T2,5 T2,6 30 37

  20. Assembly-Line Scheduling 9 20 24 35 32 18 T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 Assembly Line 1 7 9 3 4 8 4 38 3 2 T 2 3 1 3 4 enters exits 2 1 2 2 1 4 2 8 5 6 4 5 7 Assembly Line 2 T2,1 T2,2 T2,3 T2,4 T2,5 T2,6 22 25 37 30 12 16

  21. Assembly-Line Scheduling T1,5 T1,6 1. Analyze the problem T exits T = min( T1,6 + 3, T2,6 + 2) T2,5 T2,6

  22. Assembly-Line Scheduling T1,5 T1,6 1. Analyze the problem 2. Find a recursive solution. T exits T = min( T1,6 + 3, T2,6 + 2) T1,6 = min( T1,5 + 4, T2,5 + 1 + 4) T2,6 = min( T1,5 + 4 + 7, T2,5 + 7) T2,5 T2,6

  23. Assembly-Line Scheduling 9 20 18 24 32 35 1. Analyze the problem. T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 2. Find a recursive solution. T 3. Compute the fastest time. 38 T2,1 T2,2 T2,3 T2,4 T2,5 T2,6 22 25 30 37 12 16

  24. Assembly-Line Scheduling 9 20 18 24 32 35 1. Analyze the problem. T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 2. Find a recursive solution. T 3. Compute the fastest time. 38 4. Construct the fastest way. T2,1 T2,2 T2,3 T2,4 T2,5 T2,6 22 25 30 37 12 16

  25. Assembly-Line Scheduling 9 20 18 24 32 35 1. Analyze the problem. T1,1 T1,2 T1,3 T1,4 T1,5 T1,6 2. Find a recursive solution. T 3. Compute the fastest time. 38 4. Construct the fastest way. This algorithm takes time. T2,1 T2,2 T2,3 T2,4 T2,5 T2,6 22 25 30 37 12 16 Brute force :

  26. When can we use Dynamic Programming? • Optimal substructures • Overlapping subproblems

  27. Overlapping Subprograms T1,5 T1,6 T 8 4 3 T1,6 T2,6 T 4 exits T1,5 T2,5 T1,5 T2,5 1 2 5 7 T1,4 T2,4 T1,4 T2,4 T1,4 T2,4 T1,4 T2,4 T2,5 T2,6

  28. Overlapping Subprograms T1,5 T1,6 T 8 4 3 T1,6 T2,6 T 4 exits T1,5 T2,5 T1,5 T2,5 1 2 5 7 T1,4 T2,4 T1,4 T2,4 T1,4 T2,4 T1,4 T2,4 T2,5 T2,6

  29. Optimal Substructures • Problems should be divided into subproblems. • Optimal solution : from optimal subproblem solutions One part of the optimal solution to the problem should be an optimal solution to the subproblem solutions to the subproblems should be independent

  30. Optimal Substructures 2. Longest Simple Path Problem 1. Shortest Path Problem d d s s

  31. Optimal Substructures 2. Longest Simple Path Problem 1. Shortest Path Problem d d s s w w

  32. Memoization • Ordinary - Bottom-up strategy • Memoization - Top-down strategy

  33. Memoization Memoization Bottom Up

  34. Memoization T T1,6 T2,6 T1,5 T2,5 Memoization T1,4 T2,4 … …

  35. Memoization T1,3 T1,2 T2,2 Memoization T1,1 T2,1

  36. Memoization T1,3 T1,2 T2,2 Memoization T1,1 T2,1

  37. Memoization T1,3 T1,2 T2,2 Memoization T1,1 T2,1 T1,1 T2,1

  38. Memoization T1,4 T1,3 T2,3 Memoization T1,2 T2,2 T1,2 T2,2 T1,1 T2,1 T1,1 T2,1

  39. Memoization T T1,6 T2,6 T1,5 T2,5 Memoization T1,4 T2,4 T1,4 T2,4 … … T1,3 T2,3

  40. Memoization T T1,6 T2,6 T1,5 T2,5 Memoization T1,4 T2,4 T1,4 T2,4 … … … …

  41. Memoization T T1,6 T2,6 T1,5 T2,5 T1,5 T2,5 Memoization T1,4 T2,4 T1,4 T2,4 … … … …

  42. Memoization T = 38 T1,6 T2,6 T1,5 T2,5 T1,5 T2,5 Memoization T1,4 T2,4 T1,4 T2,4 … … … …

  43. Why Memoization? • Ordinary Dynamic Programming (Bottom-Up strategy) - when all subproblems need to be solved • Memoization - when some subproblems do not need to be solved

  44. Longest Common Subsequence • DNA sequences : composed of four components – {A, C, G, T} • How similar are they? S1 = ACCGGTCGTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA

  45. Longest Common Subsequence S1 = ACCGGTCGTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA Longest Common Sequence : GTCGTCGGAAGCCGGCCGAA

  46. Longest Common Subsequence X = <x1, x2, …, xm>, Y = <y1, y2, …, yn> Xi = <x1, x2, …, xi> for i = 1 to m Yj = <y1, y2, …, yj> for j = 1 to n Zi,j = <z1, z2, …, zk> for i = 1 to m, j = 1 to n Longest common subsequence of Xi and Yj

  47. Longest Common Subsequence 1. Analyze the problem. X = <x1, x2, …, xm>, Y = <y1, y2, …, yn> Xi = <x1, x2, …, xi> Yj = <y1, y2, …, yj> Zi,j = <z1, z2, …, zk> (Longest common subsequence) Zi-1,j = <z1, z2, …, zk-1> or <z1, z2, …, zk> when xi used as zk otherwise Zi,j-1 = <z1, z2, …, zk> or <z1, z2, …, zk-1>

  48. Longest Common Subsequence 1. Analyze the problem. X = <x1, x2, …, xm>, Y = <y1, y2, …, yn> Xi = <x1, x2, …, xi> Yj = <y1, y2, …, yj> Zi,j = <z1, z2, …, zk> (Longest common subsequence) Zi-1,j = <z1, z2, …, zk-1> or <z1, z2, …, zk> when xi used as zk If xi = yj then zk = xi or zk = yj / / / otherwise

  49. Longest Common Subsequence 1. Analyze the problem. X = <x1, x2, …, xm>, Y = <y1, y2, …, yn> Xi = <x1, x2, …, xi> Yj = <y1, y2, …, yj> Zi,j = <z1, z2, …, zk> (Longest common subsequence) If xi = yj then zk = xi or zk = yj / / / Zi,j = Zi-1,j or Zi,j-1

  50. Longest Common Subsequence 1. Analyze the problem. X = <x1, x2, …, xm>, Y = <y1, y2, …, yn> Xi = <x1, x2, …, xi> Yj = <y1, y2, …, yj> Zi,j = <z1, z2, …, zk> (Longest common subsequence) If xi = yj then / Zi,j = Zi-1,j or Zi,j-1 Optimal If xi = yj then If xi = yj is not used as zk then Zi,j = Zi-1,j or Zi,j-1 If xi = yj is used as zk then Zi,j = Zi-1,j-1 + zk

More Related