[CLRS] [CH 15.4] Longest common sub-sequence

Source: Internet
Author: User

---restore content starts---

Summary

This paper introduces the concept of the longest common sub-sequence and the method of solving it.

Sub-sequence Concepts

subsequence: A subsequence of a given sequence is the given sequence, minus 0 or more elements. In general, given a sequence x = <x1, x2, ..., xm>, another sequence Z = <z1, Z2, ..., zk> if there is a strictly incrementing subscript sequence for x <i1, I2, ..., ik>, so that all J = 1, 2, ..., k, with Xij = ZJ, then Z is a sub-sequence of x. For example, Z = <b, C, D, B> is a subsequence of X = <a, B, C, B, D, A, b>, corresponding subscript sequence is <2, 3, 5, 7>.

Common subsequence : given two sequences x and z, if Z is both a subsequence of X and a sub-sequence of y, then Z is called a common sub-sequence of x and Y.

Longest common subsequence (Longest Common Substring, LCS): That's the longest common sub-sequence.

In the oldest sequence problem, given two sequences x = <x1, x2, ..., xm> and y = <y1, y2, ..., yn>, want to find the LCS for X and Y. We can use dynamic programming to solve effectively.

Step 1: Describe an LCS

One powerful way to solve the LCS problem is to enumerate all of the X sub-sequences, check for the subsequence of Y, and record the oldest sequence found. For sequence x with M elements, there is a total of 2m subsequence, which is obviously impractical.

However, the problem of LCS has the best sub-structural properties. Given a sequence x = <x1, x2, ..., xm>, for i = 0, 1, ..., m, define an i prefix for x for Xi = <x1, x2, ..., xi>.
For example, if X = <a, B, C, B, D, A, b>, then X4 = <a, B, C, B>, and X0 is an empty sequence.

theorem (optimal substructure of LCS): Set x = <x1, x2, ..., xm> and Y = <y1, y2, ..., yn> for two sequences, and set Z = <z1, Z2, ..., zk> for X and y of any one LCS.
1) If xm = yn, then zk = XM = yn and Zk-1 is an LCS of Xm-1 and Yn-1;
2) if xm! = yn, then zk! = XM implies that Z is an LCS of Xm-1 and Y;
3) if xm! = yn, then zk! = yn contains Z is an LCS of Yn-1 and X.

Proof : What the hell was that?
1) if ZK! = XM, then you can add xm = yn to Z, that is, a common sub-sequence of x and y with a length of k+1, and the LCS of Z is x and y, and thus must have ZK = XM = yn. And at this time the prefix Zk-1 is the Xm-1 and Yn-1 's length for the common subsequence of k-1. Assuming that Xm-1 and Yn-1 have a common sub-sequence w greater than k-1, then Xm = Yn is added to the W to produce a common subsequence with a length greater than k, thus contradicting the LCS Z is x and Y. Evidence.
2) if ZK! = XM then Z is an LCS of Xm-1 and Y. If Xm-1 and y have a common subsequence w with a length greater than K, then W should also be a common sub-sequence of Xm and Y, which contradicts the assumption of Z for the LCS X and Y. Evidence.
3) and proof 2) symmetrical, the evidence.

explanation : The characteristic of this theorem illustrates that an LCS of two sequences also contains an LCS with a prefix of two sequences. This shows that the LCS problem has the best substructure properties.

Step 2: A recursive solution

According to the theorem we know that when looking for X = <x1, x2, ..., xm> and Y = <y1, y2, ..., an LCS for yn>, you may want to check for one or two sub-problems. That
If xm = yn, you must find an LCS for Xm-1 and Yn-1. Add xm = yn to this sub-LCS, producing an LCS for x and y;
If xm! = yn, you must resolve two sub-problems: Find an LCS for Xm-1 and Y, and an LCS for Yn-1 and X. Of these two LCS, the longer one is an LCS of X and Y.

It is easy to see the nature of overlapping sub-problems in LCS. Defines C[I,J] for an LCS length of sequence Xi and Yi. If I=0 or j=0, one of the sequence length is 0, thus the LCS length is 0. The resulting recursive equation:

$\textrm{c}[i,j]= \begin{cases} 0&,\ i = 0\or\ j=0\\ c[i-1,j-1]+1&,\ i,j>0 \or\ x_{i}=y_{i}\\ \textrm{max} (c[i , J-1],c[i-1,j]) &,\ i,j>0 \or\ x_{i}\neq y_{i} \end{cases}$

Step 3: Calculate the length of the LCS

The procedure lcs-length with two sequences X = <x1, x2, ..., xm> and Y = <y1, y2, ..., yn> as input. The run time is O (MN).
It fills the value of c[i,j] into a table c[0..m, 0..N] (for example) in a row-by-line calculation;
It also maintains table B[1..M, 1..N] to simplify the construction of the optimal solution, B[i,j] points to a table term corresponding to the solution of the optimal sub-problem selected in the calculation of c[i,j];
It returns table B and C;c[m,n], which is an LCS length of X and Y.  

lcs-LENGTH (X, Y)//Initializing table Entriesm =Length[x]n=Length[y] fori = (0to M) C[i,0] =0 forj = (0to N) c[0, j] =0//cyclic calculation of LCS length fori = (1to m) forj = (1To N)if(X[i] =Y[j]) c[i,j]= c[i-1, J-1] +1B[i,j]= &b[i-1, J-1]        Else if(c[i-1, j] >= c[i,j-1]) C[i,j]= c[i-1, J] B[i,j]= &b[i-1, J]ElseC[i,j]= c[i,j-1] B[i,j]= &b[i,j-1]//return ResultsreturnB and C
Step 4: Construct an LCS

We can easily construct the LCS of X and y based on the results of table B very quickly. Start with B[m,n] and follow the pointer. Whenever a pointer to the upper left is encountered, it means that xm = yn is an element of the LCS, and the output continues to be traced thereafter. The trace process run time is O (m+n).

Improved code

We can completely get rid of table B, each table item C[i,j] Only depends on another three C's table entries, i.e. c[i-1,j-1], c[i-1,j], c[i,j-1]. Given the value of c[i,j], we can determine in O (1) time which of the three values is used to calculate c[i,j] instead of checking table B. This allows us to refactor an LCS within the same O (m+n) time. This method saves the space of O (MN). But it does not save the running time. Of course, we can be more aggressive, if we do not need to reconstruct the LCS, only ask for results, we just have two rows of data in C can be calculated. Thus further saving space.

[CLRS] [CH 15.4] Longest common sub-sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.