M2 Dynamic Programming - LCS Problem
M2 Dynamic Programming - LCS Problem
• 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.
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