68 DESIGN OF EFFICIENT ALGORITHMS 2.8 where the dimensions of each M, are shown in the brackets. Evaluating M in the order M₁ X (MX (M3 × M₁)) requires 125.000 operations, while evaluating M in the order (M, X (MM;)) × M₁ requires only 2200 operations. ☐ Trying all possible orderings in which to evaluate the product of n matrices so as to minimize the number of operations is an exponential process (see Ex- ercise 2.31), which is impractical when n is large. However, dynamic program- ming provides an O(n³) algorithm. Let m₁, be the minimum cost of computing M₁ × Mi+1 × ·× M; for 1 ≤ i ≤ j ≤n. Clearly, (0, if i=j (2.9) mij = (MIN (mik + M+1, +1-1), if j>i ish i, is the minimum, taken over all possible values of k between i and j- 1, of the sum of these three terms. The dynamic programming approach calculates the m₁'s in order of in- creasing difference in the subscripts. We begin by calculating m₁ for all i, then mi,i+1 for all i, next mi,i+2, and so on. In this way, the terms m₁ and mk+1,j in (2.9) will be available when we calculate mij. This follows since j - i must be strictly greater than either of k-i and j― (k+1) if k is in the range i ≤ k 1, the recursive algorithm is likely to be polynomial in time complexity. However, if the obvious division of a prob- lem of size n results in n problems of size n - 1, then a recursive algorithm is likely to have exponential growth. In this case a tabular technique called dynamic programming often results in a more efficient algorithm. In essence, dynamic programming calculates the solution to all subprob- lems. The computation proceeds from the small subproblems to the larger subproblems, storing the answers in a table. The advantage of the method lies in the fact that once a subproblem is solved, the answer is stored and never recalculated. The technique is easily understood from a simple example. Consider the evaluation of the product of n matrices M = M₁x M₂ × · · · × M„ where each M, is a matrix with 1-1 rows and r; columns. The order in which the matrices are multiplied together can have a significant effect on the total number of operations required to evaluate M, no matter what matrix multi- plication algorithm is used. Example 2.7. Assume that the multiplication of a p× q matrix by a q× r matrix requires pqr operations, as it does in the "usual" algorithm. and con- sider the product M = M₁ [10 × 20] M2 [20 x 50] M3 [501] MA (2.8) [1 × 100]
68 DESIGN OF EFFICIENT ALGORITHMS 2.8 where the dimensions of each M, are shown in the brackets. Evaluating M in the order M₁ X (MX (M3 × M₁)) requires 125.000 operations, while evaluating M in the order (M, X (MM;)) × M₁ requires only 2200 operations. ☐ Trying all possible orderings in which to evaluate the product of n matrices so as to minimize the number of operations is an exponential process (see Ex- ercise 2.31), which is impractical when n is large. However, dynamic program- ming provides an O(n³) algorithm. Let m₁, be the minimum cost of computing M₁ × Mi+1 × ·× M; for 1 ≤ i ≤ j ≤n. Clearly, (0, if i=j (2.9) mij = (MIN (mik + M+1, +1-1), if j>i ish i, is the minimum, taken over all possible values of k between i and j- 1, of the sum of these three terms. The dynamic programming approach calculates the m₁'s in order of in- creasing difference in the subscripts. We begin by calculating m₁ for all i, then mi,i+1 for all i, next mi,i+2, and so on. In this way, the terms m₁ and mk+1,j in (2.9) will be available when we calculate mij. This follows since j - i must be strictly greater than either of k-i and j― (k+1) if k is in the range i ≤ k 1, the recursive algorithm is likely to be polynomial in time complexity. However, if the obvious division of a prob- lem of size n results in n problems of size n - 1, then a recursive algorithm is likely to have exponential growth. In this case a tabular technique called dynamic programming often results in a more efficient algorithm. In essence, dynamic programming calculates the solution to all subprob- lems. The computation proceeds from the small subproblems to the larger subproblems, storing the answers in a table. The advantage of the method lies in the fact that once a subproblem is solved, the answer is stored and never recalculated. The technique is easily understood from a simple example. Consider the evaluation of the product of n matrices M = M₁x M₂ × · · · × M„ where each M, is a matrix with 1-1 rows and r; columns. The order in which the matrices are multiplied together can have a significant effect on the total number of operations required to evaluate M, no matter what matrix multi- plication algorithm is used. Example 2.7. Assume that the multiplication of a p× q matrix by a q× r matrix requires pqr operations, as it does in the "usual" algorithm. and con- sider the product M = M₁ [10 × 20] M2 [20 x 50] M3 [501] MA (2.8) [1 × 100]
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 7SA
Related questions
Question
My
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage