0% found this document useful (0 votes)
46 views

CSE 205 Lab Manual 13 LCS

Uploaded by

freefirevai676
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

CSE 205 Lab Manual 13 LCS

Uploaded by

freefirevai676
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of

Computer Science and Engineering

Title: Implemention of Longest Common


Subsequence (LCS) Algorithm

Algorithms Lab
CSE 206

Green University of Bangladesh


1 Objective(s)
• To learn about Longest Common Subsequence (LCS) algorithm for determining the length of common
subsequences in strings.

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.

2.2 How LCS algorithm works


Let the sequences are X=x1,x2,x3,...,xm and Y=y1,y2,y3,...,ym. We will use the following steps to determine
the length of the longest common subsequence.

• 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.

2.3 Example of LCS algorithm


• Consider these two strings X=BACDB and Y=BDCB to find the longest common subsequence.

• Considering the steps, we need to calculate two tables 1 and 2.


• Given n = length of X, m = length of Y, X = BDCB, Y = BACDB

2.3.1 Constructing the LCS Tables


1. In the table given in figure 1 below, the zeroeth rows and columns are filled with zeroes. The remaining
values are filled by incrementing and choosing the maximum values according to the algorithm.
2. After the values have been filled in, the path is retraced starting at the last value at T[4, 5] in the table.

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

Figure 2: Identifying the longest common subsequence


3 Algorithm
Algorithm 1: LCS Algorithm

1 X and Y be two given sequences


2 m := length(X)
3 n := length(Y)
4 for i = 1 to m do do
5 LCS[i, 0] := 0
6 end
7 for j = 1 to n do do
8 LCS[0, j] := 0
9 end
10 for i = 1 to m do do
11 for j =1 to n do do
12 if X[i] = Y[j] then
13 LCS[i][j] = 1 + LCS[i-1, j-1]
14 end
15 else
16 LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])
17 end
18 end
19 end

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

6.1 Problem analysis


The longest common subsequence problem is a well-known computer science issue that serves as the foundation
for data comparison tools like the diff-utility and has a variety of real-world uses.
• For given two strings X and Y, the longest common subsequence (LCS) problem is to find the longest
subsequence common to both X and Y.
• Has applications to DNA similarity testing (alphabet is A,C,G,T), bioinformatics etc. It is also widely
used by revision control systems, such as SVN and Git to resolve numerous changes made to a collection
of files under revision control.
• Example: ABCDEFG and XZACKDFWGH have ACDFG as a longest common subsequence.
We have two nested loops where the outer one iterates m times and the inner one iterates n times. A
constant amount of work is done inside each iteration of the inner loop. Thus, the total running time is O(nm).
Answer is contained in LCS[m,n] and the subsequence can be recovered from the LCS table.

7 Lab Exercise (Submit as a report)


• Print all the common subsequences according to the descending order of the lengths for two given se-
quences.
• What will be the LCS for the sequences “ABCDEFGH" & “abcdefgh" ?

8 Discussion & Conclusion


Based on the focused objective(s) to understand about the LCS algorithms, the additional lab exercise made
me more confident towards the fulfillment of the objectives(s).

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.

You might also like