• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 23, 2024 |580 Views

Longest Common Subsequence (LCS)

  Share   Like
Description
Discussion

Longest Common Subsequence | DP-4

Are you interested in understanding how to solve the Longest Common Subsequence (LCS) problem using dynamic programming? This tutorial will guide you through the problem statement, the dynamic programming approach to solve it, and its applications.

Problem Statement

The Longest Common Subsequence (LCS) problem is a classic problem in computer science. Given two sequences, the task is to find the length of the longest subsequence that is common to both sequences. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively.

Example

Consider the two sequences:

  • X = "AGGTAB"
  • Y = "GXTXAYB"

The LCS of X and Y is "GTAB", and its length is 4.

Dynamic Programming Approach

The dynamic programming (DP) approach to solve the LCS problem involves creating a 2D table to store the lengths of the longest common subsequence between prefixes of the two sequences.

Steps to Solve the Problem

Define the DP Table:

  • Let dp[i][j] represent the length of the LCS of the substrings X[0...i-1] and Y[0...j-1].
  • Initialize a table dp of size (m+1) x (n+1) where m and n are the lengths of sequences X and Y, respectively.

Fill the DP Table:

  • The table is filled using the following recurrence relation: dp[i][j]={0if i=0 or j=0dp[i−1][j−1]+1if X[i−1]=Y[j−1]max⁡(dp[i−1][j],dp[i][j−1])if X[i−1]≠Y[j−1]dp[i][j] = \begin{cases} 0 & \text{if } i = 0 \text{ or } j = 0 \\ dp[i-1][j-1] + 1 & \text{if } X[i-1] = Y[j-1] \\ \max(dp[i-1][j], dp[i][j-1]) & \text{if } X[i-1] \neq Y[j-1] \end{cases}dp[i][j]=⎩⎨⎧​0dp[i−1][j−1]+1max(dp[i−1][j],dp[i][j−1])​if i=0 or j=0if X[i−1]=Y[j−1]if X[i−1]=Y[j−1]​
  • This relation says:
    • If the characters X[i-1] and Y[j-1] match, then the length of LCS is 1 + dp[i-1][j-1].
    • If they do not match, then the length of LCS is the maximum of the LCS excluding the current character from either X or Y.

Result:

  • The value dp[m][n] will contain the length of the longest common subsequence of X and Y.

Example Walkthrough

Consider X = "AGGTAB" and Y = "GXTXAYB".

Step 1: Initialize the DP table:

Step 2: Fill the DP table using the recurrence relation:

Final DP Table:

Result: The length of LCS is dp[6][7] = 4.

Time Complexity

  • The time complexity of this approach is O(m×n)O(m \times n)O(m×n), where m is the length of X and n is the length of Y.
  • The space complexity is also O(m×n)O(m \times n)O(m×n).

Applications of LCS

  • Bioinformatics: LCS is used to find similarities between DNA, RNA, or protein sequences.
  • Version Control: LCS is used in diff tools to find differences between two versions of a file.
  • Text Comparison: LCS helps in finding similarities between different texts or documents.

Conclusion

The Longest Common Subsequence (LCS) problem is a fundamental problem in dynamic programming, with applications in various fields. Understanding the dynamic programming approach to solving the LCS problem is crucial for tackling similar problems that involve finding common patterns or sequences.

For a detailed step-by-step guide, check out the full article: https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/.