SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Chapter 16
Dynamic Programming
What is Dynamic Programming?
 Solves problems by combining solutions to
subproblems
 Similar to divide-and-conquer
 Applicable when subproblems are not independent
 Subproblems share sub-subproblems
 Common sub-subproblems are computed once, then
answers are reused as needed
 Typically used to solve optimization problems
What Are Optimization Problems?
 Problems for which there may be many
solutions
 Each solution has a value
 We want the solution that has an “optimal” value
 Often the minimum or maximum value
 There may be more than one “optimal” solution
Development of DP Algorithms
 Four steps:
 Characterize the structure of an optimal solution
 Recursively define the value of an optimal solution
 Compute the value of an optimal solution in a bottom-up
fashion
 Construct an optimal solution from computed information
Matrix-Chain Multiplication
 Given a sequence of matrices A1..An, we wish to compute their
product
 This can be computed by repeated multiplication of matrix pairs
using the standard algorithm (next slide)
 Ambiguities must be resolved in advance by parenthesizing the
matrices
 Matrix multiplication is associative, so all parenthesizations yield
the same product
 While order of parenthesizing isn’t mathematically important, it
can make a dramatic difference to the cost of evaluation
Matrix Multiplication Algorithm
Matrix MatrixMultiply(const Matrix &A, const Matrix &B)
{
if ( A.columns() != B.rows() )
throw XIncompatibleDimensions;
Matrix C(A.rows(), B.columns());
for ( int i = 0 ; i < A.rows ; ++i )
{
for ( int j = 0 ; j < B.columns ; ++j )
{
C(i,j) = 0;
for ( int k = 0 ; k < A.columns ; ++k )
C(i,j) += A(i,k)*B(k,j);
}
}
}
Matrix Multiplication Algorithm
 To multiply two matrices A (p x q) and B (q x
r), the number of columns in A must equal the
number of rows in B.
 The resulting matrix is p x r
 Running time of algorithm is O(pqr)
Why Parenthesization Order Matters
 Consider three matrices: 10x100, 100x5, and
5x50
 If we multiply (A1A2)A3, we perform 10x100x5 +
10x5x50 = 7,500 multiplications
 If we multiply A1(A2A3), we perform 100x5x50 +
10x100x50 = 75,000 multiplications
The Matrix-Chain Multiplication Problem
 Given a chain <A1,A2,…,An> of n
matrices, where for i = 1, 2, …, n, the matrix
Ai has dimension pi-1xpi, fully parenthesize
the product A1A2…An in a way that minimizes
the number of scalar multiplications
Structure of an Optimal Parenthesization
 To compute the product of all matrices (denoted as
A1..n), we must first:
 Compute products A1..k and Ak+1..n
 Multiply them together
 The cost of computing A1..n is then just the cost of
computing A1..k, plus the cost of computing
Ak+1..n, plus the cost of multiplying them together
Structure of an Optimal Parenthesization
 The parenthesization of the “prefix” subchain A1,…,Ak and “suffix”
subchain Ak+1,…,An within the optimal parenthesization of
A1,…,An must be optimal
 If it weren’t, then there would be some other parenthesization
order with lower cost, which would result in non-optimal
parenthesization of A1,…,An
 Optimal solution to the whole problem therefore contains optimal
subproblem solutions
 This is a hallmark of dynamic programming
A Recursive Solution
 Our subproblem is the problem of
determining the minimum cost of a
parenthesization of Ai,…,Aj, where 1 <= i <= j
<= n
 Let m[i,j] be the minimum number of
multiplications needed to compute Ai..j
 Lowest cost to compute A1..n is m[1,n]
A Recursive Solution
 How do we recursively define m[i,j]?
 If i = j, no multiplications are necessary since we never
have to multiply a matrix by itself
 Thus, m[i,i] = 0 for all i
 If i < j, we compute as follows:
 Assume that there is some optimal parenthesization that splits
the range between k and k+1
 m[i,j] = m[i,k]+m[k+1,j]+pi-1pkpj, per our previous discussion
 To do this, we must find k
 There are j-i possibilities, which can be checked directly
A Recursive Solution
 Our recursive solution is now:
 m[i,j] gives the costs of optimal solutions to
subproblems
 Define a second table s[i,j] to help keep track of
how to construct an optimal solution
 Each entry contains the value k at which to split the
product
ji
ji
pppjkmkim
jim
jki
jki
if
if
}],1[],[{min
0
],[
1
Computing the Optimal Costs
 An algorithm to compute the minimum cost
for a matrix-chain multiplication should now
be simple
 It will take exponential time, which is no better
than our brute force approach
 So how can we improve the running time?
Computing the Optimal Costs
 There are only O(n2) total subproblems
 One subproblem for each choice of i and j
satisfying 1 <= i <= j <= n
 A recursive algorithm may encounter each
subproblem many times
 Recomputations of known values is costly
 This is where dynamic programming techniques
are superior
Computing the Optimal Costs
 Instead of using recursion, we will use a the
dynamic programming bottom-up approach to
compute the cost
 The following algorithm assumes that the
matrix Ai has dimension pi-1xpi
 The input sequence is an array <p0, p1, …, pn>
with length n+1
 The output is the matrices m and s
MatrixChainOrder(const double p[], int size, Matrix
&m, Matrix &s)
{
int n = size-1;
for ( int i = 1 ; i <= n ; ++i )
m(i,i) = 0;
for ( int L = 2 ; L <= n ; ++L ) {
for ( int i = 1 ; i <= n-L+1 ; ++i ) {
for ( int j = 0 ; j <= i+L-1 ; ++j ) {
m(i,j) = MAXDOUBLE;
for ( int k = i ; k <= j-1 ; ++k ) {
int q = m(i,k)+m(k+1,j)+p[i-1]*p[k]*p[j];
if ( q < m(i,j) ) m(i,j) = q;
else s(i,j) = k;
} // for ( k )
} // for ( j )
} // for ( i )
} // for ( L )
}
Example: m and s
0 15,750 7,875 9.375 11,875 15.125
0 2,625 4,375 7,125 10,500
0 750 2,500 5,375
0 1,000 3,500
0 5,000
0
Matrix m
1 1 3 3 3
2 3 3 3
3 3 3
4 5
5
Matrix s
Input dimensions: 30x35, 35x15, 15x5, 5x10, 10x20, 20x25
Computing the Optimal Costs
 The matrix m contains the costs of
multiplications, and s contains which index of
k was used to achieve the optimal cost
 What is the running time?
 Three nested loops = O(n3)
 This is better than the exponential running time
the recurrence would give us
Constructing an Optimal Solution
 So far, we only know the optimal number of scalar
multiplications, not the order in which to multiply the matrices
 This information is encoded in the table s
 Each entry s[i,j] records the value k such that the optimal
parenthesization of Ai…Aj occurs between matrix k and k+1
 To compute the product A1..n, we parenthesize at s[1,n]
 Previous matrix multiplications can be computed recursively
 E.g., s[1,s[1,n]] contains the optimal split for the left half of the
multiplication
Constructing an Optimal Solution
MatrixChainMultiply(const Matrix A[], const Matrix &s, int
i, int j)
{
if ( j > i )
{
Matrix X = MatrixChainMultiply(A, s, i, s(i,j));
Matrix Y = MatrixChainMultiply(A, s, s(i,j)+1, j);
return MatrixMultiply(X,Y);
}
else
return A[i];
}
Elements of Dynamic Programming
 There are two key ingredients that an
optimization problem must have for dynamic
programming to be applicable:
 Optimal substructure
 Overlapping subproblems
Optimal Substructure
 A problem that exhibits optimal substructure if an
optimal solution to the problem contains within it
optimal solutions to subproblems
 E.g., matrix-chain multiplication exhibited this property
 An optimal parenthesization of a matrix chain requires that
each sub-chain also be optimally parenthesized
 This property is typically shown by assuming that a better
solution exists, then showing how this contradicts the
optimality of the solution to the original problem
Overlapping Subproblems
 The “space” for subproblems must be relatively
small
 i.e., a recursive algorithm for the solution would end up
solving the same subproblems over and over
 This is called overlapping subproblems
 Dynamic programming algorithms typically compute
overlapping subproblems once and store the
solution in a table for later (constant-time) lookup
Overlapping Subproblems
 From the matrix-chain algorithm, we see earlier
computations being reused to perform later
computations:
 What if we replaced this with a recursive
algorithm?
 Figure 16.2 on page 311 shows the added
computations
int q = m(i,k)+m(k+1,j)+p[i-1]*p[k]*p[j];
if ( q < m(i,j) )
m(i,j) = q;
else
s(i,j) = k;
Exercise
 A common recursive algorithm for computing the
Fibonacci sequence is:
2if
2if
1if
)2()1(
1
1
)(
x
x
x
xFibxFib
xFib
 Is dynamic programming applicable? Why?
 Write a dynamic programming algorithm for
solving this problem
Longest Common Subsequence
 A subsequence is simply a part of a
sequence, consisting of some number of
consecutive elements
 Formally:
 If X is a sequence of size m, and Z is a sequence
of size k, Z is a subsequence of X if there exists
some j such that for 1<= i <= k, we have xi+j=zi
Longest Common Subsequence
 Given two sequences X and Y, Z is a
common subsequence of X and Y if Z is a
subsequence of both X and Y
 The longest common subsequence problem
is thus the problem of finding the longest
common subsequence of two given
sequences
Brute-Force Approach
 Enumerate all subsequences in X to see if
they are also subsequences of Y, and keep
track of the longest one found
 If X is of size m, that’s 2m possibilities!
Characterizing an LCS
 Does the LCS problem exhibit an optimal
substructure property?
 Yes, corresponding to pairs of “prefixes”
 A prefix of a sequence is simply the
beginning portion of a sequence for some
specified length
 e.g., if X = <A,B,C,B,D,A,B>, the fourth prefix of X
(X4) is <A,B,C,B>
Characterizing an LCS
 From Theorem 16.1: Let
X=<x1,…,xm>, Y=<y1,…,yn>, and Z=<z1,…,zk> be
any LCS of X and Y
 If xm=yn, then zk=xm=yn and Zk-1 is an LCS of Xm-1 and Yn-1
 If xm!=yn, then zk!=xm implies that Z is an LCS of Xm-1 and Y
 If xm!=yn, then zk!=yn implies that Z is an LCS of X and Yn-1
Characterizing an LCS
 What does this mean?
 An LCS of two sequences contains within it an
LCS of prefixes of the two sequences
 This is just the optimal substructure property
A Recursive Solution To Subproblems
 Theorem 16.1 gives us two conditions to check for:
 If xm = yn, we need to find an LCS of Xm-1 & Yn-1, to which we
append xm = yn
 If xm != yn, then we must solve two subproblems: finding an LCS
of Xm & Yn-1, and an LCS of Xm-1 & Yn
 Whichever is longer is the LCS of X & Y
 Overlapping subproblems are evident
 To find an LCS of X & Y, we must find LCS of Xm-1 and/or Yn-
1, which have still smaller overlapping subproblems
A Recursive Solution To Subproblems
 What is the cost of an optimal solution?
 Let c[i,j] be the length of an LCS of Xi & Yj
 If i or j = 0, then the LCS for that subsequence has length 0
 Otherwise, the cost follows directly from Theorem 16.1:
ji
ji
yxji
yxji
ji
jicjic
jicjic
and0,if
and0,if
0or0if
],1[],1,[max(
1]1,1[
0
],[
Computing the Length of an LCS
 A recursive algorithm for computing the length of an
LCS of two sequences can be written directly from
the recurrence formula for the cost of an optimal
solution
 This recursive algorithm will lead to an exponential-time
solution
 Dynamic programming techniques can be used to compute
the solution bottom-up and reduce the expected running
time
Computing the Length of an LCS
 The following algorithm fills in the cost table c
based on the input sequences X and Y
 It also maintains a table b that helps simplify an
optimal solution
 Entry b[i,j] points to the table entry corresponding to the
optimal subproblem solution chosen when computing
c[i,j]
Computing the Length of an LCS
void LCSLength(const sequence &X, const sequence
&Y, matrix &b, matrix &c)
{
int m = X.length, n = Y.length;
// Initialize tables
for ( int i = 0 ; i < m ; ++i )
c(i,0) = 0;
for ( int j = 0 ; j < m ; ++j )
c(0,j) = 0;
Computing the Length of an LCS
// Fill in tables
for ( int i = 1 ; i < m ; ++i )
for ( int j = 1 ; j < n ; ++j ) {
if ( x[i] == y[j] ) {
c(i,j) = c(i-1,j-1)+1;
b(i,j) = 1; // Subproblem type 1 “ã”
}
Computing the Length of an LCS
else if ( c(i-1,j) >= c(i,j-1) ) {
c(i,j) = c(i-1, j);
b(i,j) = 2; // Subproblem type 2 “á”
}
else {
c(i,j) = c(i, j-1);
b(i,j) = 3; // Subproblem type 3 “ß”
}
}
}
Constructing an LCS
 Table b can now be used to construct the
LCS of two sequences
 Begin in the bottom right corner of b, and follow
the “arrows”
 This will build the LCS in reverse order
Constructing an LCS
LCSPrint(const matrix &b, const sequence &X, int
i, int j)
{
if ( i == 0 || j == 0 )
return;
switch ( b(i,j) ) {
case 1: LCSPrint(b, X, i-1, j-1); break;
case 2: LCSPrint(b, X, i-1, j); break;
case 3: LCSPrint(b, X, i, j-1); break;
}
}
What is the Running Time to Find an
LCS?
 Total running time is now the cost to build the
tables + the cost of printing it out
 Table building = O(mn)
 Printing = O(m+n)
 So, total cost is O(mn)

Weitere ähnliche Inhalte

Was ist angesagt?

finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...MLconf
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingzukun
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...MLconf
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
CS229 Machine Learning Lecture Notes
CS229 Machine Learning Lecture NotesCS229 Machine Learning Lecture Notes
CS229 Machine Learning Lecture NotesEric Conner
 
Fuzzy inventory model with shortages in man power planning
Fuzzy inventory model with shortages in man power planningFuzzy inventory model with shortages in man power planning
Fuzzy inventory model with shortages in man power planningAlexander Decker
 
Computational Complexity: Introduction-Turing Machines-Undecidability
Computational Complexity: Introduction-Turing Machines-UndecidabilityComputational Complexity: Introduction-Turing Machines-Undecidability
Computational Complexity: Introduction-Turing Machines-UndecidabilityAntonis Antonopoulos
 
Computational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyComputational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyAntonis Antonopoulos
 
Algorithmics on SLP-compressed strings
Algorithmics on SLP-compressed stringsAlgorithmics on SLP-compressed strings
Algorithmics on SLP-compressed stringsAntonis Antonopoulos
 

Was ist angesagt? (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
DP
DPDP
DP
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
Animashree Anandkumar, Electrical Engineering and CS Dept, UC Irvine at MLcon...
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
CS229 Machine Learning Lecture Notes
CS229 Machine Learning Lecture NotesCS229 Machine Learning Lecture Notes
CS229 Machine Learning Lecture Notes
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Fuzzy inventory model with shortages in man power planning
Fuzzy inventory model with shortages in man power planningFuzzy inventory model with shortages in man power planning
Fuzzy inventory model with shortages in man power planning
 
algorithm Unit 5
algorithm Unit 5 algorithm Unit 5
algorithm Unit 5
 
Greedy method
Greedy method Greedy method
Greedy method
 
Computational Complexity: Introduction-Turing Machines-Undecidability
Computational Complexity: Introduction-Turing Machines-UndecidabilityComputational Complexity: Introduction-Turing Machines-Undecidability
Computational Complexity: Introduction-Turing Machines-Undecidability
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Computational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyComputational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial Hierarchy
 
Algorithmics on SLP-compressed strings
Algorithmics on SLP-compressed stringsAlgorithmics on SLP-compressed strings
Algorithmics on SLP-compressed strings
 

Andere mochten auch

Practica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasPractica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasNanettt
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationzukun
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesAnkita Karia
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 

Andere mochten auch (20)

Data struters
Data strutersData struters
Data struters
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Cis435 week03
Cis435 week03Cis435 week03
Cis435 week03
 
Cis435 week04
Cis435 week04Cis435 week04
Cis435 week04
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
5 searching
5 searching5 searching
5 searching
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Practica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasPractica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias Humanas
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notation
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
lecture 5
lecture 5lecture 5
lecture 5
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
4 recursion
4 recursion4 recursion
4 recursion
 

Ähnlich wie Chapter 16

Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1debolina13
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxHarshitSingh334328
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosluzenith_g
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhdanielgetachew0922
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmRajKumar323561
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfyashodamb
 
X01 Supervised learning problem linear regression one feature theorie
X01 Supervised learning problem linear regression one feature theorieX01 Supervised learning problem linear regression one feature theorie
X01 Supervised learning problem linear regression one feature theorieMarco Moldenhauer
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.pptKerbauBakar
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
Machine learning
Machine learningMachine learning
Machine learningShreyas G S
 

Ähnlich wie Chapter 16 (20)

Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Alg1
Alg1Alg1
Alg1
 
Proficient Computer Network Assignment Help
Proficient Computer Network Assignment HelpProficient Computer Network Assignment Help
Proficient Computer Network Assignment Help
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 
X01 Supervised learning problem linear regression one feature theorie
X01 Supervised learning problem linear regression one feature theorieX01 Supervised learning problem linear regression one feature theorie
X01 Supervised learning problem linear regression one feature theorie
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
Machine learning
Machine learningMachine learning
Machine learning
 

Kürzlich hochgeladen

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 

Kürzlich hochgeladen (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

Chapter 16

  • 2. What is Dynamic Programming?  Solves problems by combining solutions to subproblems  Similar to divide-and-conquer  Applicable when subproblems are not independent  Subproblems share sub-subproblems  Common sub-subproblems are computed once, then answers are reused as needed  Typically used to solve optimization problems
  • 3. What Are Optimization Problems?  Problems for which there may be many solutions  Each solution has a value  We want the solution that has an “optimal” value  Often the minimum or maximum value  There may be more than one “optimal” solution
  • 4. Development of DP Algorithms  Four steps:  Characterize the structure of an optimal solution  Recursively define the value of an optimal solution  Compute the value of an optimal solution in a bottom-up fashion  Construct an optimal solution from computed information
  • 5. Matrix-Chain Multiplication  Given a sequence of matrices A1..An, we wish to compute their product  This can be computed by repeated multiplication of matrix pairs using the standard algorithm (next slide)  Ambiguities must be resolved in advance by parenthesizing the matrices  Matrix multiplication is associative, so all parenthesizations yield the same product  While order of parenthesizing isn’t mathematically important, it can make a dramatic difference to the cost of evaluation
  • 6. Matrix Multiplication Algorithm Matrix MatrixMultiply(const Matrix &A, const Matrix &B) { if ( A.columns() != B.rows() ) throw XIncompatibleDimensions; Matrix C(A.rows(), B.columns()); for ( int i = 0 ; i < A.rows ; ++i ) { for ( int j = 0 ; j < B.columns ; ++j ) { C(i,j) = 0; for ( int k = 0 ; k < A.columns ; ++k ) C(i,j) += A(i,k)*B(k,j); } } }
  • 7. Matrix Multiplication Algorithm  To multiply two matrices A (p x q) and B (q x r), the number of columns in A must equal the number of rows in B.  The resulting matrix is p x r  Running time of algorithm is O(pqr)
  • 8. Why Parenthesization Order Matters  Consider three matrices: 10x100, 100x5, and 5x50  If we multiply (A1A2)A3, we perform 10x100x5 + 10x5x50 = 7,500 multiplications  If we multiply A1(A2A3), we perform 100x5x50 + 10x100x50 = 75,000 multiplications
  • 9. The Matrix-Chain Multiplication Problem  Given a chain <A1,A2,…,An> of n matrices, where for i = 1, 2, …, n, the matrix Ai has dimension pi-1xpi, fully parenthesize the product A1A2…An in a way that minimizes the number of scalar multiplications
  • 10. Structure of an Optimal Parenthesization  To compute the product of all matrices (denoted as A1..n), we must first:  Compute products A1..k and Ak+1..n  Multiply them together  The cost of computing A1..n is then just the cost of computing A1..k, plus the cost of computing Ak+1..n, plus the cost of multiplying them together
  • 11. Structure of an Optimal Parenthesization  The parenthesization of the “prefix” subchain A1,…,Ak and “suffix” subchain Ak+1,…,An within the optimal parenthesization of A1,…,An must be optimal  If it weren’t, then there would be some other parenthesization order with lower cost, which would result in non-optimal parenthesization of A1,…,An  Optimal solution to the whole problem therefore contains optimal subproblem solutions  This is a hallmark of dynamic programming
  • 12. A Recursive Solution  Our subproblem is the problem of determining the minimum cost of a parenthesization of Ai,…,Aj, where 1 <= i <= j <= n  Let m[i,j] be the minimum number of multiplications needed to compute Ai..j  Lowest cost to compute A1..n is m[1,n]
  • 13. A Recursive Solution  How do we recursively define m[i,j]?  If i = j, no multiplications are necessary since we never have to multiply a matrix by itself  Thus, m[i,i] = 0 for all i  If i < j, we compute as follows:  Assume that there is some optimal parenthesization that splits the range between k and k+1  m[i,j] = m[i,k]+m[k+1,j]+pi-1pkpj, per our previous discussion  To do this, we must find k  There are j-i possibilities, which can be checked directly
  • 14. A Recursive Solution  Our recursive solution is now:  m[i,j] gives the costs of optimal solutions to subproblems  Define a second table s[i,j] to help keep track of how to construct an optimal solution  Each entry contains the value k at which to split the product ji ji pppjkmkim jim jki jki if if }],1[],[{min 0 ],[ 1
  • 15. Computing the Optimal Costs  An algorithm to compute the minimum cost for a matrix-chain multiplication should now be simple  It will take exponential time, which is no better than our brute force approach  So how can we improve the running time?
  • 16. Computing the Optimal Costs  There are only O(n2) total subproblems  One subproblem for each choice of i and j satisfying 1 <= i <= j <= n  A recursive algorithm may encounter each subproblem many times  Recomputations of known values is costly  This is where dynamic programming techniques are superior
  • 17. Computing the Optimal Costs  Instead of using recursion, we will use a the dynamic programming bottom-up approach to compute the cost  The following algorithm assumes that the matrix Ai has dimension pi-1xpi  The input sequence is an array <p0, p1, …, pn> with length n+1  The output is the matrices m and s
  • 18. MatrixChainOrder(const double p[], int size, Matrix &m, Matrix &s) { int n = size-1; for ( int i = 1 ; i <= n ; ++i ) m(i,i) = 0; for ( int L = 2 ; L <= n ; ++L ) { for ( int i = 1 ; i <= n-L+1 ; ++i ) { for ( int j = 0 ; j <= i+L-1 ; ++j ) { m(i,j) = MAXDOUBLE; for ( int k = i ; k <= j-1 ; ++k ) { int q = m(i,k)+m(k+1,j)+p[i-1]*p[k]*p[j]; if ( q < m(i,j) ) m(i,j) = q; else s(i,j) = k; } // for ( k ) } // for ( j ) } // for ( i ) } // for ( L ) }
  • 19. Example: m and s 0 15,750 7,875 9.375 11,875 15.125 0 2,625 4,375 7,125 10,500 0 750 2,500 5,375 0 1,000 3,500 0 5,000 0 Matrix m 1 1 3 3 3 2 3 3 3 3 3 3 4 5 5 Matrix s Input dimensions: 30x35, 35x15, 15x5, 5x10, 10x20, 20x25
  • 20. Computing the Optimal Costs  The matrix m contains the costs of multiplications, and s contains which index of k was used to achieve the optimal cost  What is the running time?  Three nested loops = O(n3)  This is better than the exponential running time the recurrence would give us
  • 21. Constructing an Optimal Solution  So far, we only know the optimal number of scalar multiplications, not the order in which to multiply the matrices  This information is encoded in the table s  Each entry s[i,j] records the value k such that the optimal parenthesization of Ai…Aj occurs between matrix k and k+1  To compute the product A1..n, we parenthesize at s[1,n]  Previous matrix multiplications can be computed recursively  E.g., s[1,s[1,n]] contains the optimal split for the left half of the multiplication
  • 22. Constructing an Optimal Solution MatrixChainMultiply(const Matrix A[], const Matrix &s, int i, int j) { if ( j > i ) { Matrix X = MatrixChainMultiply(A, s, i, s(i,j)); Matrix Y = MatrixChainMultiply(A, s, s(i,j)+1, j); return MatrixMultiply(X,Y); } else return A[i]; }
  • 23. Elements of Dynamic Programming  There are two key ingredients that an optimization problem must have for dynamic programming to be applicable:  Optimal substructure  Overlapping subproblems
  • 24. Optimal Substructure  A problem that exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems  E.g., matrix-chain multiplication exhibited this property  An optimal parenthesization of a matrix chain requires that each sub-chain also be optimally parenthesized  This property is typically shown by assuming that a better solution exists, then showing how this contradicts the optimality of the solution to the original problem
  • 25. Overlapping Subproblems  The “space” for subproblems must be relatively small  i.e., a recursive algorithm for the solution would end up solving the same subproblems over and over  This is called overlapping subproblems  Dynamic programming algorithms typically compute overlapping subproblems once and store the solution in a table for later (constant-time) lookup
  • 26. Overlapping Subproblems  From the matrix-chain algorithm, we see earlier computations being reused to perform later computations:  What if we replaced this with a recursive algorithm?  Figure 16.2 on page 311 shows the added computations int q = m(i,k)+m(k+1,j)+p[i-1]*p[k]*p[j]; if ( q < m(i,j) ) m(i,j) = q; else s(i,j) = k;
  • 27. Exercise  A common recursive algorithm for computing the Fibonacci sequence is: 2if 2if 1if )2()1( 1 1 )( x x x xFibxFib xFib  Is dynamic programming applicable? Why?  Write a dynamic programming algorithm for solving this problem
  • 28. Longest Common Subsequence  A subsequence is simply a part of a sequence, consisting of some number of consecutive elements  Formally:  If X is a sequence of size m, and Z is a sequence of size k, Z is a subsequence of X if there exists some j such that for 1<= i <= k, we have xi+j=zi
  • 29. Longest Common Subsequence  Given two sequences X and Y, Z is a common subsequence of X and Y if Z is a subsequence of both X and Y  The longest common subsequence problem is thus the problem of finding the longest common subsequence of two given sequences
  • 30. Brute-Force Approach  Enumerate all subsequences in X to see if they are also subsequences of Y, and keep track of the longest one found  If X is of size m, that’s 2m possibilities!
  • 31. Characterizing an LCS  Does the LCS problem exhibit an optimal substructure property?  Yes, corresponding to pairs of “prefixes”  A prefix of a sequence is simply the beginning portion of a sequence for some specified length  e.g., if X = <A,B,C,B,D,A,B>, the fourth prefix of X (X4) is <A,B,C,B>
  • 32. Characterizing an LCS  From Theorem 16.1: Let X=<x1,…,xm>, Y=<y1,…,yn>, and Z=<z1,…,zk> be any LCS of X and Y  If xm=yn, then zk=xm=yn and Zk-1 is an LCS of Xm-1 and Yn-1  If xm!=yn, then zk!=xm implies that Z is an LCS of Xm-1 and Y  If xm!=yn, then zk!=yn implies that Z is an LCS of X and Yn-1
  • 33. Characterizing an LCS  What does this mean?  An LCS of two sequences contains within it an LCS of prefixes of the two sequences  This is just the optimal substructure property
  • 34. A Recursive Solution To Subproblems  Theorem 16.1 gives us two conditions to check for:  If xm = yn, we need to find an LCS of Xm-1 & Yn-1, to which we append xm = yn  If xm != yn, then we must solve two subproblems: finding an LCS of Xm & Yn-1, and an LCS of Xm-1 & Yn  Whichever is longer is the LCS of X & Y  Overlapping subproblems are evident  To find an LCS of X & Y, we must find LCS of Xm-1 and/or Yn- 1, which have still smaller overlapping subproblems
  • 35. A Recursive Solution To Subproblems  What is the cost of an optimal solution?  Let c[i,j] be the length of an LCS of Xi & Yj  If i or j = 0, then the LCS for that subsequence has length 0  Otherwise, the cost follows directly from Theorem 16.1: ji ji yxji yxji ji jicjic jicjic and0,if and0,if 0or0if ],1[],1,[max( 1]1,1[ 0 ],[
  • 36. Computing the Length of an LCS  A recursive algorithm for computing the length of an LCS of two sequences can be written directly from the recurrence formula for the cost of an optimal solution  This recursive algorithm will lead to an exponential-time solution  Dynamic programming techniques can be used to compute the solution bottom-up and reduce the expected running time
  • 37. Computing the Length of an LCS  The following algorithm fills in the cost table c based on the input sequences X and Y  It also maintains a table b that helps simplify an optimal solution  Entry b[i,j] points to the table entry corresponding to the optimal subproblem solution chosen when computing c[i,j]
  • 38. Computing the Length of an LCS void LCSLength(const sequence &X, const sequence &Y, matrix &b, matrix &c) { int m = X.length, n = Y.length; // Initialize tables for ( int i = 0 ; i < m ; ++i ) c(i,0) = 0; for ( int j = 0 ; j < m ; ++j ) c(0,j) = 0;
  • 39. Computing the Length of an LCS // Fill in tables for ( int i = 1 ; i < m ; ++i ) for ( int j = 1 ; j < n ; ++j ) { if ( x[i] == y[j] ) { c(i,j) = c(i-1,j-1)+1; b(i,j) = 1; // Subproblem type 1 “ã” }
  • 40. Computing the Length of an LCS else if ( c(i-1,j) >= c(i,j-1) ) { c(i,j) = c(i-1, j); b(i,j) = 2; // Subproblem type 2 “á” } else { c(i,j) = c(i, j-1); b(i,j) = 3; // Subproblem type 3 “ß” } } }
  • 41. Constructing an LCS  Table b can now be used to construct the LCS of two sequences  Begin in the bottom right corner of b, and follow the “arrows”  This will build the LCS in reverse order
  • 42. Constructing an LCS LCSPrint(const matrix &b, const sequence &X, int i, int j) { if ( i == 0 || j == 0 ) return; switch ( b(i,j) ) { case 1: LCSPrint(b, X, i-1, j-1); break; case 2: LCSPrint(b, X, i-1, j); break; case 3: LCSPrint(b, X, i, j-1); break; } }
  • 43. What is the Running Time to Find an LCS?  Total running time is now the cost to build the tables + the cost of printing it out  Table building = O(mn)  Printing = O(m+n)  So, total cost is O(mn)