1 / 7

Chapter 15: Dynamic Programming

Chapter 15: Dynamic Programming. Matrix-chain multiplication. Compute the product of n matrices A1,A2,..,An Where to put the parentheses (A1( A2 (A3 A4))) (A1(( A2 A3) A4)) ((A1 A2) (A3 A4)) ((A1 (A2 A3)) A4) (((A1 A2) A3) A4) P(n) - How many parenthesizations ? 1 if n=1

rachel
Télécharger la présentation

Chapter 15: Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 15:Dynamic Programming

  2. Matrix-chain multiplication • Compute the product of n matrices A1,A2,..,An • Where to put the parentheses (A1( A2 (A3 A4))) (A1(( A2 A3) A4)) ((A1 A2) (A3 A4)) ((A1 (A2 A3)) A4) (((A1 A2) A3) A4) P(n) - How many parenthesizations? 1 if n=1 P(n) = or P(1) P(n-1) + P(2) P(n-2) + … + P(n-1)P(1) Known as Catalan numbers. Grow as OMEGA(4^n / n^{3/2} ) Observation Suppose that an optimal parenthesization splits between A_k and A_{k+1}. Then parenthesization of A_1 through A_k must be optimal and so is for A_{k+1} through A_n The fact that this type of observation is correct is crucial for the ability to use dynamic programming

  3. Recursive presentation of solution 0 if i=j Min[i,j] = or min {m[i,k] + m[k+1,j] + p_{i-1} * p_k * p_j} if i< j { i <= k < j} Understanding the complexity: How many pairs i < j for a given difference k (=j-i) Zoom on n/4 < k < 3n/4 For a given k, O(n) pairs each requiring min of O(n) values Since the number of values of k is O(n) the complexity is O(n^3)

  4. Matrix-chain multiplication # of operations Where to split A1 30X35 A2 35X15 A3 15X5 A4 5X10 A5 10X20 A6 20X25 6 matrices :in which order to multiply m[2,5] is the minimum of 3 candidates: m[2,2] + m[3,5] +p1p2p5 = 0+2500+35X15X20=13000 m[2,3] + m[4,5] + p1p3p5 = 2625+1000+35X5X20=7125 m[2,4] + m[5,5] + p1p4p5 =4375+0+35X10X20=11375 =7125

  5. Longest common subsequence (LCS) Z = {B,C,D,B} is a subsequence of X = {A,B,C,B,D,A,B} Given two sequences X and Y, and a subsequence Z, Z is a common subsequence of X and Y if it is a subsequence of X and a subsequence of Y Given two sequences X and Y, the LCS problem is to find the maximum-length common subsequence of X and Y

  6. Theorem 15.1: Optimal substructure of an LCS Let X = <x_1,x_2,..,x_m> and Y=<y_1,y_2,..,y_n> and Z=<z_1,z_2,..,z_k> be sequences, where Z is any LCS of X and Y. Then: • If x_m=y_n then z_k= x_m=y_n and Z_{k-1} is an LCS of X_{m-1} and Y_{n-1} If x_m != y_n then 2. z_k != X_m implies that Z is an LCS of X_{m-1} and Y 3. z_k != Y_n implies that Z is an LCS of X and Y_{n-1} This structure implies the following Recursive solution: The LCS-length of <x_1,..x_i> and <y_1,..,y_j> is: 0 if i=0 or j=0 c[i,j] = c[i-1,j-1] + 1 if i,j >0 and x_i = y_j max(c[i-1,j],c[i,j-1]) if i,j >0 and x_i != y_j

  7. LCS-length table X = <A,B,C,B,D,A,B>, Y = <B,D,C,A,B,A> Complexity O(nm) Correctness By induction on i+j. Assume: correct for all i+j < k Prove: for every i+j = k.

More Related