Algorithms / Longest common subsequence

Longest common subsequence

How to find the longest run of letters two words share in the same order, gaps allowed: the match-or-not rule, the table it fills, the walk back, and why diff tools use it.

Intermediate7 steps~7 min
First word — PLANE, down the side of the table
Second word — PANEL, along the top
Cell — LCS length of two beginnings
Result — letters kept from both words
// THE SHORT VERSION

In 30 seconds

A subsequence is what remains of a word after crossing out some letters without reordering the rest. Two texts can share a long subsequence even when no long block of them is identical.

To find the longest one, compare the last letters. If they match, count one and shorten both words; if not, shorten one or the other and keep the better result.

Writing those small answers in a table gives the length, and walking back through it gives the letters. Diff tools do this with lines to show what changed between two files.

Longest common subsequence, step by step

STEPS01 / 07

Two words can have several longest common subsequences of the same length; the walk back finds one. Real diff tools use faster variants of this idea (such as Myers’ algorithm).

STEP 01 / 07 Subsequence, not substring
THE MATH
PLANE subsequence: P A E substring: L A N
  1. Step 1: Subsequence, not substring. Cross out any letters of a word and read what is left, in order: that is a subsequence. PAE is one for PLANE, even though its letters are not next to each other. A substring is stricter: a block of neighbours, like LAN. Every substring is a subsequence, but not the other way round.
  2. Step 2: The longest one they share. Given two words, the longest common subsequence (LCS) is the longest string that is a subsequence of both. Join equal letters with lines that never cross, and count the lines. PLANE and PANEL share PANE, four letters; joining the two Ls as well would cross the other lines, which means breaking the order.
  3. Step 3: Compare the last letters. Trying every subsequence is hopeless: a word of n letters has up to 2 to the power n of them. Instead look only at the last letters. If they are equal, that letter ends the common part: count 1 and solve the rest. If not, at least one of them is unused: drop each in turn and keep the better result.
  4. Step 4: Fill the table: a match. Store every small answer in a table. A cell holds the LCS length of the letters of PLANE down to its row and the letters of PANEL up to its column; the blank row and column stand for an empty word and are all 0. Where the row and column letters are equal, the cell is the diagonal neighbour plus 1.
  5. Step 5: Fill the table: no match. Where the letters differ, copy the larger of the cell above (the row letter dropped) and the cell to the left (the column letter dropped). The last cell compares the whole words: E and L differ, so it takes the 4 on its left. The LCS has length 4, found by filling 36 cells, each from its neighbours.
  6. Step 6: Walk back to read it. The table holds lengths; the letters come from walking back from the last cell. Where the row and column letters match, keep that letter and step diagonally. Otherwise move to the larger neighbour, above or left. The matches come out last letter first: E, N, A, P. Reversed, that is PANE.
  7. Step 7: This is how diff works. A diff tool does this with lines of text instead of letters. It finds a longest common subsequence of the old and new file: those lines are unchanged. Old lines outside it are shown as removed, new lines outside it as added. Because the LCS is as long as possible, the list of changes is as short as possible.
VALUES SO FAR
XPLANEinput
Y····input
LCS····goal
equal····rule
differ····rule
table····size
length····result
letters····reversed
diff····changes

Spotted a mistake on this page?