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:

  1. Input: Two sequences and of characters.
  2. Output: The maximum length of a common subsequence between and .
  3. Example: For example, if “blurry” and “burger”, the longest common subsequence is “burr”.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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 .