SlideShare a Scribd company logo
5
Most read
13
Most read
15
Most read
Longest Common Subsequence
Subsequences
• A subsequence is a sequence that appears in the same
relative order, but not necessarily contiguous.
• In LCS ,we have to find Longest Common
Subsequence that is in the same relative order.
• String of length n has 2^n different possible
subsequences.
• E.g.—
• Subsequences of “ABCDEFG”.
• “ABC”,”ABG”,”BDF”,”AEG”,’ACEFG”,…….
2
Common Subsequences
Suppose that X and Y are two sequences
over a set S.
X: ABCBDAB
Y: BDCABA
Z: BCBA
We say that Z is a common subsequence
of X and Y if and only if
• Z is a subsequence of X
• Z is a subsequence of Y 3
The Longest Common
Subsequence Problem
Given two sequences X and Y over a set S,
the longest common subsequence problem
asks to find a common subsequence of X
and Y that is of maximal length.
Z= (B,C,A) Length 3
Z= (B,C,A,B) Length 4
Z= (B,D,A,B) Length 4
4
Longest
LCS Notation
Let X and Y be sequences.
We denote by LCS(X, Y) the set of
longest common subsequences of X and
Y.
LCS(X,Y)
Functional notation,
but not a function
5
6
A Poor Approach to the LCS
Problem
• A Brute-force solution:
• Enumerate all subsequences of X
• Test which ones are also subsequences of Y
• Pick the longest one.
• Analysis:
• If X is of length n, then it has 2n
subsequences
• This is an exponential-time algorithm!
Dynamic Programming
Let us try to develop a dynamic
programming solution to the LCS problem.
7
Optimal Substructure
Let X = ( x1,x2,…,xm)
and Y = ( y1,y2,…,yn) be two sequences.
Let Z = ( z1,z2,…,zk) is any LCS of X and Y.
a) If xm = yn then certainly xm = yn = zk
and Zk-1 is in LCS(Xm-1 , Yn-1)
8
Optimal Substructure (2)
Let X = (x1,x2,…,xm)
and Y = ( y1,y2,…,yn) be two sequences.
Let Z = ( z1,z2,…,zk) is any LCS of X and Y.
b) If xm =! yn then xm =! zk implies that Z is
in LCS(Xm-1 , Y)
c)If xm =! yn then yn =! zk implies that Z is
in LCS(X, Yn-1)
9
Recursive Solution
Let X and Y be sequences.
Let c[i,j] be the length of an element in LCS(Xi, Yj).
c[i,j] =
10
Dynamic Programming Solution
• Define L[i,j] to be the length of the longest common
subsequence of X[0..i] and Y[0..j].
• L[i,j-1] = 0 and L[i-1,j]=0, to indicate that the null
part of X or Y has no match with the other.
• Then we can define L[i,j] in the general case as
follows:
1. If xi=yj, then L[i,j] = L[i-1,j-1] + 1 (we can add this
match)
2. If xi≠yj, then L[i,j] = max{L[i-1,j], L[i,j-1]} (we
have no match here)
X:ABCB
Y:BDCA LCS:BC 11
Dynamic Programming Solution (2)
How can we get an actual longest common
subsequence?
Store in addition to the array c an array
b pointing to the optimal subproblem
chosen when computing c[i,j].
12
13
Example
T yj B D C A
Xi 0 0 0 0 0
A 0 0 0 0 1
B 0 1 1 1 1
C 0 1 1 2 2
B 0 1 1 2 2
Start at b[m,n]. Follow the arrows. Each
diagonal array gives one element of the LCS.
14
ALGORITHM
LCS(X,Y)
m ← length[X]
n ← length[Y]
for i ← 1 to m do
c[i,0] ← 0
for j ← 1 to n do
c[0,j] ← 0
15
LCS(X,Y)
for i ← 1 to m do
for j ← 1 to n do
if xi
= yj
c[i, j] ← c[i-1, j-1]+1
b[i, j] ← “D”
else
if c[i-1, j] ≥ c[i, j-1]
c[i, j] ← c[i-1, j]
b[i, j] ← “U”
else
c[i, j] ← c[i, j-1]
b[i, j] ← “L”
return c and b
CONSTRUCTING AN LCS
• PRINT-LCS(b, X, i, j)
1. If i=0 or j=0
2. then return
3.If b[i, j]=“D”
4. then PRINT- LCS(b,X,i-1,j-1)
5. print xi
6.Else if b[i, j]=“U”
7. then PRINT-LCS(b,X,i-1,j)
8.Else PRINT-LCS(b,X,i,j-1) 16
Analysis of LCS Algorithm
• We have two nested loops
• The outer one iterates n times
• The inner one iterates m times
• A constant amount of work is done inside
each iteration of the inner loop
• Thus, the total running time is O(nm)
• Answer is contained in L[n,m] (and the
subsequence can be recovered from the
T table).
17
usage
• Biological applications often need to
compare the DNA of two (or more)
different organisms.
• We can say that two DNA strands are
similar if one is a substring of the
other.
18

More Related Content

PPTX
Longest Common Subsequence
PPTX
Dynamic Programming
PPTX
Longest Common Subsequence
DOC
Unit 3 daa
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Brute force method
PPTX
unit-4-dynamic programming
PPT
Introduction to Design Algorithm And Analysis.ppt
Longest Common Subsequence
Dynamic Programming
Longest Common Subsequence
Unit 3 daa
NON-LINEAR DATA STRUCTURE-TREES.pptx
Brute force method
unit-4-dynamic programming
Introduction to Design Algorithm And Analysis.ppt

What's hot (20)

PPTX
Longest Common Subsequence (LCS) Algorithm
PPTX
Lecture optimal binary search tree
PPTX
Single source Shortest path algorithm with example
PPT
Midpoint circle algo
DOC
Branch and bound
PPTX
daa-unit-3-greedy method
PDF
Longest common subsequence
PDF
Daa notes 3
PPTX
sum of subset problem using Backtracking
PPTX
The sutherland hodgeman polygon clipping algorithm
PPTX
strassen matrix multiplication algorithm
PPT
Branch and bound
PPTX
Raster animation
PPTX
Stressen's matrix multiplication
PPTX
Daa:Dynamic Programing
PPTX
Tsp branch and-bound
PPT
COMPOSITE TRANSFORMATION COMPUTER GRAPHICDS.ppt
PPTX
LINEAR BOUNDED AUTOMATA (LBA).pptx
PPTX
Algorithm analysis in fundamentals of data structure
PPTX
Backtracking
Longest Common Subsequence (LCS) Algorithm
Lecture optimal binary search tree
Single source Shortest path algorithm with example
Midpoint circle algo
Branch and bound
daa-unit-3-greedy method
Longest common subsequence
Daa notes 3
sum of subset problem using Backtracking
The sutherland hodgeman polygon clipping algorithm
strassen matrix multiplication algorithm
Branch and bound
Raster animation
Stressen's matrix multiplication
Daa:Dynamic Programing
Tsp branch and-bound
COMPOSITE TRANSFORMATION COMPUTER GRAPHICDS.ppt
LINEAR BOUNDED AUTOMATA (LBA).pptx
Algorithm analysis in fundamentals of data structure
Backtracking
Ad

Similar to Longest common subsequence(dynamic programming). (20)

PPT
csce411-set8.ppthhfhgfhgfugfghhfguyguugu
PPT
Longest common substring and Longest common subsequence
PPTX
Longest Common Sub-sequence (LCS)
PPT
Longest Common Subsequence
PDF
Longest common subsequence
PDF
An Optimized Parallel Algorithm for Longest Common Subsequence Using Openmp –...
PPT
Dynamic Programming
PPTX
PPTX
LCS Presentation
PPT
Longest common subsequences in Algorithm Analysis
PPTX
DAArealtime.pptx,To design an algorithm to determine the longest subsequence ...
PPT
17-dynprog2.ppt
PPT
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
PPT
lecture 24
PPT
Dynamic Programing_LCS.ppt
PPT
Free video lectures for mca
PPTX
design and analysis of algorithm (Longest common subsequence)
PDF
Mychurch File Upload
PDF
Mychurch File Upload
csce411-set8.ppthhfhgfhgfugfghhfguyguugu
Longest common substring and Longest common subsequence
Longest Common Sub-sequence (LCS)
Longest Common Subsequence
Longest common subsequence
An Optimized Parallel Algorithm for Longest Common Subsequence Using Openmp –...
Dynamic Programming
LCS Presentation
Longest common subsequences in Algorithm Analysis
DAArealtime.pptx,To design an algorithm to determine the longest subsequence ...
17-dynprog2.ppt
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
lecture 24
Dynamic Programing_LCS.ppt
Free video lectures for mca
design and analysis of algorithm (Longest common subsequence)
Mychurch File Upload
Mychurch File Upload
Ad

Recently uploaded (20)

PDF
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
PDF
Introduction to the R Programming Language
PDF
How to run a consulting project- client discovery
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PDF
Transcultural that can help you someday.
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
DOCX
Factor Analysis Word Document Presentation
PPTX
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
Introduction to Inferential Statistics.pptx
PPTX
Leprosy and NLEP programme community medicine
PDF
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
PDF
annual-report-2024-2025 original latest.
PPTX
New ISO 27001_2022 standard and the changes
PDF
Business Analytics and business intelligence.pdf
PPTX
Database Infoormation System (DBIS).pptx
PPTX
A Complete Guide to Streamlining Business Processes
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
Introduction to the R Programming Language
How to run a consulting project- client discovery
Optimise Shopper Experiences with a Strong Data Estate.pdf
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
Transcultural that can help you someday.
Topic 5 Presentation 5 Lesson 5 Corporate Fin
Factor Analysis Word Document Presentation
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
[EN] Industrial Machine Downtime Prediction
Introduction to Inferential Statistics.pptx
Leprosy and NLEP programme community medicine
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
annual-report-2024-2025 original latest.
New ISO 27001_2022 standard and the changes
Business Analytics and business intelligence.pdf
Database Infoormation System (DBIS).pptx
A Complete Guide to Streamlining Business Processes
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...

Longest common subsequence(dynamic programming).

  • 2. Subsequences • A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. • In LCS ,we have to find Longest Common Subsequence that is in the same relative order. • String of length n has 2^n different possible subsequences. • E.g.— • Subsequences of “ABCDEFG”. • “ABC”,”ABG”,”BDF”,”AEG”,’ACEFG”,……. 2
  • 3. Common Subsequences Suppose that X and Y are two sequences over a set S. X: ABCBDAB Y: BDCABA Z: BCBA We say that Z is a common subsequence of X and Y if and only if • Z is a subsequence of X • Z is a subsequence of Y 3
  • 4. The Longest Common Subsequence Problem Given two sequences X and Y over a set S, the longest common subsequence problem asks to find a common subsequence of X and Y that is of maximal length. Z= (B,C,A) Length 3 Z= (B,C,A,B) Length 4 Z= (B,D,A,B) Length 4 4 Longest
  • 5. LCS Notation Let X and Y be sequences. We denote by LCS(X, Y) the set of longest common subsequences of X and Y. LCS(X,Y) Functional notation, but not a function 5
  • 6. 6 A Poor Approach to the LCS Problem • A Brute-force solution: • Enumerate all subsequences of X • Test which ones are also subsequences of Y • Pick the longest one. • Analysis: • If X is of length n, then it has 2n subsequences • This is an exponential-time algorithm!
  • 7. Dynamic Programming Let us try to develop a dynamic programming solution to the LCS problem. 7
  • 8. Optimal Substructure Let X = ( x1,x2,…,xm) and Y = ( y1,y2,…,yn) be two sequences. Let Z = ( z1,z2,…,zk) is any LCS of X and Y. a) If xm = yn then certainly xm = yn = zk and Zk-1 is in LCS(Xm-1 , Yn-1) 8
  • 9. Optimal Substructure (2) Let X = (x1,x2,…,xm) and Y = ( y1,y2,…,yn) be two sequences. Let Z = ( z1,z2,…,zk) is any LCS of X and Y. b) If xm =! yn then xm =! zk implies that Z is in LCS(Xm-1 , Y) c)If xm =! yn then yn =! zk implies that Z is in LCS(X, Yn-1) 9
  • 10. Recursive Solution Let X and Y be sequences. Let c[i,j] be the length of an element in LCS(Xi, Yj). c[i,j] = 10
  • 11. Dynamic Programming Solution • Define L[i,j] to be the length of the longest common subsequence of X[0..i] and Y[0..j]. • L[i,j-1] = 0 and L[i-1,j]=0, to indicate that the null part of X or Y has no match with the other. • Then we can define L[i,j] in the general case as follows: 1. If xi=yj, then L[i,j] = L[i-1,j-1] + 1 (we can add this match) 2. If xi≠yj, then L[i,j] = max{L[i-1,j], L[i,j-1]} (we have no match here) X:ABCB Y:BDCA LCS:BC 11
  • 12. Dynamic Programming Solution (2) How can we get an actual longest common subsequence? Store in addition to the array c an array b pointing to the optimal subproblem chosen when computing c[i,j]. 12
  • 13. 13 Example T yj B D C A Xi 0 0 0 0 0 A 0 0 0 0 1 B 0 1 1 1 1 C 0 1 1 2 2 B 0 1 1 2 2 Start at b[m,n]. Follow the arrows. Each diagonal array gives one element of the LCS.
  • 14. 14 ALGORITHM LCS(X,Y) m ← length[X] n ← length[Y] for i ← 1 to m do c[i,0] ← 0 for j ← 1 to n do c[0,j] ← 0
  • 15. 15 LCS(X,Y) for i ← 1 to m do for j ← 1 to n do if xi = yj c[i, j] ← c[i-1, j-1]+1 b[i, j] ← “D” else if c[i-1, j] ≥ c[i, j-1] c[i, j] ← c[i-1, j] b[i, j] ← “U” else c[i, j] ← c[i, j-1] b[i, j] ← “L” return c and b
  • 16. CONSTRUCTING AN LCS • PRINT-LCS(b, X, i, j) 1. If i=0 or j=0 2. then return 3.If b[i, j]=“D” 4. then PRINT- LCS(b,X,i-1,j-1) 5. print xi 6.Else if b[i, j]=“U” 7. then PRINT-LCS(b,X,i-1,j) 8.Else PRINT-LCS(b,X,i,j-1) 16
  • 17. Analysis of LCS Algorithm • We have two nested loops • The outer one iterates n times • The inner one iterates m times • A constant amount of work is done inside each iteration of the inner loop • Thus, the total running time is O(nm) • Answer is contained in L[n,m] (and the subsequence can be recovered from the T table). 17
  • 18. usage • Biological applications often need to compare the DNA of two (or more) different organisms. • We can say that two DNA strands are similar if one is a substring of the other. 18