dynamic programming _ set 12 (longest palindromic subsequence) - geeksforgeeks.pdf
TRANSCRIPT
-
6/10/2015 DynamicProgramming|Set12(LongestPalindromicSubsequence)GeeksforGeeks
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width% 1/4
DynamicProgramming|Set12(LongestPalindromicSubsequence)
Givenasequence,findthelengthofthelongestpalindromicsubsequenceinit.Forexample,ifthegivensequenceisBBABCBCAB,thentheoutputshouldbe7asBABCBABisthelongestpalindromicsubseuqnceinit.BBBBBandBBCBBarealsopalindromicsubsequencesofthegivensequence,butnotthelongestones.
Thenaivesolutionforthisproblemistogenerateallsubsequencesofthegivensequenceandfindthelongestpalindromicsubsequence.Thissolutionisexponentialintermoftimecomplexity.LetusseehowthisproblempossessesbothimportantpropertiesofaDynamicProgramming(DP)ProblemandcanefficientlysolvedusingDynamicProgramming.
1)OptimalSubstructure:LetX[0..n1]betheinputsequenceoflengthnandL(0,n1)bethelengthofthelongestpalindromicsubsequenceofX[0..n1].
IflastandfirstcharactersofXaresame,thenL(0,n1)=L(1,n2)+2.ElseL(0,n1)=MAX(L(1,n1),L(0,n2)).
Followingisageneralrecursivesolutionwithallcaseshandled.
//Everaysinglecharacterisapalindromoflength1
L(i,i)=1forallindexesiingivensequence
//IFfirstandlastcharactersarenotsame
If(X[i]!=X[j])L(i,j)=max{L(i+1,j),L(i,j1)}
//Ifthereareonly2charactersandbotharesame
Elseif(j==i+1)L(i,j)=2
//Iftherearemorethantwocharacters,andfirstandlast
//charactersaresame
ElseL(i,j)=L(i+1,j1)+2
2)OverlappingSubproblemsFollowingissimplerecursiveimplementationoftheLPSproblem.Theimplementationsimplyfollowstherecursivestructurementionedabove.
#include#include
-
6/10/2015 DynamicProgramming|Set12(LongestPalindromicSubsequence)GeeksforGeeks
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width% 2/4
Output:
ThelnegthoftheLPSis5
Consideringtheaboveimplementation,followingisapartialrecursiontreeforasequenceoflength6withalldifferentcharacters.
L(0,5)
/\
/\
L(1,5)L(0,4)
/\/\
/\/\
L(2,5)L(1,4)L(1,4)L(0,3)
Intheabovepartialrecursiontree,L(1,4)isbeingsolvedtwice.Ifwedrawthecompleterecursiontree,thenwecanseethattherearemanysubproblemswhicharesolvedagainandagain.Sincesamesuproblemsarecalledagain,thisproblemhasOverlappingSubprolemsproperty.SoLPSproblemhasboth
//Autilityfunctiontogetmaxoftwointegersintmax(intx,inty){return(x>y)?x:y;}//Returnsthelengthofthelongestpalindromicsubsequenceinseqintlps(char*seq,inti,intj){//BaseCase1:Ifthereisonly1characterif(i==j)return1;//BaseCase2:Ifthereareonly2charactersandbotharesameif(seq[i]==seq[j]&&i+1==j)return2;//Ifthefirstandlastcharactersmatchif(seq[i]==seq[j])returnlps(seq,i+1,j1)+2;//Ifthefirstandlastcharactersdonotmatchreturnmax(lps(seq,i,j1),lps(seq,i+1,j));}/*Driverprogramtotestabovefunctions*/intmain(){charseq[]="GEEKSFORGEEKS";intn=strlen(seq);printf("ThelnegthoftheLPSis%d",lps(seq,0,n1));getchar();return0;}
-
6/10/2015 DynamicProgramming|Set12(LongestPalindromicSubsequence)GeeksforGeeks
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width% 3/4
properties(seethisandthis)ofadynamicprogrammingproblem.LikeothertypicalDynamicProgramming(DP)problems,recomputationsofsamesubproblemscanbeavoidedbyconstructingatemporaryarrayL[][]inbottomupmanner.
DynamicProgrammingSolution
#include#include//Autilityfunctiontogetmaxoftwointegersintmax(intx,inty){return(x>y)?x:y;}//Returnsthelengthofthelongestpalindromicsubsequenceinseqintlps(char*str){intn=strlen(str);inti,j,cl;intL[n][n];//Createatabletostoreresultsofsubproblems//Stringsoflength1arepalindromeoflentgh1for(i=0;i
-
6/10/2015 DynamicProgramming|Set12(LongestPalindromicSubsequence)GeeksforGeeks
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width% 4/4
Output:
ThelnegthoftheLPSis7
TimeComplexityoftheaboveimplementationisO(n^2)whichismuchbetterthantheworstcasetimecomplexityofNaiveRecursiveimplementation.
ThisproblemisclosetotheLongestCommonSubsequence(LCS)problem.Infact,wecanuseLCSasasubroutinetosolvethisproblem.FollowingisthetwostepsolutionthatusesLCS.1)Reversethegivensequenceandstorethereverseinanotherarraysayrev[0..n1]2)LCSofthegivensequenceandrev[]willbethelongestpalindromicsequence.ThissolutionisalsoaO(n^2)solution.
Pleasewritecommentsifyoufindanythingincorrect,oryouwanttosharemoreinformationaboutthetopicdiscussedabove.
References:http://users.eecs.northwestern.edu/~dda902/336/hw6sol.pdf
return0;}