Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 1 Dynamic.

Similar presentations


Presentation on theme: "CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 1 Dynamic."— Presentation transcript:

1

2 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 1 http://www.cs.cityu.edu.hk/~helena Dynamic Programming

3 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 2 http://www.cs.cityu.edu.hk/~helena Dynamic Programming Coming up Assembly-line scheduling Matrix-chain multiplication Elements of dynamic programming Longest common subsequence Optimal binary search trees (Chap 15)

4 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 3 http://www.cs.cityu.edu.hk/~helena First Dynamic Programming Example: Assembly-line Scheduling

5 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 4 http://www.cs.cityu.edu.hk/~helena Assembly line 1 Assembly line 2 Assembly-line Scheduling a 1,1 a 1,2 a 1,n-1 a 1,n t 1,1 t 2,1 t 1,2 t 2,2 a 1,4 a 1,3 t 1,3 t 2,3 t 1,n-1 t 2,n-1 … x1x1 Completed auto exits a 2,1 a 2,2 a 2,3 a 2,n-1 a 2,n a 2,4 x2x2 e1e1 e2e2 chassis enters Station S 2,1 Station S 2,2 Station S 2,3 Station S 2,4 …Station S 2,n-1 Station S 2,n Station S 1,1 Station S 1,2 Station S 1,3 Station S 1,4 …Station S 1,n-1 Station S 1,n

6 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 5 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling Problem: To minimize the total processing time for one auto. a 1,1 a 1,2 a 1,n-1 a 1,n t 1,1 a 1,4 a 1,3 t 2,3 t 2,n-1 … x1x1 Completed auto exits a 2,1 a 2,2 a 2,3 a 2,n- 1 a 2,n a 2,4 x1x1 chassis enters Station S 2,1 Station S 2,2 Station S 2,3 Station S 2,4 … Station S 2,n-1 Station S 2,n Station S 1,1 Station S 1,2 Station S 1,3 Station S 1,4 … Station S 1,n-1 Station S 1,n …

7 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 6 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling To find the fastest ways, we may inspect the speeds of all possible ways. Solve it by dynamic-programming. But … there are 2 n possible ways !!! It takes  (2 n ) time.

8 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 7 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 1) Step 1. Characterize the structure of an optimal solution (the fastest way through the factory) or The fastest way through the factory = The faster one of either a 2,j-1 The fastest way to reach S 2,j-1 x2x2 a 1,n x1x1 The fastest way to reach S 1,j-1

9 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 8 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 1) Step 1. Characterize the structure of an optimal solution (the fastest way through the factory) or a 2,j-1 a 1,j The fastest way to reach S 2,j-1 t 2,n-1 The fastest way to reach station S 1,j = The faster one of either a 1,j-1 a 1,j The fastest way to reach S 1,j-1

10 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 9 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 2) Step 2. Recursively define an optimal solution (fastest way through the factory), in terms of the optimal solutions to subproblems Let f * denote the fastest time through the factory. Let f i [j] denote the fastest time to reach and get through S i,j. Then, f * = min (f 1 [n] + x 1, f 2 [n] + x 2 ) e 1 +a 1,1 if j=1 min (f 1 [j-1] + a 1,j, f 2 [j-1] + t 2,j-1 + a 1,j ) if j>1 f 1 (j) = e 2 +a 2,1 if j=1 min (f 2 [j-1] + a 2,j, f 1 [j-1] + t 1,j-1 + a 2,j ) if j>1 f 2 (j) = Most of the sub- problems are visited more than once. Any clever method to handle them?

11 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 10 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 3) Step 3. Compute the value of an optimal solution in a bottom-up fashion FASTEST-WAY (a,t,e,x,n) 1f 1 [1]  e 1 + a 1,1 2f 2 [1]  e 2 + a 2,1 3for j  2 to n 4do if f 1 [j-1] + a 1,j  f 2 [j-1] + t 2,j-1 + a 1,j 5thenf 1 [j]  f 1 [j-1] + a 1,j 6l 1 [j]  1 7elsef 1 [j]  f 2 [j-1] + t 2,j-1 + a 1,j 8l 1 [j]  2 9do if f 2 [j-1] + a 2,j  f 1 [j-1] + t 1,j-1 + a 2,j 10then f 2 [j]  f 2 [j-1] + a 2,j 11l 2 [j]  2 12elsef 2 [j]  f 1 [j-1] + t 1,j-1 + a 2,j 13l 2 [j]  1 14if f 1 [n] + x 1  f 2 [n] + x 2 15then f *  f 1 [n]+x 1 16l*  1 17elsef *  f 2 [n]+x 2 18l*  2 Let l i [j]be the line number, 1 or 2, whose station j-1 is used in the solution. eg. l 1 [5] = 2 means that the fastest way to reach station 5 of line 1 should pass through station 4 of line 2. Let l * be the line whose station n is used in the solution. eg. l * =2 means that the fastest way involves station n of line 2.

12 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 11 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 3) What we are doing, is: Starting at the bottom level, continuously filling in the tables (f 1 [], f 2 [] and l 1 [], l 2 []), and Looking up the tables to compute new results for next higher level Exercise: what is the complexity of this algorithm?

13 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 12 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling (Step 4) Step 4. Construct an optimal solution from computed information. PRINT-STATIONS() 1i  l * 2print “line ” i “, station ” n 3for j  n downto 2 4do i  l i [j] 5print “line ” i “, station ” j-1 Sample line 1, station 6 line 2, station 5 line 2, station 4 line 1, station 3 line 2, station 2 line 1, station 1

14 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 13 http://www.cs.cityu.edu.hk/~helena Assembly-line Scheduling Dynamic Programming Step ? Characterize the structure of an optimal solution Eg. Study the structure of the fastest way through the factory. Step ? Recursively define the value of an optimal solution Step ? Compute the value of an optimal solution in a bottom-up fashion Step ? Construct an optimal solution from computed information. What we have done is indeed Dynamic Programming Now it is time to test your memory:

15 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 14 http://www.cs.cityu.edu.hk/~helena Second Dynamic Programming Example: Matrix-Chain Multiplication

16 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 15 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication 5 5 5 5 5 5 5 5 5 5 5 5

17 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 16 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication No. They must be compatible: To multiply A and B, number of columns of A must be equal to number of rows of B. Can we freely multiply matrices of any different shapes?

18 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 17 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication We can also multiply them in different ways: However, we must consider the different efficiencies of the different ways. ( parenthesizations ).

19 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 18 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication 4x3x2= multiplications Total = multiplications 5x3x2= multiplications 4x5x2=40 multiplications 5 Total = multiplications 5 5 5 5 5 5 5 5 5 5 5 5 5x3x4=60 multiplications

20 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 19 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication Number of scalar multiplications =No. of rows of A * No. of columns of A * No. of columns of B or no. of rows of B Another Example: A … … … … 10 100 B … … … … 5 C … … … … 5 50 (AB):No. of multiplication = 10 * 100 * 5 = 5000 (BC):No. of multiplication = 100 * 5 * 50 = 25000 ((AB)C): No. of multiplication = 5000 + 10 * 5 * 50 = (A(BC)):No. of multiplication = 25000 + 10 * 100 * 50 = Size of resultant matrix: No. of rows = No. of rows of A No. of columns = No. of columns of B A B 5 5 5 5 5 5 5 5 5 5 5 5

21 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 20 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication So before multiplying the 3 matrices, we better decide the optimal parenthesization. But how to decide? Shall we check all the parenthesizations and find out the optimal one? If we are multiplying 100 matrices, it seems there are a lot of candidates. Mmm, Let’s see how many parenthesizations we’d need to check …

22 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 21 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication Suppose we need to compute A 1 A 2 A 3 …A n Our question is “How many parenthesizations we need to check”. We may compute in many ways: 1 st way: A 1 (A 2 …A n ) but, there are different methods to compute for (A 2 …A n ). 2 nd way: (A 1 A 2 )(A 3 …A n ) but, there are different methods to compute for (A 3 …A n )... n-1 th way: (A 1 …A n-1 )A n but, there are different methods to compute for (A 1 …A n-1 ). Let’s handle in k th way, then no. of methods for (A 1..A n ) =no. of methods for (A 1..A k )(A k+1 A n ) =(no. of methods for k matrices) x (no. of methods for n-k matrices) Suppose we need to compute A 1 A 2 A 3 …A n Our question is “How many parenthesizations we need to check”. We may compute in many ways: 1 st way: A 1 (A 2 …A n ) 2 nd way: (A 1 A 2 )(A 3 …A n ).. n-1 th way: (A 1 …A n-1 )An Let P(n) = no. of alternative parenthesizations of n matrices, then 1if n=1  k=1 to n-1 P(k) P(n-k) if n>1 P(n) = Total no. of methods = sum of the methods for all n-1 ways. n-1 ways

23 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 22 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication 1if n=1  k=1 to n-1 P(k) P(n-k) if n>1 P(n) = Hence, P(1) = 1 P(2) = P(1)*P(1) = 1 P(3) = P(1)*P(2)+P(2)*P(1) = 2 P(4) = P(1)*P(3)+P(2)*P(2)+P(3)*P(1) = 5 P(5) = P(1)*P(4)+P(2)*P(3)+P(3)*P(2)+P(4)*P(1) = 14 P(6) = P(1)*P(5)+P(2)*P(4)+P(3)*P(3)+P(4)*P(2)+P(5)*P(1) = … This function increases very fast [  (2 n )]. So it is not feasible to check all possible parenthesizations. We’ll solve it using dynamic programming. Hence, P(1) = 1 P(2) = P(1)*P(1) = 1 P(3) = P(1)*P(2)+P(2)*P(1) = 2 P(4) = P(1)*P(3)+P(2)*P(2)+P(3)*P(1) = 5 P(5) = P(1)*P(4)+P(2)*P(3)+P(3)*P(2)+P(4)*P(1) = 14 P(6) = P(1)*P(5)+P(2)*P(4)+P(3)*P(3)+P(4)*P(2)+P(5)*P(1) = …

24 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 23 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication (Step 1) Step 1. The structure of an optimal parenthesization For..A i A 2 A 3 …A j.. Let ( A i …A k )( A k+1 …A j ) be the optimal parenthesization. Then A i …A k must be the optimal parenthesization for A i..A k. And A k+1 …A j must be the optimal parenthesization for A k+1..A j.

25 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 24 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication (Step 2) Step 2. A recursive solution Define m[i,j] as minimum number of scalar multiplications for A i …A j. And let p s-1 be the number of columns of A s-1 (= number of rows of A s ). Then, 0if i=j min i  k < j {m[i,k]+m[k+1,j]+p i-1 p k p j } if i<j m[i,j] = Then, each m[i,j] problem can be solved by making use of the solutions of smaller problems. Indeed there are not many problems: one problem for each choice of i and j satisfying 1  i  j  n. But their solutions are to be visited many times. => Any clever method?

26 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 25 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication (Step 3) Step 3. Compute an optimal solution from bottom-up 0if i=j min i  k < j {m[i,k]+m[k+1,j]+p i-1 p k p j } if i<j m[i,j] = Example: col x row A135x30 A215x35 A35x15 A410x5 A520x10 A625x20 i m123456 1 2 j3 4 5 6 m[1,2] = m[1,1]+m[2,2]+30x35x15 = m[2,3] = m[2,2]+m[3,3]+35x15x5 = For chain length = 1: For chain length = 2: 0 0 0 0 0 0 15750 2625 750 1000 5000 7875 4375 2500 3500 9375 118757125 15125105005375 m[1,2] + m[3,3] + 30x15x5 = m[1,1] + m[2,3] + 30x35x5 = m[1,3] = min m [2,3] + m[4,4] + 35x5x10 = m[2,2] + m[3,4] + 35x15x10 = m[1,4] = min

27 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 26 http://www.cs.cityu.edu.hk/~helena MATRIX-CHAIN-ORDER 1for i  1 to n 2m[i,i]  0 3for len  2 to n //len is chain length 4for i  1 to n - len + 1 5j  i + len - 1 6m[i,j]   7for k  i to j-1 8q  m[i,k]+m[k+1,j]+p i-1 p k p j 9if q < m[i,j] 10thenm[i,j]  q 11s[i,j]  k Matrix-Chain Multiplication (Step 3) Step 3. Compute an optimal solution from bottom-up Let s[i,j]be the value of k where optimal splitting occurs. col x row A135x30 A215x35 A35x15 A410x5 A520x10 A625x20 553336 43335 3334 213j 12 1 654321s i O()  ()

28 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 27 http://www.cs.cityu.edu.hk/~helena Matrix-Chain Multiplication (Step 4) Step 4. Constructing an optimal solution A recursive printing procedure: Recall that s[i,j] = the value of k where optimal splitting occurs. PRINT-OPTIMAL-PARENS(i,j) 1if i=j then print “A”I 2elseprint “(“ 3PRINT-OPTIMAL-PARENS(i,s[i,j]) 4PRINT-OPTIMAL-PARENS(s[i,j]+1,j) 5print “)“ Sample ((A 1 (A 2 A 3 ))((A 4 A 5 )A 6 )) 553336 43335 3334 213j 12 1 654321s i

29 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 28 http://www.cs.cityu.edu.hk/~helena Elements of Dynamic Programming: 2 Key Ingredients & Memoization

30 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 29 http://www.cs.cityu.edu.hk/~helena Elements of Dynamic Programming Comparison: Divide & Conquer: Break up into smaller problems Dynamic Programming: Solve all smaller problems but only reuse optimal subproblem solutions But when should we apply dynamic programming? 2 key ingredients: Optimal Substructure Overlapping Subproblems Optimizatio n Problems Table-based Solution Bottom-up approach

31 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 30 http://www.cs.cityu.edu.hk/~helena Elements of Dynamic Programming 2 key ingredients of Dynamic Programming: Optimal Substructure For any problem, if an optimal solution consists of optimal solutions to its subproblems, then, it exhibits optimal substructure. Overlapping Subproblems If a recursive algorithm revisits the same subproblem over and over again, we say that the subproblems are overlapping. If both ingredients happen, it is a good clue that dynamic programming might apply.

32 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 31 http://www.cs.cityu.edu.hk/~helena Elements of Dynamic Programming Memoization - a variation of dynamic programming. Uses a top-down (recursive) strategy: After computing solutions to subproblems, store in the table. Subsequent calls do table lookup. Memorization? Memoization? MEMOIZED-MATRIX-CHAIN() 1for i  1 to n 2for j  i to n 3m[i,j]   4return LOOKUP-CHAIN(i,j) LOOKUP-CHAIN(i,j) 1if m[i,j] <  2return m[i,j] 3if i=j 4then m[i,j]  0 5else for k  i to j-1 6q  LOOKUP-CHAIN(i,k) + LOOKUP-CHAIN(k+1,j) + p i-1 p k p j 7if q < m[i,j] 8then m[i,j]  q 9return m[i,j]

33 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 32 http://www.cs.cityu.edu.hk/~helena Elements of Dynamic Programming When Memoization outperforms bottom-up dynamic programming? For the some cases when some subproblems need not be solved at all. Eg. In the assembly line, some stations’ delivery paths are blocked. Then Memoization is faster. Otherwise, a bottom-up dynamic programming is usually more efficient: No overhead for recursion Less overhead for maintaining the table.

34 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 33 http://www.cs.cityu.edu.hk/~helena Third Dynamic Programming Example: Longest Common Subsequence

35 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 34 http://www.cs.cityu.edu.hk/~helena Longest Common Subsequence Suppose we have 2 strings: S 1 =ACCGGTCGAGTGCGCGGAAGCCGGCCGAA S 2 =GTCGTTCGGAATGCCGTTGCTCTGTAAT The Longest Common Subsequence is GTCGTCGGAAGCCGGCCGAA The Longest Common Subsequence problem : Given 2 sequences X= and Y=, find a maximum-length common subsequence (LCS) of X and Y. Suppose we have 2 strings: S 1 =ACCGGTCGAGTGCGCGGAAGCCGGCCGAA S 2 =GTCGTTCGGAATGCCGTTGCTCTGTAAT The Longest Common Subsequence is GTCGTCGGAAGCCGGCCGAA

36 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 35 http://www.cs.cityu.edu.hk/~helena Longest Common Subsequence Optimal substructure of LCS: Let Z= be any LCS of X= and Y=. Let’s define the notations: X r =, Y r =, Z r =. Then, if x m =y n, then Z k-1 is an LCS of the pair X m-1 and Y n-1. if x m  y n, then Z is an LCS of the pair X m-1 and Y n, or Z is an LCS of the pair X m and Y n-1, orZ is an LCS of both pairs. We can work out a recursive formula for the problem: let c[i,j] = length of an LCS of X i and Y j, then C[i,j] = 0if i=0 or j=0 c[i-1,j-1]+1if i,j>0 and x i =y j Max(c[i,j-1],c[i-1,j])if i,j>0 and x i  y j Note that in this case we skip the computation of the subproblems c[i,j-1] and c[i- 1,j]

37 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 36 http://www.cs.cityu.edu.hk/~helena Longest Common Subsequence To finish the solution by dynamic programming? You may like to try out yourselves, Or, read Chap 15.4.

38 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 37 http://www.cs.cityu.edu.hk/~helena Forth Dynamic Programming Example: Optimal binary search trees

39 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 38 http://www.cs.cityu.edu.hk/~helena Optimal binary search trees k2k2 k1k1 k4k4 k3k3 k5k5 d0d0 d1d1 d2d2 d3d3 d4d4 d5d5 A binary search tree for dictionary lookup: Let K= be n words (distinct keys) in sorted order Let d 0,d 1,..d n be n “dummy keys” representing values not in K Let p i = probability of searching k 1 q i = probability representing d i i01234 5 p i 0.150.100.050.100.20 q i 0.050.100.050.050.050.10 Define the cost of a search as the number of nodes examined in a search. (depth of the key + 1) Expected cost of an arbitrary search = 2.8 k2k2 k1k1 k5k5 k4k4 k3k3 d0d0 d1d1 d2d2 d3d3 d4d4 d5d5 Expected cost of an arbitrary search = 2.75 Optimal binary search tree

40 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 39 http://www.cs.cityu.edu.hk/~helena Optimal binary search trees The Optimal Binary Search Tree problem : For a given set of probabilities of the keys and dummy nodes, construct an optimal binary search tree. Optimal substructure: For an optimal binary search tree with root k r as the root, the left sub-tree and the right sub-tree are also optimal binary search trees. krkr

41 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 40 http://www.cs.cityu.edu.hk/~helena Optimal binary search trees Let e[i,j] = expected cost of searching an optimal binary search tree containing k i,..,k j. When j = i-1, it corresponds to searching d i-1, then e[i,i-1]=q i-1. e[i,j] = q i-1 if j=i-1 Min i  r  j { e[i,r-1] + e[r+1,j] +  s=i to j p s +  s=i-1 to j q s } if i  j k2k2 k1k1 k5k5 k4k4 k3k3 d0d0 d1d1 d2d2 d3d3 d4d4 d5d5 k2k2 k1k1 k5k5 k4k4 k3k3 d0d0 d1d1 d2d2 d3d3 d4d4 d5d5 We can work out a recursive formula for the problem:

42 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 41 http://www.cs.cityu.edu.hk/~helena Optimal binary search trees To finish the solution by dynamic programming? You may like to try out yourselves, Or, read Chap 15.5.

43 CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 42 http://www.cs.cityu.edu.hk/~helena Dynamic Programming Summary Assembly-line scheduling Matrix-chain multiplication Elements of dynamic programming Optimal Substructure Overlapping Subproblems Memoization Longest common subsequence Optimal binary search trees


Download ppt "CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 1 Dynamic."

Similar presentations


Ads by Google