One day I will be seen on television sideline reporting for all NBA teams. Think of Doris Burke, America’s woman sports announcer and analyst for the NBA and ESPN. To reach any dream you have to…
Dynamic Programming is a common approach used by the programmers to reduce the time complexity to a certain extent(if possible) so that the time of computation can be improved.
Now since we have gone through the process of Dynamic Programming let us see the problem we are going to solve using Dynamic Programming.
Suppose we have a chain of matrices that are required to be computed.
Let us suppose the matrix chain be as follows
Hence, as shown, we have 4 matrices with each dimension mentioned.
Now there can be multiple possibilities of multiplication.
For example CASE 1: Suppose we first perform A34=A3 *A4, where the resultant matrix is A34. Now to this matrix, we multiply A2. Thus, A234=A2*A34. To the resultant matrix, we finally multiply A1.
CASE 2: Suppose in this case we multiply A1 and A2. Thus, A12=A1*A2. Now we multiply A3 and A4. Thus, we get A34=A3*A4. Now we multiply the two resultants to retrieve our resultant. Result= A12 * A34.
In total there are several combinations possible. Now if the chain is small then we can try each and every process naively and find the resultant. But this cannot be a permanent solution.
Suppose if the chain is of 3 matrices, there are only 2 combinations
Suppose if the chain is of 4 matrices (as shown above), there are 3 different combinations. Now if there are many more matrices in the chain then combinations also increase exponentially.
Now that we are through with the understanding of the problem, let us try to resolve it.
We note that we can solve it by storing the resultant number of multiplications of two matrices in a data structure.
Actually, we need two different 2-D arrays. one to store the number of multiplication 2 matrices need to undergo in order to form a pair and the second one to store the position where brackets need to be put.
Thus, let us derive the formula
Suppose we are storing the resultant number of multiplications in an array A
Before we move to the algorithm formula let’s first see the way of finding the number of multiplication and number of additions 2 matrices need to undergo in order to give the result.
Suppose we have two matrices namely A and B of dimensions mxn and nxr respectively.
Then the number of multiplication to give AB=m.n.r
The number of additions in this =m.r
Well now going back to our above example. Or let us bring that fresh again
Let us see this with the help of an example. Or let us take a use case.
Well first we spot the value of already known blocks
A[1,1]=A[2,2]=A[3,3]=A[4,4]=0
Mark these values in our array.
A[1,4] gives the minimum number of multiplications required.
In this way, we can perform the matrix chain multiplication using DP.
This is a chapter from my book “Who We Are”. The full book can be ordered on Amazon.com in Kindle eBook or Paperback version. Enjoy reading!!! Humanity was always striving towards perfection. Every…
Billie Eilish was just fourteen when “Ocean Eyes” was first uploaded to SoundCloud on November 18th, 2015. It kicked off her rapid climb to the top of music, leading an entire generation of young…
We predict that selling on non-Amazon marketplaces such as Walmart, eBay and Wish will become mainstream by the end of this year, pushing more online sellers to adopt a multi-channel selling…