Longest Common Subsequence Problem (LCS)
The Longest Common Subsequence (LCS) problem is a classic Dynamic Programming problem. It involves finding the longest sequence which appears in both given sequences (not necessarily contiguous).
Approach:
- Input: Two sequences and of characters.
- Output: The maximum length of a common subsequence between and .
- Example: For example, if âblurryâ and âburgerâ, the longest common subsequence is âburrâ.
- Idea: The LCS problem can be solved using dynamic programming, typically using a two-dimensional array to store the lengths of the LCS of prefixes of the sequences.
- Recurrence: At each cell of the 2D array (where represents the index of and represents the index of ), you consider three cases:
- If , then the LCS does not include either or , so the length of the LCS up to that point remains the same as it was without including these characters. You can look at the cell or to find the length of the LCS.
- If , then you can extend the LCS by one. In this case, the length of the LCS would be one plus the length of the LCS up to the previous characters, i.e., .
- You take the maximum of these two options.
- Adding 1 in the diagonal arrow: When characters match (case 3), we add one to the LCS length because weâre extending the LCS by one character. However, in cases where characters donât match (cases 1 and 2), we donât add anything because the current characters donât contribute to the LCS. So, we donât add one in those cases.
- Finding the Solution: Once the dynamic programming table is filled, the solution lies in the bottom-right cell of the table, which represents the LCS length.
- Complexity: The time complexity of this approach is , where and are the lengths of sequences and respectively. This is because you fill a 2D array of size .