THE LONGEST COMMON
SUBSEQUENCEPROBLEM
SUBJECT: ANALYSIS AND DESIGN OF ALGORITHMS
PRESENTED BY:
HARSH CHAUHAN 230130107017
BHAVY CHAUDHARY 230130107014
PUJAN DAVE 230130107024
2.
What is aSubsequence?
Definition
• A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements
without changing the order of the remaining elements.
• Crucial difference from a Substring: Elements do not have to be contiguous (next to each other).
Examples
• Original Sequence (S): A B C D E F
• Valid Subsequence :
• A C F (Deleted B, D, E)
• B D F
• A B C D E F (Zero deletions)
• C
• Invalid Subsequence: F E (Order changed)
3.
THE LONGEST COMMONSUBSEQUENCE (LCS)
• Definition:
Given two sequences, X and Y, the goal is to find the longest sequence Z that is a subsequence of both X
and Y.
• Goal
• Determine the length of the LCS.
• Construct the LCS itself.
• Example Scenario
• Sequence X: A G G T A B
• Sequence Y: G X T X A Y B
• Common Subsequence include: G T A B, G T B, A B, etc.
• The LCS is: G T A B (Length 4)
4.
APPROACH 1: BRUTEFORCE (WHY IT FAILS)
Method
1. Generate all subsequences of the first sequence, X.
2. Check which of these are also subsequences of the second sequence, Y.
3. Find the longest one among the common subsequences.
Complexity Analysis
• If X has length m, it has 2 m subsequences.
• Time Complexity: O(m 2 m )
⋅
• This approach is exponential (O(2 n )) and becomes infeasible even for small sequences (e.g., m=50).
Key Issue
• Brute force recalculates the common subsequences of the same small sub-problems over and over. This signals a
need for Dynamic Programming.
5.
APPROACH 2: DYNAMICPROGRAMMING
Principle of Optimality
• The LCS problem exhibits Optimal Substructure and Overlapping Subproblems.
• The optimal solution to the overall problem can be constructed from the optimal solutions to its subproblems.
The DP Strategy
• We use a 2D array (or table), let's call it L[m][n], where m= X and n= Y .
∣ ∣ ∣ ∣
• L[i][j] will store the length of the LCS of the prefixes X[1..i] and Y[1..j].
• We fill the table bottom-up, starting with the smallest subproblems.
6.
DP FORMULATION: THERECURRENCE RELATION
Let L(i,j) be the length of LCS of X[1..i] and
Case 1: The characters match (X[i]=Y[j])
• The matching character is part of the LCS.
• The length increases by 1, based on the LCS of the sequences before these characters.
L( i, j) = 1 + L(i-1, j-1)
Case 2: The characters do not match (X[i]!=Y[j])
• We take the maximum length obtained by either
1. Ignoring X[i] (LCS of X[1..i 1] and Y[1..j]).
−
2. Ignoring Y[j] (LCS of X[1..i] and Y[1..j 1]).
−
L( i , j) = max(L(i-1,j), L(I,j-1))
7.
DP Table Illustration(Example)
Sequences
• X: A B C D (m=4)
• Y: A C B D A (n=5)
0 A C B D A
0 0 0 0 0 0 0
A 0 1 1 1 1 1
B 0 1 1 2 2 2
C 0 1 2 2 3 3
D 0 1 2 2 3 3
Result
• The length of the LCS is L(4,5)=3.
8.
CONSTRUCTING THE LCSSEQUENCE
Backtracking
• To reconstruct the sequence, we trace backwards from L[m][n].
• If X[i]=Y[j]:This character is part of the LCS. Prepend X[i] to the result and move diagonally up-left to L[i 1]
−
[j 1].
−
• If X[i]!=Y[j]:
• Move to the cell with the larger value: max(L[i 1][j],L[i][j 1]). (If equal, choose either).
− −
9.
REAL-WORLD APPLICATIONS
1. Bioinformatics(Genomics)
• Problem: Comparing DNA or protein sequences (e.g., comparing two species' genomes).
• Use Case: Measuring similarity to determine evolutionary relationships or function.
2. File Comparison Utilities (diff)
• Problem: Identifying the minimal set of changes (insertions and deletions) needed to transform one
file into another.
• Use Case: Version control (like Git), document comparison tools.
3. Text Editing & Spell Checkers
• Problem: Calculating the "distance" between two strings (a related concept called Edit Distance,
which often uses LCS as a component).
• Use Case: Suggesting corrections for typos.
10.
PERFORMANCE SUMMARY
Algorithm
1. InitializeL[m+1][n+1] table with zeros.
2. Iterate through the table from i=1 to m and j=1 to n.
3. Apply the recurrence relation to fill L[i][j].
4. Backtrack from L[m][n] to reconstruct the sequence.
Time Complexity
• The DP approach involves two nested loops to fill the m × n table.
• Time Complexity: O(m n)
⋅
• m: Length of sequence X
• n: Length of sequence Y
11.
SUMMARY AND CONCLUSION
KeyTakeaways
1. Subsequence vs. Substring: Order must be maintained ; elements don't need to be adjacent.
2. LCS is Foundational: Crucial for text comparison and bioinformatics.
3. Dynamic Programming Solution: The O(m n) DP approach efficiently solves the problem by storing
⋅
and reusing solutions to overlapping
subproblems.