Dynamic Programming Pasi Fränti 23.9.2014 Sample problems solved by dynamic programming 1. 2. 3. 4. 5. Fibonacci number Partition of natural number Associative Matrix multiplication Shortest path (Djikstra) Segmentation of sequence 2 Fibonacci numbers 1 1 2 3 5 8 13 21 34 55… Definition: F N 1 F N 2 if N 2 FN 1 if N 0 N 1 Straightforward solution: Fibonacci(N) IF N=0 OR N=1 THEN RETURN 1 ELSE RETURN Fibonacci(N-1) + Fibonacci(N-2) Fibonacci numbers Time complexity F10 F9 F8 F7 F8 F7 F6 F6 F7 F5 F6 T N T N 1 T N 2 1 2 T N 2 1 2 N Fibonacci numbers Solved 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 T N N Partition of natural number Problem 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∙2 1∙1∙1∙2∙2 … =1 =2 =4 2∙2∙3 3∙4 1∙7 = 12 = 12 =7 1∙2∙4 =8 Partition of natural number Solution by dynamic programming T k n max x T k 1 n x x 1,.., n k 1 n k 1 2 3 4 5 6 7 1 1 2 3 4 5 6 7 2 - 1 3 - - 1 4 - - - 1 5 - - - - 1 6 - - - - - 1 7 - - - - - - 1 =N Partition of natural number Example completed T k n max x T k 1 n x x 1,.., n k 1 n k 1 2 3 4 5 6 7 1 1 2 3 4 5 6 7 2 - 1 2 4 6 9 12 3 - - 1 2 4 8 12 4 - - - 1 2 4 8 5 - - - - 1 2 4 6 - - - - - 1 2 7 - - - - - - 1 =N Partition of natural number Dynamic 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,x]}; Blank space for notes Associative matrix multiplication Finding the optimal order Multiplication: M1 M2 M3 … MN M1 m1 n1 T M 1 M m2 2 n1 m 2 n 2 M2 n2 m2 = n1 Constraint: m1=n2 Associative matrix multiplication Example Multiplication: [32] ∙ [26] ∙ [62] Order 1: ([32] ∙ [26]) ∙ [62] 36+36 operations Order 2: [32] ∙ ([26] ∙ [62]) 12+24 operations Less operations! Associative matrix multiplication Dynamic 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; Solution of right part Solution of FOR length=1 TO N-1 DO left part FOR i=1 TO N-length DO j i+length; m[i,j] mini≤k≤j-1{ m[i,k] + m[k+1,j] + Ri-1∙ Rk∙ Rj }; Associative matrix multiplication Sub-problems of size N=2 2 1020 2040 4050 5040 1040 8.000 2050 40.000 4040 80.000 5010 20.000 3 4 5 *(10x20)x(20x40) => 10x20x40 mult = 8000 multiplications 4010 Associative matrix multiplication Sub-problems of size N=3 1020 2040 4050 5040 2 1040 8.000 2050 40.000 4040 80.000 5010 20.000 3 1050 28.000 2040 80.000 4010 40.000 4 5 4010 Associative matrix multiplication Sub-problems of size N=4 1020 2040 4050 5040 2 1040 8.000 2050 40.000 4040 80.000 5010 20.000 3 1050 28.000 4 1040 48.000 5 2040 20x10 80.000 (8000 mult) 2010 48.000 4010 40.000 4010 Associative matrix multiplication Final solution 1020 2040 4050 5040 2 1040 8.000 2050 40.000 4040 80.000 5010 20.000 3 1050 28.000 2040 80.000 4010 40.000 4 1040 48.000 2010 48.000 5 1010 50.000 4010 What about these? A 1 A 2 1 1 1 B 1 2 1 B