## Longest Common Subsequence

### June 9, 2009

Finding the longest common subsequence of two sequences is a classic computer science problem with an equally classic solution that dates to the folklore of computing. The longest common subsequence is the longest set of elements that appear in order (not necessarily contiguous) in two sequences; for instance, the longest common subsequence of PROGRAMMING and PRAXIS is PRAI:

`P R O G R A M M I N G`

| | | |

P R A X I S

The classic solution uses dynamic programming. A matrix is prepared with one sequence in the rows and the other sequence in the columns, giving in each cell the minimum *edit distance* between the two, where the edit distance is the least number of adds, deletes and changes that can convert one sequence to the other. For instance:

` P R O G R A M M I N G`

0 0 0 0 0 0 0 0 0 0 0 0

P 0 1 1 1 1 1 1 1 1 1 1 1

R 0 1 2 2 2 2 2 2 2 2 2 2

A 0 1 2 2 2 2 3 3 3 3 3 3

X 0 1 2 2 2 2 3 3 3 3 3 3

I 0 1 2 2 2 2 3 3 3 4 4 4

S 0 1 2 2 2 2 3 3 3 4 4 4

If we represent the rows as *X _{i}* and the columns as

*Y*, the matrix can be filled top-to-bottom, left-to-right using the formula:

_{j}` { 0, if i=0 or j=0`

{

LCS(i,j) = { LCS(i-1,j-1) + 1, if X(i) = Y(j)

{

{ max(LCS(i-1,j), LCS(i,j-1)), otherwise

Intuitively, the two sequences are scanned in parallel. If the current two cells are identical, the length of the longest common subsequence increases by one; otherwise, there are two possibilities to consider recursively, after deleting the current cell from either one input sequence or the other, and the length of the longest common subsequence is simply the greater of the two possibilities.

Once the matrix of longest common subsequence lengths has been calculated, the longest common subsequence itself can be recovered by noting each point where the length “bumps” to the next lower value along the diagonal, starting at the lower right-hand corner: from 4 to 3 when both sequences are at I, from 3 to 2 when both sequences are at A, from 2 to 1 when both sequences are at R, and from 1 to 0 when both sequences are at P.

Note that the longest common sequence is not necessarily unique. For instance, given the two sequences ABC and BAC, there are two longest common subsequences, AC and BC. Either answer is correct.

Your task is to write a function that takes two sequences and returns their longest common subsequence. When you are finished, you are welcome to read or run a suggested solution, or to post your solution or discuss the exercise in the comments below.

Pages: 1 2

My Haskell solution (see http://bonsaicode.wordpress.com/2009/06/09/programming-praxis-longest-common-subsequence/ for a version with comments):

import Data.Array

at :: Int -> Int -> Array Int (Array Int e) -> e

at x y a = a ! y ! x

lcs :: Eq a => [a] -> [a] -> [a]

lcs xs ys = reverse $ walk imax jmax where

imax = length xs

jmax = length ys

ax = listArray (1, imax) xs

ay = listArray (1, jmax) ys

ls = listArray (0, jmax) [listArray (0, imax)

[lcs’ i j | i <- [0..imax]] | j <- [0..jmax]] lcs' 0 _ = 0 lcs' _ 0 = 0 lcs' i j | ax ! i == ay ! j = 1 + at (i-1) (j-1) ls | otherwise = max (at (i-1) j ls) (at i (j-1) ls) walk 0 _ = [] walk _ 0 = [] walk i j | at (i-1) j ls == at i j ls = walk (i-1) j | at i (j-1) ls < at i j ls = ax ! i : walk i (j-1) | otherwise = walk i (j-1) main :: IO () main = print $ lcs "programming" "praxis" [/sourcecode]

[…] Praxis – Longest Common Subsequence By Remco Niemeijer Today’s Programming Praxis problem is about finding the longest common subsequence of two sequences. Our […]

My Python version. Incorporates a couple optimizations:

1. identifies matching sequences at start and end of the two input sequences, and only runs LCS algorithm on the middle portions of the input sequences.

2. a row of the matrix in the LCS algorithm only depends on the previous row, so only keep one previous row instead of entire matrix.

The LCS algorithm accumulates the indices of the members of the LCS. The reduce call at the end, assembles the LCS from the indices. The result has the same type (i.e., string, list, tuple) as seq1.

Ooooops… memory management bits me in the backside.

Lines 6 & 7 should read

7 #define element(M, i, j) (*((M)->elt + \

8 ((M)->width * (i) + (j))))

Nasty bug there, the sizeof(int) in the macro I’d initially written was completely uncalled for: gcc knows perfectly well already that (M)->elt is an int*. As a result, my code was writing the matrix to an area 4 times the size of what I’d malloc’d in init_matrix.

I’ve worked on a generic version of this in preparation for the 08 June 2010 exercise. I’ve packed all the generic functions in a str_util.c file. The headers:

the lcs_util.c file itself:

…and (finally) the string-specific bits:

[…] Longest Common Subsequence Finding the longest common subsequence of two sequences is a classic computer science problem with an equally classic solution that dates to the folklore of computing. The longest common subsequence is the longest set of elements that appear in order (not necessarily contiguous) in two sequences, the solution can be used in matching DNA sequences and it is also the basis of Unix “DIFF” utility. […]

my implementation in c

#include

int max(int a, int b){

return a>b?a:b;

}

void printResult( char * str1 , char * str2 ){

int len1=0,len2=0;

int i,j,count=1;

int a[20][20];

// find out the lengths

while(str1[len1++] != ” );

while(str2[len2++] != ” );

len1–;

len2–;

// populate the matrix

for ( i=0 ; i<=len2 ; i++ )

for ( j=0 ; j<=len1 ; j++ ){

if ( i==0 || j==0 ){

a[i][j] = 0;

continue;

}

if ( str1[j-1] == str2[i-1] )

a[i][j]=a[i-1][j-1] +1;

else

a[i][j]=max(a[i-1][j],a[i][j-1]);

}

// print the answer

for ( i=0 ; i<len1 ; i++ ){

if ( a[len2][i+1] == count ){

printf("%c",str1[i]);

count++;

}

}

printf("\n");

}

void main(){

int n,i;

char a[10][10];

char b[10][10];

// scan the number of elements

scanf("%d",&n);

// scan elements

for ( i=0 ; i<n ; i++ ){

scanf("%s",a[i]);

scanf("%s",b[i]);

}

for ( i=0 ; i<n ; i++ )

printResult(a[i],b[i]);

}