# Computer Algorithms: Strassen’s Matrix Multiplication

## Introduction

The Strassen’s method of matrix multiplication is a typical divide and conquer algorithm. We’ve seen so far some divide and conquer algorithms like merge sort and the Karatsuba’s fast multiplication of large numbers. However let’s get again on what’s behind the divide and conquer approach.

Unlike the dynamic programming where we “expand” the solutions of sub-problems in order to get the final solution, here we are talking more on joining sub-solutions together. These solutions of some sub-problems of the general problem are equal and their merge is somehow well defined.

A typical example is the merge sort algorithm. In merge sort we have two sorted arrays and all we want is to get the array representing their union again sorted. Of course, the tricky part in merge sort is the merging itself. That’s because we’ve to pass through the two arrays, A and B, and we’ve to compare each “pair” of items representing an item from A and from B. A bit off topic, but this is the weak point of merge sort and although its worst-case time complexity is O(n.log(n)), quicksort is often preferred in practice because there’s no “merge”. Quicksort just concatenates the two sub-arrays. Note that in quicksort the sub-arrays aren’t with an equal length in general and although its worst-case time complexity is O(n^2) it often outperforms merge sort.

This simple example from the paragraph above shows us how sometimes merging the solutions of two sub-problems actually isn’t a trivial task to do. Thus we must be careful when applying any divide and conquer approach.

## History

Volker Strassen is a German mathematician born in 1936. He is well known for his works on probability, but in the computer science and algorithms he’s mostly recognized because of his algorithm for matrix multiplication that’s still one of the main methods that outperforms the general matrix multiplication algorithm.

Strassen firstly published this algorithm in 1969 and proved that the n^3 algorithm isn’t the optimal one. Actually the given solution by Strassen is slightly better, but his contribution is enormous because this resulted in many more researches about matrix multiplication that led to some faster approaches, i.e. the Coppersmith-Winograd algorithm with O(n^2,3737).

## Overview

The general algorithm on multiplying two matrices A[NxN] and B[NxN] is fairly simple. Although it’s more difficult than multiplying two numbers and also it is not commutative it’s still very simple – but slow.

Let’s first define what’s a matrix A[NxN]. As we speak about matrices NxN we usually think of a square grid with N rows and N columns. In each row and column A[i][j] we’ve a value.

Of course, as developers, we can think of a matrix as a two-dimensional array.

```// PHP two-dimensional array \$a = array( 0 => array(\$v1, \$v2, \$v3, \$v4), 1 => array(\$v5, \$v6, \$v7, \$v8), 2 => array(\$v9, \$v10, \$v11, \$v12), );```

Don’t forget that a NxN matrix is just a private case for a matrix. We can equally likely have any other size of a matrix NxM (N <> M).

However the size of a matrix is crucial in order to multiply it with another matrix. Why is that?

As I said above multiplying matrices isn’t the same as multiplying numbers. First of all this operation isn’t commutative.

And the second problem is the way you multiply two matrices A with B.

Just because this works with NxN matrices we can see the problem with multiplying rectangular matrices. Indeed, this wouldn’t be possible unless the second dimension of A isn’t exactly equal to the first dimension of B.

Hopefully we are now talking about square matrices with exactly the same dimensions.

OK, so now we know how to multiply two square matrices (with the same dimensions NxN) and now let’s evaluate the time complexity for the general purpose algorithm.

As we know A.B = C only when:

```C[i][j] = sum(A[i][k] * B[k][j]) for k = 0 .. n
```

Thus we have n^3 operations. Let’s try to find out a divide and conquer approach.

Indeed this isn’t difficult in case of matrices because as we know we can divide in matrix in smaller sub-matrices.

Now what do we have?

Again – the same complexity – we have 8 products and 4 sums. Where’s the catch?

Of course in order to get faster solution we’ve to be looking as Strassen did in 1969. He defined P1, P2, P3, P4, P5, P6 and P7 as defined on the image below.

## Complexity

As I mentioned above the Strassen’s algorithm is slightly faster than the general matrix multiplication algorithm. The general algorithm’s time complexity is O(n^3), while the Strassen’s algorithm is O(n^2.80).

You can see on the chart below how slightly faster is this even for large n.

## Application

Although this algorithm seems to be more close to pure mathematics than to computer practically everywhere we use NxN arrays we can benefit from matrix multiplication.

In the other hand the algorithm of Strassen is not much faster than the general n^3 matrix multiplication algorithm. That’s very important because for small n (usually n < 45) the general algorithm is practically a better choice. However as you can see from the chart above for n > 100 the difference can be very big.

In the same time typically NxN arrays are used always when we talk about adjacency matrix of graphs |V| = n and some graph algorithms practically depend on matrix multiplication.

## 25 thoughts on “Computer Algorithms: Strassen’s Matrix Multiplication”

1. Naveen Tayal says:

thank you…

2. PL says:

Amazing! But why…

3. sanjayan says:

Can you explain the Coppersmith-Winograd algorithm as well?

4. pramod says:

really very good…
thank you….

5. tanuj says:

Plz explain concept of defining p1,p2,p3,p4,p5,p6,p7

7. anju says:

good..but for a 4X 4 matrix how can we apply it..?

8. Sudheer says:

Very Nice

9. jayesh patil says:

very nice

10. kiran says:

thank you

11. sam says:

hell with amazing thing there is no explanation provided

12. Saurav says:

thank u
you really help me.

13. maithili says:

will you explain me the whole algorithm…………:)

14. David says:

Explain more of the concept of p1,p2,p3 etc

nice

16. Abhilasha says:

nice information helpful for me !!!

17. Fernando Pauer says:

Hi,

Wrong: p1 + p5 – p3 – p7

Correct: p1 + p5 – p3 + p7

18. Fernando Pauer says:
19. strassen algorithm is an efficient algorithm to multiply two matrices.A simple method to multiply two matrices need 3 nested loops and is 0(n^3).Strassens algorithm multiplies two matrices in 0(n^2.8974) time.

20. binary search is a searching algorithm.in each step.the agorithm compares the input element x with the value of the middle element in array.if the value match,return the index of middle.otherwise.if x is less than the middle element.then the algorithm recurs for left side middle element.else recurs for right side middle element.

21. GM says:

how you got this equation
p1,p2,p3,p4,p5,p6,p7

22. Shruti says: