1 / 26

Dynamic Programming

Pasi Fränti. Dynamic Programming. 29.10.2018. Sample problems solved by dynamic programming. Fibonacci number Partition of natural number Associative Matrix multiplication Shortest path (Djikstra) Segmentation of sequence. Fibonacci numbers 1 1 2 3 5 8 13 21 34 55…. Definition:.

bbrenton
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. Pasi Fränti Dynamic Programming 29.10.2018

  2. Sample problems solved by dynamic programming • Fibonacci number • Partition of natural number • Associative Matrix multiplication • Shortest path (Djikstra) • Segmentation of sequence

  3. Fibonacci numbers1 1 2 3 5 8 13 21 34 55… Definition: Straightforward solution: Fibonacci(N) IF N=0 OR N=1 THEN RETURN 1 ELSE RETURN Fibonacci(N-1) + Fibonacci(N-2)

  4. Fibonacci numbersTime complexity F10 F9 F8 F8 F7 F7 F6 F7 F6 F5 F6

  5. Fibonacci numbersSolved by dynamic programming Fibonacci(N) IF N≤2 RETURN 1; a=1; b=1; FOR i=3 TO N DO f  a+b; ba; af; RETURN f

  6. Partition of natural numberProblem definition • Find k natural numbers that sums up to n but their product is maximized: • n1 + n2 + n3 + … + nk = n • n1 ∙ n2 ∙ n3 ∙ … ∙ nk = max! • Example (n=7): • 1∙1∙1∙1∙1∙1∙1 = 1 • 1∙1∙1∙1∙1∙2= 2 • 1∙1∙1∙2∙2 = 4 • … • 1∙2∙4 = 8 • 2∙2∙3 = 12 • 3∙4 = 12 • 1∙6 = 6

  7. 1 1 - - 1 - - - - - - 2 - 1 - 2 - 3 1 - - 3 - - - - 4 - 1 4 5 - 5 - 1 1 6 6 - 7 7 1 2 3 4 5 6 7 Partition of natural numberSolution by dynamic programming n =N k

  8. 1 1 - - 1 - - - - - 1 2 - 2 - - - - - 2 3 - 3 1 - - 1 4 - 4 2 4 - 4 2 - 5 - 1 6 5 - 6 2 1 6 9 4 8 12 4 8 1 7 7 2 12 2 3 4 5 6 7 Partition of natural numberExample completed n =N k

  9. Partition of natural numberDynamic programming algorithm MaxProduct (N) FOR i=1 TO N DO m[i,i]1; m[1,i] i; FOR k=2 TO N-1 DO FOR n=k+1 TO N DO m[k,n]  max1≤x≤n-k+1{x ∙ m[k-1,n-x]};

  10. Blank space for notes

  11. Associative matrix multiplication Finding the optimal order Multiplication: M1 M2  M3 … MN M1 M2 m1 m2 m2  = n1 n2 n1 Constraint: m1=n2

  12. Associative matrix multiplicationExample Multiplication: [32] ∙ [26] ∙ [62] Order 1: ([32] ∙ [26]) ∙ [62] 36+36 operations Order 2: [32] ∙ ([26] ∙ [62]) 12+24 operations Less operations!

  13. Associative matrix multiplicationDynamic programming algorithm Sequence: [1020] ∙ [2050] ∙ [501] ∙ [1100] R0 R1 R2 R3 R4 MM-optimizer(M1,..MN) FOR i=1 TO N DO m[i,i]0; m[0,i]0; FOR length=1 TO N-1 DO FOR i=1 TO N-length DO j  i+length; m[length,j]  mini≤k≤j-1{ m[k-i,k] + m[j-k-1,j] + Ri-1∙ Rk∙ Rj }; Solution ofright part Solution ofleft part

  14. Associative matrix multiplicationSub-problems of size N=2 *(10x20)x(20x40) => 10x20x40 mult = 8000 multiplications

  15. Associative matrix multiplicationSub-problems of size N=3

  16. Associative matrix multiplicationSub-problems of size N=4

  17. Associative matrix multiplicationFinal solution

  18. 4 1 2 A B C D 3 4 1 3 E F ? What about these? 1 1 A A 2 2 1 1 1 B B 1

  19. Blank space for notes

  20. 8 5 5 5 3 6 1 2 6 8 2 4 5 5 10 1 7 3 3 Shortest pathDjikstra’s algorithm

  21. 117 216 110 246 199 182 170 121 231 315 142 79 242 136 191 148 78 120 191 126 178 149 89 116 234 170 112 51 79 131 109 86 73 163 143 72 90 63 53 105 59 27 58 135 116

  22. 117 216 110 246 199 182 170 121 231 315 142 79 242 136 191 148 78 120 191 126 178 149 89 116 234 170 112 51 79 131 109 86 73 163 143 72 90 63 53 105 59 27 58 135 116

  23. 231 136 457 120 246 209 395 234 413 340 320 412 445

  24. Blank space for notes

  25. Segmentation See: LAMI route processing!

  26. Blank space for notes

More Related