Given two square matrices A and B of size n x n each, find their Strassen’s Matrix Multiplication.

Naive Method

[pastacode lang=”c” manual=”void%20multiply(int%20A%5B%5D%5BN%5D%2C%20int%20B%5B%5D%5BN%5D%2C%20int%20C%5B%5D%5BN%5D)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20N%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20C%5Bi%5D%5Bj%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20k%20%3D%200%3B%20k%20%3C%20N%3B%20k%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20C%5Bi%5D%5Bj%5D%20%2B%3D%20A%5Bi%5D%5Bk%5D*B%5Bk%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Time Complexity of above method is O(N3).

[ad type=”banner”]

Divide and Conquer

Following is simple Divide and Conquer method to multiply two square matrices.

  1. Divide matrices A and B in 4 sub-matrices of size N/2 x N/2 as shown in the below diagram.
  2. Calculate following values recursively. ae + bg, af + bh, ce + dg and cf + dh.

Strassen’s Matrix Multiplication

In the above method, we do 8 multiplications for matrices of size N/2 x N/2 and 4 additions. Addition of two matrices takes O(N2) time. So the time complexity can be written as

T(N) = 8T(N/2) + O(N2)  

From Master's Theorem, time complexity of above method is O(N3)
which is unfortunately same as the above naive method.

Simple Divide and Conquer also leads to O(N3), can there be a better way?

[ad type=”banner”] In the above divide and conquer method, the main component for high time complexity is 8 recursive calls. The idea of Strassen’s method is to reduce the number of recursive calls to 7. Strassen’s method is similar to above simple divide and conquer method in the sense that this method also divide matrices to sub-matrices of size N/2 x N/2 as shown in the above diagram, but in Strassen’s method, the four sub-matrices of result are calculated using following formulae.

Strassen’s Matrix Multiplication

Time Complexity of Strassen’s Method
Addition and Subtraction of two matrices takes O(N2) time. So time complexity can be written as

T(N) = 7T(N/2) +  O(N2)

From Master's Theorem, time complexity of above method is 
O(NLog7) which is approximately O(N2.8074)
[ad type=”banner”]

Generally Strassen’s Matrix Multiplication Method is not preferred for practical applications for following reasons

  1. The constants used in Strassen’s method are high and for a typical application Naive method works better.
  2. For Sparse matrices, there are better methods especially designed for them.
  3. The submatrices in recursion take extra space.
  4. Because of the limited precision of computer arithmetic on noninteger values, larger errors accumulate in Strassen’s algorithm than in Naive Method

Categorized in: