SlideShare a Scribd company logo
1 of 13
Download to read offline
Longest Common Subsequence
Dr. Kiran K
Assistant Professor
Department of CSE
UVCE
Bengaluru, India.
Introduction
Similarity between two sequences can be defined as:
1. One sequence is a substring of the other.
2. If the number of changes needed to turn one into the other is small.
3. Find a third sequence such that its alphabets appear in two other sequences in the
same order, but not necessarily consecutively. Longer the third sequence, the more
similar the other two sequences are.
Subsequence:
Given a sequence X = <x1, x2, . . . xm>, another sequence Z = <Z1, Z2, . . . Zk> is a
subsequence of X if there exists a strictly increasing sequence <i1, i2, . . . ik> of indices
of X such that for all j = 1, 2, . . . , k, we have xij
= zj.
Eg.: X = <A, B, C, B, D, A, B>
Subsequence: Z = <B, C, D, B,>, Index Sequence <2, 3, 5, 7>.
Introduction…
Common Subsequence:
Given two sequences X and Y, we say that a sequence Z is a common subsequence of X
and Y if Z is a subsequence of both X and Y
Eg.: X = <A, B, C, B, D, A, B> and Y = <B, D, C, A, B, A>
Common Subsequence: Z = <B, C, A>
Longest Common Subsequence (LCS) Problem:
Given two sequences X = <x1, x2, . . . xm> and Y = <y1, y2, . . . yn>, and wish to find a
Maximum Length Common Subsequence of X and Y.
Eg.: S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA
LCS: S3 = GTCGTCGGAAGCCGGCCGAA
Introduction…
Dynamic Programming Approach to solve the LCS Problem:
1. Characterizing a LCS.
2. A Recursive Solution.
3. Computing the length of an LCS.
4. Constructing an LCS.
Characterizing a LCS
Theorem:
Let X = <x1, x2, . . . xm> and Y = <y1, y2, . . . yn>, be sequences, and let Z = <Z1, Z2, . . .
Zk> be any LCS of X and Y .
1. If (xm = yn), then zk = xm = yn and Z k-1 is an LCS of Xm-1 and Yn-1.
2. If (xm ≠ yn), then (zk ≠ xm) implies that Z is an LCS of Xm-1 and Y.
3. If (xm ≠ yn), then (zk ≠ yn) implies that Z is an LCS of X and Yn-1.
Proof:
1. xm = yn
Assumption: (zk ≠ xm) (T1.1)
(T1.1) → xm = yn can be appended to Z to obtain a common subsequence of X and
Y of length k + 1 (T1.2)
(T1.2) contradicts the supposition that Z is a longest common subsequence of X
and Y. Hence, zk = xm = yn
Characterizing a LCS…
Prefix Zk-1 is a length k – 1 common subsequence of xm-1 and yn-1:
Assumption: Let W be a common subsequence of xm-1 and yn-1 with length greater than
k – 1. (T1.3)
(T1.3) → Appending xm = yn to W produces a common subsequence of X and Y whose
length is greater than k. (T1.4)
(T1.4) contradicts the supposition that Z is a longest common subsequence of X and Y.
Hence, Zk-1 is a length k – 1 common subsequence of xm-1 and yn-1.
2. If (xm ≠ yn), then (zk ≠ xm) implies that Z is an LCS of Xm-1 and Y
Assumption: Let W be a common subsequence of Xm-1 and Y with length greater than k.(T2.1)
xm ≠ yn (T2.2)
(T2.1) and (T2.2) → W would also be a common subsequence of Xm and Y (T2.3)
(T2.3) contradicts the assumption that Z is an LCS of X and Y.
Hence, there is no other common subsequence of Xm-1 and Y which is longer than Z.
Characterizing a LCS…
The Theorem implies than an LCS of two sequences contains within it an LCS of
prefixes of the two sequences.
Thus, the LCS problem has an Optimal-Substructure property.
A Recursive Solution
To Find the LCS of X and Y:
1. If (xm = yn)
 Find an LCS of Xm-1 and Yn-1.
 Append xm = yn to this LCS.
2. If (xm ≠ yn)
 Find an LCS of Xm-1 and Y
 Find an LCS of X and Yn-1
 Return the LCS that is longer.
Overlapping Subproblems:
• To find an LCS of X and Y, Find LCSs of X and Yn-1 and of Xm-1 and Y.
• Each of these subproblems has the subproblem of finding an LCS of Xm-1 and Yn-1.
A Recursive Solution…
c [i, j]: Length of an LCS of the sequences Xi and Yj
Algorithm LCS-LENGTH (X, Y):
• c [0 . . m, 0 . . n]: Stores the c [i, j] values
• b [1 . . m, 1 . . n]: Points to the table entry corresponding to the optimal subproblem
solution chosen when computing c [i, j].
• c [m, n] : Contains the Length of an LCS of X and Y.
Computing the Length of an LCS
LCS-LENGTH (X, Y)
m = X.length
n = Y.length
Let b [1. . m, 1 . . n] and
c [0. . m, 0 . . n] be new tables
For (i = 1 to m)
c [i, 0] = 0
For (j = 0 to n)
c [0, j] = 0
For (i = 1 to m)
For (j = 1 to n)
if (Xi == Yj)
c [i, j] = c [i - 1, j - 1] + 1
b [i, j] = “ ”
else if (c [i - 1, j] ≥ c [i, j - 1])
c [i, j] = c [i - 1, j]
b [i, j] = “↑”
else c [i, j] = c [i, j - 1]
b [i, j] = “←”
Return c and b
Computing the Length of an LCS…
Running Time = Ө (mn)
(The algorithm has to fill n x m
entries of the table)
Eg.:
X = <A, B, C, B, D, A, B>
Y = <B, D, C, A, B, A>
Result:
Length of the LCS: c [7, 6] = 4
LCS: <B, C, B, A>
PRINT-LCS (b, X, i, j)
If (i == 0 or j == 0)
return
If (b [i, j] = “ ”)
PRINT-LCS (b, X, i - 1, j - 1)
print xi
else If (b [i, j] = “↑”)
PRINT-LCS (b, X, i - 1, j)
Else PRINT-LCS (b, X, i, j - 1)
Constructing an LCS
• b table enables to quickly construct
an LCS of X = <x1, x2, . . . xm> and
Y = <y1, y2, . . . yn>.
• Begin at b [m, n] and trace through
the table by following the arrows.
• Running Time: O (m + n)
References:
• Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein,
Introduction to Algorithms, Third Edition, The MIT Press Cambridge,
Massachusetts London, England.

More Related Content

What's hot

Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray ProblemKamran Ashraf
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxSyed Zaid Irshad
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).munawerzareef
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 

What's hot (20)

Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Np complete
Np completeNp complete
Np complete
 
Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 

Similar to Longest common subsequence

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
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptmanasgaming4
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Badrul Alam
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
8 arc length and area of surfaces x
8 arc length and area of surfaces x8 arc length and area of surfaces x
8 arc length and area of surfaces xmath266
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programmingklyni777
 
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
 
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 & 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
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesAntonis Antonopoulos
 
lecture 24
lecture 24lecture 24
lecture 24sajinsc
 
The Multivariate Gaussian Probability Distribution
The Multivariate Gaussian Probability DistributionThe Multivariate Gaussian Probability Distribution
The Multivariate Gaussian Probability DistributionPedro222284
 
Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential slides
 

Similar to Longest common subsequence (20)

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
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
8 arc length and area of surfaces x
8 arc length and area of surfaces x8 arc length and area of surfaces x
8 arc length and area of surfaces x
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
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...
 
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 subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
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
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity Classes
 
lecture 24
lecture 24lecture 24
lecture 24
 
The Multivariate Gaussian Probability Distribution
The Multivariate Gaussian Probability DistributionThe Multivariate Gaussian Probability Distribution
The Multivariate Gaussian Probability Distribution
 
Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential
 

More from Kiran K

String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmKiran K
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithmKiran K
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp AlgorithmKiran K
 
Naive string matching algorithm
Naive string matching algorithmNaive string matching algorithm
Naive string matching algorithmKiran K
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
Bellman ford
Bellman fordBellman ford
Bellman fordKiran K
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplicationKiran K
 

More from Kiran K (7)

String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
 
Naive string matching algorithm
Naive string matching algorithmNaive string matching algorithm
Naive string matching algorithm
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Longest common subsequence

  • 1. Longest Common Subsequence Dr. Kiran K Assistant Professor Department of CSE UVCE Bengaluru, India.
  • 2. Introduction Similarity between two sequences can be defined as: 1. One sequence is a substring of the other. 2. If the number of changes needed to turn one into the other is small. 3. Find a third sequence such that its alphabets appear in two other sequences in the same order, but not necessarily consecutively. Longer the third sequence, the more similar the other two sequences are. Subsequence: Given a sequence X = <x1, x2, . . . xm>, another sequence Z = <Z1, Z2, . . . Zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, . . . ik> of indices of X such that for all j = 1, 2, . . . , k, we have xij = zj. Eg.: X = <A, B, C, B, D, A, B> Subsequence: Z = <B, C, D, B,>, Index Sequence <2, 3, 5, 7>.
  • 3. Introduction… Common Subsequence: Given two sequences X and Y, we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y Eg.: X = <A, B, C, B, D, A, B> and Y = <B, D, C, A, B, A> Common Subsequence: Z = <B, C, A> Longest Common Subsequence (LCS) Problem: Given two sequences X = <x1, x2, . . . xm> and Y = <y1, y2, . . . yn>, and wish to find a Maximum Length Common Subsequence of X and Y. Eg.: S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA LCS: S3 = GTCGTCGGAAGCCGGCCGAA
  • 4. Introduction… Dynamic Programming Approach to solve the LCS Problem: 1. Characterizing a LCS. 2. A Recursive Solution. 3. Computing the length of an LCS. 4. Constructing an LCS.
  • 5. Characterizing a LCS Theorem: Let X = <x1, x2, . . . xm> and Y = <y1, y2, . . . yn>, be sequences, and let Z = <Z1, Z2, . . . Zk> be any LCS of X and Y . 1. If (xm = yn), then zk = xm = yn and Z k-1 is an LCS of Xm-1 and Yn-1. 2. If (xm ≠ yn), then (zk ≠ xm) implies that Z is an LCS of Xm-1 and Y. 3. If (xm ≠ yn), then (zk ≠ yn) implies that Z is an LCS of X and Yn-1. Proof: 1. xm = yn Assumption: (zk ≠ xm) (T1.1) (T1.1) → xm = yn can be appended to Z to obtain a common subsequence of X and Y of length k + 1 (T1.2) (T1.2) contradicts the supposition that Z is a longest common subsequence of X and Y. Hence, zk = xm = yn
  • 6. Characterizing a LCS… Prefix Zk-1 is a length k – 1 common subsequence of xm-1 and yn-1: Assumption: Let W be a common subsequence of xm-1 and yn-1 with length greater than k – 1. (T1.3) (T1.3) → Appending xm = yn to W produces a common subsequence of X and Y whose length is greater than k. (T1.4) (T1.4) contradicts the supposition that Z is a longest common subsequence of X and Y. Hence, Zk-1 is a length k – 1 common subsequence of xm-1 and yn-1. 2. If (xm ≠ yn), then (zk ≠ xm) implies that Z is an LCS of Xm-1 and Y Assumption: Let W be a common subsequence of Xm-1 and Y with length greater than k.(T2.1) xm ≠ yn (T2.2) (T2.1) and (T2.2) → W would also be a common subsequence of Xm and Y (T2.3) (T2.3) contradicts the assumption that Z is an LCS of X and Y. Hence, there is no other common subsequence of Xm-1 and Y which is longer than Z.
  • 7. Characterizing a LCS… The Theorem implies than an LCS of two sequences contains within it an LCS of prefixes of the two sequences. Thus, the LCS problem has an Optimal-Substructure property.
  • 8. A Recursive Solution To Find the LCS of X and Y: 1. If (xm = yn)  Find an LCS of Xm-1 and Yn-1.  Append xm = yn to this LCS. 2. If (xm ≠ yn)  Find an LCS of Xm-1 and Y  Find an LCS of X and Yn-1  Return the LCS that is longer. Overlapping Subproblems: • To find an LCS of X and Y, Find LCSs of X and Yn-1 and of Xm-1 and Y. • Each of these subproblems has the subproblem of finding an LCS of Xm-1 and Yn-1.
  • 9. A Recursive Solution… c [i, j]: Length of an LCS of the sequences Xi and Yj Algorithm LCS-LENGTH (X, Y): • c [0 . . m, 0 . . n]: Stores the c [i, j] values • b [1 . . m, 1 . . n]: Points to the table entry corresponding to the optimal subproblem solution chosen when computing c [i, j]. • c [m, n] : Contains the Length of an LCS of X and Y.
  • 10. Computing the Length of an LCS LCS-LENGTH (X, Y) m = X.length n = Y.length Let b [1. . m, 1 . . n] and c [0. . m, 0 . . n] be new tables For (i = 1 to m) c [i, 0] = 0 For (j = 0 to n) c [0, j] = 0 For (i = 1 to m) For (j = 1 to n) if (Xi == Yj) c [i, j] = c [i - 1, j - 1] + 1 b [i, j] = “ ” else if (c [i - 1, j] ≥ c [i, j - 1]) c [i, j] = c [i - 1, j] b [i, j] = “↑” else c [i, j] = c [i, j - 1] b [i, j] = “←” Return c and b
  • 11. Computing the Length of an LCS… Running Time = Ө (mn) (The algorithm has to fill n x m entries of the table) Eg.: X = <A, B, C, B, D, A, B> Y = <B, D, C, A, B, A> Result: Length of the LCS: c [7, 6] = 4 LCS: <B, C, B, A>
  • 12. PRINT-LCS (b, X, i, j) If (i == 0 or j == 0) return If (b [i, j] = “ ”) PRINT-LCS (b, X, i - 1, j - 1) print xi else If (b [i, j] = “↑”) PRINT-LCS (b, X, i - 1, j) Else PRINT-LCS (b, X, i, j - 1) Constructing an LCS • b table enables to quickly construct an LCS of X = <x1, x2, . . . xm> and Y = <y1, y2, . . . yn>. • Begin at b [m, n] and trace through the table by following the arrows. • Running Time: O (m + n)
  • 13. References: • Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein, Introduction to Algorithms, Third Edition, The MIT Press Cambridge, Massachusetts London, England.