Presentation is loading. Please wait.

Presentation is loading. Please wait.

§3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) Given N words w 1 < w 2 < …… <

Similar presentations


Presentation on theme: "§3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) Given N words w 1 < w 2 < …… <"— Presentation transcript:

1

2 §3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) 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. 〖 Example 〗 Given the following table of probabilities: word probability doubleelseifintfloatforwhile 0.220.180.250.020.200.050.08 Optimal Tree float double else if forwhile int Time = 2.15 1/5 Discussion 27: Please draw the trees obtained by greedy methods and by AVL rotations. What are their expected total access times?

3 §3 Dynamic Programming T i j ::= OBST for w i, ……, w j ( i < j ) c i j ::= cost of T i j ( c i i = 0 ) r i j ::= root of T i j ( r i i = 0 ) w i j ::= weight of T i j = ( w i i = p i ) T 1N with root r 1N, weight w 1N, and cost c 1N. wkwk Lwi…wk1Lwi…wk1 R w k+1 …w j T i j c i j = ?p k + cost( L ) + cost( R )+ weight( L ) + weight( R ) = p k + c i, k  1 + c k+1, j + w i, k  1 + w k +1, j = w i j + c i, k  1 + c k +1, j T i j is optimal  r i j = k is such that 2/5

4 §3 Dynamic Programming word probability doubleelseifintfloatforwhile 0.220.180.250.020.200.050.08 double..doublewhile..whileint..intif..iffloat..floatfor..forelse..else 0.22double0.18else0.20float0.05for0.25if0.02int0.08while double..else 0.58double else..float 0.56float float..for 0.30float for..if 0.35if if..int 0.29if int..while 0.12while double..float 1.02else else..for 0.66float float..if 0.80if for..int 0.39if if..while 0.47if double..for 1.17else else..if 1.21float float..int 0.84if for..while 0.57if double..if 1.83float else..int 1.27float float..while 1.02if double..int 1.89float else..while 1.53float double..while 2.15float T(N) = O(N 3 ) Please read 10.33 on p.419 for an O( N 2 ) algorithm. 3/5 Discussion 28: Please construct the tree from this table.

5 §3 Dynamic Programming 4. All-Pairs Shortest Path For all pairs of v i and v j ( i  j ), find the shortest path between. Method 2 Define D k [ i ] [ j ] = min{ length of path i  { l  k }  j } and D  1 [ i ] [ j ] = Cost [ i ] [ j ]. Then the length of the shortest path from i to j is D N  1 [ i ] [ j ]. Algorithm Start from D  1 and successively generate D 0, D 1,..., D N  1. If D k  1 is done, then either  k  the shortest path i  { l  k }  j  D k = D k  1 ; or  k  the shortest path i  { l  k }  j = { the S.P. from i to k }  {the S.P. from k to j }  D k [ i ] [ j ] = D k  1 [ i ] [ k ] + D k  1 [ k ] [ j ] Method 1 Use single-source algorithm for |V| times. T = O( |V| 3 ) – works fast on sparse graph. 4/5

6 §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 ]; } T(N) = O(N 3 ), but faster in a dense graph. To record the paths please refer to Figure 10.53 on p.393 Works if there are negative edge costs, but no negative-cost cycles. 5/5


Download ppt "§3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) Given N words w 1 < w 2 < …… <"

Similar presentations


Ads by Google