19 dynamic programming i
play

19. Dynamic Programming I Memoization, Optimal Substructure, - PowerPoint PPT Presentation

19. Dynamic Programming I Memoization, Optimal Substructure, Overlapping Sub-Problems, Dependencies, General Procedure. Examples: Fibonacci, Rod Cutting, Longest Ascending Subsequence, Longest Common Subsequence, Edit Distance, Matrix Chain


  1. Algorithm RodCut( v , n ) Input: n ≥ 0 , Prices v Output: best value q ← 0 if n > 0 then for i ← 1 , . . . , n do q ← max { q, v i + RodCut ( v, n − i ) } ; return q Running time T ( n ) = � n − 1 ⇒ 39 T ( n ) ∈ Θ(2 n ) i =0 T ( i ) + c 39 T ( n ) = T ( n − 1) + � n − 2 i =0 T ( i ) + c = T ( n − 1) + ( T ( n − 1) − c ) + c = 2 T ( n − 1) ( n > 0) 566

  2. Recursion Tree 5 4 3 2 1 3 2 1 2 1 1 2 1 1 1 1 567

  3. Algorithm RodCutMemoized( m, v, n ) Input: n ≥ 0 , Prices v , Memoization Table m Output: best value q ← 0 if n > 0 then if ∃ m [ n ] then q ← m [ n ] else for i ← 1 , . . . , n do q ← max { q, v i + RodCutMemoized ( m, v, n − i ) } ; m [ n ] ← q return q Running time � n i =1 i = Θ( n 2 ) 568

  4. Subproblem-Graph Describes the mutual dependencies of the subproblems 4 3 2 1 0 and must not contain cycles 569

  5. Construction of the Optimal Cut During the (recursive) computation of the optimal solution for each k ≤ n the recursive algorithm determines the optimal length of the first rod Store the lenght of the first rod in a separate table of length n 570

  6. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 571

  7. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . 571

  8. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 571

  9. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 Value r 0 is 0 571

  10. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 Value r 0 is 0 What is the execution order such that required entries are always available? 3 . 571

  11. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 Value r 0 is 0 What is the execution order such that required entries are always available? 3 r i , i = 1 , . . . , n . 571

  12. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 Value r 0 is 0 What is the execution order such that required entries are always available? 3 r i , i = 1 , . . . , n . Wie kann sich Lösung aus der Tabelle konstruieren lassen? 4 571

  13. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 n × 1 table. n th entry contains the best value of a rod of length n . Which entries do not depend on other entries? 2 Value r 0 is 0 What is the execution order such that required entries are always available? 3 r i , i = 1 , . . . , n . Wie kann sich Lösung aus der Tabelle konstruieren lassen? 4 r n is the best value for the rod of length n . 571

  14. Rabbit! 0 2 0 1 , 1 2 , 1 3 , 1 4 , 1 A rabbit sits on cite (1 , 1) 4 0 3 3 of an n × n grid. It can 1 4 1 1 , 2 2 , 2 3 , 2 4 , 2 only move to east or south. On each pathway there is 3 1 2 4 a number of carrots. How 0 1 1 many carrots does the rab- 1 , 3 2 , 3 3 , 3 4 , 3 bit collect maximally? 0 3 1 3 2 0 2 1 , 4 2 , 4 3 , 4 4 , 4 572

  15. Rabbit! Number of possible paths? Choice of n − 1 ways to south out of 2 n − 2 ways overal. The path 100011 ⇒ No chance for a naive algorithm (1:to south, 0: to east) 573

  16. Rabbit! Number of possible paths? Choice of n − 1 ways to south out of 2 n − 2 ways overal. � 2 n − 2 � ∈ Ω(2 n ) n − 1 The path 100011 ⇒ No chance for a naive algorithm (1:to south, 0: to east) 573

  17. Recursion Wanted: T 0 , 0 = maximal number carrots from (0 , 0) to ( n, n ) . Let w ( i,j ) − ( i ′ ,j ′ ) number of carrots on egde from ( i, j ) to ( i ′ , j ′ ) . Recursion (maximal number of carrots from ( i, j ) to ( n, n )  max { w ( i,j ) − ( i,j +1) + T i,j +1 , w ( i,j ) − ( i +1 ,j ) + T i +1 ,j } , i < n, j < n    w ( i,j ) − ( i,j +1) + T i,j +1 , i = n, j < n  T ij = w ( i,j ) − ( i +1 ,j ) + T i +1 ,j , i < n, j = n    0 i = j = n  574

  18. Graph of Subproblem Dependencies (1 , 1) (2 , 1) (3 , 1) (4 , 1) (1 , 2) (2 , 2) (3 , 2) (4 , 2) (1 , 3) (2 , 3) (3 , 3) (4 , 3) (1 , 4) (2 , 4) (3 , 4) (4 , 4) 575

  19. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 576

  20. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . 576

  21. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 576

  22. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 Value T n,n is 0 576

  23. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 Value T n,n is 0 What is the execution order such that required entries are always available? 3 . 576

  24. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 Value T n,n is 0 What is the execution order such that required entries are always available? 3 T i,j with i = n ց 1 and for each i : j = n ց 1 , (or vice-versa: j = n ց 1 and for each j : i = n ց 1 ). 576

  25. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 Value T n,n is 0 What is the execution order such that required entries are always available? 3 T i,j with i = n ց 1 and for each i : j = n ց 1 , (or vice-versa: j = n ց 1 and for each j : i = n ց 1 ). Wie kann sich Lösung aus der Tabelle konstruieren lassen? 4 576

  26. Bottom-up Description with the example Dimension of the table? Semantics of the entries? 1 Table T with size n × n . Entry at i, j provides the maximal number of carrots from ( i, j ) to ( n, n ) . Which entries do not depend on other entries? 2 Value T n,n is 0 What is the execution order such that required entries are always available? 3 T i,j with i = n ց 1 and for each i : j = n ց 1 , (or vice-versa: j = n ց 1 and for each j : i = n ց 1 ). Wie kann sich Lösung aus der Tabelle konstruieren lassen? 4 T 1 , 1 provides the maximal number of carrots. 576

  27. Longest Ascending Sequence (LAS) 3 5 6 3 5 6 1 2 4 7 1 2 4 7 3 2 4 6 5 7 1 3 2 4 6 5 7 1 Connect as many as possible fitting ports without lines crossing. 577

  28. Longest Ascending Sequence (LAS) 3 5 6 3 5 6 1 2 4 7 1 2 4 7 3 2 4 6 5 7 1 3 2 4 6 5 7 1 Connect as many as possible fitting ports without lines crossing. 577

  29. Longest Ascending Sequence (LAS) 3 5 6 3 5 6 1 2 4 7 1 2 4 7 3 2 4 6 5 7 1 3 2 4 6 5 7 1 Connect as many as possible fitting ports without lines crossing. 577

  30. Longest Ascending Sequence (LAS) 3 5 6 3 5 6 1 2 4 7 1 2 4 7 3 2 4 6 5 7 1 3 2 4 6 5 7 1 Connect as many as possible fitting ports without lines crossing. 577

  31. Formally Consider Sequence A n = ( a 1 , . . . , a n ) . 1 2 3 4 5 6 7 Search for a longest increasing subsequence of A n . A Examples of increasing subsequences: 3 2 4 6 5 7 1 (3 , 4 , 5) , (2 , 4 , 5 , 7) , (3 , 4 , 5 , 7) , (3 , 7) . 578

  32. Formally Consider Sequence A n = ( a 1 , . . . , a n ) . 1 2 3 4 5 6 7 Search for a longest increasing subsequence of A n . A Examples of increasing subsequences: 3 2 4 6 5 7 1 (3 , 4 , 5) , (2 , 4 , 5 , 7) , (3 , 4 , 5 , 7) , (3 , 7) . Generalization: allow any numbers, even with duplicates (still only strictly increasing subsequences permitted). Example: (2 , 3 , 3 , 3 , 5 , 1) with increasing subsequence (2 , 3 , 5) . 578

  33. First idea Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: LAS L k of A k known for Now want to compute L k +1 for A k +1 . 579

  34. First idea Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: LAS L k of A k known for Now want to compute L k +1 for A k +1 . If a k +1 fits to L k , then L k +1 = L k ⊕ a k +1 ? 579

  35. First idea Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: LAS L k of A k known for Now want to compute L k +1 for A k +1 . If a k +1 fits to L k , then L k +1 = L k ⊕ a k +1 ? Counterexample A 5 = (1 , 2 , 5 , 3 , 4) . Let A 3 = (1 , 2 , 5) with L 3 = A . Determine L 4 from L 3 ? 579

  36. First idea Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: LAS L k of A k known for Now want to compute L k +1 for A k +1 . If a k +1 fits to L k , then L k +1 = L k ⊕ a k +1 ? Counterexample A 5 = (1 , 2 , 5 , 3 , 4) . Let A 3 = (1 , 2 , 5) with L 3 = A . Determine L 4 from L 3 ? It does not work this way, we cannot infer L k +1 from L k . 579

  37. Second idea. Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: a LAS L j is known for each j ≤ k . Now compute LAS L k +1 for k + 1 . 580

  38. Second idea. Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: a LAS L j is known for each j ≤ k . Now compute LAS L k +1 for k + 1 . Look at all fitting L k +1 = L j ⊕ a k +1 ( j ≤ k ) and choose a longest sequence. 580

  39. Second idea. Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: a LAS L j is known for each j ≤ k . Now compute LAS L k +1 for k + 1 . Look at all fitting L k +1 = L j ⊕ a k +1 ( j ≤ k ) and choose a longest sequence. Counterexample: A 5 = (1 , 2 , 5 , 3 , 4) . Let A 4 = (1 , 2 , 5 , 3) with L 1 = (1) , L 2 = (1 , 2) , L 3 = (1 , 2 , 5) , L 4 = (1 , 2 , 5) . Determine L 5 from L 1 , . . . , L 4 ? 580

  40. Second idea. Let L i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: a LAS L j is known for each j ≤ k . Now compute LAS L k +1 for k + 1 . Look at all fitting L k +1 = L j ⊕ a k +1 ( j ≤ k ) and choose a longest sequence. Counterexample: A 5 = (1 , 2 , 5 , 3 , 4) . Let A 4 = (1 , 2 , 5 , 3) with L 1 = (1) , L 2 = (1 , 2) , L 3 = (1 , 2 , 5) , L 4 = (1 , 2 , 5) . Determine L 5 from L 1 , . . . , L 4 ? That does not work either: cannot infer L k +1 from only an arbitrary solution L j . We need to consider all LAS. Too many. 580

  41. Third approach Let M n,i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: the LAS M j for A k , that end with smallest element are known for each of the lengths 1 ≤ j ≤ k . 581

  42. Third approach Let M n,i = longest ascending subsequence of A i (1 ≤ i ≤ n ) Assumption: the LAS M j for A k , that end with smallest element are known for each of the lengths 1 ≤ j ≤ k . Consider all fitting M k,j ⊕ a k +1 ( j ≤ k ) and update the table of the LAS,that end with smallest possible element. 581

  43. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) 582

  44. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) 582

  45. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) 582

  46. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) + 4 (1) , (1 , 4) , (1 , 1000 , 1001) 582

  47. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) + 4 (1) , (1 , 4) , (1 , 1000 , 1001) + 5 (1) , (1 , 4) , (1 , 4 , 5) 582

  48. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) + 4 (1) , (1 , 4) , (1 , 1000 , 1001) + 5 (1) , (1 , 4) , (1 , 4 , 5) + 2 (1) , (1 , 2) , (1 , 4 , 5) 582

  49. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) + 4 (1) , (1 , 4) , (1 , 1000 , 1001) + 5 (1) , (1 , 4) , (1 , 4 , 5) + 2 (1) , (1 , 2) , (1 , 4 , 5) + 6 (1) , (1 , 2) , (1 , 4 , 5) , (1 , 4 , 5 , 6) 582

  50. Third approach Example Example: A = (1 , 1000 , 1001 , 4 , 5 , 2 , 6 , 7) A LAT M k, · 1 (1) + 1000 (1) , (1 , 1000) + 1001 (1) , (1 , 1000) , (1 , 1000 , 1001) + 4 (1) , (1 , 4) , (1 , 1000 , 1001) + 5 (1) , (1 , 4) , (1 , 4 , 5) + 2 (1) , (1 , 2) , (1 , 4 , 5) + 6 (1) , (1 , 2) , (1 , 4 , 5) , (1 , 4 , 5 , 6) + 7 (1) , (1 , 2) , (1 , 4 , 5) , (1 , 4 , 5 , 6) , (1 , 4 , 5 , 6 , 7) 582

  51. DP Table Idea: save the last element of the increasing sequence M k,j at slot j . 583

  52. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 583

  53. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 0 1 2 3 4 Index ... - ∞ ∞ ∞ ∞ ∞ ( L j ) j 583

  54. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 0 1 2 3 4 Index ... - ∞ ∞ ∞ ∞ ( L j ) j 3 583

  55. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 0 1 2 3 4 Index ... - ∞ ∞ ∞ ∞ ( L j ) j 2 583

  56. DP Table Idea: save the last element of the increasing sequence M k,j Index 1 2 3 4 5 6 at slot j . Wert 3 2 5 1 6 4 Example: 3 2 5 1 6 4 Index 0 1 2 3 4 ... ( L j ) j - ∞ 2 5 ∞ ∞ 583

  57. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 0 1 2 3 4 Index ... - ∞ ∞ ∞ ( L j ) j 1 5 583

  58. DP Table Idea: save the last element of the increasing sequence M k,j Index 1 2 3 4 5 6 at slot j . Wert 3 2 5 1 6 4 Example: 3 2 5 1 6 4 Index 0 1 2 3 4 ... ( L j ) j - ∞ 1 5 6 ∞ 583

  59. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 Problem: Table does not 0 1 2 3 4 Index ... contain the subsequence, only - ∞ ∞ ( L j ) j 1 4 6 the last value. 583

  60. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 Problem: Table does not 0 1 2 3 4 Index ... contain the subsequence, only - ∞ ∞ ( L j ) j 1 4 6 the last value. Solution: second table with the predecessors. 583

  61. DP Table Idea: save the last element of the increasing sequence M k,j 1 2 3 4 5 6 Index at slot j . 3 2 5 1 6 4 Wert Example: 3 2 5 1 6 4 2 5 1 Predecessor −∞ −∞ −∞ Problem: Table does not 0 1 2 3 4 Index ... contain the subsequence, only - ∞ ∞ ( L j ) j 1 4 6 the last value. Solution: second table with the predecessors. 583

  62. Dynamic Programming Algorithm LAS Table dimension? Semantics? 1 584

  63. Dynamic Programming Algorithm LAS Table dimension? Semantics? Two tables T [0 , . . . , n ] and V [1 , . . . , n ] . 1 T [ j ] : last Element of the increasing subequence M n,j V [ j ] : Value of the predecessor of a j . Start with T [0] ← −∞ , T [ i ] ← ∞ ∀ i > 1 584

  64. Dynamic Programming Algorithm LAS Table dimension? Semantics? Two tables T [0 , . . . , n ] and V [1 , . . . , n ] . 1 T [ j ] : last Element of the increasing subequence M n,j V [ j ] : Value of the predecessor of a j . Start with T [0] ← −∞ , T [ i ] ← ∞ ∀ i > 1 Computation of an entry 2 584

  65. Dynamic Programming Algorithm LAS Table dimension? Semantics? Two tables T [0 , . . . , n ] and V [1 , . . . , n ] . 1 T [ j ] : last Element of the increasing subequence M n,j V [ j ] : Value of the predecessor of a j . Start with T [0] ← −∞ , T [ i ] ← ∞ ∀ i > 1 Computation of an entry Entries in T sorted in ascending order. For each new entry a k +1 binary 2 search for l , such that T [ l ] < a k < T [ l + 1] . Set T [ l + 1] ← a k +1 . Set V [ k ] = T [ l ] . 584

  66. Dynamic Programming algorithm LAS Computation order 3 585

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend