Dynamic Programming
7 -1
0,1,1,2,3,5,8,13,21,34,...
, ,
0 1 1 2 3 ...
1 1 2 3 5 8 ...
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat
.html
7 -2
0,1,1,2,3,5,8,13,21,34,...
7 -3
0,1,1,2,3,5,8,13,21,34,...
fn = 0 if n = 0
fn = 1 if n = 1
fn = fn-1 + fn-2 if n 2
lim n
fn
f n 1
1
x
1 5
Golden number
2
x
1
1 x 1
x-1
x2 x 1 0
1 5
x
2
7 -4
Computation of Fibonacci
sequence
fn = 0
fn = 1
if n = 0
if n = 1
fn = fn-1 + fn-2 if n 2
7 -5
Dynamic Programming
Dynamic Programming is an
algorithm design method that can
be used when the solution to a
problem may be viewed as the
result of a sequence of decisions
7 -6
e.g.
1
11
18
16
13
Dynamic programming
approach
1
11
16
13
d(A, T)
d(B, T)
18
9
5
d(C, T)
1
11
18
16
13
7 -10
Backward approach
(forward reasoning)
4
1
11
B
16
18
d(S, A) = 1
5
d(S, B) = 2
C
F
2
d(S, C) = 5
d(S,D)=min{d(S,A)+d(A,D), d(S,B)+d(B,D)}
= min{ 1+4, 2+9 } = 5
d(S,E)=min{d(S,A)+d(A,E), d(S,B)+d(B,E)}
= min{ 1+11, 2+5 } = 7
d(S,F)=min{d(S,B)+d(B,F), d(S,C)+d(C,F)}
= min{ 2+16, 5+2 } = 7
13
7 -11
1
11
18
16
13
7 -12
Principle of optimality
7 -13
Dynamic programming
7 -14
m resources, n projects
profit Pi, j : j resources are allocated to
project i.
maximize the total profit.
Resource
Project
1
2
3
4
1
2
5
4
2
2
8
6
4
4
3
9
7
4
5
7 -15
I
0,3
E
0,2
B
1,1
C
2,1
F 4
0
1,2
0
6
5
G
2,2
5
D
3,1
J
1,3
2,3
4
H
3,2
0
L
3,3
7 -16
7 -17
7 -18
Let A = a1 a2 am and B = b1 b2 bn
Li,j = Li-1,j-1 + 1
if ai=bj
7 -19
L1,2
L2,1
L2,2
L1,3
L3,1
7 -20
e.g. A = b a c a d, B = a c c b a d c b
b
a
A c
a
d
0
0
0
0
0
0
B
b a
0
0
1
1
1
1
0
0
1
2
2
2
0
0
1
2
2
2
0
1
1
2
2
2
0 0
1 1
2 2
2 2
3 3
3 4
0
1
2
3
3
4
0
1
2
3
3
4
maximize 1i n
Wi xi
subject to1in
M
xi = 0 or 1, 1in
e. g.
i
1
2
3
Wi
10
3
5
Pi
40
20
30
M=10
7 -22
10
40
0
20
0
x3=0
x3=1
30
x2=1
01
0
x2=0
00
30
0
0
010
0
x3=0
100
011
x3=1
S
x1=0
x3=0
001
000
7 -23
P x= 20+30 = 50
i
7 -24
7
3
12
12
12
3
7
12
(a)
(b)
(c)
(d)
7 -25
i 0
7 -26
10
14
Identifiers : 4, 5, 8, 10,
11, 12, 14
Internal node :
successful search, Pi
11
External node :
unsuccessful search, Qi
12
P level(a ) Q (level(E ) 1)
n 1
n 0
m 1
P 1 . .P
Q 0 . .Q k-1
ak
k-1
a 1 ...a k-1
C(1,k-1)
. .P
Q k . .Q
n
n
a k+1 ...a n
C(k+1,n)
m k 1
k+1
7 -28
General formula
k 1
Q k Pm Q m C k 1, j
m k 1
min C i, k 1 C k 1, j Qi-1 Pm Q m
i k j
m i
Pi...Pk-1
Qi-1...Qk-1
ak
Pk+1...Pj
Qk...Qj
ai...ak-1
ak+1...aj
C(i,k-1)
C(k+1,j)
7 -29
Computation relationships
of subtrees
e.g. n=4
C(1,4)
C(1,3)
C(2,4)
C(1,2)
C(2,3)
C(3,4)
Time complexity : O(n3)
(n-m) C(i, j)s are computed when j-i=m.
Each C(i, j) with j-i=m can be computed in O(m) time.
O(
1 m n
7 -30
Matrix-chain multiplication
7 -31
0
if i j
m(i, j)
m(i, k) m(k 1, j) pi 1p k p j if i j
imin
k j-1
Computation sequence :
7 -32
extra guards
7 -33
a
e1
e2
7 -34
T(vi): the tree includes vi , vj (parent of vi) and all descendant nodes of vi.
C(T(vi), vi , vj ): cost of an optimal searching plan with searching from vi to
vj.
C(T(v4), v4 , v2 )=5
C(T(v4), v2 , v4 )=2
C(T(v2), v2 , v1 )=6
C(T(v2), v1 , v2 )=9
7 -35
C
(
T
(
v
),
r
,
v
)
,
i
i
C (T (r ), r ) min im1
C (T (vi ), vi , r )
i 1
7 -36
7 -37
i 1
C (T (u ), w, u, u ) wt ( w) C (T ( vi ), vi , u )
m
C (T (u ), w, u, u ) wt ( w) wt (u ) min{C (T ( vi ), vi , u ), C (T ( vi ), u, vi )}
i 1
7 -38
i 1
m
C (T (u ), u, w, u ) wt (u ) C (T ( vi ), u, vi )
C (T (u ), u, w, u ) 2 wt (u ) min{C (T ( vi ), vi , u ), C (T ( vi ), u, vi )}
i 1
7 -39
C (T (u ), w, u ) wt ( w)
C (T (u ), u , w) wt (u )
7 -40