CSE 205 Lab Manual 13 LCS
CSE 205 Lab Manual 13 LCS
Algorithms Lab
CSE 206
2 Problem Analysis
2.1 Longest Common Subsequence (LCS) Algorithm
A subsequence is a group of sequences that appear in the same relative order, whether they are adjacent or not.
The longest subsequence that is shared by all the given sequences, is known as the longest common subsequence
(LCS), assuming that the elements of the subsequence are not needed to occupy consecutive position within the
original sequences.
• Create an empty adjacency table with the dimensions n m, where n is the length of the X sequence and
m is the length of the Y sequence. The elements in sequence X are represented by the table’s rows, and
the elements in sequence Y are represented by the table’s columns.
• Rows and columns starting at zero must contain only zeros. And the remaining values are filled in based
on different cases, by maintaining a counter value.
• The number is increased by one if it comes across a common element in both the X and Y sequences.
• To fill in T[i, j] if the counter does not pass into any common elements in the X and Y sequences, choose
the biggest value between T[i-1, j] and T[i, j-1].
• Once the table is filled, backtrack from the last value in the table. Backtracking here is done by tracing
the path where the counter incremented first.
• The longest common subsequence which can be determined by observing the path’s elements.
3. It is possible to figure out the longest common subsequence from the observed path By selecting the values
where the counter is first incremented.
4. Throughout this scenerio, the number is increased three times, from B to C to B, so the final count is 3.
As a result, BCB is the longest common subsequence of the sequences X and Y. The final table is depicted
in figure-2
Figure 1: Constructing the LCS table
4 Implementation in Java
1 import java.util.*;
2 import java.lang.*;
3 public class LCS {
4
5 public static int lcs(char X[], char Y[], int m ,int n) {
6 // The zeroeth rows and columns are filled with zeroes
7 if(m == 0 || n == 0) {
8 return 0;
9 }
10 // The remaining values are filled by incrementing and choosing the
maximum values according to the algorithm.
11 if(X[m−1] == Y[n−1]) {
12 return 1 + lcs(X,Y,m−1,n−1);
13 } else {
14 return max(lcs(X, Y, m, n−1), lcs(X,Y, m−1,n));
15 }
16 }
17
18 // Finding the maximum between lcs(X, Y, m, n−1), lcs(X,Y, m−1,n)
19 static int max(int l1, int l2) {
20 if(l1<l2) {
21 return l2;
22 } else {
23 return l1;
24 }
25 }
26
27 public static void main(String[] args) {
28 LCS lcs = new LCS();
29 String X, Y;
30 X = "BDCB"; // Sequence 1
31 Y = "BACDB"; // Sequence 2
32 char arr1[] = X.toCharArray();
33 char arr2[] = Y.toCharArray();
34 // Finding the length of two sequences
35 int len1 = arr1.length;
36 int len2 = arr2.length;
37 // Printing the length of the longest common subsequence
38 System.out.print("Length of LCS is: " + lcs(arr1, arr2, len1, len2));
39 }
40 }
5 Sample Input/Output
Length of LCS is: 3
6 Lab Task (Please implement yourself and show the output to the
instructor)
1. Write a Program in java to print the length of second longest subsequence.
2. Write a Program in java to print the longest common subsequence using LCS algorithm
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.