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

9457Lab Manual Expt No. 7 AOA - Longest Common Subsequence

Uploaded by

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

9457Lab Manual Expt No. 7 AOA - Longest Common Subsequence

Uploaded by

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

PART A

(PART A: TO BE REFFERED BY STUDENTS)

Experiment No.07
A.1 Aim:
Write a program to implement longest common subsequence using Dynamic
Programming Approach.

A.2 Prerequisite:

A.3 Outcome:

After successful completion of this experiment students will be able to solve a problem
by applying dynamic programming approach and analyze the complexity of the problem.

A.4 Theory:
Given two strings: string S of length n, and string T of length m. Goal is to produce their
longestcommon subsequence: the longest sequence of characters that appear left-to-right,
but notnecessarily in a contiguous block, in both strings.For example, consider: S =
ABAZDC and T = BACBAD.The LCS has length 4 and is the string ABAD. This type of
problem comes up in genomics: given two DNA fragments, the LCS gives information
about what they have in common and the best way to line them up.

Let A = <a1, a2, a3,…an> and B = <b1, b2, b3,…bn> be two strings over alphabets. Then B
is a subsequence of A if B can be generated by striking out some elements from A.

If A = {a, b, a, c, b, c, b} and B = {a, b, c, b, b, a, c}, then the sequence {a, c}, {a, b, c},
{a, c, c}, {a, b, c, b} etc are common subsequences of A and B, but {a, b, c, b, a} is not.
{a, b, c, b, a} is a subsequence of B but it is not a subsequence of A.

Let two strings Am and Bn of length m and n respectively. If thelast character of both the
strings is same i.e. am = bn, then the length of LCS is incremented by one and length of
both strings is reduced by one. The problem is to to find LCS of strings Am-1 and Bn-1.

If am ≠ bn, following two problems are considered.

- Apply LCS on strings Am-1 and B


- Apply LCS on strings A and Bn-1

Select the result which gives longest subsequence.

Thus optimal substructure of LCS problem is defined as,


Algorithm:

Example:
Let A be “XMJYAUZ” and B be “MZJAWXU”. The longest common subsequence
between A and B is “MJAU”. The tableshown below, which is generated by the
function LCSLength, shows the lengths of the longest common subsequences between
A and B. The ith row and jth column shows the length of the LCS between A and
B.The highlighted numbers show the path that would follow from the bottom right to the
top left corner, when reading out an LCS. If the current symbols in A and B are equal,
they are the part of LCS.

Time Complexity:
In dynamic programming, the only table of size m × n is filled by using two nested for
loops. So running time of this algorithm in O(mn).
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No.: A05 Name: PRASHANTH NAIDU


Class: SE -A (COMPS) Batch: A1
Date of Experiment: Date of Submission: 25/03/24
22/03/24
Grade:

B.1 Software Code written by student:

#include <stdio.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

void lcs(char *X, char *Y, int m, int n) {


int L[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = MAX(L[i - 1][j], L[i][j - 1]);
}
}

int index = L[m][n];

char lcs[index + 1];


lcs[index] = '\0';

i = m;
j = n;
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcs[index - 1] = X[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
printf("Longest Common Subsequence: %s\n", lcs);
}

int main() {
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";
int m = strlen(X);
int n = strlen(Y);
lcs(X, Y, m, n);
return 0;
}

B.2 Input and Output:


B.3 Conclusion:
The C program efficiently computes the Longest Common Subsequence (LCS)
using dynamic programming. By constructing a table to store LCS lengths of
all substring pairs and tracing back to find the LCS itself, the algorithm
accurately determines the longest common subsequence between two given
strings. This approach offers a systematic and efficient method for solving the
LCS problem in C programming.

B.4 Question of Curiosity


Q1: What are the characteristics of dynamic
programming?
Q2: Why is Brute Force approach not efficient to solve
LCS?
Q3: Compare Brute Force approach and Dynamics
Programming approach.
Q4: Explain application areas of LCS.

************************

You might also like