
Alongest common subsequence (LCS) is the longestsubsequence common to all sequences in a set of sequences (often just two sequences). It differs from thelongest common substring: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences. The problem of computing longest common subsequences is a classiccomputer science problem. Because it ispolynomial and has an efficient algorithm to solve it, it is employed tocompare data andmerge changes to files in programs such as thediff utility andrevision control systems such asGit. It has similar applications incomputational linguistics andbioinformatics.
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.
For the general case of an arbitrary number of input sequences, the problem isNP-hard.[1] When the number of sequences is constant, the problem is solvable in polynomial time bydynamic programming.
Given sequences of lengths, a naive search would test each of the subsequences of the first sequence to determine whether they are also subsequences of the remaining sequences; each subsequence may be tested in time linear in the lengths of the remaining sequences, so the time for this algorithm would be
For the case of two sequences ofn andm elements, the running time of the dynamic programming approach isO(n ×m).[2] For an arbitrary number of input sequences, the dynamic programming approach gives a solution in
There exist methods with lower complexity,[3]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 listingall common subsequences must be at least exponential.[4]
The LCS problem has anoptimal substructure: the problem can be broken down into smaller, simpler subproblems, which can, in turn, be broken down into simpler subproblems, and so on, until, finally, the solution becomes trivial. LCS in particular hasoverlapping subproblems: the solutions to high-level subproblems often reuse solutions to lower level subproblems. Problems with these two properties are amenable todynamic programming approaches, in which subproblem solutions arememoized, that is, the solutions of subproblems are saved for reuse.
The prefixSn ofS is defined as the firstn characters ofS.[5] For example, the prefixes ofS = (AGCA) are
LetLCS(X,Y) be a function that computes a longest subsequence common toX andY. Such a function has two interesting properties.
LCS(X^A,Y^A) =LCS(X,Y)^A, for all stringsX,Y and all symbolsA, where ^ denotes string concatenation. This allows one to simplify theLCS 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".
IfA andB are distinct symbols (A≠B), thenLCS(X^A,Y^B) is one of the maximal-length strings in the set {LCS(X^A,Y),LCS(X,Y^B) }, for all stringsX,Y.
For example,LCS("ABCDEFG","BCDGK") is the longest string amongLCS("ABCDEFG","BCDG") andLCS("ABCDEF","BCDGK"); if both happened to be of equal length, one of them could be chosen arbitrarily.
To realize the property, distinguish two cases:
Let two sequences be defined as follows: and. The prefixes of are; the prefixes of are. Let represent the set of longest common subsequence of prefixes and. This set of sequences is given by the following.
To find the LCS of and, compare and. If they are equal, then the sequence is extended by that element,. If they are not equal, then the longest among the two sequences,, and, is retained. (If they are the same length, but not identical, then both are retained.) The base case, when either or is empty, is theempty string,.
The longest subsequence common toR = (GAC), andC = (AGCAT) will be found. Because theLCS function uses a "zeroth" element, it is convenient to define zero prefixes that are empty for these sequences:R0 = ε; andC0 = ε. All the prefixes are placed in a table withC in the first row (making it acolumn header) andR in the first column (making it arow header).
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | ε | ε | ε | ε | ε | ε |
| G | ε | |||||
| A | ε | |||||
| C | ε |
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(R1,C1) 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(R1,C0) andLCS(R0,C1). According to the table, both of these are empty, soLCS(R1,C1) is also empty, as shown in the table below. The arrows indicate that the sequence comes from both the cell above,LCS(R0,C1) and the cell on the left,LCS(R1,C0).
LCS(R1,C2) is determined by comparing G and G. They match, so G is appended to the upper left sequence,LCS(R0,C1), which is (ε), giving (εG), which is (G).
ForLCS(R1,C3), 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(R1,C3) is (G). The arrow points to the left, since that is the longest of the two sequences.
LCS(R1,C4), likewise, is (G).
LCS(R1,C5), likewise, is (G).
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | ε | ε | ε | ε | ε | ε |
| G | ε | ε | (G) | (G) | (G) | (G) |
| A | ε | |||||
| C | ε |
ForLCS(R2,C1), A is compared with A. The two elements match, so A is appended to ε, giving (A).
ForLCS(R2,C2), A and G do not match, so the longest ofLCS(R1,C2), which is (G), andLCS(R2,C1), which is (A), is used. In this case, they each contain one element, so this LCS is given two subsequences: (A) and (G).
ForLCS(R2,C3), A does not match C.LCS(R2,C2) contains sequences (A) and (G); LCS(R1,C3) is (G), which is already contained inLCS(R2,C2). The result is thatLCS(R2,C3) also contains the two subsequences, (A) and (G).
ForLCS(R2,C4), A matches A, which is appended to the upper left cell, giving (GA).
ForLCS(R2,C5), A does not match T. Comparing the two sequences, (GA) and (G), the longest is (GA), soLCS(R2,C5) is (GA).
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | ε | ε | ε | ε | ε | ε |
| G | ε | ε | (G) | (G) | (G) | (G) |
| A | ε | (A) | (A) & (G) | (A) & (G) | (GA) | (GA) |
| C | ε |
ForLCS(R3,C1), C and A do not match, soLCS(R3,C1) gets the longest of the two sequences, (A).
ForLCS(R3,C2), C and G do not match. BothLCS(R3,C1) andLCS(R2,C2) have one element. The result is thatLCS(R3,C2) contains the two subsequences, (A) and (G).
ForLCS(R3,C3), C and C match, so C is appended toLCS(R2,C2), which contains the two subsequences, (A) and (G), giving (AC) and (GC).
ForLCS(R3,C4), C and A do not match. CombiningLCS(R3,C3), which contains (AC) and (GC), andLCS(R2,C4), which contains (GA), gives a total of three sequences: (AC), (GC), and (GA).
Finally, forLCS(R3,C5), C and T do not match. The result is thatLCS(R3,C5) also contains the three sequences, (AC), (GC), and (GA).
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | ε | ε | ε | ε | ε | ε |
| G | ε | ε | (G) | (G) | (G) | (G) |
| A | ε | (A) | (A) & (G) | (A) & (G) | (GA) | (GA) |
| C | ε | (A) | (A) & (G) | (AC) & (GC) | (AC) & (GC) & (GA) | (AC) & (GC) & (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).
Calculating the LCS of a row of the LCS table requires only the solutions to the current row and the previous row. Still, for long sequences, these sequences can get numerous and long, requiring a lot of storage space. Storage space can be saved by saving not the actual subsequences, but the length of the subsequence and the direction of the arrows, as in the table below.
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | 0 | 0 | 0 | 0 | 0 | 0 |
| G | 0 | 0 | 1 | 1 | 1 | 1 |
| A | 0 | 1 | 1 | 1 | 2 | 2 |
| C | 0 | 1 | 1 | 2 | 2 | 2 |
The actual subsequences are deduced in a "traceback" procedure that follows the arrows backwards, starting from the last cell in the table. When the length decreases, the sequences must have had a common element. Several paths are possible when two arrows are shown in a cell. Below is the table for such an analysis, with numbers colored in cells where the length is about to decrease. The bold numbers trace out the sequence, (GA).[6]
| ε | A | G | C | A | T | |
|---|---|---|---|---|---|---|
| ε | 0 | 0 | 0 | 0 | 0 | 0 |
| G | 0 | 0 | 1 | 1 | 1 | 1 |
| A | 0 | 1 | 1 | 1 | 2 | 2 |
| C | 0 | 1 | 1 | 2 | 2 | 2 |
For two strings and, the length of theshortest common supersequence is related to the length of the LCS by[3]
Theedit 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:
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(March 2013) (Learn how and when to remove this message) |
The function below takes as input sequencesX[1..m] andY[1..n], computes the LCS betweenX[1..i] andY[1..j] for all1 ≤ i ≤ m and1 ≤ j ≤ n, and stores it inC[i,j].C[m,n] will contain the length of the LCS ofX andY.[7]
function LCSLength(X[1..m], Y[1..n]) C = array(0..m, 0..n)for i := 0..m C[i,0] = 0for j := 0..n C[0,j] = 0for i := 1..mfor j := 1..nif X[i] = Y[j] C[i,j] := C[i-1,j-1] + 1else C[i,j] := max(C[i,j-1], C[i-1,j])return C[m,n]
Alternatively,memoization could be used.
The following functionbacktracks the choices taken when computing theC 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 and, and make the same choice. Just choose one if they were equally long. Call the function withi=m andj=n.
function backtrack(C[0..m,0..n], X[1..m], Y[1..n], i, j)if i = 0or j = 0return ""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 choosing and would give an equally long result, read out both resulting subsequences. This is returned as a set by this function. Notice that this function is not polynomial, as it might branch in almost every step if the strings are similar.
function backtrackAll(C[0..m,0..n], X[1..m], Y[1..n], i, j)if i = 0or j = 0return {""}if X[i] = Y[j]return {Z + X[i]for all Zin 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 RThis function will backtrack through the C matrix, and print thediff between the two sequences. Notice that you will get a different answer if you exchange≥ and<, with> and≤ below.
function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)if i >= 0and j >= 0and X[i] = Y[j] printDiff(C, X, Y, i-1, j-1) print " " + X[i]else if j > 0and (i = 0or C[i,j-1] ≥ C[i-1,j]) printDiff(C, X, Y, i, j-1) print "+ " + Y[j]else if i > 0and (j = 0or C[i,j-1] < C[i-1,j]) printDiff(C, X, Y, i-1, j) print "- " + X[i]else print ""
Let be "XMJYAUZ" and be "MZJAWXU". The longest common subsequence between and is "MJAU". The tableC shown below, which is generated by the functionLCSLength, shows the lengths of the longest common subsequences between prefixes of and. Theth row andth column shows the length of the LCS between and.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ||
|---|---|---|---|---|---|---|---|---|---|
| ε | M | Z | J | A | W | X | U | ||
| 0 | ε | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | X | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 2 | M | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 3 | J | 0 | 1 | 1 | 2 | 2 | 2 | 2 | 2 |
| 4 | Y | 0 | 1 | 1 | 2 | 2 | 2 | 2 | 2 |
| 5 | A | 0 | 1 | 1 | 2 | 3 | 3 | 3 | 3 |
| 6 | U | 0 | 1 | 1 | 2 | 3 | 3 | 3 | 4 |
| 7 | Z | 0 | 1 | 2 | 2 | 3 | 3 | 3 | 4 |
Thehighlighted numbers show the path the functionbacktrack would follow from the bottom right to the top left corner, when reading out an LCS. If the current symbols in and are equal, they are part of the LCS, and we go both up and left (shown inbold). If not, we go up or left, depending on which cell has a higher number. This corresponds to either taking the LCS between and, or and.
Several optimizations can be made to the algorithm above to speed it up for real-world cases.
The C matrix in the naive algorithmgrows quadratically with the lengths of the sequences. For two 100-item sequences, a 10,000-item matrix would be needed, and 10,000 comparisons would need to be done. In most real-world cases, especially source code diffs and patches, the beginnings and ends of files rarely change, and almost certainly not both at the same time. If only a few items have changed in the middle of the sequence, the beginning and end can be eliminated. This reduces not only the memory requirements for the matrix, but also the number of comparisons that must be done.
function LCS(X[1..m], Y[1..n]) start := 1 m_end := m n_end := ntrim off the matching items at the beginningwhile start ≤ m_endand start ≤ n_endand X[start] = Y[start] start := start + 1trim off the matching items at the endwhile start ≤ m_endand start ≤ n_endand 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 changedfor i := start..m_endfor j := start..n_endthe 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.
Most of the time taken by the naive algorithm is spent performing comparisons between items in the sequences. For textual sequences such as source code, you want to view lines as the sequence elements instead of single characters. This can mean comparisons of relatively long strings for each step in the algorithm. Two optimizations can be made that can help to reduce the time these comparisons consume.
Ahash function orchecksum can be used to reduce the size of the strings in the sequences. That is, for source code where the average line is 60 or more characters long, the hash or checksum for that line might be only 8 to 40 characters long. Additionally, the randomized nature of hashes and checksums would guarantee that comparisons would short-circuit faster, as lines of source code will rarely be changed at the beginning.
There are three primary drawbacks to this optimization. First, an amount of time needs to be spent beforehand to precompute the hashes for the two sequences. Second, additional memory needs to be allocated for the new hashed sequences. However, in comparison to the naive algorithm used here, both of these drawbacks are relatively minimal.
The third drawback is that ofcollisions. Since the checksum or hash is not guaranteed to be unique, there is a small chance that two different items could be reduced to the same hash. This is unlikely in source code, but it is possible. A cryptographic hash would therefore be far better suited for this optimization, as its entropy is going to be significantly greater than that of a simple checksum. However, the benefits may not be worth the setup and computational requirements of a cryptographic hash for small sequence lengths.
If only the length of the LCS is required, the matrix can be reduced to a matrix, or to a vector as the dynamic programming approach requires only the current and previous columns of the matrix.Hirschberg's algorithm allows the construction of the optimal sequence itself in the same quadratic time and linear space bounds.[8]
Chowdhury and Ramachandran devised a quadratic-time linear-space algorithm[9][10] for finding the LCS length along with an optimal sequence which runs faster than Hirschberg's algorithm in practice due to its superior cache performance.[9] The algorithm has an asymptotically optimal cache complexity under theIdeal cache model.[11] Interestingly, the algorithm itself iscache-oblivious[11] meaning that it does not make any choices based on the cache parameters (e.g., cache size and cache line size) of the machine.
Several algorithms exist that run faster than the presented dynamic programming approach. One of them isHunt–Szymanski algorithm, which typically runs in time (for), where is the number of matches between the two sequences.[12] For problems with a bounded alphabet size, theMethod of Four Russians can be used to reduce the running time of the dynamic programming algorithm by a logarithmic factor.[13]
Beginning withChvátal & Sankoff (1975),[14] a number of researchers have investigated the behavior of the longest common subsequence length when the two given strings are drawn randomly from the same alphabet. When the alphabet size is constant, the expected length of the LCS is proportional to the length of the two strings, and the constants of proportionality (depending on alphabet size) are known as theChvátal–Sankoff constants. Their exact values are not known, but upper and lower bounds on their values have been proven,[15] and it is known that they grow inversely proportionally to the square root of the alphabet size.[16] Simplified mathematical models of the longest common subsequence problem have been shown to be controlled by theTracy–Widom distribution.[17]
For decades, it had been considered folklore that the longest palindromic subsequence of a string could be computed by finding the longest common subsequence between the string and its reversal, using the classical dynamic programming approach introduced by Wagner and Fischer. However, a formal proof of the correctness of this method was only established in 2024 by Brodal, Fagerberg, and Moldrup Rysgaard.[18]
{{cite book}}: CS1 maint: multiple names: authors list (link)