9457Lab Manual Expt No. 7 AOA - Longest Common Subsequence
9457Lab Manual Expt No. 7 AOA - Longest Common Subsequence
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.
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)
#include <stdio.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
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;
}
************************