260 likes | 298 Views
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:.
E N D
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 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)
Fibonacci numbersTime complexity F10 F9 F8 F8 F7 F7 F6 F7 F6 F5 F6
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; ba; af; RETURN f
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
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
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
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]};
Associative matrix multiplication Finding the optimal order Multiplication: M1 M2 M3 … MN M1 M2 m1 m2 m2 = n1 n2 n1 Constraint: m1=n2
Associative matrix multiplicationExample Multiplication: [32] ∙ [26] ∙ [62] Order 1: ([32] ∙ [26]) ∙ [62] 36+36 operations Order 2: [32] ∙ ([26] ∙ [62]) 12+24 operations Less operations!
Associative matrix multiplicationDynamic programming algorithm Sequence: [1020] ∙ [2050] ∙ [501] ∙ [1100] 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
Associative matrix multiplicationSub-problems of size N=2 *(10x20)x(20x40) => 10x20x40 mult = 8000 multiplications
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
8 5 5 5 3 6 1 2 6 8 2 4 5 5 10 1 7 3 3 Shortest pathDjikstra’s algorithm
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
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
231 136 457 120 246 209 395 234 413 340 320 412 445
Segmentation See: LAMI route processing!