Download - Longest Common Subsequence (LCS) Algorithm
![Page 1: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/1.jpg)
ANALYSIS AND DESIGN OF ALGORITHMS
Prepared By::
Metaliya Darshit (130110107020)
LONGEST COMMON SUBSEQUENCE
![Page 2: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/2.jpg)
CONTEXTIntroduction to LCSConditions for recursive call of LCSExample of LCSAlgorithm
![Page 3: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/3.jpg)
LONGEST COMMON SUBSEQUENCEA subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. In LSC , we have to find Longest common Subsequence that is in same relative order.String of length n has 2^n different possible subsequences.E.g.--Subsequences of “ABCDEFG”. “ABC”, “ABG”, “BDF”, “AEG”, ‘”ACEFG”, …
![Page 4: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/4.jpg)
EXAMPLE LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.
![Page 5: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/5.jpg)
LONGEST COMMON SUBSEQUENCE
Let the input sequences be X[0..m-1] and Y[0..n-1] of lengths m and n respectively. let L(X[0..m-1], Y[0..n-1]) be the length of LCS of the two sequences X and Y. If last characters of both sequences match (or X[m-1] == Y[n-1]) then L(X[0..m-1], Y[0..n-1]) = 1 + L(X[0..m-2], Y[0..n-2]) If last characters of both sequences do not match (or X[m-1] != Y[n-1] then L(X[0..m-1], Y[0..n-1]) = MAX ( L(X[0..m-2], Y[0..n-1]), L(X[0..m-1], Y[0..n-2])
![Page 6: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/6.jpg)
LONGEST COMMON SUBSEQUENCEConsider the input strings “ABCDGH” and “AEDFHR”. Last characters do not match for the strings. So length of LCS can be written as:L(“ABCDGH”, “AEDFHR”) = MAX ( L(“ABCDG”, “AEDFHR”), L(“ABCDGH”, “AEDFH”) )
![Page 7: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/7.jpg)
EXAMPLE Following is a partial recursion tree for input strings “AXYT” and “AYZX”
![Page 8: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/8.jpg)
EXAMPLEX=M,Z,J,A,W,X,UY=X,M,J,Y,A,U,ZAnd Longest Common Subsequence is,LCS(X,Y)=M,J,A,U
Now we will see table from which we can find LCS of Above sequences.
![Page 9: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/9.jpg)
CONDITIONS FOR ARROWS
![Page 10: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/10.jpg)
![Page 11: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/11.jpg)
LCS ALGORITHM
![Page 12: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/12.jpg)
CONSTRUCTING AN LCS
![Page 13: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/13.jpg)
COMPLEXITY Complexity of Longest Common Subsequence is O(mn). Where m and n are lengths of the two Strings.
![Page 14: Longest Common Subsequence (LCS) Algorithm](https://reader035.vdocuments.us/reader035/viewer/2022062311/5871f0b71a28ab5c348b578d/html5/thumbnails/14.jpg)
THANK YOU