50 likes | 205 Views
§3 Dynamic Programming. Given N words w 1 < w 2 < …… < w N , and the probability of searching for each w i is p i . Arrange these words in a binary search tree in a way that minimize the expected total access time. word. double. else. float. for. if. int. while.
E N D
§3 Dynamic Programming 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 total access time. word double else float for if int while probability 0.22 0.18 0.20 0.05 0.25 0.02 float 0.08 double if else for while int 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) 〖Example〗Given the following table of probabilities: Optimal Tree Discussion 27: Please draw the trees obtained by greedy methods and by AVL rotations. What are their expected total access times? Time = 2.15 1/5
§3 Dynamic Programming wi j ::= weight of Ti j = ( wi i = pi ) wk L wi…wk1 R wk+1…wj Ti jis optimal ri j = kis such that 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 ) T1N with root r1N, weight w1N, and cost c1N . Ti j ci j = ? pk + 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 2/5
§3 Dynamic Programming double..double else..else float..float for..for if..if int..int while..while 0.22 double 0.18 else 0.20 float 0.05 for 0.25 if 0.02 int 0.08 while double..else 0.58 double word double else float for if int while probability 0.22 0.18 0.20 0.05 0.25 0.02 0.08 double..int double..for double..while double..float double..if for..if if..int else..while int..while else..int float..while else..for float..if for..int if..while for..while else..if float..int else..float float..for 1.17 1.83 1.89 2.15 1.02 float float else float else 1.21 1.02 0.80 0.35 0.39 0.84 1.53 0.29 0.57 1.27 0.66 0.12 0.47 float if float if if if while if if if if float float 0.30 0.56 float float Discussion 28: Please construct the tree from this table. T(N) = O(N3) Please read 10.33 on p.419 for an O( N2) algorithm. 3/5
§3 Dynamic Programming 4. All-Pairs Shortest Path For all pairs of vi and vj( i j ), find the shortest path between. Method 1 Use single-source algorithmfor |V| times. T = O( |V|3 ) – works fast on sparse graph. Method 2 Define Dk[ i ] [ j ] = min{ length of path i { l k } j } and D1[ i ] [ j ] = Cost [ i ] [ j ]. Then the length of the shortest path from i to j is DN1[ i ] [ j ]. Start from D1 and successively generate D0, D1, ..., DN1. If Dk1 is done, then either Algorithm k the shortest path i { l k } j Dk = Dk1 ; or k the shortest path i { l k } j = { the S.P. from i to k } {the S.P. from k to j } Dk [ i ] [ j ] = Dk1[ i ] [ k ] + Dk1[ k ] [ j ] 4/5
§3 Dynamic Programming /* A[ ] contains the adjacency matrix with A[ i ][ i ] = 0 */ /* D[ ] contains the values of the shortest path */ /* N is the number of vertices */ /* A negative cycle exists iff D[ i ][ i ] < 0 */ void AllPairs( TwoDimArray A, TwoDimArray D, int N ) { int i, j, k; for ( i = 0; i < N; i++ ) /* Initialize D */ for( j = 0; j < N; j++ ) D[ i ][ j ] = A[ i ][ j ]; for( k = 0; k < N; k++ ) /* add one vertex k into the path */ for( i = 0; i < N; i++ ) for( j = 0; j < N; j++ ) if( D[ i ][ k ] + D[ k ][ j ] < D[ i ][ j ] ) /* Update shortest path */ D[ i ][ j ] = D[ i ][ k ] + D[ k ][ j ]; } Works if there are negative edge costs, but no negative-cost cycles. T(N) = O(N3), but faster in a dense graph. To record the paths please refer to Figure 10.53 on p.393 5/5