SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdf
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdfGiven two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdf
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdfwailesalekzydelore94
 
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
 
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docx
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docxAlgorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docx
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docxwviola
 
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docxtodd991
 
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
 
Find the recccurence relation of longest common substring- Algorithm b.docx
Find the recccurence relation of longest common substring- Algorithm b.docxFind the recccurence relation of longest common substring- Algorithm b.docx
Find the recccurence relation of longest common substring- Algorithm b.docxmichael1810
 

Ähnlich wie 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
 
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdf
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdfGiven two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdf
Given two strings x = x_1x_2...x_n and y = y_1y_2...ym, we wish to fi.pdf
 
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
 
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docx
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docxAlgorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docx
Algorithm below--- a) IF n - 0 return 0 (b) IF m - 0 return 0 (c) IF w.docx
 
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx
1- The longest common substring of two words w 1 --w n and v 1 ---v m.docx
 
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
 
Find the recccurence relation of longest common substring- Algorithm b.docx
Find the recccurence relation of longest common substring- Algorithm b.docxFind the recccurence relation of longest common substring- Algorithm b.docx
Find the recccurence relation of longest common substring- Algorithm b.docx
 

Mehr von 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
 

Kürzlich hochgeladen

5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best PracticesDataArchiva
 
The Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerThe Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerPavel Šabatka
 
ChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics InfrastructureChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics Infrastructuresonikadigital1
 
Optimal Decision Making - Cost Reduction in Logistics
Optimal Decision Making - Cost Reduction in LogisticsOptimal Decision Making - Cost Reduction in Logistics
Optimal Decision Making - Cost Reduction in LogisticsThinkInnovation
 
Mapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxMapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxVenkatasubramani13
 
CI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionCI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionajayrajaganeshkayala
 
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxTINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxDwiAyuSitiHartinah
 
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityStrategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityAggregage
 
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Guido X Jansen
 
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptx
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptxCCS336-Cloud-Services-Management-Lecture-Notes-1.pptx
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptxdhiyaneswaranv1
 
Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Vladislav Solodkiy
 
Rock Songs common codes and conventions.pptx
Rock Songs common codes and conventions.pptxRock Songs common codes and conventions.pptx
Rock Songs common codes and conventions.pptxFinatron037
 
Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...PrithaVashisht1
 
How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?sonikadigital1
 
Virtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product IntroductionVirtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product Introductionsanjaymuralee1
 
Master's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationMaster's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationGiorgio Carbone
 

Kürzlich hochgeladen (16)

5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices
 
The Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerThe Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayer
 
ChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics InfrastructureChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics Infrastructure
 
Optimal Decision Making - Cost Reduction in Logistics
Optimal Decision Making - Cost Reduction in LogisticsOptimal Decision Making - Cost Reduction in Logistics
Optimal Decision Making - Cost Reduction in Logistics
 
Mapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxMapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptx
 
CI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionCI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual intervention
 
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxTINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
 
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityStrategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
 
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
 
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptx
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptxCCS336-Cloud-Services-Management-Lecture-Notes-1.pptx
CCS336-Cloud-Services-Management-Lecture-Notes-1.pptx
 
Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023
 
Rock Songs common codes and conventions.pptx
Rock Songs common codes and conventions.pptxRock Songs common codes and conventions.pptx
Rock Songs common codes and conventions.pptx
 
Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...
 
How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?
 
Virtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product IntroductionVirtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product Introduction
 
Master's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationMaster's Thesis - Data Science - Presentation
Master's Thesis - Data Science - Presentation
 

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