Strassen's algorithm
matrix multiplication
computational mathematics
algorithm optimization
numerical analysis

Strassen's algorithm for matrix multiplication

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Strassen's algorithm is a groundbreaking method for matrix multiplication that significantly reduces the complexity of multiplying two matrices, as compared to the conventional approach. Introduced by Volker Strassen in 1969, this algorithm marked a paradigm shift in computational mathematics, providing vital insights and inspiring future developments in efficient computation.

Understanding Matrix Multiplication

Before delving into Strassen's algorithm, it's pivotal to understand the standard matrix multiplication method. For two matrices AA and BB of dimensions n×nn \times n, their product C=A×BC = A \times B is calculated using the formula:

Cij=k=1nAikBkjC_{ij} = \sum_{k=1}^{n} A_{ik} \cdot B_{kj}

For each of the n2n^2 entries in the resultant matrix CC, this traditional method requires nn multiplications and (n1)(n-1) additions, resulting in an overall time complexity of O(n3)O(n^3).

The Essence of Strassen's Algorithm

Strassen's algorithm reduces the number of necessary matrix multiplication operations. The key idea is to partition each of the input matrices into four sub-matrices. For simplicity, assume AA and BB are square matrices where nn is a power of two. The matrices are split as follows:

A=[A_11A_12 A_21A_22],;B=[B_11B_12 B_21B_22]A = \begin{bmatrix} A\_{11} & A\_{12} \ A\_{21} & A\_{22} \end{bmatrix}, ; B = \begin{bmatrix} B\_{11} & B\_{12} \ B\_{21} & B\_{22} \end{bmatrix}

The product matrix C=A×BC = A \times B is then composed of four blocks:

C=[C_11C_12 C_21C_22]C = \begin{bmatrix} C\_{11} & C\_{12} \ C\_{21} & C\_{22} \end{bmatrix}

The blocks are computed through:

  1. M1=(A11+A22)(B11+B22)M_1 = (A_{11} + A_{22})(B_{11} + B_{22})
  2. M2=(A21+A22)B11M_2 = (A_{21} + A_{22})B_{11}
  3. M3=A11(B12B22)M_3 = A_{11}(B_{12} - B_{22})
  4. M4=A22(B21B11)M_4 = A_{22}(B_{21} - B_{11})
  5. M5=(A11+A12)B22M_5 = (A_{11} + A_{12})B_{22}
  6. M6=(A21A11)(B11+B12)M_6 = (A_{21} - A_{11})(B_{11} + B_{12})
  7. M7=(A12A22)(B21+B22)M_7 = (A_{12} - A_{22})(B_{21} + B_{22})

With these products, the sub-matrices of CC are derived as:

C11=M1+M4M5+M7C_{11} = M_1 + M_4 - M_5 + M_7C12=M3+M5C_{12} = M_3 + M_5C21=M2+M4C_{21} = M_2 + M_4C22=M1M2+M3+M6C_{22} = M_1 - M_2 + M_3 + M_6

Instead of eight multiplications, Strassen's algorithm performs only seven, thereby improving efficiency.

Complexity Analysis

The advantage of Strassen's approach lies in its reduced multiplicative operations. The theoretical time complexity is:

T(n)=7T(n/2)+O(n2)T(n) = 7T(n/2) + O(n^2)

Using the Master Theorem, it can be deduced that:

T(n)=O(nlog27)O(n2.81)T(n) = O(n^{\log_2 7}) \approx O(n^{2.81})

This is a significant improvement over the classical O(n3)O(n^3) complexity, particularly for large matrices.

Example

Let's demonstrate Strassen's method for two 2×22 \times 2 matrices:

A=[13 75],;B=[68 42]A = \begin{bmatrix} 1 & 3 \ 7 & 5 \end{bmatrix}, ; B = \begin{bmatrix} 6 & 8 \ 4 & 2 \end{bmatrix}

Following Strassen's steps:

M1=(1+5)(6+2)=6×8=48M_1 = (1+5)(6+2) = 6 \times 8 = 48M2=(7+5)6=12×6=72M_2 = (7+5)6 = 12 \times 6 = 72M3=1(82)=1×6=6M_3 = 1(8-2) = 1 \times 6 = 6M4=5(46)=5×(2)=10M_4 = 5(4-6) = 5 \times (-2) = -10M5=(1+3)2=4×2=8M_5 = (1+3)2 = 4 \times 2 = 8M6=(71)(6+8)=6×14=84M_6 = (7-1)(6+8) = 6 \times 14 = 84M7=(35)(4+2)=(2)×6=12M_7 = (3-5)(4+2) = (-2) \times 6 = -12

The resulting matrix CC:

C11=48108+(12)=18C_{11} = 48 - 10 - 8 + (-12) = 18C12=6+8=14C_{12} = 6 + 8 = 14C21=7210=62C_{21} = 72 - 10 = 62C22=4872+6+84=66C_{22} = 48 - 72 + 6 + 84 = 66

Thus, C=[18146266]C = \begin{bmatrix} 18 & 14 \\ 62 & 66 \end{bmatrix}.

Limitations and Extensions

  1. Divide-and-Conquer Approach: Strassen’s method inherently suits matrices where nn is a power of 2. Padding with zeros is often used for arbitrary dimensions, which may complicate implementation.
  2. Numerical Stability: Compared to conventional methods, Strassen's algorithm may be less stable numerically due to subtractions that may amplify errors, especially for floating-point operations.
  3. Practical Performance: Despite its reduced theoretical complexity, the constant factors in Strassen’s algorithm often make it less efficient for smaller matrices. Thresholding is used in practice, where classical methods are used for matrices below a certain size to optimize performance.
  4. Extensions: Numerous algorithms building upon Strassen's foundations, such as the Coppersmith-Winograd algorithm, have emerged, further pushing down the time complexity for matrix multiplication.

Key Points Summary

FeatureStandard MethodStrassen's Algorithm
Multiplicationsn3n^37×(n/2)37 \times (n/2)^3
Additions/Subtractions(n1)n2(n-1)n^2More (due to decomposition and combination)
ComplexityO(n3)O(n^3)O(n2.81)O(n^{2.81})
Numerical StabilityHighPotentially lower
Use CaseBest for small matricesEfficient for large matrices, especially where nn is a power of 2

Strassen's algorithm opened avenues in algorithmic optimizations and remains instrumental in computing disciplines where large-scale linear algebraic operations are performed.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.