You are on page 1of 5

6/10/2015

DynamicProgramming|Set4(LongestCommonSubsequence)GeeksforGeeks

DynamicProgramming|Set4
(LongestCommonSubsequence)
WehavediscussedOverlappingSubproblemsandOptimalSubstructure
propertiesinSet1andSet2respectively.Wealsodiscussedoneexample
probleminSet3.LetusdiscussLongestCommonSubsequence(LCS)
problemasonemoreexampleproblemthatcanbesolvedusingDynamic
Programming.
LCSProblemStatement:Giventwosequences,findthelengthoflongest
subsequencepresentinbothofthem.Asubsequenceisasequencethat
appearsinthesamerelativeorder,butnotnecessarilycontiguous.For
example,abc,abg,bdf,aeg,acefg,..etcaresubsequencesof
abcdefg.Soastringoflengthnhas2^ndifferentpossiblesubsequences.
Itisaclassiccomputerscienceproblem,thebasisofdiff(afilecomparison
programthatoutputsthedifferencesbetweentwofiles),andhasapplicationsin
bioinformatics.
Examples:
LCSforinputSequencesABCDGHandAEDFHRisADHoflength3.
LCSforinputSequencesAGGTABandGXTXAYBisGTABoflength4.
Thenaivesolutionforthisproblemistogenerateallsubsequencesofboth
givensequencesandfindthelongestmatchingsubsequence.Thissolutionis
exponentialintermoftimecomplexity.Letusseehowthisproblempossesses
bothimportantpropertiesofaDynamicProgramming(DP)Problem.
1)OptimalSubstructure:
LettheinputsequencesbeX[0..m1]andY[0..n1]oflengthsmandn
respectively.AndletL(X[0..m1],Y[0..n1])bethelengthofLCSofthetwo
sequencesXandY.FollowingistherecursivedefinitionofL(X[0..m1],Y[0..n
1]).
Iflastcharactersofbothsequencesmatch(orX[m1]==Y[n1])then
L(X[0..m1],Y[0..n1])=1+L(X[0..m2],Y[0..n2])

data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20wid

1/5

6/10/2015

DynamicProgramming|Set4(LongestCommonSubsequence)GeeksforGeeks

Iflastcharactersofbothsequencesdonotmatch(orX[m1]!=Y[n1])then
L(X[0..m1],Y[0..n1])=MAX(L(X[0..m2],Y[0..n1]),L(X[0..m1],Y[0..n2])
Examples:
1)ConsidertheinputstringsAGGTABandGXTXAYB.Lastcharacters
matchforthestrings.SolengthofLCScanbewrittenas:
L(AGGTAB,GXTXAYB)=1+L(AGGTA,GXTXAY)
2)ConsidertheinputstringsABCDGHandAEDFHR.Lastcharactersdonot
matchforthestrings.SolengthofLCScanbewrittenas:
L(ABCDGH,AEDFHR)=MAX(L(ABCDG,AEDFHR),L(ABCDGH,
AEDFH))
SotheLCSproblemhasoptimalsubstructurepropertyasthemainproblemcan
besolvedusingsolutionstosubproblems.
2)OverlappingSubproblems:
FollowingissimplerecursiveimplementationoftheLCSproblem.The
implementationsimplyfollowstherecursivestructurementionedabove.
/*ANaiverecursiveimplementationofLCSproblem*/
#include<stdio.h>
#include<stdlib.h>

intmax(inta,intb);

/*ReturnslengthofLCSforX[0..m1],Y[0..n1]*/
intlcs(char*X,char*Y,intm,intn)
{
if(m==0||n==0)
return0;
if(X[m1]==Y[n1])
return1+lcs(X,Y,m1,n1);
else
returnmax(lcs(X,Y,m,n1),lcs(X,Y,m1,n));
}

/*Utilityfunctiontogetmaxof2integers*/
intmax(inta,intb)
{
return(a>b)?a:b;
}

data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20wid

2/5

6/10/2015

DynamicProgramming|Set4(LongestCommonSubsequence)GeeksforGeeks

/*Driverprogramtotestabovefunction*/
intmain()
{
charX[]="AGGTAB";
charY[]="GXTXAYB";

intm=strlen(X);
intn=strlen(Y);

printf("LengthofLCSis%d\n",lcs(X,Y,m,n));

getchar();
return0;
}
TimecomplexityoftheabovenaiverecursiveapproachisO(2^n)inworstcase
andworstcasehappenswhenallcharactersofXandYmismatchi.e.,lengthof
LCSis0.
Consideringtheaboveimplementation,followingisapartialrecursiontreefor
inputstringsAXYTandAYZX
lcs("AXYT","AYZX")
/\
lcs("AXY","AYZX")lcs("AXYT","AYZ")
/\/\
lcs("AX","AYZX")lcs("AXY","AYZ")lcs("AXY","AYZ")lcs("AXYT","AY")

Intheabovepartialrecursiontree,lcs(AXY,AYZ)isbeingsolvedtwice.Ifwe
drawthecompleterecursiontree,thenwecanseethattherearemany
subproblemswhicharesolvedagainandagain.Sothisproblemhas
OverlappingSubstructurepropertyandrecomputationofsamesubproblems
canbeavoidedbyeitherusingMemoizationorTabulation.Followingisa
tabulatedimplementationfortheLCSproblem.
/*DynamicProgrammingimplementationofLCSproblem*/
#include<stdio.h>
#include<stdlib.h>

intmax(inta,intb);

/*ReturnslengthofLCSforX[0..m1],Y[0..n1]*/
intlcs(char*X,char*Y,intm,intn)
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20wid

3/5

6/10/2015

DynamicProgramming|Set4(LongestCommonSubsequence)GeeksforGeeks

{
intL[m+1][n+1];
inti,j;

/*FollowingstepsbuildL[m+1][n+1]inbottomupfashion.Note
thatL[i][j]containslengthofLCSofX[0..i1]andY[0..j1]
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
if(i==0||j==0)
L[i][j]=0;

elseif(X[i1]==Y[j1])
L[i][j]=L[i1][j1]+1;

else
L[i][j]=max(L[i1][j],L[i][j1]);
}
}

/*L[m][n]containslengthofLCSforX[0..n1]andY[0..m1]*/
returnL[m][n];
}

/*Utilityfunctiontogetmaxof2integers*/
intmax(inta,intb)
{
return(a>b)?a:b;
}

/*Driverprogramtotestabovefunction*/
intmain()
{
charX[]="AGGTAB";
charY[]="GXTXAYB";

intm=strlen(X);
intn=strlen(Y);

printf("LengthofLCSis%d\n",lcs(X,Y,m,n));

getchar();
return0;
}
TimeComplexityoftheaboveimplementationisO(mn)whichismuchbetter
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20wid

4/5

6/10/2015

DynamicProgramming|Set4(LongestCommonSubsequence)GeeksforGeeks

thantheworstcasetimecomplexityofNaiveRecursiveimplementation.
Theabovealgorithm/codereturnsonlylengthofLCS.Pleaseseethefollowing
postforprintingtheLCS.
PrintingLongestCommonSubsequence
Pleasewritecommentsifyoufindanythingincorrect,oryouwanttosharemore
informationaboutthetopicdiscussedabove.
References:
http://www.youtube.com/watch?v=V5hZoJ6uKs
http://www.algorithmist.com/index.php/Longest_Common_Subsequence
http://www.ics.uci.edu/~eppstein/161/960229.html
http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20wid

5/5

You might also like