SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Longest Common Subsequence(LCS)
研究生 鍾聖彥
指導老師 許慶昇
Dynamic Programming
1
2014/05/07
最長共同子序列
Dynamic
Programming
Optimal substructure(當一個問題存在著最
佳解,則表示其所有子問題也必存在著最佳解)
Overlapping subproblems(子問題重複出
現)
2
Longest Common
Subsequence???
Biological applications often need to compare the
DNA of tow(or more) different organisms.
3
Subsequence
A subsequence of a given sequence is just the given
sequence with zero or more elements left out.
Ex: app、le、ple and so on are
subsequences of “apple”.
4
Common Subsequence
X = (A, B, C, B, D, A, B)
Y = (B, D, C, A, B, A)
Two sequences:
Sequence Z is a common subsequence of X and
Y if Z is a subsequence of both X and Y
Z = (B, C, A) — length 3
Z = (B, C, A, B) - length 4
Z = (B, D, A, B) — length 4
Z= — length 5 ???
longest
5
What is longest Common
Subsequence problem?
X = (x1, x2,……., xm)
Y = (y1, y2,……., yn)
6
Find a maximum-length common subsequence of X
and Y
How to do?
Dynamic Programming!!!
Brute Force!!!
Step 1: Characterize
optimality
Sequence X = (x1, x2,……., xm)
Define the ith prefix of X, for i = 0, 1,…, m
as Xi = (x1, x2, ..., xi)
with X0 representing the empty sequence.
EX: if X = (A, B, C, A, D, A, B) then
X4 = (A, B, C, A)
X0 = ( ) empty sequence
7
Theorem (Optimal substructure of LCS)
8
1. If Xm = Yn, then Zk = Xm = Yn and Zk-1 is a
LCS of Xm-1 and Yn-1
2. If Xm ≠ Yn, then Zk ≠ Xm implies that Z is a
LCS of Xm-1 and Y
3. If Xm ≠ Yn, then Zk ≠ Yn implies that Z is a
LCS of X and Yn-1
X = (X1, X2,…, Xm) and Y = (Y1, Y2,…, Yn)
Sequences
Z = (Z1, Z2,…, Zk) be any LCS of X and Y
We assume:
Optimal substructure problem
The LCS of the original two sequences contains a LCS
of prefixes of the two sequences.
(當一個問題存在著最佳解,則表示其所有子問題也必存
在著最佳解)
9
Step 2: A recursive solution
Xi and Yj end with xi=yj
Zk is Zk -1 followed by Zk = Xi = Yj
where Zk-1 is an LCS of Xi-1 and Yj -1
LenLCS(i, j) = LenLCS(i-1, j-1)+1
Xi
x1 x2 … xi-1 xi
Yj
y1 y2 … yj-1 yj=xi
Zk
z1 z2…zk-1 zk =yj=xi
Case 1:
Step 2: A recursive solution
Case 2,3: Xi and Yj end with xi ≠ yj
Xi
x1 x2 … xi-1 xi
Yj
y1 y2 … yj-1 yj
Zk
z1 z2…zk-1 zk ≠yj
Xi
x1 x2 … xi-1 x i
Yj
yj y1 y2 …yj-1 yj
Zk
z1 z2…zk-1 zk ≠ xi
Zk is an LCS of Xi and Yj -1 Zk is an LCS of Xi-1 and Yj
LenLCS(i, j)=max{LenLCS(i, j-1), LenLCS(i-1, j)}
Step 2:A recursive solution
Let c[i,j] be the length of a LCS for Xi and Yj
the recursion described by the above cases as
12
Case 1
Reduces to the single subproblem of finding a LCS of
Xm-1, Yn-1 and adding Xm = Yn to the end of Z.
Cases 2 and 3
Reduces to two subproblems of finding a LCS of Xm-1,
Y and X, Yn-1 and selecting the longer of the two.
Step 3: Compute the length of
the LCS
LCS problem has only ɵ(mn) distinct subproblems.
So?
Use Dynamic programming!!!
13
Step 3: Compute the length of the LCS
Procedure 1
LCS-length takes two Sequences
X = (x1, x2,…, xm) and Y = (y1, y2,…, yn) as input.
Procedure 2
It stores the c[i, j] values in a table c[0..m, 0..n] and
it computes the entries in row-major order.
Procedure 3
Table b[1..m, 1..n] to construct an optimal solution.
b[i, j] points to the table entry corresponding to the
optimal solution chosen when computing c[i, j]
Procedure 4
Return the b and c tables; c[m, n] contains the length
of an LCS X and Y14
LCS-Length(X, Y)
1 m = X.length
2 n = Y.length
3 let b[1..m, 1..n] and c[0..m, 0..n] be new tables.
4 for i 1 to m do

5 c[i, 0] = 0
6 for j 1 to n do

7 c[0, j] = 0
8 for i 1 to m do

9 for j 1 to n do

10 if xi ==yj

11 c[i, j] = c[i-1, j-1]+1

12 b[i, j] = “ ” 

13 else if c[i-1, j] ≥ c[i, j-1]

14 c[i, j] = c[i-1, j]

15 b[i, j] = “ ”

16 else
17 c[i, j] = c[i, j-1]

18 b[i, j] = “ ”
19 return c and b 15
The table produced by LCS-Length on the sequences
X = (A, B, C, B, D, A, B) and Y = (B, D, C, A, B, A).
16
The running time of the procedure is O(mn), since each table
entry table O(1) time to compute
Step 4: Construct an optimal LCS
PRINT-LCS(b, X, i, j)
PRINT-LCS(b, X, X.length, Y.length)
1 if i == 0 or j == 0
2 return
3 if b[i, j] == “ ”
4 PRINT-LCS(b,X,i-1, j-1)
5 print Xi
6 else if b[i, j] == “ ”
7 PRINT-LCS(b,X,i-1, j)
8 else PRINT-LCS(b,X,i, j-1)
This procedure prints BCBA.
The procedure takes time O(m+n)
Example
X = <A, B, C, B, A>
Y = <B, D, C, A, B>
We will fill in the table in row-major order starting in
the upper left corner using the following formulas:
Example
X = <A, B, C, B, A>
Y = <B, D, C, A, B>
We will fill in the table in row-major order starting in
the upper left corner using the following formulas:
Answer
Thus the optimal LCS length is c[m,n] = 3.
Optimal LCS starting at c[5,5] we get Z = <B, C, B>
Alternatively start at c[5,4]
we would produce Z = <B, C, A>.
*Note that the LCS is not unique but the optimal length of
the LCS is.
20
Reference
Lecture 13: Dynamic Programming - Longest
Common Subsequence http://faculty.ycp.edu/
~dbabcock/cs360/lectures/lecture13.html
http://www.csie.ntnu.edu.tw/~u91029/
LongestCommonSubsequence.html
Longest common subsequence (Cormen et al., Sec.
15.4)
https://www.youtube.com/watch?v=Wv1y45iqsbk
https://www.youtube.com/watch?v=wJ-rP9hJXO0

Mais conteúdo relacionado

Mais procurados

Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceKrishma Parekh
 
Longest common subsequence lcs
Longest common subsequence  lcsLongest common subsequence  lcs
Longest common subsequence lcsShahariar Rabby
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceSyeda
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceDipta Das
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingShakil Ahmed
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theoremJamesMa54
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...JamesMa54
 
Iit jam 2016 physics solutions BY Trajectoryeducation
Iit jam 2016 physics solutions BY TrajectoryeducationIit jam 2016 physics solutions BY Trajectoryeducation
Iit jam 2016 physics solutions BY TrajectoryeducationDev Singh
 
Solving the energy problem of helium final report
Solving the energy problem of helium final reportSolving the energy problem of helium final report
Solving the energy problem of helium final reportJamesMa54
 
IIT Jam math 2016 solutions BY Trajectoryeducation
IIT Jam math 2016 solutions BY TrajectoryeducationIIT Jam math 2016 solutions BY Trajectoryeducation
IIT Jam math 2016 solutions BY TrajectoryeducationDev Singh
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsemGopi Saiteja
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Integral Calculus
Integral CalculusIntegral Calculus
Integral Calculusitutor
 
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by AntonSolutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by AntonPamelaew
 

Mais procurados (20)

Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Longest common subsequence lcs
Longest common subsequence  lcsLongest common subsequence  lcs
Longest common subsequence lcs
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Imc2017 day2-solutions
Imc2017 day2-solutionsImc2017 day2-solutions
Imc2017 day2-solutions
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theorem
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
 
Iit jam 2016 physics solutions BY Trajectoryeducation
Iit jam 2016 physics solutions BY TrajectoryeducationIit jam 2016 physics solutions BY Trajectoryeducation
Iit jam 2016 physics solutions BY Trajectoryeducation
 
Solving the energy problem of helium final report
Solving the energy problem of helium final reportSolving the energy problem of helium final report
Solving the energy problem of helium final report
 
Imc2017 day1-solutions
Imc2017 day1-solutionsImc2017 day1-solutions
Imc2017 day1-solutions
 
IIT Jam math 2016 solutions BY Trajectoryeducation
IIT Jam math 2016 solutions BY TrajectoryeducationIIT Jam math 2016 solutions BY Trajectoryeducation
IIT Jam math 2016 solutions BY Trajectoryeducation
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Lecture5
Lecture5Lecture5
Lecture5
 
Talk5
Talk5Talk5
Talk5
 
Integral Calculus
Integral CalculusIntegral Calculus
Integral Calculus
 
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by AntonSolutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
 

Semelhante a Dynamic programming lcs

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
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
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mcaEdhole.com
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptmanasgaming4
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxHarshitSingh334328
 
Generating Chebychev Chaotic Sequence
Generating Chebychev Chaotic SequenceGenerating Chebychev Chaotic Sequence
Generating Chebychev Chaotic SequenceCheng-An Yang
 
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...BRNSS Publication Hub
 
DSP_DiscSignals_LinearS_150417.pptx
DSP_DiscSignals_LinearS_150417.pptxDSP_DiscSignals_LinearS_150417.pptx
DSP_DiscSignals_LinearS_150417.pptxHamedNassar5
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxjacksnathalie
 
Longest Common Sequence Algorithm Analysis
Longest Common Sequence Algorithm AnalysisLongest Common Sequence Algorithm Analysis
Longest Common Sequence Algorithm AnalysisRex Yuan
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Pydata Katya Vasilaky
Pydata Katya VasilakyPydata Katya Vasilaky
Pydata Katya Vasilakyknv4
 

Semelhante a Dynamic programming lcs (20)

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
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
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
05_AJMS_332_21.pdf
05_AJMS_332_21.pdf05_AJMS_332_21.pdf
05_AJMS_332_21.pdf
 
Generating Chebychev Chaotic Sequence
Generating Chebychev Chaotic SequenceGenerating Chebychev Chaotic Sequence
Generating Chebychev Chaotic Sequence
 
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...
On Application of the Fixed-Point Theorem to the Solution of Ordinary Differe...
 
DSP_DiscSignals_LinearS_150417.pptx
DSP_DiscSignals_LinearS_150417.pptxDSP_DiscSignals_LinearS_150417.pptx
DSP_DiscSignals_LinearS_150417.pptx
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
 
02_AJMS_186_19_RA.pdf
02_AJMS_186_19_RA.pdf02_AJMS_186_19_RA.pdf
02_AJMS_186_19_RA.pdf
 
02_AJMS_186_19_RA.pdf
02_AJMS_186_19_RA.pdf02_AJMS_186_19_RA.pdf
02_AJMS_186_19_RA.pdf
 
Longest Common Sequence Algorithm Analysis
Longest Common Sequence Algorithm AnalysisLongest Common Sequence Algorithm Analysis
Longest Common Sequence Algorithm Analysis
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Pydata Katya Vasilaky
Pydata Katya VasilakyPydata Katya Vasilaky
Pydata Katya Vasilaky
 
1551 limits and continuity
1551 limits and continuity1551 limits and continuity
1551 limits and continuity
 
Unit 3
Unit 3Unit 3
Unit 3
 

Mais de Department of Information Management Ming Chuan University, Taiwan (9)

Android googlemapv2 keyApplicance
Android googlemapv2 keyApplicanceAndroid googlemapv2 keyApplicance
Android googlemapv2 keyApplicance
 
Greedy minimum spanning tree- prim's algorithm
Greedy minimum spanning tree- prim's algorithmGreedy minimum spanning tree- prim's algorithm
Greedy minimum spanning tree- prim's algorithm
 
類神經網路
類神經網路類神經網路
類神經網路
 
Examining the impact of rich media on consumer willingness to pay in online ...
Examining the impact of rich media  on consumer willingness to pay in online ...Examining the impact of rich media  on consumer willingness to pay in online ...
Examining the impact of rich media on consumer willingness to pay in online ...
 
How online social ties and product-related risks influence purchase intention...
How online social ties and product-related risks influence purchase intention...How online social ties and product-related risks influence purchase intention...
How online social ties and product-related risks influence purchase intention...
 
Android google mapv2
Android google mapv2Android google mapv2
Android google mapv2
 
No sql
No sqlNo sql
No sql
 
Page rank
Page rankPage rank
Page rank
 
Semantic web
Semantic webSemantic web
Semantic web
 

Último

Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxBoston Institute of Analytics
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Learn How Data Science Changes Our World
Learn How Data Science Changes Our WorldLearn How Data Science Changes Our World
Learn How Data Science Changes Our WorldEduminds Learning
 
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
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
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
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
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
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
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
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 

Último (20)

Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Learn How Data Science Changes Our World
Learn How Data Science Changes Our WorldLearn How Data Science Changes Our World
Learn How Data Science Changes Our World
 
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
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
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
 
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)
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
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
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 

Dynamic programming lcs

  • 1. Longest Common Subsequence(LCS) 研究生 鍾聖彥 指導老師 許慶昇 Dynamic Programming 1 2014/05/07 最長共同子序列
  • 3. Longest Common Subsequence??? Biological applications often need to compare the DNA of tow(or more) different organisms. 3
  • 4. Subsequence A subsequence of a given sequence is just the given sequence with zero or more elements left out. Ex: app、le、ple and so on are subsequences of “apple”. 4
  • 5. Common Subsequence X = (A, B, C, B, D, A, B) Y = (B, D, C, A, B, A) Two sequences: Sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y Z = (B, C, A) — length 3 Z = (B, C, A, B) - length 4 Z = (B, D, A, B) — length 4 Z= — length 5 ??? longest 5
  • 6. What is longest Common Subsequence problem? X = (x1, x2,……., xm) Y = (y1, y2,……., yn) 6 Find a maximum-length common subsequence of X and Y How to do? Dynamic Programming!!! Brute Force!!!
  • 7. Step 1: Characterize optimality Sequence X = (x1, x2,……., xm) Define the ith prefix of X, for i = 0, 1,…, m as Xi = (x1, x2, ..., xi) with X0 representing the empty sequence. EX: if X = (A, B, C, A, D, A, B) then X4 = (A, B, C, A) X0 = ( ) empty sequence 7
  • 8. Theorem (Optimal substructure of LCS) 8 1. If Xm = Yn, then Zk = Xm = Yn and Zk-1 is a LCS of Xm-1 and Yn-1 2. If Xm ≠ Yn, then Zk ≠ Xm implies that Z is a LCS of Xm-1 and Y 3. If Xm ≠ Yn, then Zk ≠ Yn implies that Z is a LCS of X and Yn-1 X = (X1, X2,…, Xm) and Y = (Y1, Y2,…, Yn) Sequences Z = (Z1, Z2,…, Zk) be any LCS of X and Y We assume:
  • 9. Optimal substructure problem The LCS of the original two sequences contains a LCS of prefixes of the two sequences. (當一個問題存在著最佳解,則表示其所有子問題也必存 在著最佳解) 9
  • 10. Step 2: A recursive solution Xi and Yj end with xi=yj Zk is Zk -1 followed by Zk = Xi = Yj where Zk-1 is an LCS of Xi-1 and Yj -1 LenLCS(i, j) = LenLCS(i-1, j-1)+1 Xi x1 x2 … xi-1 xi Yj y1 y2 … yj-1 yj=xi Zk z1 z2…zk-1 zk =yj=xi Case 1:
  • 11. Step 2: A recursive solution Case 2,3: Xi and Yj end with xi ≠ yj Xi x1 x2 … xi-1 xi Yj y1 y2 … yj-1 yj Zk z1 z2…zk-1 zk ≠yj Xi x1 x2 … xi-1 x i Yj yj y1 y2 …yj-1 yj Zk z1 z2…zk-1 zk ≠ xi Zk is an LCS of Xi and Yj -1 Zk is an LCS of Xi-1 and Yj LenLCS(i, j)=max{LenLCS(i, j-1), LenLCS(i-1, j)}
  • 12. Step 2:A recursive solution Let c[i,j] be the length of a LCS for Xi and Yj the recursion described by the above cases as 12 Case 1 Reduces to the single subproblem of finding a LCS of Xm-1, Yn-1 and adding Xm = Yn to the end of Z. Cases 2 and 3 Reduces to two subproblems of finding a LCS of Xm-1, Y and X, Yn-1 and selecting the longer of the two.
  • 13. Step 3: Compute the length of the LCS LCS problem has only ɵ(mn) distinct subproblems. So? Use Dynamic programming!!! 13
  • 14. Step 3: Compute the length of the LCS Procedure 1 LCS-length takes two Sequences X = (x1, x2,…, xm) and Y = (y1, y2,…, yn) as input. Procedure 2 It stores the c[i, j] values in a table c[0..m, 0..n] and it computes the entries in row-major order. Procedure 3 Table b[1..m, 1..n] to construct an optimal solution. b[i, j] points to the table entry corresponding to the optimal solution chosen when computing c[i, j] Procedure 4 Return the b and c tables; c[m, n] contains the length of an LCS X and Y14
  • 15. LCS-Length(X, Y) 1 m = X.length 2 n = Y.length 3 let b[1..m, 1..n] and c[0..m, 0..n] be new tables. 4 for i 1 to m do
 5 c[i, 0] = 0 6 for j 1 to n do
 7 c[0, j] = 0 8 for i 1 to m do
 9 for j 1 to n do
 10 if xi ==yj
 11 c[i, j] = c[i-1, j-1]+1
 12 b[i, j] = “ ” 
 13 else if c[i-1, j] ≥ c[i, j-1]
 14 c[i, j] = c[i-1, j]
 15 b[i, j] = “ ”
 16 else 17 c[i, j] = c[i, j-1]
 18 b[i, j] = “ ” 19 return c and b 15
  • 16. The table produced by LCS-Length on the sequences X = (A, B, C, B, D, A, B) and Y = (B, D, C, A, B, A). 16 The running time of the procedure is O(mn), since each table entry table O(1) time to compute
  • 17. Step 4: Construct an optimal LCS PRINT-LCS(b, X, i, j) PRINT-LCS(b, X, X.length, Y.length) 1 if i == 0 or j == 0 2 return 3 if b[i, j] == “ ” 4 PRINT-LCS(b,X,i-1, j-1) 5 print Xi 6 else if b[i, j] == “ ” 7 PRINT-LCS(b,X,i-1, j) 8 else PRINT-LCS(b,X,i, j-1) This procedure prints BCBA. The procedure takes time O(m+n)
  • 18. Example X = <A, B, C, B, A> Y = <B, D, C, A, B> We will fill in the table in row-major order starting in the upper left corner using the following formulas:
  • 19. Example X = <A, B, C, B, A> Y = <B, D, C, A, B> We will fill in the table in row-major order starting in the upper left corner using the following formulas:
  • 20. Answer Thus the optimal LCS length is c[m,n] = 3. Optimal LCS starting at c[5,5] we get Z = <B, C, B> Alternatively start at c[5,4] we would produce Z = <B, C, A>. *Note that the LCS is not unique but the optimal length of the LCS is. 20
  • 21. Reference Lecture 13: Dynamic Programming - Longest Common Subsequence http://faculty.ycp.edu/ ~dbabcock/cs360/lectures/lecture13.html http://www.csie.ntnu.edu.tw/~u91029/ LongestCommonSubsequence.html Longest common subsequence (Cormen et al., Sec. 15.4) https://www.youtube.com/watch?v=Wv1y45iqsbk https://www.youtube.com/watch?v=wJ-rP9hJXO0