SlideShare a Scribd company logo
Longest Common Subsequence
Subsequences
• A subsequence is a sequence that appears in the same
relative order, but not necessarily contiguous.
• In LCS ,we have to find Longest Common
Subsequence that is in the same relative order.
• String of length n has 2^n different possible
subsequences.
• E.g.—
• Subsequences of “ABCDEFG”.
• “ABC”,”ABG”,”BDF”,”AEG”,’ACEFG”,…….
2
Common Subsequences
Suppose that X and Y are two sequences
over a set S.
X: ABCBDAB
Y: BDCABA
Z: BCBA
We say that Z is a common subsequence
of X and Y if and only if
• Z is a subsequence of X
• Z is a subsequence of Y 3
The Longest Common
Subsequence Problem
Given two sequences X and Y over a set S,
the longest common subsequence problem
asks to find a common subsequence of X
and Y that is of maximal length.
Z= (B,C,A) Length 3
Z= (B,C,A,B) Length 4
Z= (B,D,A,B) Length 4
4
Longest
LCS Notation
Let X and Y be sequences.
We denote by LCS(X, Y) the set of
longest common subsequences of X and
Y.
LCS(X,Y)
Functional notation,
but not a function
5
6
A Poor Approach to the LCS
Problem
• A Brute-force solution:
• Enumerate all subsequences of X
• Test which ones are also subsequences of Y
• Pick the longest one.
• Analysis:
• If X is of length n, then it has 2n
subsequences
• This is an exponential-time algorithm!
Dynamic Programming
Let us try to develop a dynamic
programming solution to the LCS problem.
7
Optimal Substructure
Let X = ( x1,x2,…,xm)
and Y = ( y1,y2,…,yn) be two sequences.
Let Z = ( z1,z2,…,zk) is any LCS of X and Y.
a) If xm = yn then certainly xm = yn = zk
and Zk-1 is in LCS(Xm-1 , Yn-1)
8
Optimal Substructure (2)
Let X = (x1,x2,…,xm)
and Y = ( y1,y2,…,yn) be two sequences.
Let Z = ( z1,z2,…,zk) is any LCS of X and Y.
b) If xm =! yn then xm =! zk implies that Z is
in LCS(Xm-1 , Y)
c)If xm =! yn then yn =! zk implies that Z is
in LCS(X, Yn-1)
9
Recursive Solution
Let X and Y be sequences.
Let c[i,j] be the length of an element in LCS(Xi, Yj).
c[i,j] =
10
Dynamic Programming Solution
• Define L[i,j] to be the length of the longest common
subsequence of X[0..i] and Y[0..j].
• L[i,j-1] = 0 and L[i-1,j]=0, to indicate that the null
part of X or Y has no match with the other.
• Then we can define L[i,j] in the general case as
follows:
1. If xi=yj, then L[i,j] = L[i-1,j-1] + 1 (we can add this
match)
2. If xi≠yj, then L[i,j] = max{L[i-1,j], L[i,j-1]} (we
have no match here)
X:ABCB
Y:BDCA LCS:BC 11
Dynamic Programming Solution (2)
How can we get an actual longest common
subsequence?
Store in addition to the array c an array
b pointing to the optimal subproblem
chosen when computing c[i,j].
12
13
Example
T yj B D C A
Xi 0 0 0 0 0
A 0 0 0 0 1
B 0 1 1 1 1
C 0 1 1 2 2
B 0 1 1 2 2
Start at b[m,n]. Follow the arrows. Each
diagonal array gives one element of the LCS.
14
ALGORITHM
LCS(X,Y)
m ← length[X]
n ← length[Y]
for i ← 1 to m do
c[i,0] ← 0
for j ← 1 to n do
c[0,j] ← 0
15
LCS(X,Y)
for i ← 1 to m do
for j ← 1 to n do
if xi
= yj
c[i, j] ← c[i-1, j-1]+1
b[i, j] ← “D”
else
if c[i-1, j] ≥ c[i, j-1]
c[i, j] ← c[i-1, j]
b[i, j] ← “U”
else
c[i, j] ← c[i, j-1]
b[i, j] ← “L”
return c and b
CONSTRUCTING AN LCS
• PRINT-LCS(b, X, i, j)
1. If i=0 or j=0
2. then return
3.If b[i, j]=“D”
4. then PRINT- LCS(b,X,i-1,j-1)
5. print xi
6.Else if b[i, j]=“U”
7. then PRINT-LCS(b,X,i-1,j)
8.Else PRINT-LCS(b,X,i,j-1) 16
Analysis of LCS Algorithm
• We have two nested loops
• The outer one iterates n times
• The inner one iterates m times
• A constant amount of work is done inside
each iteration of the inner loop
• Thus, the total running time is O(nm)
• Answer is contained in L[n,m] (and the
subsequence can be recovered from the
T table).
17
usage
• Biological applications often need to
compare the DNA of two (or more)
different organisms.
• We can say that two DNA strands are
similar if one is a substring of the
other.
18

More Related Content

What's hot

Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
Tanmay Baranwal
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
 
Satisfiability
SatisfiabilitySatisfiability
Satisfiability
Jim Kukula
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
Kiran K
 
LCS Presentation
LCS PresentationLCS Presentation
LCS Presentation
Tania Sahito
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
Badrul Alam
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Syeda
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
Waqar Akram
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Non- Deterministic Algorithms
Non- Deterministic AlgorithmsNon- Deterministic Algorithms
Non- Deterministic Algorithms
Dipankar Boruah
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
Amit Kumar Rathi
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 

What's hot (20)

Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Satisfiability
SatisfiabilitySatisfiability
Satisfiability
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
LCS Presentation
LCS PresentationLCS Presentation
LCS Presentation
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Non- Deterministic Algorithms
Non- Deterministic AlgorithmsNon- Deterministic Algorithms
Non- Deterministic Algorithms
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 

Similar to Longest common subsequence(dynamic programming).

Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
Edhole.com
 
17-dynprog2.ppt
17-dynprog2.ppt17-dynprog2.ppt
17-dynprog2.ppt
GGHSJANDAWALA
 
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
Shanmuganathan C
 
lecture 24
lecture 24lecture 24
lecture 24sajinsc
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programmingklyni777
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
manasgaming4
 
Beam buckling
Beam bucklingBeam buckling
Beam buckling
Mohanad Habh
 
Animation in video games
Animation in video gamesAnimation in video games
Animation in video gamesXban1
 
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Ali Ajouz
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
alt klausur
alt klausuralt klausur
alt klausur
zhongchengdai
 
RNA Secondary Structure Prediction
RNA Secondary Structure PredictionRNA Secondary Structure Prediction
RNA Secondary Structure PredictionSumin Byeon
 
Cs229 notes7a
Cs229 notes7aCs229 notes7a
Cs229 notes7a
VuTran231
 
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppte048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
ChandraKumarGalaju
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)NYversity
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Vivekananda Samiti
 

Similar to Longest common subsequence(dynamic programming). (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
 
lecture 24
lecture 24lecture 24
lecture 24
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Beam buckling
Beam bucklingBeam buckling
Beam buckling
 
Animation in video games
Animation in video gamesAnimation in video games
Animation in video games
 
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
Hecke Operators on Jacobi Forms of Lattice Index and the Relation to Elliptic...
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
alt klausur
alt klausuralt klausur
alt klausur
 
RNA Secondary Structure Prediction
RNA Secondary Structure PredictionRNA Secondary Structure Prediction
RNA Secondary Structure Prediction
 
Som3
Som3Som3
Som3
 
Cs229 notes7a
Cs229 notes7aCs229 notes7a
Cs229 notes7a
 
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppte048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
e048b527dca76980222bf258a30545f8_MIT15_082JF10_lec16.ppt
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
 

Recently uploaded

一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 

Recently uploaded (20)

一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 

Longest common subsequence(dynamic programming).

  • 2. Subsequences • A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. • In LCS ,we have to find Longest Common Subsequence that is in the same relative order. • String of length n has 2^n different possible subsequences. • E.g.— • Subsequences of “ABCDEFG”. • “ABC”,”ABG”,”BDF”,”AEG”,’ACEFG”,……. 2
  • 3. Common Subsequences Suppose that X and Y are two sequences over a set S. X: ABCBDAB Y: BDCABA Z: BCBA We say that Z is a common subsequence of X and Y if and only if • Z is a subsequence of X • Z is a subsequence of Y 3
  • 4. The Longest Common Subsequence Problem Given two sequences X and Y over a set S, the longest common subsequence problem asks to find a common subsequence of X and Y that is of maximal length. Z= (B,C,A) Length 3 Z= (B,C,A,B) Length 4 Z= (B,D,A,B) Length 4 4 Longest
  • 5. LCS Notation Let X and Y be sequences. We denote by LCS(X, Y) the set of longest common subsequences of X and Y. LCS(X,Y) Functional notation, but not a function 5
  • 6. 6 A Poor Approach to the LCS Problem • A Brute-force solution: • Enumerate all subsequences of X • Test which ones are also subsequences of Y • Pick the longest one. • Analysis: • If X is of length n, then it has 2n subsequences • This is an exponential-time algorithm!
  • 7. Dynamic Programming Let us try to develop a dynamic programming solution to the LCS problem. 7
  • 8. Optimal Substructure Let X = ( x1,x2,…,xm) and Y = ( y1,y2,…,yn) be two sequences. Let Z = ( z1,z2,…,zk) is any LCS of X and Y. a) If xm = yn then certainly xm = yn = zk and Zk-1 is in LCS(Xm-1 , Yn-1) 8
  • 9. Optimal Substructure (2) Let X = (x1,x2,…,xm) and Y = ( y1,y2,…,yn) be two sequences. Let Z = ( z1,z2,…,zk) is any LCS of X and Y. b) If xm =! yn then xm =! zk implies that Z is in LCS(Xm-1 , Y) c)If xm =! yn then yn =! zk implies that Z is in LCS(X, Yn-1) 9
  • 10. Recursive Solution Let X and Y be sequences. Let c[i,j] be the length of an element in LCS(Xi, Yj). c[i,j] = 10
  • 11. Dynamic Programming Solution • Define L[i,j] to be the length of the longest common subsequence of X[0..i] and Y[0..j]. • L[i,j-1] = 0 and L[i-1,j]=0, to indicate that the null part of X or Y has no match with the other. • Then we can define L[i,j] in the general case as follows: 1. If xi=yj, then L[i,j] = L[i-1,j-1] + 1 (we can add this match) 2. If xi≠yj, then L[i,j] = max{L[i-1,j], L[i,j-1]} (we have no match here) X:ABCB Y:BDCA LCS:BC 11
  • 12. Dynamic Programming Solution (2) How can we get an actual longest common subsequence? Store in addition to the array c an array b pointing to the optimal subproblem chosen when computing c[i,j]. 12
  • 13. 13 Example T yj B D C A Xi 0 0 0 0 0 A 0 0 0 0 1 B 0 1 1 1 1 C 0 1 1 2 2 B 0 1 1 2 2 Start at b[m,n]. Follow the arrows. Each diagonal array gives one element of the LCS.
  • 14. 14 ALGORITHM LCS(X,Y) m ← length[X] n ← length[Y] for i ← 1 to m do c[i,0] ← 0 for j ← 1 to n do c[0,j] ← 0
  • 15. 15 LCS(X,Y) for i ← 1 to m do for j ← 1 to n do if xi = yj c[i, j] ← c[i-1, j-1]+1 b[i, j] ← “D” else if c[i-1, j] ≥ c[i, j-1] c[i, j] ← c[i-1, j] b[i, j] ← “U” else c[i, j] ← c[i, j-1] b[i, j] ← “L” return c and b
  • 16. CONSTRUCTING AN LCS • PRINT-LCS(b, X, i, j) 1. If i=0 or j=0 2. then return 3.If b[i, j]=“D” 4. then PRINT- LCS(b,X,i-1,j-1) 5. print xi 6.Else if b[i, j]=“U” 7. then PRINT-LCS(b,X,i-1,j) 8.Else PRINT-LCS(b,X,i,j-1) 16
  • 17. Analysis of LCS Algorithm • We have two nested loops • The outer one iterates n times • The inner one iterates m times • A constant amount of work is done inside each iteration of the inner loop • Thus, the total running time is O(nm) • Answer is contained in L[n,m] (and the subsequence can be recovered from the T table). 17
  • 18. usage • Biological applications often need to compare the DNA of two (or more) different organisms. • We can say that two DNA strands are similar if one is a substring of the other. 18