written 6.9 years ago by | modified 2.6 years ago by |
Find longest subsequence of given two steps
X = {B A T A}
Y = {T A T A}
written 6.9 years ago by | modified 2.6 years ago by |
Find longest subsequence of given two steps
X = {B A T A}
Y = {T A T A}
written 2.6 years ago by |
Steps involved in finding the Longest Common Subsequence (LCS) -
Step 1 - Create a table with dimension $[x+1*y+1]$ where x and y represent the lengths of strings X and Y respectively.
Step 2 - The first row and the first column are always filled with zeros.
Step 3 - Fill the entire table based on any of the following conditions:
IF Condition -
ELSE Condition -
Step 4 - Repeat step 3 until the entire table is filled.
Step 5 - The value in the last row and the last column represent the length of the longest common subsequence.
Step 6 - To find the longest common subsequence, start from the last element and follow the direction of the arrow.
Let's find out the longest common subsequence for the given strings step by step.
String X = BATA
String Y = TATA
Step 1 –
Create a table with dimension $[4 + 1 * 4 + 1] = [5 * 5]$
Because strings X and Y both have the same length of 5.
Step 2 –
Make the first row and the first column filled with zeros and called as Row -1 and Col -1.
The resultant table looks as follows:
Step 3 –
Row 0 Computation
In Row 0 & Col 0 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value and showing an arrow pointing to the previous row cell.
In Row 0 & Col 1 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value and showing an arrow pointing to the previous row cell.
In Row 0 & Col 2 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (0 + 1 = 1) and show an arrow pointing to the diagonal cell.
In Row 0 & Col 3 characters are NOT MATCHED. Therefore, fill the cell by taking the previous column value (MAX) and showing an arrow pointing to the previous column cell.
Step 4 –
Repeat step 3 until the entire table is filled.
Therefore,
Row 1 Computation
In Row 1 & Col 0 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value and showing an arrow pointing to the previous row cell.
In Row 1 & Col 1 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (0 + 1 = 1) and show an arrow pointing to the diagonal cell.
In Row 1 & Col 2 characters are NOT MATCHED. Therefore, fill the cell by taking the previous column value and showing an arrow pointing to the previous column cell.
In Row 1 & Col 3 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (1 + 1 = 2) and show an arrow pointing to the diagonal cell.
Row 2 Computation
In Row 2 & Col 0 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value and showing an arrow pointing to the previous row cell.
In Row 2 & Col 1 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value (MAX) and showing an arrow pointing to the previous row cell.
In Row 2 & Col 2 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (1 + 1 = 2) and show an arrow pointing to the diagonal cell.
In Row 2 & Col 3 characters are NOT MATCHED. Therefore, fill the cell by taking the previous column value and showing an arrow pointing to the previous column cell.
Row 3 Computation
In Row 3 & Col 0 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value and showing an arrow pointing to the previous row cell.
In Row 3 & Col 1 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (0 + 1 = 1) and show an arrow pointing to the diagonal cell.
In Row 3 & Col 2 characters are NOT MATCHED. Therefore, fill the cell by taking the previous row value (MAX) and showing an arrow pointing to the previous row cell.
In Row 3 & Col 3 characters are MATCHED. Therefore, fill the cell by adding 1 to the value of the diagonal element (2 + 1 = 3) and show an arrow pointing to the diagonal cell.
Step 5 –
The value in the last row and the last column represent the length of the longest common subsequence.
Therefore,
The length of the longest common subsequence for the given strings X & Y is 3.
Step 6 –
In order to find the longest common subsequence, start from the last element and follow the direction of the arrow and finally select the cells with diagonal arrows in the path.