Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Dynamic Programming.

Similar presentations


Presentation on theme: "Chapter 8 Dynamic Programming."— Presentation transcript:

1 Chapter 8 Dynamic Programming

2

3

4

5

6

7

8

9

10

11

12

13

14 Binomial Coefficients
Binomial Coefficients are coefficients of the binomial formula:- (a + b)n = C(n,0)anb0 + … + C(n, k)an-kbk + … + C(n, n)a0bn Recurrence:- C(n, k) = C(n-1, k) + C(n-1, k-1) for n>k>0 C(n,0) = 1 C(n, n) = 1 for n >0

15

16 Time Efficiency:- ɵ(nk)
Space Efficiency:- ɵ(nk)

17 Warshall’s Algo for finding Transitive Closure

18

19

20

21

22

23

24

25

26

27

28

29

30 Optimal Binary Search Tree
An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. Problem: Given n keys a1 < …< an and probabilities p1, …, pn Searching for them, find a BST with a minimum average number of comparisons in successful search. Since total number of BSTs with n nodes is given by C(2n,n)/(n+1), which grows exponentially, brute force is hopeless.

31 Brute Force Technique for BST

32 Optimal BST

33 Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys ai< …< aj, where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all BSTs with some ak (i ≤ k ≤ j ) as their root; T[i,j] is the best among them:-

34

35

36 Example: What is an optimal BST for keys A, B,C, and D with search probabilities 0.1, 0.2, 0.4, and 0.3, respectively?

37 The table is filled diagonal by diagonal, the left one id filled using the recurrence:-

38

39

40 Time efficiency: Θ(n3) but can be reduced to Θ(n2) by Taking advantage of monotonicity of entries in the root table, i.e., R[i,j] is always in the range between R[i,j-1] and R[i+1,j] Space efficiency: Θ(n2) Method can be expanded to include unsuccessful searches

41

42

43

44

45

46

47 Matrix Chain Multiplication
Given some matrices to multiply, determine the best order to multiply them so you minimize the number of single element multiplications. i.e. Determine the way the matrices are parenthesized. First off, it should be noted that matrix multiplication is associative, but not commutative. But since it is associative, we always have: ((AB)(CD)) = (A(B(CD))), or any other grouping as long as the matrices are in the same consecutive order. BUT NOT: ((AB)(CD)) = ((BA)(DC))

48 Matrix Chain Multiplication
It may appear that the amount of work done won’t change if you change the parenthesization of the expression, but we can prove that is not the case! Let us use the following example: Let A be a 2x10 matrix Let B be a 10x50 matrix Let C be a 50x20 matrix But FIRST, let’s review some matrix multiplication rules…

49 Matrix Chain Multiplication
Let’s get back to our example: We will show that the way we group matrices when multiplying A, B, C matters: Let A be a 2x10 matrix Let B be a 10x50 matrix Let C be a 50x20 matrix Consider computing A(BC): # multiplications for (BC) = 10x50x20 = 10000, creating a 10x20 answer matrix # multiplications for A(BC) = 2x10x20 = 400 Total multiplications = = Consider computing (AB)C: # multiplications for (AB) = 2x10x50 = 1000, creating a 2x50 answer matrix # multiplications for (AB)C = 2x50x20 = 2000, Total multiplications = = 3000 Consider computing A(BC) We multiply BC first, how many element multiplications are required? What are the dimensions of our resulting matrix? Then we multiply A(BC) How many element multiplications are required? What are the TOTAL element multiplications?? Consider computing (AB)C We multiply AB first, how many element multiplications are required? Then we multiply (AB)C IS THERE A DIFFERENCE BETWEEN THE NUMBER OF MULTIPLICATIONS necessary for the different groupings??

50 Matrix Chain Multiplication
Thus, our goal today is: Given a chain of matrices to multiply, determine the fewest number of multiplications necessary to compute the product.

51 Matrix Chain Multiplication
Formal Definition of the problem: Let A = A0 A1 ... An-1 Let Ni,j denote the minimal number of multiplications necessary to find the product: Ai Ai+1 ... Aj. And let dixdi+1 denote the dimensions of matrix Ai. We must attempt to determine the minimal number of multiplications necessary(N0,n-1) to find A, assuming that we simply do each single matrix multiplication in the standard method.

52 Matrix Chain Multiplication
The key to solving this problem is noticing the sub-problem optimality condition: If a particular parenthesization of the whole product is optimal, then any sub-parenthesization in that product is optimal as well. Say What? If (A (B ((CD) (EF)) ) ) is optimal Then (B ((CD) (EF)) ) is optimal as well Proof on the next slide…

53 Matrix Chain Multiplication
Assume that we are calculating ABCDEF and that the following parenthesization is optimal: (A (B ((CD) (EF)) ) ) Then it is necessarily the case that (B ((CD) (EF)) ) is the optimal parenthesization of BCDEF. Why is this? Because if it wasn't, and say ( ((BC) (DE)) F) was better, then it would also follow that (A ( ((BC) (DE)) F) ) was better than (A (B ((CD) (EF)) ) ), contradicting its optimality!

54 Matrix Chain Multiplication
Our final multiplication will ALWAYS be of the form (A0 A1 ... Ak)  (Ak+1 Ak+2 ... An-1) In essence, there is exactly one value of k for which we should "split" our work into two separate cases so that we get an optimal result. Here is a list of the cases to choose from:  (A0)  (A1 Ak+2 ... An-1) (A0 A1)  (A2 Ak+2 ... An-1) (A0 A1A2)  (A3 Ak+2 ... An-1) ... (A0 A1 ... An-3)  (An-2  An-1) (A0 A1 ... An-2)  (An-1) Basically, count the number of multiplications in each of these choices and pick the minimum. One other point to notice is that you have to account for the minimum number of multiplications in each of the two products.

55 Matrix Chain Multiplication
Consider the case multiplying these 4 matrices: A: 2x4 B: 4x2 C: 2x3 D: 3x1 1. (A)(BCD) - This is a 2x4 multiplied by a 4x1, so 2x4x1 = 8 multiplications, plus whatever work it will take to multiply (BCD). 2. (AB)(CD) - This is a 2x2 multiplied by a 2x1, so 2x2x1 = 4 multiplications, plus whatever work it will take to multiply (AB) and (CD). 3. (ABC)(D) - This is a 2x3 multiplied by a 3x1, so 2x3x1 = 6 multiplications, plus whatever work it will take to multiply (ABC).

56 Matrix Chain Multiplication
This leads us to the following recursive formula: Our recursive formula: Ni,j = min value of Ni,k + Nk+1,j + didk+1dj+1, over all valid values of k. Now let’s turn this recursive formula into a dynamic programming solution Which sub-problems are necessary to solve first? Clearly it's necessary to solve the smaller problems before the larger ones. In particular, we need to know Ni,i+1, the number of multiplications to multiply any adjacent pair of matrices before we move onto larger tasks. Similarly, the next task we want to solve is finding all the values of the form Ni,i+2, then Ni,i+3, etc.

57 Matrix Chain Multiplication
Algorithm: 1) Initialize N[i][i] = 0, and all other entries in N to . 2) for i=1 to n-1 do the following 2i) for j=0 to n-1-i do 2ii) for k=j to j+i-1 2iii) if (N[j][j+i-1] > N[j][k]+N[k+1][j+i-1]+djdk+1di+j)   N[j][j+i-1]= N[j][k]+N[k+1][j+i-1]+djdk+1di+j Basically, we’re checking different places to “split” our matrices by checking different values of k and seeing if they improve our current minimum value.

58 Longest Common Subsequence (LCS)
Application: comparison of two DNA strings Ex: X= {A B C B D A B }, Y= {B D C A B A} Longest Common Subsequence: X = A B C B D A B Y = B D C A B A Brute force algorithm would compare each subsequence of X with the symbols in Y 11/14/2018

59 LCS Algorithm if |X| = m, |Y| = n, then there are 2m subsequences of x; we must compare each with Y (n comparisons) So the running time of the brute-force algorithm is O(n 2m) Notice that the LCS problem has optimal substructure: solutions of subproblems are parts of the final solution. Subproblems: “find LCS of pairs of prefixes of X and Y” 11/14/2018

60 LCS Algorithm First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS itself. Define Xi, Yj to be the prefixes of X and Y of length i and j respectively Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n] 11/14/2018

61 LCS recursive solution
We start with i = j = 0 (empty substrings of x and y) Since X0 and Y0 are empty strings, their LCS is always empty (i.e. c[0,0] = 0) LCS of empty string and any other string is empty, so for every i and j: c[0, j] = c[i,0] = 0 11/14/2018

62 LCS recursive solution
When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LCS Xi and Yj equals to the length of LCS of smaller strings Xi-1 and Yi-1 , plus 1 11/14/2018

63 LCS recursive solution
Second case: x[i] != y[j] As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e. maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj) 11/14/2018

64 LCS Length Algorithm LCS-Length(X, Y)
1. m = length(X) // get the # of symbols in X 2. n = length(Y) // get the # of symbols in Y 3. for i = 1 to m c[i,0] = 0 // special case: Y0 4. for j = 1 to n c[0,j] = 0 // special case: X0 5. for i = 1 to m // for all Xi 6. for j = 1 to n // for all Yj 7. if ( Xi == Yj ) 8. c[i,j] = c[i-1,j-1] + 1 9. else c[i,j] = max( c[i-1,j], c[i,j-1] ) 10. return c 11/14/2018

65 LCS Example We’ll see how LCS algorithm works on the following example: X = ABCB Y = BDCAB What is the Longest Common Subsequence of X and Y? LCS(X, Y) = BCB X = A B C B Y = B D C A B 11/14/2018

66 LCS Example (0) ABCB BDCAB X = ABCB; m = |X| = 4
j i Yj B D C A B Xi A 1 B 2 3 C 4 B X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 Allocate array c[5,4] 11/14/2018

67 LCS Example (1) ABCB BDCAB for i = 1 to m c[i,0] = 0
j i Yj B D C A B Xi A 1 B 2 3 C 4 B for i = 1 to m c[i,0] = 0 for j = 1 to n c[0,j] = 0 11/14/2018

68 LCS Example (2) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C
A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

69 LCS Example (3) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C
A 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

70 LCS Example (4) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 B 2 3
A 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

71 LCS Example (5) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

72 LCS Example (6) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

73 LCS Example (7) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 1 1 1 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

74 LCS Example (8) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2
A 1 1 1 B 2 1 1 1 1 2 3 C 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

75 LCS Example (10) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

76 LCS Example (11) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

77 LCS Example (12) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

78 LCS Example (13) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

79 LCS Example (14) ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

80 LCS Example (15) 3 ABCB BDCAB j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 11/14/2018

81 LCS Algorithm Running Time
LCS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array 11/14/2018

82 How to find actual LCS So far, we have just found the length of LCS, but not LCS itself. We want to modify this algorithm to make it output Longest Common Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=3 2 2 2 3 11/14/2018

83 How to find actual LCS - continued
Remember that So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LCS) When i=0 or j=0 (i.e. we reached the beginning), output remembered letters in reverse order 11/14/2018

84 Finding LCS 3 j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 3
A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 11/14/2018

85 Finding LCS (2) 3 B C B LCS (reversed order): B C B
j i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 B C B LCS (reversed order): B C B (this string turned out to be a palindrome) LCS (straight order): 11/14/2018


Download ppt "Chapter 8 Dynamic Programming."

Similar presentations


Ads by Google