Longest Common Sequence (LCS)A subsequence of a given sequence is just the given sequence with some elements left out. Given two sequences X and Y, we say that the sequence Z is a common sequence of X and Y if Z is a subsequence of both X and Y. In the longest common subsequence problem, we are given two sequences X = (x_{1} x_{2}....x_{m}) and Y = (y_{1} y_{2} y_{n}) and wish to find a maximum length common subsequence of X and Y. LCS Problem can be solved using dynamic programming. Characteristics of Longest Common SequenceA bruteforce approach we find all the subsequences of X and check each subsequence to see if it is also a subsequence of Y, this approach requires exponential time making it impractical for the long sequence. Given a sequence X = (x_{1} x_{2}.....x_{m}) we define the ith prefix of X for i=0, 1, and 2...m as X_{i}= (x_{1} x_{2}.....x_{i}). For example: if X = (A, B, C, B, C, A, B, C) then X_{4}= (A, B, C, B) Optimal Substructure of an LCS: Let X = (x_{1} x_{2}....x_{m}) and Y = (y_{1} y_{2}.....) y_{n}) be the sequences and let Z = (z_{1} z_{2}......z_{k}) be any LCS of X and Y.
Step 2: Recursive Solution: LCS has overlapping subproblems property because to find LCS of X and Y, we may need to find the LCS of X_{m1} and Y_{n1}. If x_{m} ≠ y_{n}, then we must solve two subproblems finding an LCS of X and Y_{n1}.Whenever of these LCS's longer is an LCS of x and y. But each of these subproblems has the subproblems of finding the LCS of X_{m1} and Y_{n1}. Let c [i,j] be the length of LCS of the sequence X_{i}and Y_{j}.If either i=0 and j =0, one of the sequences has length 0, so the LCS has length 0. The optimal substructure of the LCS problem given the recurrence formula Step3: Computing the length of an LCS: let two sequences X = (x_{1} x_{2}.....x_{m}) and Y = (y_{1} y_{2}..... y_{n}) as inputs. It stores the c [i,j] values in the table c [0......m,0..........n].Table b [1..........m, 1..........n] is maintained which help us to construct an optimal solution. c [m, n] contains the length of an LCS of X,Y.
Next TopicLongest Common Sequence Algorithm
