Chapter 8 Dynamic Programming.

Slides:



Advertisements
Similar presentations
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
Advertisements

Overview What is Dynamic Programming? A Sequence of 4 Steps
Algorithms Dynamic programming Longest Common Subsequence.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Data Structures Lecture 10 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
CS 8833 Algorithms Algorithms Dynamic Programming.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Advanced Algorithms Analysis and Design
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Matrix Chain Multiplication
Least common subsequence:
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Matrix Chain Multiplication
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Chapter 8 Dynamic Programming
CS 3343: Analysis of Algorithms
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
CS6045: Advanced Algorithms
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Longest Common Subsequence
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Lecture 8. Paradigm #6 Dynamic Programming
Ch. 15: Dynamic Programming Ming-Te Chi
Dynamic Programming-- Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Dynamic Programming II DP over Intervals
Matrix Chain Product 張智星 (Roger Jang)
Algorithms and Data Structures Lecture X
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Longest common subsequence (LCS)
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Analysis of Algorithms CS 477/677
Matrix Chain Multiplication
Longest Common Subsequence
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Chapter 8 Dynamic Programming

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

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

Warshall’s Algo for finding Transitive Closure

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.

Brute Force Technique for BST

Optimal BST

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:-

 

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?

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

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

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))

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…

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 = 10000 + 400 = 10400. Consider computing (AB)C: # multiplications for (AB) = 2x10x50 = 1000, creating a 2x50 answer matrix # multiplications for (AB)C = 2x50x20 = 2000, Total multiplications = 1000 + 2000 = 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??

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.

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.

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…

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!

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.

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).

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.

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.

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

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

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

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

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

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

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

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

LCS Example (0) ABCB BDCAB X = ABCB; m = |X| = 4 j 0 1 2 3 4 5 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

LCS Example (1) ABCB BDCAB for i = 1 to m c[i,0] = 0 j 0 1 2 3 4 5 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Finding LCS (2) 3 B C B LCS (reversed order): B C B 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 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