Download Dynamic programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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:
FN 1  FN 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; ba; af;
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
Tk n  max x  Tk 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
Tk n  max x  Tk 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
n1
m1
m2

T M1  M 2   n1  m2  n2 
M2
n2
m2
=
n1
Constraint: m1=n2
Associative matrix multiplication
Example
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!
Associative matrix multiplication
Dynamic 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;
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
1020
2040
4050
5040
1040
8.000
2050
40.000
4040
80.000
5010
20.000
3
4
5
*(10x20)x(20x40) => 10x20x40 mult = 8000 multiplications
4010
Associative matrix multiplication
Sub-problems of size N=3
1020
2040
4050
5040
2
1040
8.000
2050
40.000
4040
80.000
5010
20.000
3
1050
28.000
2040
80.000
4010
40.000
4
5
4010
Associative matrix multiplication
Sub-problems of size N=4
1020
2040
4050
5040
2
1040
8.000
2050
40.000
4040
80.000
5010
20.000
3
1050
28.000
4
1040
48.000
5
2040
20x10
80.000
(8000
mult)
2010
48.000
4010
40.000
4010
Associative matrix multiplication
Final solution
1020
2040
4050
5040
2
1040
8.000
2050
40.000
4040
80.000
5010
20.000
3
1050
28.000
2040
80.000
4010
40.000
4
1040
48.000
2010
48.000
5
1010
50.000
4010
What about these?
A
1
A
2
1
1
1
B
1
2
1
B
Related documents