Characterize the lcs problem the lcs problem has an

• No School
• AA 1
• 34

This preview shows page 27 - 34 out of 34 pages.

Characterize the LCS problem The LCS problem has an optimal substructure property. Can we formulate optimal substructure? ith preifx of a sequence X is X i , e.g., X 4 of sequence X = <A, B, C, B, D, A, B> is <A, B, C, B>
Longest Common Subsequence (LCS) Problem Dynamic Programming Approach Optimal Substructure of an LCS: Let X = <x 1 , x 2 , x 3 ,…..,x m > and Y = <y 1 , y 2 , y 3 ,…..,y n > and let Z = <z 1 , z 2 , z 3 ,….z k > be any LCS of X and Y 1. 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 . 2. If x m y n then z k x m implies that Z is an LCS of X m-1 and Y. 3. If x m y n then z k y n implies that Z is an LCS of X and Y n-1
Longest Common Subsequence (LCS) Problem Dynamic Programming Approach Step 1: Characterizing an LCS LCS problem has an optimal substructure property. I.e. There are either one or two subproblems to examine when finding an LCS of X = <x 1 , x 2 ,…..,x m >, Y = <y 1 ,y 2 ,…..,y n > if x m = y n find an LCS of X m-1 and Y n-1 appending x m and y n (x m = y n ) to this LCS yields an LCS of X and Y. If x m y n solve two subproblems: I. Finding an LCS of Xm-1 and Y II. Finding an LCS of X and Yn-1 Longer of I and II is an LCS of X and Y. This shows overlapping subproblmes property in LCS.
Step II:Define a recursive solution Establish a recurrence for the cost of an optimal solution. Let c[i, j] to be the length of the longest subsequences (LCS) of Xi and Yj. 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, j-1], c[i-1, j]) if i, j > 0 and x i y j ¨ Write a recursive algorithm to compute the length of an LCS of two sequences? Longest Common Subsequence (LCS) Problem Dynamic Programming Approach
Step III:Devise an algorithm to compute the length of an LCS and then construct the LCS. Development of an algorithm Input two sequences X and Y and a two dimensional table c[0..m, 0..n] According to the definition for(i = 1; I <= m; I++) { for(j = 1; j <= n; j++) { if (X[i] = = Y[j]) c[i, j] = c[i-1, j-1] + 1 else if (c[i-1, j] >= c[i, j-1]) c[i, j] = c[i-1, j]; else c[i, j] = c[i, j-1]; } } /* It means to compute c[i, j] We need to know c[i-1, j-1], c[i, j-1] and c[i-1, j] */
7 1 2 3 4 5 6 i 0 j 0 1 2 3 4 5 6 y j B D C A B A x i A B C B D A B 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 2 2 0 1 1 2 2 2 2 0 1 1 2 2 3 3 0 1 2 2 2 3 3 0 1 2 2 3 3 4 0 1 2 2 3 4 4 Longest Common Subsequence (LCS) Problem Dynamic Programming Approach
LCS – Algorithm: Computing the length LCS-Length(X, Y) { m = length[X]; n = length[Y]; for(i = 1; i <= m; i++) c[i, 0] = 0; for(j = 0; j <= n; j++) c[0, j] = 0; for(i = 1; i <= m; I++) { for(j = 1; j <= n; j++) { if(X[i] = = Y[i] { c[i, j] = c[i-1, j-1] + 1; b[I, j] = “ } else if(c[i-1, j] >= c[i, j-1]) { c[i, j] = c[i-1, j]; b[I, j] = “ } else { c[i, j] = c[i, j-1]; b[I, j] = “ } } } return c and b; } Complexity: O(m*n)
LCS – Algorithm: Constructing an LCS Table b[1..m, 1..n] returned by LCS-length() can be used to

You've reached the end of your free preview.

Want to read all 34 pages?

• Fall '19

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern