📚 DAA Tutorial

Design and Analysis of Algorithms

Learn Design and Analysis of Algorithms concepts with simple explanations, algorithms, examples and practical notes.

← Back to DAA

Strassen’s Matrix Multiplication

Conventional matrix multiplication

Let A and B be two n × n matrices. The product matrix C = AB is also an n × n matrix, whose (i, j)th element is formed by taking the elements in the ith row of A and the jth column of B:

C(i,j)=∑1≤k≤nA(i,k)×B(k,j)∀i,j between 1 and  n

To compute C(i, j) we need n multiplications. Hence conventional matrix multiplication takes Θ(n3) time.

Divide-and-conquer strategy

Let n be a power of 2, n=2k. If n is not a power of 2, append enough rows and columns of zeros to both A and B, so that the resulting dimensions are a power of two.

Imagine A and B are partitioned into 4 square submatrices, each submatrix having dimension n2×n2. Then the product AB of 2 × 2 matrices is:

( A11A12 A21A22 ) ( B11B12 B21B22 ) = ( C11C12 C21C22 )

then

C11=A11B11+A12B21 C12=A11B12+A12B22 C21=A21B11+A22B21 C22=A21B12+A22B22

Since n is a power of 2, these matrix products can be recursively computed by the same algorithm, until the submatrices become small with n = 2.

Cost of the plain divide-and-conquer method

To compute AB, we need 8 multiplications of n2×n2 matrices and 4 additions of n2×n2 matrices.

T(n)={ bn≤2 8T(n2)+cn2n>2

Here cn2 is the addition time, and b and c are constants. This gives T(n)=O(n3). Hence there is no improvement over the traditional method.

Strassen's method

Strassen discovered a way to compute the Cij's using 7 multiplications and 18 additions or subtractions.

Seven products

P=(A11+A22)(B11+B22) Q=(A21+A22)B11 R=A11(B12−B22) S=A22(B21−B11) T=(A11+A12)B22 U=(A21−A11)(B11+B12) V=(A12−A22)(B21+B22)

Combining them

C11=P+S−T+V C12=R+T C21=Q+S C22=P+R−Q+U

Time complexity of Strassen's method

The resulting recurrence relation is:

T(n)={ bn≤2 7T(n2)+an2n>2

Solution (short form)

T(n)=an2[1+74+(74)2+⋯+(74)k−1]+7kT(1) ≤cn2(74)log2n+7log2n, c a constant =cnlog24+log27−log24+nlog27 =O(nlog27)≈O(n2.81)

Solution by repeated substitution

T(n)=7T(n2)+cn2 =7(7T(n4)+c(n24))+cn2 =72(7T(n8)+c(n216))+7c(n24)+cn2 =73T(n23)+cn2(74)2+cn2(74)+cn2 ⋮ =7kT(n2k)+cn2[(74)k−1+(74)k−2+⋯+(74)+1] =7kT(n2k)+cn2(74)k =7kT(n2k)+cn2(7k4k)

Let n=2k, so k=log2n:

=7log2nT(1)+cn27log2n4log2n =7log2nT(1)+cn2nlog27nlog24 =nlog27+cn2nlog27n2 =(1+c)nlog27

∴ The time complexity is O(nlog27) or O(n2.81), which is better than the O(n3) of the conventional method.

In the derivation above, T(1) is taken as 1 and the constant factor from summing the geometric series is absorbed into c, as in the handwritten notes.