Professional Documents
Culture Documents
EXPERIMENT NO: 07
AIM:
To implement longest common subsequence using Dynamic
ProgrammingApproach.
THEORY:
ALGORITHM:
1. Create a 2D array L of size (m+1) x (n+1), where m and n are the lengths
ofsequences X and Y respectively. Initialize all values to 0.
2. Iterate through the sequences X and Y:
3. If the characters at indices i in X and j in Y match (X[i-1] == Y[j-1]),
incrementL[i][j] by 1 and set L[i][j] equal to L[i-1][j-1] + 1.
4. Otherwise, set L[i][j] equal to the maximum of L[i-1][j] and L[I][j-1].
5. The value L[m][n] will contain the length of the LCS.
INPUT/OUTPUT :
OBSERVATION AND CONCLUSION:
OBSERVATION:
1. The time complexity of the dynamic programming approach for LCS is O(m
*n), where m and n are the lengths of sequences X and Y respectively.
2. This algorithm ef ciently finds the length of the LCS and can optionally
reconstruct the LCS string.
3. It's particularly useful in bioinformatics for sequence alignment
andcomparison tasks.
CONCLUSION:
1. The dynamic programming approach ef ciently solves the Longest
CommonSubsequence (LCS) problem.
2. It finds the length of the LCS in O(m * n) time complexity.
3. Additionally, it can reconstruct the LCS string if required.
4. This algorithm is widely used in various applications, including
bioinformatics,version control systems, and text comparison tasks.
f
f