Longest common subsequence
≈ 12 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
A longest common subsequence (LCS) is the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from the longest common substring: unlike substrings, elements of subsequences are not required to occupy consecutive positions within the original sequences. The problem of computing longest common subsequences is a classic computer science problem. Because it is polynomial and has an efficient algorithm to solve it, it is employed to compare data and merge changes to files in programs such as the diff utility and revision control systems such as Git. It has similar applications in computational linguistics and bioinformatics. For example, consider the sequences (ABCD) and (ACBAD). They have five length-2 common subsequences: (AB), (AC), (AD), (BD), and (CD); two length-3 common subsequences: (ABD) and (ACD); and no longer common subsequences. So (ABD) and (ACD) are their longest common subsequences.
A longest common subsequence (LCS) is the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from the longest common substring: unlike substrings, elements of subsequences are not required to occupy consecutive positions within the original sequences. The problem of computing longest common subsequences is a classic computer science problem. Because it is polynomial and has an efficient algorithm to solve it, it is employed to compare data and merge changes to files in programs such as the diff utility and revision control systems such as Git. It has similar applications in computational linguistics and bioinformatics. For example, consider the sequences (ABCD) and (ACBAD). They have five length-2 common subsequences: (AB), (AC), (AD), (BD), and (CD); two length-3 common subsequences: (ABD) and (ACD); and no longer common subsequences. So (ABD) and (ACD) are their longest common subsequences. O\left(N \prod_{i=1}^{N} n_i\right). There exist methods with lower complexity, which often depend on the length of the LCS, the size of the alphabet, or both. The LCS is not necessarily unique; in the worst case, the number of common subsequences is exponential in the lengths of the inputs, so the algorithmic complexity of listing all common subsequences must be at least exponential. The prefix S n of S is defined as the first n characters of S. For example, the prefixes of S = (AGCA) are S 0 = () S 1 = (A) S 2 = (AG) S 3 = (AGC) S 4 = (AGCA). Let LCS(X, Y) be a function that computes a longest subsequence common to X and Y. Such a function has two interesting properties. LCS(X^A,Y^A) = LCS(X,Y)^A, for all strings X, Y and all symbols A, where ^ denotes string concatenation. This allows one to simplify the LCS computation for two sequences ending in the same symbol. For example, LCS("BANANA","ATANA") = LCS("BANAN","ATAN")^"A", Continuing for the remaining common symbols, LCS("BANANA","ATANA") = LCS("BAN","AT")^"ANA". If LCS("ABCDEFG","BCDGK") ends with a "G", then the final "K" cannot be in the LCS, hence LCS("ABCDEFG","BCDGK") = LCS("ABCDEFG","BCDG"). If LCS("ABCDEFG","BCDGK") does not end with a "G", then the final "G" cannot be in the LCS, hence LCS("ABCDEFG","BCDGK") = LCS("ABCDEF","BCDGK"). Let two sequences be defined as follows: X=(x_1 x_2 \cdots x_m) and Y=(y_1 y_2 \cdots y_n) . The prefixes of X are X_0, X_1, X_2, \dots, X_m ; the prefixes of Y are Y_0, Y_1, Y_2, \dots, Y_n . Let \mathit{LCS}(X_i,Y_j) represent the set of longest common subsequence of prefixes X_i and Y_j . This set of sequences is given by the following. \mathit{LCS}(X_i,Y_j)=\begin{cases} \epsilon & \mbox{if }i=0\mbox{ or }j=0 \\ \mathit{LCS}(X_{i-1},Y_{j-1}) \hat{} x_i & \mbox{if }i,j>0\mbox{ and }x_i=y_j \\ \operatorname{\max}\{\mathit{LCS}(X_i,Y_{j-1}),\mathit{LCS}(X_{i-1},Y_j)\} & \mbox{if }i,j>0\mbox{ and }x_i\ne y_j. \end{cases} To find the LCS of X_i and Y_j , compare x_i and y_j . If they are equal, then the sequence \mathit{LCS}(X_{i-1},Y_{j-1}) is extended by that element, x_i . If they are not equal, then the longest among the two sequences, \mathit{LCS}(X_i,Y_{j-1}) , and \mathit{LCS}(X_{i-1},Y_j) , is retained. (If they are the same length, but not identical, then both are retained.) The base case, when either X_i or Y_i is empty, is the empty string, \epsilon . The longest subsequence common to R = (GAC), and C = (AGCAT) will be found. Because the LCS function uses a "zeroth" element, it is convenient to define zero prefixes that are empty for these sequences: R 0 = ε; and C 0 = ε. All the prefixes are placed in a table with C in the first row (making it a c olumn header) and R in the first column (making it a r ow header). This table is used to store the LCS sequence for each step of the calculation. The second column and second row have been filled in with ε, because when an empty sequence is compared with a non-empty sequence, the longest common subsequence is always an empty sequence. LCS(R 1 , C 1 ) is determined by comparing the first elements in each sequence. G and A are not the same, so this LCS gets (using the "second property") the longest of the two sequences, LCS(R 1 , C 0 ) and LCS(R 0 , C 1 ). According to the table, both of these are empty, so LCS(R 1 , C 1 ) is also empty, as shown in the table below. The arrows indicate that the sequence comes from both the cell above, LCS(R 0 , C 1 ) and the cell on the left, LCS(R 1 , C 0 ). LCS(R 1 , C 2 ) is determined by comparing G and G. They match, so G is appended to the upper left sequence, LCS(R 0 , C 1 ), which is (ε), giving (εG), which is (G). For LCS(R 1 , C 3 ), G and C do not match. The sequence above is empty; the one to the left contains one element, G. Selecting the longest of these, LCS(R 1 , C 3 ) is (G). The arrow points to the left, since that is the longest of the two sequences. LCS(R 1 , C 4 ), likewise, is (G). LCS(R 1 , C 5 ), likewise, is (G). For LCS(R 2 , C 1 ), A is compared with A. The two elements match, so A is appended to ε, giving (A). For LCS(R 2 , C 2 ), A and G do not match, so the longest of LCS(R 1 , C 2 ), which is (G), and LCS(R 2 , C 1 ), which is (A), is used. In this case, they each contain one element, so this LCS is given two subsequences: (A) and (G). For LCS(R 2 , C 3 ), A does not match C. LCS(R 2 , C 2 ) contains sequences (A) and (G); LCS(R 1 , C 3 ) is (G), which is already contained in LCS(R 2 , C 2 ). The result is that LCS(R 2 , C 3 ) also contains the two subsequences, (A) and (G). For LCS(R 2 , C 4 ), A matches A, which is appended to the upper left cell, giving (GA). For LCS(R 2 , C 5 ), A does not match T. Comparing the two sequences, (GA) and (G), the longest is (GA), so LCS(R 2 , C 5 ) is (GA). For LCS(R 3 , C 1 ), C and A do not match, so LCS(R 3 , C 1 ) gets the longest of the two sequences, (A). For LCS(R 3 , C 2 ), C and G do not match. Both LCS(R 3 , C 1 ) and LCS(R 2 , C 2 ) have one element. The result is that LCS(R 3 , C 2 ) contains the two subsequences, (A) and (G). For LCS(R 3 , C 3 ), C and C match, so C is appended to LCS(R 2 , C 2 ), which contains the two subsequences, (A) and (G), giving (AC) and (GC). For LCS(R 3 , C 4 ), C and A do not match. Combining LCS(R 3 , C 3 ), which contains (AC) and (GC), and LCS(R 2 , C 4 ), which contains (GA), gives a total of three sequences: (AC), (GC), and (GA). Finally, for LCS(R 3 , C 5 ), C and T do not match. The result is that LCS(R 3 , C 5 ) also contains the three sequences, (AC), (GC), and (GA). The final result is that the last cell contains all the longest subsequences common to (AGCAT) and (GAC); these are (AC), (GC), and (GA). The table also shows the longest common subsequences for every possible pair of prefixes. For example, for (AGC) and (GA), the longest common subsequence are (A) and (G). \left|SCS(X,Y)\right| = n + m - \left|LCS(X,Y)\right|. The edit distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion, is: d'(X,Y) = n + m - 2 \cdot \left|LCS(X,Y)\right|. C = array(0..m, 0..n) for i := 0..m C[i,0] = 0 for j := 0..n C[0,j] = 0 for i := 1..m for j := 1..n if X[i] = Y[j] C[i,j] := C[i-1,j-1] + 1 else C[i,j] := max(C[i,j-1], C[i-1,j]) return C[m,n] Alternatively, memoization could be used. The following function backtracks the choices taken when computing the C table. If the last characters in the prefixes are equal, they must be in an LCS. If not, check what gave the largest LCS of keeping x_i and y_j , and make the same choice. Just choose one if they were equally long. Call the function with i=m and j=n . function backtrack(C[0..m,0..n], X[1..m], Y[1..n], i, j) if i = 0 or j = 0 return "" if X[i] = Y[j] return backtrack(C, X, Y, i-1, j-1) + X[i] if C[i,j-1] > C[i-1,j] return backtrack(C, X, Y, i, j-1) return backtrack(C, X, Y, i-1, j) if i = 0 or j = 0 return {""} if X[i] = Y[j] return {Z + X[i] for all Z in backtrackAll(C, X, Y, i-1, j-1)} R := {} if C[i,j-1] ≥ C[i-1,j] R := backtrackAll(C, X, Y, i, j-1) if C[i-1,j] ≥ C[i,j-1] R := R ∪ backtrackAll(C, X, Y, i-1, j) return R if i >= 0 and j >= 0 and X[i] = Y[j] printDiff(C, X, Y, i-1, j-1) print " " + X[i] else if j > 0 and (i = 0 or C[i,j-1] ≥ C[i-1,j]) printDiff(C, X, Y, i, j-1) print "+ " + Y[j] else if i > 0 and (j = 0 or C[i,j-1] highlighted numbers show the path the function backtrack would follow from the bottom right to the top left corner, when reading out an LCS. If the current symbols in X and Y are equal, they are part of the LCS, and we go both up and left (shown in bold). If not, we go up or left, depending on which cell has a higher number. This corresponds to either taking the LCS between X_{1..i-1} and Y_{1..j} , or X_{1..i} and Y_{1..j-1} . start := 1 m_end := m n_end := n trim off the matching items at the beginning while start ≤ m_end and start ≤ n_end and X[start] = Y[start] start := start + 1 trim off the matching items at the end while start ≤ m_end and start ≤ n_end and X[m_end] = Y[n_end] m_end := m_end - 1 n_end := n_end - 1 C = array(start-1..m_end, start-1..n_end) only loop over the items that have changed for i := start..m_end for j := start..n_end the algorithm continues as before ... In the best-case scenario, a sequence with no changes, this optimization would eliminate the need for the C matrix. In the worst-case scenario, a change to the very first and last items in the sequence, only two additional comparisons are performed.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1375229678 under CC BY-SA 4.0; modifications have been made.