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

M2 Dynamic Programming - LCS Problem

The document discusses the Longest Common Subsequence (LCS) problem, which involves finding the length of the longest subsequence present in two sequences. It outlines various methods to solve the problem, including brute force, recursion, memorization, and dynamic programming, highlighting their time complexities. The document emphasizes that dynamic programming offers a more efficient solution with a time complexity of O(mn).

Uploaded by

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

M2 Dynamic Programming - LCS Problem

The document discusses the Longest Common Subsequence (LCS) problem, which involves finding the length of the longest subsequence present in two sequences. It outlines various methods to solve the problem, including brute force, recursion, memorization, and dynamic programming, highlighting their time complexities. The document emphasizes that dynamic programming offers a more efficient solution with a time complexity of O(mn).

Uploaded by

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

Longest Common Subsequence

Tuesday, January 3, 2023 6:49 PM

• LCS Definition:
• LCS Problem Statement: Given two sequences, find the length of longest subsequence
present in both of them. A subsequence is a sequence that appears in the same relative
order, but not necessarily contiguous.
• For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”.
• Methods:
1. Brute Force Approach
2. Using Recursion
3. Using Memorization
4. Using Dynamic Programming
Brute Force Approach:
• In order to find out the complexity of brute force approach, we need to first know the
number of possible different subsequences of a string with length n, i.e., find the number
of subsequences with lengths ranging from 1,2,..n-1.
• Recall from theory of permutation and combination that number of combinations with 1
element are nC1.
• Number of combinations with 2 elements are nC2 and so forth and so on.
• We know that nC0 + nC1 + nC2 + … nCn = 2n. So a string of length n has 2n-1 different
possible subsequences since we do not consider the subsequence with length 0.
• This implies that the time complexity of the brute force approach will be O(n * 2n).
Note that it takes O(n) time to check if a subsequence is common to both the strings.
This time complexity can be improved using dynamic programming.
Recursion:
LCS(i, j)
IF (A[i] and B[i] == '\0')
RETURN 0;
ELSE IF ( A[i] == B[j])
RETURN 1+ LCS(i+1, j+1);
ELSE
RETURN MAX{ LCS(i-1,j), LCS(i,j-1)} ;
Endfunction
• Time complexity of the above naive recursive approach is O(2n) in worst case and worst
case happens when all characters of X and Y mismatch i.e., length of LCS is 0.
Memorization:
• Time Complexity : O(m*n) ignoring recursion stack space, Auxiliary Space: O(m*n).
Dynamic Programming:
• Time Complexity of the above implementation is O(mn) which is much better than the
worst-case time complexity of Naive Recursive implementation.

Dynamic Programming Page 1


Dynamic Programming Page 2
IF ( A[i] == B[j])
LCS(i, j)= 1+ LCS(i+1, j+1);
ELSE
LCS(i, j)= MAX{ LCS(i-1,j), LCS(i,j-1)} ;

Dynamic Programming Page 3


B A G T \0
0 1 2 3 4 5
0 0 0 0 0 0 0
A 1 0
T 2 0
G 3 0
\0 4 0

B A G T \0
0 1 2 3 4 5
0 0 0 0 0 0 0
A 1 0
T 2 0
G 3 0
\0 4 0

Dynamic Programming Page 4

You might also like