SlideShare une entreprise Scribd logo
1  sur  18
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

Contenu connexe

Tendances

Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programmingmaharajdey
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplicationRespa Peter
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceSyeda
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingShakil Ahmed
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologDataminingTools Inc
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic ProgrammingFenil Shah
 

Tendances (20)

Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programming
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Network flows
Network flowsNetwork flows
Network flows
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
First order logic
First order logicFirst order logic
First order logic
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 

Similaire à Longest common subsequence(dynamic programming).

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Badrul Alam
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mcaEdhole.com
 
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2Shanmuganathan C
 
lecture 24
lecture 24lecture 24
lecture 24sajinsc
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programmingklyni777
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptmanasgaming4
 
Animation in video games
Animation in video gamesAnimation in video games
Animation in video gamesXban1
 
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...Ali Ajouz
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
RNA Secondary Structure Prediction
RNA Secondary Structure PredictionRNA Secondary Structure Prediction
RNA Secondary Structure PredictionSumin Byeon
 
Cs229 notes7a
Cs229 notes7aCs229 notes7a
Cs229 notes7aVuTran231
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)NYversity
 

Similaire à Longest common subsequence(dynamic programming). (20)

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
 
Dynamic programming lcs
Dynamic programming lcsDynamic programming lcs
Dynamic programming lcs
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
 
17-dynprog2.ppt
17-dynprog2.ppt17-dynprog2.ppt
17-dynprog2.ppt
 
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog217-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
 
lecture 24
lecture 24lecture 24
lecture 24
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Beam buckling
Beam bucklingBeam buckling
Beam buckling
 
Animation in video games
Animation in video gamesAnimation in video games
Animation in video games
 
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
alt klausur
alt klausuralt klausur
alt klausur
 
RNA Secondary Structure Prediction
RNA Secondary Structure PredictionRNA Secondary Structure Prediction
RNA Secondary Structure Prediction
 
Som3
Som3Som3
Som3
 
Cs229 notes7a
Cs229 notes7aCs229 notes7a
Cs229 notes7a
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)
 

Dernier

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxUnduhUnggah1
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 

Dernier (20)

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docx
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 

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