动态规划
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
case 0.18
char.. char 0.20 0.30 0.80 0.84 1.02 char char char..do char.. return
char 0.20
do 0.05
return switch 0.25 0.02
void 0.08
void.. void void
do..do 0.05 0.35 do do.. return do.. switch
Discussion 10: Please construct the tree from this table.
T(N) = O(N3 )
Please read 10.33 on p.419 for an O( N2 ) algorithm.
8/11
4. All-Pairs Shortest Path
n 1
bn O( n4 n )
/* Catalan number y – x < k */ .
Suppose we are to multiply n matrices M1 Mn where Mi is an ri1 ri matrix. Let mij be the cost of the optimal way to compute Mi Mj . Then we have the recurrence equations: if i j 0
return switch 0.25 0.02
void 0.08
Discussion 9: Please draw the trees obtained by greedy methods and by AVL rotations. What are their expected total access times?
break probability 0.22
word
break.. break 0.22 0.58 1.02 1.17 1.83 1.89 2.15 break break case case char char char break.. case break.. char case..case 0.18 0.56 0.66 1.21 1.27 1.53 case char char char char char case.. char case..do
m ij min{ m il m l 1 j ri 1rl r j } if j i i l j
4/11
§3 Dynamic Programming /* r contains number of columns for each of the N matrices */ /* r[ 0 ] is the number of rows in matrix 1 */ /* Minimum number of multiplications is left in M[ 1 ][ N ] */ void OptMatrix( const long r[ ], int N, TwoDimArray M ) { int i, j, k, L; long ThisM; for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0; for( k = 1; k < N; k++ ) /* k = j - i */ for( i = 1; i <= N - k; i++ ) { /* For each position */ j = i + k; M[ i ][ j ] = Infinity; for( L = i; L < j; L++ ) { ThisM = M[ i ][ L ] + M[ L + 1 ][ j ] m1, N + r[ i - 1 ] * r[ L ] * r[ j ]; m1, N 1 m2, N if ( ThisM < M[ i ][ j ] ) /* Update min */ M[ i ][ j ] = ThisM; m1, 2 m2, 3 } /* end for-L */ m1,1 m2, 2 3 } /* end for-i */ T(N) = O(N ) }
6/11
Ti j ::= OBST for wi , ……, wj ( i < j ) ci j ::= cost of Ti j ( ci i = 0 ) ri j ::= root of Ti j ( ri i = 0 )
§3 Dynamic Programming
wi j ::= weight of Ti j = pk ( wi i = pi )
If we multiply in the order
Then the computing来自百度文库time is
M1 [ 1020 ] ( M2 [ 2050 ] ( M3 [ 501 ] M4 [ 1100 ] ) )
50 1 100 + 20 50 100 + 10 20 100 = 125,000
2/11
§3 Dynamic Programming
2. Ordering Matrix Multiplications
〖Example〗 Suppose we are to multiply 4 matrices M1 [ 1020 ] M2 [ 2050 ] M3 [ 501 ] M4 [ 1100 ] .
Ti j is optimal ri j = k is such that ci j min{ w i j ci ,l 1 cl 1, j }
il j
7/11
c i j min{ w i j c i ,l 1 c l 1, j }
il j
§3 Dynamic Programming
return..return switch..switch 0.25 return..switch switch.. void return 0.12 return void return.. void
return 0.02 switch 0.08
return 0.29 return 0.47 return
If we multiply in the order
Then the computing time is
( M1 [ 1020 ] ( M2 [ 2050 ] M3 [ 501 ] ) ) M4 [ 1100 ]
20 50 1 + 10 20 1 + 10 1 100 = 2,200
§3 Dynamic Programming
Use a table instead of recursion
1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2)
int Fib( int N ) { if ( N <= 1 ) return 1; else return Fib( N - 1 ) + Fib( N - 2 ); }
return 0.39 return 0.57 return
break..do
break.. return break.. switch break.. void
case.. return char.. switch
case.. switch case.. void char.. void
do.. void
with minimal computing time?
Problem: In which order can we compute the product of n matrices
3/11
§3 Dynamic Programming
Let bn = number of different ways to compute M1 M2 Mn. Then we have b2 = 1, b3 = 2, b4 = 5,
Let Mij = Mi Mj . Then M1n = M1 Mn = M1i Mi+1 n
bn bi bn i whereIf n i1 and b1the 1 . j – = k , then only bn b b 2n i 1 i N i There are O( )to values Monly required xy values M . computeof M satisfy ij ij n
T(N) T(N – 1) + T(N – 2) T(N) F(N)
1/11
F6 F5 F4 F3 F2 F1 F0 F1 F1 F2 F0 F1 F2 F0 F3 F1 F1 F2 F0 F3 F4
§3 Dynamic Programming F2 F1 F1 F0
Trouble-maker: The growth of redundant calculations is explosive. Solution: Record the two most recently computed values to avoid recursive calls.
§3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion)
Given N words w1 < w2 < …… < wN, and the probability of searching for each wi is pi . Arrange these words in a binary search tree in a way that minimize the expected
m N 1, N m N 1, N 1 mN ,N
if i j 10.46 on p.388 0 ordering please refer to Figure To record the
5/11
m ij min{ m il m l 1 j ri 1rl r j } if j i i l j
int Fibonacci ( int N ) { int i, Last, NextToLast, Answer; if ( N <= 1 ) return 1; Last = NextToLast = 1; /* F(0) = F(1) = 1 */ for ( i = 2; i <= N; i++ ) { Answer = Last + NextToLast; /* F(i) = F(i-1) + F(i-2) */ NextToLast = Last; Last = Answer; /* update F(i-1) and F(i-2) */ } /* end-for */ return Answer; } T(N) = O(N)
k i
j
Ti j
L wi…wk1
wk
T1N with root r1N, weight w1N, and R cost c1N . w …w
k+1 j
ci j = p? k + cost( L ) + cost( R ) + weight( L ) + weight( R )
= pk + ci, k1 + ck+1, j + wi, k1 + wk +1, j = wi j + ci, k1 + ck +1, j
total access time. T ( N ) pi (1 d i )
i 1
N
〖Example〗Given the following table of probabilities:
break probability 0.22
word
case 0.18
char 0.20
do 0.05