Longest Common Subsequence�
Subsequence
Amity School of Engineering & Technology
Common Subsequence�
Suppose, X and Y are two sequences over a finite set of elements. We can say that Z is a common subsequence of X and Y, if Z is a subsequence of both X and Y.
Longest Common Subsequence:-
Amity School of Engineering & Technology
Steps
The following steps are followed for finding the longest common subsequence in string X and Y.
1. Create a table of dimension n+1*m+1 where n and m are the lengths of X and Y respectively. The first row and the first column are filled with zeros.�
Amity School of Engineering & Technology
2. Fill each cell of the table using the following logic
Amity School of Engineering & Technology
3. Step 2 is repeated until the table is filled.
4. The value in the last row and the last column is the length of the longest common subsequence.
5.In order to find the longest common subsequence, start from the last element and follow the direction of the arrow. The elements corresponding to diagonal arrow form the longest common subsequence.
Amity School of Engineering & Technology
Example�
X = BACDB
Y = BDCB
Amity School of Engineering & Technology
Answer
Amity School of Engineering & Technology
Problem
X = ACADB
Y = CBDA
Amity School of Engineering & Technology
Answer
Amity School of Engineering & Technology