You are on page 1of 4

Roll No.

: C052 Name: SHRAVANI SAWEKAR


Class: SE-C Batch: C3
Date of Submission: 11-04-2024 Grade:

EXPERIMENT NO: 07

AIM:
To implement longest common subsequence using Dynamic
ProgrammingApproach.

THEORY:

The Longest Common Subsequence (LCS) problem is a classic computer


scienceproblem where the goal is to find the longest subsequence that is common
to two given sequences. A subsequence is a sequence that appears in the same
relative order but not necessarily contiguous.

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

You might also like