The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+22 votes
3.8k views

Four Matrices $M_1, M_2, M_3$ and $M_4$ of dimensions $ p \times q, \:\:q \times r, \:\:r \times s$ and $s \times t$ respectively can be multiplied in several ways with different number of total scalar multiplications. For example when multiplied as $((M_1 \times M_2) \times (M_3 \times M_4))$, the total number of scalar multiplications is $pqr + rst + prt$. When multiplied as $(((M_1 \times M_2) \times M_3) \times M_4)$, the total number of scalar multiplications is $pqr + prs + pst$.

If $p=10, q=100, r=20, s=5$ and $t=80$, then the minimum number of scalar multiplications needed is

  1.  $248000$
  2.  $44000$
  3.  $19000$
  4.  $25000$
asked in Algorithms by Veteran (96.1k points)
edited by | 3.8k views
+5

19,000 is the correct answer!
 

+2

This Might Help ...

+1
I read @Arjun sir comment of doing it using DP but I didn't believe it can be done in less time. So I tried it using timer and after doing it 2 times I finally endup doing it in less than 2 minutes.

So the conclusion is if you have enough practice, DP is most reliable and it can get you 2 marks within 3 minutes.

p.s :

I was actually looking for such comment so i thought posting it will be helpful for some and clear the things that solving it with DP is effective too.

Peace :)

2 Answers

+28 votes
Best answer

Answer is C.

Ordering: 

  • First Multiply  $M_2 \times M_3.$
    This requires $100*20*5$ multiplications. 
  • Then Multiply  $M_1 \times (M_2 \times M_3).$
    This requires $10*100*5$ multiplications.
  • Then Multiply $(M_1 \times (M_2 \times M_3)) \times M_4.$
    This requires $10*5*8$ multiplications.

Total $19000$ Multiplications.


Brute Force approach - anyone can do. 

No. of possible ordering for 4 matrices is $C_3$ where $C_3$ is the $3^{rd}$ Catalan number and given by $n=3$ in $\frac{1}{n+1} {}^{2n}C_n = 5.$

So, here we have 

  1. $(M_1 \times M_2) \times (M_3 \times M_4)$
  2. $(M_1 \times (M_2 \times M_3)) \times M_4$
  3. $((M_1 \times M_2) \times M_3) \times M_4$
  4. $M_1 \times (M_2 \times (M_3 \times M_4))$
  5. $M_1 \times ((M_2 \times M_3) \times M_4))$

Each of these would give no. of multiplications required as

  1. $pqr + rst + prt $
  2. $qrs + pqs + pst$
  3. $pqr + prs + pst$
  4. $rst + qrt + pqt$
  5. $qrs + qst + pst$

The last 2 are having $qt$ terms which are the highest terms by far and hence we can avoid them from consideration $qt = 8000$ multiplied by one other term would be larger than any value in choice. So, just find the value of first 3 terms. 

  1. $pqr + rst + prt  = 20000 + 8000 + 16000 = 44000$
  2. $qrs + pqs + pst = 10000 + 5000 + 4000 = 19000$ - smallest value in choice, we can stop here. 
  3. $pqr + prs + pst$

Dynamic Programming Solution (should know Matrix Chain Ordering algorithm)

Here we have a chain of length 4.

Dynamic programming solution of Matrix chain ordering has the solution

$m[i, j] = \begin{cases} 0 &\text{ if } i=j\\ \min_{i\leq k < j } m[i]k] + m[k+1][j] + p_{i-1}p_{j}p_{k} &\text{ if } i < j\end{cases}$

So, we can fill the following table starting with the diagonals and moving upward diagonally. Here $k < j$ but $\geq i.$
$$\begin{array}{|l|l|l|l|l|} \hline \text{} & \textbf{j=1} & \textbf{j=2} & \textbf{j=3} & \textbf{j=4}\\\hline 
\textbf{i=1} &  \text{0} & \text{$p_0p_1p_2$} & \min(10000+p_0p_1p_3, & \min(18000+ p_0p_1p_4, \\
&&=20000&20000+p_0+p_2p_3)&20000+8000+p_0+p_2+p_4, \\
&&&=15000&15000+p_0p_3p_4) = 19000
\\\hline 
\textbf{i=2} & \text{} & \text{0} & \text{$p_1p_2p_3 = 10000$} & \text{$\min(10000 + p_2p_3p_4), p_1p_3p_4)$}\\
&&&&=18000
\\\hline 
\textbf{i=3} &\text{} & \text{} & \text{0} &\text{$p_2p_3p_4 = 8000$} \\\hline \textbf{i=4} &\text{} &\text{} & \text{} & \text{0} \\\hline \end{array}$$

Our required answer is given by $m[1,4] = 19000.$

answered by Active (4.2k points)
edited by
0
checked all combinations in the exam? any shortcuts?
+3
Use dynamic programming approach. It is standard matrix multiplication problem.
0
even the dynamic one takes time. how to approach this kind of problem in lesser time??
+5
It is just 5 pairs we have to consider. Dynamic programming is recommended if one has tried it before- then its easy to try for problems. See the solution now.
+3

Values are obtained by the formula: m[i,j] = { 0                                                                                     ,         i==j

                                                                                       min i<=k<j    { m[i,k] + m[k+1,j] + Pi-1.Pk.Pj }       ,        i < j

                                                                                       }

DP table
0 20000 15000 19000
  0 10000 50000
    0 8000
      0

ans: 19000(option C)

0
How to solve such matrix chain multiplication questions fast in Exam ??
0
M1×((M2×M3)×M4)) = qrs+qst+pst .......?

it should be  pqt+qrs+qst
0

Then Multiply (M1×(M2×M3))×M4. This requires 10*5*8 multiplications.

please correct this to 10*5*80 

thanks

 

+9 votes

For those who still doen't understand how multiplication works, very simple.

Only thing is you should not alter the sequence.

For ex, (M1 x M2) = First term X Common Term X Last term

(M1.x M2) = (10 x 100 ) x ( 100 x 20) = 10 x 100 x 20

((M1  x M2 ) x M3) = (10 x 100 x 20) x (20 x 5) = 10 x 20 x 5 ( In this case, we have cancelled all remaining terms except first term, common term and last term)

(((M1  x M2 ) x M3) x M4) = (10 x 20 x 5) x (5 x 80) = 10 x 5 x 80 ( In this case also, we have cancelled all remaining terms except first term, common term and last term)

Finally, addition of all steps will give us result.

Result = (10 x 100 x 20) + (10 x 20 x 5) + (10 x 5 x 80) = 19000

Isn't it simple !

answered by (177 points)
0
25000?
0
Plz aad total no
0
Your summation is wrong. Check it !
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,535 questions
54,122 answers
187,321 comments
71,039 users