You are on page 1of 11

LONGEST COMMON

SUBSEQUENCE(LCS)

Presented by
PRINCE KUMAR
MT/CS/10005/18
• The longest common subsequence (LCS)
problem is the problem of finding the
longest subsequence common to all
sequences in a set of sequences (often
just two sequences).

• It differs from the longest common


substring problem: unlike substrings,
subsequences are not required to
occupy consecutive positions within the
original sequences
• The LCS Problem says Given two different string
find longest set of character coming in sequence
from both string. .

String 1: a b c d e f g h i j
String 2: f c d g i
LCS -> c d g i
LCS IS IMPLEMENTED BY TWO
WAYS:-

1: LCS USING RECUSRION

2: LCS USING DYNAMIC


PROGRAMMING
LCS USING RECURSION
Int LCS(i,j)
b d /0
A 0 1 2 {
If (A[i]==‘/0’||B[j]==‘/0’)
Return 0;
B a
0
b c d /0
1 2 3 4
else if (A[i]==B[j]){
Return 1+LCS(i+1,j+1);
Else
Return max(LCS(i+1,j),LCS(i,j+1));
}

LCS Using Recursion takes 2n+m.


LCS USING DYNAMIC PROGRAMMING
In LCS using dynamic programming a table is
Used.
Dynamic programming follows Botom up
Approach but table is filled from Top down
Approach.

ALGO:
if (A[i]==B[j])
LCS (i,j)=1+LCS[i-1,j-1]
else
LCS[i,j]=max(LCS[i-1,j],LCS[i,j-1])
A b d /0
0 1 2
a b c d
B a b c d /0 0 1 2 3 4
0 1 2 3 4
0 0 0 0 0 0

1 0 0 1 1 1
b
d 2 0 0 1 1 2

Starting row and column will be 0.


Start from 0th row and 0th row column.
If match occur then add 1 from previous horizontal value.
If match not occur then take maximum value from previous
row and previous column.
Size of the longest common subsequence 2.
To find what is the longest common subsequence.
a b c d
0 1 2 3 4

0 0 0 0 0 0

b 1 0 0 1 1 1

d 2 0 0 1 1 2

List down the diagonal character and ie longest common


subsequence.
Time Complexity-> O(mn)
Application:-
DNA Matching –
In biological applications, we often want to
compare the DNA of two (or more) different
organisms.For example, the DNA of one organism
may be S1 = AGCCTCAGT while the DNA of another
organism may be S2 = ATCCT S2 =AGTAGC . One goal
of comparing two strands of DNA is to determine
how “similar” the two strands are, as some measure
of how cosely related the two organisms are.
Here S1 and S3 are more similar.
Edit Distance –
Edit distance all about finding the smallest number of point
mutations needed to change one sequence into another.
In standard Edit Distance where we are allowed operations,
insert, delete and replace.
Consider a variation of edit distance where we are allowed
Only two operations insert and delete, find edit distance in
this variation.
-> Input : str1 = "cat", st2 = "cut" Output : 2
We are allowed to insert and delete. We delete 'a' from
"cat“ and insert "u" to make it "cut".
-> Input : str1 = "acb", st2 = "ab" Output : 1
We can convert "acb" to "ab" by removing 'c'.

You might also like