SlideShare a Scribd company logo
07/22/13 1
ITCS 6114
Dynamic programming
Longest Common Subsequence
2
Dynamic programming
It is used, when the solution can be
recursively described in terms of solutions
to subproblems (optimal substructure)
Algorithm finds solutions to subproblems
and stores them in memory for later use
More efficient than “brute-force methods”,
which solve the same subproblems over
and over again
07/22/13 3
Longest Common Subsequence
(LCS)
Application: comparison of two DNA strings
Ex: X= {A B C B D A B }, Y= {B D C A B A}
Longest Common Subsequence:
X = A B C B D A B
Y = B D C A B A
Brute force algorithm would compare each
subsequence of X with the symbols in Y
07/22/13 4
LCS Algorithm
if |X| = m, |Y| = n, then there are 2m
subsequences of x; we must compare each
with Y (n comparisons)
So the running time of the brute-force
algorithm is O(n 2m
)
Notice that the LCS problem has optimal
substructure: solutions of subproblems are
parts of the final solution.
Subproblems: “find LCS of pairs of prefixes
of X and Y”
07/22/13 5
LCS Algorithm
First we’ll find the length of LCS. Later we’ll
modify the algorithm to find LCS itself.
Define Xi, Yj to be the prefixes of X and Y of
length i and j respectively
Define c[i,j] to be the length of LCS of Xi and
Yj
Then the length of LCS of X and Y will be
c[m,n]



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
07/22/13 6
LCS recursive solution
We start with i = j = 0 (empty substrings of x
and y)
Since X0 and Y0 are empty strings, their LCS is
always empty (i.e. c[0,0] = 0)
LCS of empty string and any other string is
empty, so for every i and j: c[0, j] = c[i,0] = 0



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
07/22/13 7
LCS recursive solution
When we calculate c[i,j], we consider two
cases:
First case: x[i]=y[j]: one more symbol in
strings X and Y matches, so the length of LCS
Xi and Yj equals to the length of LCS of
smaller strings Xi-1 and Yi-1 , plus 1



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
07/22/13 8
LCS recursive solution
Second case: x[i] != y[j]
As symbols don’t match, our solution is not
improved, and the length of LCS(Xi , Yj) is the
same as before (i.e. maximum of LCS(Xi, Yj-1)
and LCS(Xi-1,Yj)



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
Why not just take the length of LCS(Xi-1, Yj-1) ?
07/22/13 9
LCS Length Algorithm
LCS-Length(X, Y)
1. m = length(X) // get the # of symbols in X
2. n = length(Y) // get the # of symbols in Y
3. for i = 1 to m c[i,0] = 0 // special case: Y0
4. for j = 1 to n c[0,j] = 0 // special case: X0
5. for i = 1 to m // for all Xi
6. for j = 1 to n // for all Yj
7. if ( Xi == Yj )
8. c[i,j] = c[i-1,j-1] + 1
9. else c[i,j] = max( c[i-1,j], c[i,j-1] )
10
LCS Example
We’ll see how LCS algorithm works on the
following example:
X = ABCB
Y = BDCAB
LCS(X, Y) = BCB
X = A B C B
Y = B D C A B
What is the Longest Common Subsequence
of X and Y?
11
LCS Example (0)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,4]
ABCB
BDCAB
12
LCS Example (1)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
ABCB
BDCAB
07/22/13 13
LCS Example (2)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
0
ABCB
BDCAB
07/22/13 14
LCS Example (3)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
0 0 0
ABCB
BDCAB
07/22/13 15
LCS Example (4)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
0 0 0 1
ABCB
BDCAB
07/22/13 16
LCS Example (5)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
000 1 1
ABCB
BDCAB
07/22/13 17
LCS Example (6)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
0 0 10 1
1
ABCB
BDCAB
07/22/13 18
LCS Example (7)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 1 11
ABCB
BDCAB
07/22/13 19
LCS Example (8)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 1 1 1 2
ABCB
BDCAB
07/22/13 20
LCS Example (10)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
21 1 11
1 1
ABCB
BDCAB
07/22/13 21
LCS Example (11)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 21 11
1 1 2
ABCB
BDCAB
07/22/13 22
LCS Example (12)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 21 1
1 1 2
1
22
ABCB
BDCAB
07/22/13 23
LCS Example (13)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 21 1
1 1 2
1
22
1
ABCB
BDCAB
07/22/13 24
LCS Example (14)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 21 1
1 1 2
1
22
1 1 2 2
ABCB
BDCAB
07/22/13 25
LCS Example (15)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
B
Yj BB ACD
0
0
00000
0
0
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
1000 1
1 21 1
1 1 2
1
22
1 1 2 2 3
ABCB
BDCAB
07/22/13 26
LCS Algorithm Running Time
LCS algorithm calculates the values of each
entry of the array c[m,n]
So what is the running time?
O(m*n)
since each c[i,j] is calculated in
constant time, and there are m*n
elements in the array
27
How to find actual LCS
So far, we have just found the length of LCS,
but not LCS itself.
We want to modify this algorithm to make it
output Longest Common Subsequence of X
and Y
Each c[i,j] depends on c[i-1,j] and c[i,j-1]
or c[i-1, j-1]
For each c[i,j] we can say how it was acquired:
2
2 3
2 For example, here
c[i,j] = c[i-1,j-1] +1 = 2+1=3
07/22/13 28
How to find actual LCS - continued
Remember that



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
So we can start from c[m,n] and go backwards
Whenever c[i,j] = c[i-1, j-1]+1, remember
x[i] (because x[i] is a part of LCS)
When i=0 or j=0 (i.e. we reached the
beginning), output remembered letters in
reverse order
07/22/13 29
Finding LCS
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
Yj BB ACD
0
0
00000
0
0
0
1000 1
1 21 1
1 1 2
1
22
1 1 2 2 3B
30
Finding LCS (2)
j 0 1 2 3 4 5
0
1
2
3
4
i
Xi
A
B
C
Yj BB ACD
0
0
00000
0
0
0
1000 1
1 21 1
1 1 2
1
22
1 1 2 2 3B
B C BLCS (reversed order):
LCS (straight order): B C B
(this string turned out to be a palindrome)
07/22/13 31
Knapsack problem
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their value.
Item # Weight Value
1 1 8
2 3 6
3 5 5
07/22/13 32
Knapsack problem
There are two versions of the problem:
(1) “0-1 knapsack problem” and
(2) “Fractional knapsack problem”
(1) Items are indivisible; you either take an item
or not. Solved with dynamic programming
(2) Items are divisible: you can take any fraction
of an item. Solved with a greedy algorithm.

More Related Content

What's hot

Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theorem
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 Trajectoryeducation
Dev Singh
 
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
Dev Singh
 
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
 
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
JamesMa54
 
Maths formulae booklet (iit jee)
Maths formulae booklet (iit jee)Maths formulae booklet (iit jee)
Maths formulae booklet (iit jee)
s9182647608y
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Shakil Ahmed
 
22nd BSS meeting poster
22nd BSS meeting poster 22nd BSS meeting poster
22nd BSS meeting poster
Samuel Gbari
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
Edhole.com
 
conference_poster_5_UCSB
conference_poster_5_UCSBconference_poster_5_UCSB
conference_poster_5_UCSBXining Li
 
Complex analysis notes
Complex analysis notesComplex analysis notes
Complex analysis notes
Prakash Dabhi
 
Pc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans KeyPc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans Keyingroy
 
Hideitsu Hino
Hideitsu HinoHideitsu Hino
Hideitsu Hino
Suurist
 
Tetsunao Matsuta
Tetsunao MatsutaTetsunao Matsuta
Tetsunao Matsuta
Suurist
 
Lecture3 linear svm_with_slack
Lecture3 linear svm_with_slackLecture3 linear svm_with_slack
Lecture3 linear svm_with_slackStéphane Canu
 
structural analysis CE engg. solved ex.
structural analysis CE engg. solved ex.structural analysis CE engg. solved ex.
structural analysis CE engg. solved ex.
IMALONE1
 

What's hot (20)

Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theorem
 
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
 
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
 
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...
 
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
 
Talk5
Talk5Talk5
Talk5
 
Maths formulae booklet (iit jee)
Maths formulae booklet (iit jee)Maths formulae booklet (iit jee)
Maths formulae booklet (iit jee)
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
22nd BSS meeting poster
22nd BSS meeting poster 22nd BSS meeting poster
22nd BSS meeting poster
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
presentation
presentationpresentation
presentation
 
conference_poster_5_UCSB
conference_poster_5_UCSBconference_poster_5_UCSB
conference_poster_5_UCSB
 
Complex analysis notes
Complex analysis notesComplex analysis notes
Complex analysis notes
 
Pc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans KeyPc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans Key
 
Hideitsu Hino
Hideitsu HinoHideitsu Hino
Hideitsu Hino
 
Lecture5 kernel svm
Lecture5 kernel svmLecture5 kernel svm
Lecture5 kernel svm
 
Tetsunao Matsuta
Tetsunao MatsutaTetsunao Matsuta
Tetsunao Matsuta
 
Lecture3 linear svm_with_slack
Lecture3 linear svm_with_slackLecture3 linear svm_with_slack
Lecture3 linear svm_with_slack
 
structural analysis CE engg. solved ex.
structural analysis CE engg. solved ex.structural analysis CE engg. solved ex.
structural analysis CE engg. solved ex.
 

Similar to Dynamic Programming

Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Swati Swati
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
manasgaming4
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
Kiran K
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
Edhole.com
 
Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).
munawerzareef
 
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
AM Publications
 
A fixed point theorem for weakly c contraction mappings of integral type.
A fixed point theorem for  weakly c   contraction mappings of integral type.A fixed point theorem for  weakly c   contraction mappings of integral type.
A fixed point theorem for weakly c contraction mappings of integral type.
Alexander Decker
 
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
 
CS323: Longest Common Subsequences
CS323: Longest Common SubsequencesCS323: Longest Common Subsequences
CS323: Longest Common Subsequences
Jinho Choi
 
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Shizuoka Inst. Science and Tech.
 
SIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithmsSIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithms
Jagadeeswaran Rathinavel
 
jacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equationsjacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equations
Department of Telecommunications, Ministry of Communication & IT (INDIA)
 
Optimal control problem for processes
Optimal control problem for processesOptimal control problem for processes
Optimal control problem for processes
IJCI JOURNAL
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation Algorithms
Ajay Bidyarthy
 
Chapter four
Chapter fourChapter four
Chapter four
Mohamed Daahir
 
Ols
OlsOls
Solucao_Marion_Thornton_Dinamica_Classic (1).pdf
Solucao_Marion_Thornton_Dinamica_Classic (1).pdfSolucao_Marion_Thornton_Dinamica_Classic (1).pdf
Solucao_Marion_Thornton_Dinamica_Classic (1).pdf
FranciscoJavierCaedo
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
mailund
 

Similar to Dynamic Programming (20)

Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
 
Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).Longest common subsequence(dynamic programming).
Longest common subsequence(dynamic programming).
 
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
 
A fixed point theorem for weakly c contraction mappings of integral type.
A fixed point theorem for  weakly c   contraction mappings of integral type.A fixed point theorem for  weakly c   contraction mappings of integral type.
A fixed point theorem for weakly c contraction mappings of integral type.
 
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
 
CS323: Longest Common Subsequences
CS323: Longest Common SubsequencesCS323: Longest Common Subsequences
CS323: Longest Common Subsequences
 
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
 
Numerical Methods Solving Linear Equations
Numerical Methods Solving Linear EquationsNumerical Methods Solving Linear Equations
Numerical Methods Solving Linear Equations
 
SIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithmsSIAM - Minisymposium on Guaranteed numerical algorithms
SIAM - Minisymposium on Guaranteed numerical algorithms
 
jacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equationsjacobi method, gauss siedel for solving linear equations
jacobi method, gauss siedel for solving linear equations
 
Optimal control problem for processes
Optimal control problem for processesOptimal control problem for processes
Optimal control problem for processes
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation Algorithms
 
Chapter four
Chapter fourChapter four
Chapter four
 
Ols
OlsOls
Ols
 
Solucao_Marion_Thornton_Dinamica_Classic (1).pdf
Solucao_Marion_Thornton_Dinamica_Classic (1).pdfSolucao_Marion_Thornton_Dinamica_Classic (1).pdf
Solucao_Marion_Thornton_Dinamica_Classic (1).pdf
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
 

Recently uploaded

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
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
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
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
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

Dynamic Programming

  • 1. 07/22/13 1 ITCS 6114 Dynamic programming Longest Common Subsequence
  • 2. 2 Dynamic programming It is used, when the solution can be recursively described in terms of solutions to subproblems (optimal substructure) Algorithm finds solutions to subproblems and stores them in memory for later use More efficient than “brute-force methods”, which solve the same subproblems over and over again
  • 3. 07/22/13 3 Longest Common Subsequence (LCS) Application: comparison of two DNA strings Ex: X= {A B C B D A B }, Y= {B D C A B A} Longest Common Subsequence: X = A B C B D A B Y = B D C A B A Brute force algorithm would compare each subsequence of X with the symbols in Y
  • 4. 07/22/13 4 LCS Algorithm if |X| = m, |Y| = n, then there are 2m subsequences of x; we must compare each with Y (n comparisons) So the running time of the brute-force algorithm is O(n 2m ) Notice that the LCS problem has optimal substructure: solutions of subproblems are parts of the final solution. Subproblems: “find LCS of pairs of prefixes of X and Y”
  • 5. 07/22/13 5 LCS Algorithm First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS itself. Define Xi, Yj to be the prefixes of X and Y of length i and j respectively Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n]    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic
  • 6. 07/22/13 6 LCS recursive solution We start with i = j = 0 (empty substrings of x and y) Since X0 and Y0 are empty strings, their LCS is always empty (i.e. c[0,0] = 0) LCS of empty string and any other string is empty, so for every i and j: c[0, j] = c[i,0] = 0    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic
  • 7. 07/22/13 7 LCS recursive solution When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LCS Xi and Yj equals to the length of LCS of smaller strings Xi-1 and Yi-1 , plus 1    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic
  • 8. 07/22/13 8 LCS recursive solution Second case: x[i] != y[j] As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e. maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj)    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic Why not just take the length of LCS(Xi-1, Yj-1) ?
  • 9. 07/22/13 9 LCS Length Algorithm LCS-Length(X, Y) 1. m = length(X) // get the # of symbols in X 2. n = length(Y) // get the # of symbols in Y 3. for i = 1 to m c[i,0] = 0 // special case: Y0 4. for j = 1 to n c[0,j] = 0 // special case: X0 5. for i = 1 to m // for all Xi 6. for j = 1 to n // for all Yj 7. if ( Xi == Yj ) 8. c[i,j] = c[i-1,j-1] + 1 9. else c[i,j] = max( c[i-1,j], c[i,j-1] )
  • 10. 10 LCS Example We’ll see how LCS algorithm works on the following example: X = ABCB Y = BDCAB LCS(X, Y) = BCB X = A B C B Y = B D C A B What is the Longest Common Subsequence of X and Y?
  • 11. 11 LCS Example (0) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 Allocate array c[5,4] ABCB BDCAB
  • 12. 12 LCS Example (1) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 for i = 1 to m c[i,0] = 0 for j = 1 to n c[0,j] = 0 ABCB BDCAB
  • 13. 07/22/13 13 LCS Example (2) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 0 ABCB BDCAB
  • 14. 07/22/13 14 LCS Example (3) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 0 0 0 ABCB BDCAB
  • 15. 07/22/13 15 LCS Example (4) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 0 0 0 1 ABCB BDCAB
  • 16. 07/22/13 16 LCS Example (5) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 000 1 1 ABCB BDCAB
  • 17. 07/22/13 17 LCS Example (6) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 0 0 10 1 1 ABCB BDCAB
  • 18. 07/22/13 18 LCS Example (7) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 1 11 ABCB BDCAB
  • 19. 07/22/13 19 LCS Example (8) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 1 1 1 2 ABCB BDCAB
  • 20. 07/22/13 20 LCS Example (10) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 21 1 11 1 1 ABCB BDCAB
  • 21. 07/22/13 21 LCS Example (11) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 21 11 1 1 2 ABCB BDCAB
  • 22. 07/22/13 22 LCS Example (12) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 21 1 1 1 2 1 22 ABCB BDCAB
  • 23. 07/22/13 23 LCS Example (13) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 21 1 1 1 2 1 22 1 ABCB BDCAB
  • 24. 07/22/13 24 LCS Example (14) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 21 1 1 1 2 1 22 1 1 2 2 ABCB BDCAB
  • 25. 07/22/13 25 LCS Example (15) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C B Yj BB ACD 0 0 00000 0 0 0 if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 1000 1 1 21 1 1 1 2 1 22 1 1 2 2 3 ABCB BDCAB
  • 26. 07/22/13 26 LCS Algorithm Running Time LCS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array
  • 27. 27 How to find actual LCS So far, we have just found the length of LCS, but not LCS itself. We want to modify this algorithm to make it output Longest Common Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: 2 2 3 2 For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=3
  • 28. 07/22/13 28 How to find actual LCS - continued Remember that    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LCS) When i=0 or j=0 (i.e. we reached the beginning), output remembered letters in reverse order
  • 29. 07/22/13 29 Finding LCS j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C Yj BB ACD 0 0 00000 0 0 0 1000 1 1 21 1 1 1 2 1 22 1 1 2 2 3B
  • 30. 30 Finding LCS (2) j 0 1 2 3 4 5 0 1 2 3 4 i Xi A B C Yj BB ACD 0 0 00000 0 0 0 1000 1 1 21 1 1 1 2 1 22 1 1 2 2 3B B C BLCS (reversed order): LCS (straight order): B C B (this string turned out to be a palindrome)
  • 31. 07/22/13 31 Knapsack problem Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their value. Item # Weight Value 1 1 8 2 3 6 3 5 5
  • 32. 07/22/13 32 Knapsack problem There are two versions of the problem: (1) “0-1 knapsack problem” and (2) “Fractional knapsack problem” (1) Items are indivisible; you either take an item or not. Solved with dynamic programming (2) Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm.