SlideShare a Scribd company logo
The Longest Common
Subsequence Problem
CSE 373
Data Structures
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
2
Reading
Goodrich and Tamassia, 3rd ed, Chapter
12, section 11.5, pp.570-574.
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
3
Motivation
• Two Problems and Methods for String
Comparison:
› The substring problem
› The longest common subsequence
problem.
• In both cases, good algorithms do
substantially better than the brute force
methods.
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
4
String Matching Problem
• Given two strings TEXT and PATTERN,
find the first occurrence of PATTERN in
TEXT.
• Useful in text editing, document
analysis, genome analysis, etc.
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
5
String Matching Problem:
Brute-Force Algorithm
For i = 0 to n – m {
For j = 0 to m – 1 {
If TEXT[j]  PATTERN[i] then break
If j = m – 1 then return i
}
return -1;
}
Suppose
TEXT = 0000000000001
PATTERN = 0000001
This type of problem has (n2) behavior.
A more efficient algorithm is the Boyer-Moore algorithm. (We
will not be covering it in this course.)
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
6
Longest Common
Subsequence Problem
• A Longest Common Subsequence LCS
of two strings S1 and S2 is a longest
string the can be obtained from S1 and
from S2 by deleting elements.
• For example, S1 = “thoughtful” and S2
= “shuffle” have an LCS: “hufl”.
• Useful in spelling correction, document
comparison, etc.
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
7
Dynamic Programming
• Analyze the problem in terms of a
number of smaller subproblems.
• Solve the subproblems and keep their
answers in a table.
• Each subproblem’s answer is easily
computed from the answers to its own
subproblems.
1/8/2024 CSE 373 AU 04 -- Longest
Common Subsequences
8
Longest Common Subsequence:
Algorithm using Dynamic Programming
• For every prefix of S1 and prefix of S2
we’ll compute the length L of an LCS.
• In the end, we’ll get the length of an
LCS for S1 and S2 themselves.
• The subsequence can be recovered
from the matrix of L values.
• (see demonstration)

More Related Content

Similar to LongestCS (1).ppt

User_42751212015Module1and2pagestocompetework.pdf.docx
User_42751212015Module1and2pagestocompetework.pdf.docxUser_42751212015Module1and2pagestocompetework.pdf.docx
User_42751212015Module1and2pagestocompetework.pdf.docx
dickonsondorris
 
Urban strategies to promote resilient cities The case of enhancing Historic C...
Urban strategies to promote resilient cities The case of enhancing Historic C...Urban strategies to promote resilient cities The case of enhancing Historic C...
Urban strategies to promote resilient cities The case of enhancing Historic C...
inventionjournals
 

Similar to LongestCS (1).ppt (20)

Emnlp2015 reading festival_lstm_cws
Emnlp2015 reading festival_lstm_cwsEmnlp2015 reading festival_lstm_cws
Emnlp2015 reading festival_lstm_cws
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
computationalchemistry_12-6.ppt
computationalchemistry_12-6.pptcomputationalchemistry_12-6.ppt
computationalchemistry_12-6.ppt
 
Electromagnetic add-in to SolidWorks
Electromagnetic add-in to SolidWorksElectromagnetic add-in to SolidWorks
Electromagnetic add-in to SolidWorks
 
Typeset equations
Typeset equationsTypeset equations
Typeset equations
 
Сергей Кольцов —НИУ ВШЭ —ICBDA 2015
Сергей Кольцов —НИУ ВШЭ —ICBDA 2015Сергей Кольцов —НИУ ВШЭ —ICBDA 2015
Сергей Кольцов —НИУ ВШЭ —ICBDA 2015
 
Role of Tensors in Machine Learning
Role of Tensors in Machine LearningRole of Tensors in Machine Learning
Role of Tensors in Machine Learning
 
CV Newest 3
CV Newest 3CV Newest 3
CV Newest 3
 
Mathematical Modelling and Parameter Optimization of Pulsating Heat Pipes
Mathematical Modelling and Parameter Optimization of Pulsating Heat PipesMathematical Modelling and Parameter Optimization of Pulsating Heat Pipes
Mathematical Modelling and Parameter Optimization of Pulsating Heat Pipes
 
Probabilistic Methods Of Signal And System Analysis, 3rd Edition
Probabilistic Methods Of Signal And System Analysis, 3rd EditionProbabilistic Methods Of Signal And System Analysis, 3rd Edition
Probabilistic Methods Of Signal And System Analysis, 3rd Edition
 
Information Retrieval-05(wild card query_positional index_spell correction)
Information Retrieval-05(wild card query_positional index_spell correction)Information Retrieval-05(wild card query_positional index_spell correction)
Information Retrieval-05(wild card query_positional index_spell correction)
 
240311_JW_labseminar[Sequence to Sequence Learning with Neural Networks].pptx
240311_JW_labseminar[Sequence to Sequence Learning with Neural Networks].pptx240311_JW_labseminar[Sequence to Sequence Learning with Neural Networks].pptx
240311_JW_labseminar[Sequence to Sequence Learning with Neural Networks].pptx
 
Metody logiczne w analizie danych
Metody logiczne w analizie danych Metody logiczne w analizie danych
Metody logiczne w analizie danych
 
DS Lecture-1 about discrete structure .ppt
DS Lecture-1 about discrete structure .pptDS Lecture-1 about discrete structure .ppt
DS Lecture-1 about discrete structure .ppt
 
User_42751212015Module1and2pagestocompetework.pdf.docx
User_42751212015Module1and2pagestocompetework.pdf.docxUser_42751212015Module1and2pagestocompetework.pdf.docx
User_42751212015Module1and2pagestocompetework.pdf.docx
 
Ec
EcEc
Ec
 
Hybrid Machine Translation by Combining Multiple Machine Translation Systems
Hybrid Machine Translation by Combining Multiple Machine Translation SystemsHybrid Machine Translation by Combining Multiple Machine Translation Systems
Hybrid Machine Translation by Combining Multiple Machine Translation Systems
 
2015 bioinformatics database_searching_wimvancriekinge
2015 bioinformatics database_searching_wimvancriekinge2015 bioinformatics database_searching_wimvancriekinge
2015 bioinformatics database_searching_wimvancriekinge
 
Urban strategies to promote resilient cities The case of enhancing Historic C...
Urban strategies to promote resilient cities The case of enhancing Historic C...Urban strategies to promote resilient cities The case of enhancing Historic C...
Urban strategies to promote resilient cities The case of enhancing Historic C...
 
Graphs-LeX12016
Graphs-LeX12016Graphs-LeX12016
Graphs-LeX12016
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 

Recently uploaded (20)

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 

LongestCS (1).ppt

  • 1. The Longest Common Subsequence Problem CSE 373 Data Structures
  • 2. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 2 Reading Goodrich and Tamassia, 3rd ed, Chapter 12, section 11.5, pp.570-574.
  • 3. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 3 Motivation • Two Problems and Methods for String Comparison: › The substring problem › The longest common subsequence problem. • In both cases, good algorithms do substantially better than the brute force methods.
  • 4. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 4 String Matching Problem • Given two strings TEXT and PATTERN, find the first occurrence of PATTERN in TEXT. • Useful in text editing, document analysis, genome analysis, etc.
  • 5. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 5 String Matching Problem: Brute-Force Algorithm For i = 0 to n – m { For j = 0 to m – 1 { If TEXT[j]  PATTERN[i] then break If j = m – 1 then return i } return -1; } Suppose TEXT = 0000000000001 PATTERN = 0000001 This type of problem has (n2) behavior. A more efficient algorithm is the Boyer-Moore algorithm. (We will not be covering it in this course.)
  • 6. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 6 Longest Common Subsequence Problem • A Longest Common Subsequence LCS of two strings S1 and S2 is a longest string the can be obtained from S1 and from S2 by deleting elements. • For example, S1 = “thoughtful” and S2 = “shuffle” have an LCS: “hufl”. • Useful in spelling correction, document comparison, etc.
  • 7. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 7 Dynamic Programming • Analyze the problem in terms of a number of smaller subproblems. • Solve the subproblems and keep their answers in a table. • Each subproblem’s answer is easily computed from the answers to its own subproblems.
  • 8. 1/8/2024 CSE 373 AU 04 -- Longest Common Subsequences 8 Longest Common Subsequence: Algorithm using Dynamic Programming • For every prefix of S1 and prefix of S2 we’ll compute the length L of an LCS. • In the end, we’ll get the length of an LCS for S1 and S2 themselves. • The subsequence can be recovered from the matrix of L values. • (see demonstration)