Question

This is from algorithm's class:

4. Consider the problem of multiplying n rectangular matrices discussed in class. Assume, in contrast to what we did in class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

// Matrix Ai has dimension p[i-1] x p[i]
// for i = 1..n

int MaxMultiplicationsMatrix(int p[], int i, int j)
{
    if(i == j)
        return 0;
    int k;
    int max = -1;
    int count;

    // place parenthesis at different places
    // between first and last matrix, recursively
    // count of multiplications for
    // each parenthesis placement and return the
    // minimum count
    for (k = i; k < j; k++)
    {
        count = MaxMultiplicationsMatrix(p, i, k) +
                MaxMultiplicationsMatrix(p, k + 1, j) +
                p[i - 1] * p[k] * p[j];

        if (count > max) {
       /* cout<<p[i-1]<<' '<<p[k]<<' '<<p[j]<<endl;
           If we print this, we'll know that last 3 lines are:
           9 3 9 (meaning that dimension 9*3 and 3*9 got multiplied before below, it becomes 9*9)
           9 9 1 (now 9*9 and 9*1 will get multiplied, it will become 9*1 and then multiply 1*8 with this so it becomes 9*8 size).
           9 8 4 (this means that dimension of the last 2 matrices are 9*8 and 8*4
       */
            max = count;
       }
    }
   //cout<<max<<endl;
    // Return minimum count
    return max;
}

// Driver Code
int main()
{
    int arr[] = {9,3,9,1,8,4};
    int n = sizeof(arr)/sizeof(arr[0]);

    cout << "Maximum number of multiplications is "
         << MaxMultiplicationsMatrix(arr, 1, n - 1);
}

/*
Answer explanation steps:
Matrices sizes: 9x3, 3x9, 9x1, 1x8, 8x4
For multiplying 2 matrices, it will follow below order:
(((M93 * M39)*(M91*M18))*M84)

Matrics       No. of multiplications       Final matrix

M93*M39   -> 9*3*9 = 243   -> M99
M91*M18 -> 9*1*8 = 72   -> M98
M99*M98 -> 9*9*8 = 648   -> M98
M98*M84 -> 9*8*4 = 288   -> M94
Total = 243+72+648+288 = 1251

*/

Add a comment
Know the answer?
Add Answer to:
This is from algorithm's class: 4. Consider the problem of multiplying n rectangular matrices discussed in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if...

    III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if we interpret as numbers, numbers) of base-b and a given length, say l. The number of strings enumerated is b l . Now if we interpret the outputs as strings, or lists, rather than base-b numbers and decide that we only want to enumerate those strings that have unique members, the number of possible strings reduces from b l to b!. Furthermore, consider a...

  • The Box Problem Take an 8% x 11 sheet of paper and cut out 4 congruent squares (one from each corner) as shown belo...

    The Box Problem Take an 8% x 11 sheet of paper and cut out 4 congruent squares (one from each corner) as shown below on the left. This creates a net for an open-topped box (rectangular prism) which can be folded up as shown on the right. We're going to use our box to carry as many M & M's as possible. If the side-length of each cut-out square is 1 inch, then the box created will have dimensions 1...

  • 4. In class you discussed a model for fishery management based on the logistic equation with...

    4. In class you discussed a model for fishery management based on the logistic equation with a parameterization of harvesting, N =EN (1-)-mN, N(0) = No where m is the fishing rate ("m" for mortality). With m = 0, there are two fixed points: Ni = 0 (unstable) and N = K (stable). With m > 0, the second fixed point becomes N = K(1 - m/r) <K (a) At what critical fishing rate, me, will the population die out?...

  • ld ts biovs Part II: Analysis of recursive algorithms is somewhat different from that of non-recursive...

    ld ts biovs Part II: Analysis of recursive algorithms is somewhat different from that of non-recursive algorithms. We are very much interested in how many times the method gets called. The text refers to this as the number of activations. In inefficient algorithms, the number of calls to a method grows rapidly, in fact much worse than algorithms such as bubble sort. Consider the following: public static void foo ( int n ) { if n <=1 ow ura wor...

  • must be coded in c++ without any STL libraries sadly :( so im struggling on this...

    must be coded in c++ without any STL libraries sadly :( so im struggling on this problem, any help would be greatly appreciated, thanks in advance! :) assignment is due tomorrow and im really struggling on this last question :( a. Begin by implementing a BST for integers. The underlying structure is a linked list. You need these methods: i. BST(); -- Constructor ii. void put (int) – Inserts a value into the BST. iii. Void put(int[] a) – Inserts...

  • Here is the data analysis project, please I need an excellent project, it is due 4 hours! Statist...

    here is the data analysis project, please I need an excellent project, it is due 4 hours! Statistics course. GENERAL DESCRIPTION For the data analysis project, you address some questions that interest you with the statistical methodology we learn in MAT 235.   You choose the question; you decide how to collect data; you do the analyses. The questions can address almost any topic (although I have veto power), including topics in economics, psychology, sociology, natural science, medicine, public policy, sports,...

  • Consider a cylindrical capacitor like that shown in Fig. 24.6. Let d = rb − ra...

    Consider a cylindrical capacitor like that shown in Fig. 24.6. Let d = rb − ra be the spacing between the inner and outer conductors. (a) Let the radii of the two conductors be only slightly different, so that d << ra. Show that the result derived in Example 24.4 (Section 24.1) for the capacitance of a cylindrical capacitor then reduces to Eq. (24.2), the equation for the capacitance of a parallel-plate capacitor, with A being the surface area of...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • there are 5 pictures attached to this question. The first picture which talk about P-R-O strategy...

    there are 5 pictures attached to this question. The first picture which talk about P-R-O strategy is the question itslef which I want to know thw aswer. the next 4 pictures are useful information that may help you to get the answer. please answer to this question. I need to know ASAP Increase in hominin brain size Task: Use the P-R-O strategy to construct a theory-based explanation for a puzzling problem. Refer to the handout you received in lecture to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT