Strassen’s algorithm is used for the multiplication of Square Matrices that is the order of matrices should be (N x N). Strassen’s Algorithm is based on the divide and conquer technique. In simpler terms, it is used for matrix multiplication. Strassen’s method of matrix multiplication is a typical divide and conquer algorithm. However, let’s get again on what’s behind the divide and conquer approach and implement it considering an illustration as follows For example: Let A and B are two matrices then the resultant matrix C such that
Matrix C = Matrix A * Matrix B
Consider for now the mathematical computation of matrices is that it can be concluded out why the implementation for the Strassen matrices comes out into play. Suppose two matrices are operated to be multiplied then the approach would have been
- Take input of two matrices.
- Check the compatibility of matrix multiplication which holds true only and only if the number of rows of the first matrix equals the number of columns of the second matrix.
- Multiply the matrix and assign multiplication of two matrices to another matrix known as the resultant matrix.
- Print the resultant matrix.
In the above approach, two assumptions are drawn which show why Strassen’s algorithm need arises into play
- Firstly, the time complexity of the algorithm is O(n3) which is too high.
- Secondly, the multiplication of more than two matrices will not only increase the confusion and complexity of the program but also increase the time complexity accordingly.
Volker Strassen’s is a name who published his algorithm to prove that the time complexity O(n3) of general matrix multiplication wasn’t optimal. So it was published Strassen’s matrix chain multiplication and reduced the time complexity. This algorithm is faster than standard matrix multiplication and is useful when numerous large matrices multiplication is computed in the daily world.
Strassen’s Algorithm for Matrix Multiplication
Step 1: Take three matrices to suppose A, B, C where C is the resultant matrix and A and B are Matrix which is to be multiplied using Strassen’s Method.
Step 2: Divide A, B, C Matrix into four (n/2)×(n/2) matrices and take the first part of each as shown below
Step 3: Use the below formulas for solving part 1 of the matrix
M1:=(A1+A3)×(B1+B2) M2:=(A2+A4)×(B3+B4) M3:=(A1−A4)×(B1+A4) M4:=A1×(B2−B4) M5:=(A3+A4)×(B1) M6:=(A1+A2)×(B4) M7:=A4×(B3−B1) Then, P:=M2+M3−M6−M7 Q:=M4+M6 R:=M5+M7 S:=M1−M3−M4−M5
Step 4: After Solving the first part, compute the second, third, and fourth, and as well as final output, a multiplied matrix is generated as a result as shown in the above image.
Step 5: Print the resultant matrix.
Strassen Multiplication Algorithm Implementation For Matrix Multipication : Product of matrices A and B : 7 21 15 22 8 8 21 12 12 17 28 22 8 31 16 31
Time Complexity Of Strassen’s Method
By Analysis the time complexity Function can be written as:
T(N) = 7T(N/2) + O(N2)
By Solving this using Master Theorem we get :
Thus time Complexity Of Strassen’s Algorithm for matrix multiplication is derived as:
O(nlog7) = O (n2.81)
O(n3) Vs O(n2.81)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.