Strassen's Matrix Multiplication
Introduction
Strassen's Matrix Multiplication is an efficient matrix multiplication algorithm based on the Divide and Conquer technique. It was introduced by Volker Strassen in 1969. The main idea is to divide each matrix into smaller sub-matrices and reduce the number of multiplications required to obtain the final result.
In conventional multiplication of two 2 × 2 matrices, 8 scalar multiplications are required. Strassen's method calculates the same result using only 7 multiplications, at the cost of additional additions and subtractions.
This reduction becomes increasingly useful when the matrices are large because multiplication is generally more computationally expensive than addition or subtraction.
Why Do We Need Strassen's Algorithm?
Consider the multiplication of two large n × n matrices. The conventional method requires O(n3) time. As the value of n increases, the number of multiplication operations increases rapidly.
Strassen's Algorithm reduces the number of recursive multiplications from eight to seven. Its recurrence relation is:
T(n) = 7T(n/2) + O(n2)
Using the Master Theorem:
T(n) = O(nlog27) ≈ O(n2.807)
Therefore, the theoretical multiplication complexity is better than the conventional O(n3) algorithm.
Prerequisites
| Concept | Description |
|---|---|
| Matrix | A rectangular arrangement of numbers in rows and columns. |
| Square Matrix | A matrix having the same number of rows and columns. |
| Sub-Matrix | A smaller matrix obtained by dividing a larger matrix into blocks. |
| Divide and Conquer | Dividing a problem into smaller subproblems, solving them and combining their results. |
| Recursion | Solving a problem by repeatedly applying the same method to smaller instances. |
Basic Idea of Strassen's Algorithm
Suppose we want to multiply two square matrices A and B. For simplicity, first consider 2 × 2 matrices.
| A11 | A12 |
| A21 | A22 |
| B11 | B12 |
| B21 | B22 |
Figure 1: Division of matrices into four sub-matrices
For larger matrices, the same process is applied recursively. Each matrix is divided into four approximately equal-sized blocks until the base case is reached.
Conventional Multiplication of Two 2 × 2 Matrices
Let:
A =
[ a b ]
[ c d ]
and
B =
[ e f ]
[ g h ]
The conventional multiplication is:
C11 = ae + bg
C12 = af + bh
C21 = ce + dg
C22 = cf + dh
This requires eight multiplications:
ae, bg, af, bh, ce, dg, cf, dh
Strassen's method rearranges these calculations so that only seven multiplications are needed.
Seven Intermediate Products
Strassen's Algorithm calculates seven intermediate products, traditionally called P1 to P7.
| Product | Formula |
|---|---|
| P1 | (A11 + A22) × (B11 + B22) |
| P2 | (A21 + A22) × B11 |
| P3 | A11 × (B12 − B22) |
| P4 | A22 × (B21 − B11) |
| P5 | (A11 + A12) × B22 |
| P6 | (A21 − A11) × (B11 + B12) |
| P7 | (A12 − A22) × (B21 + B22) |
Construction of the Result Matrix
After calculating the seven products, the four blocks of the result matrix are calculated as follows:
| Result Block | Formula |
|---|---|
| C11 | P1 + P4 − P5 + P7 |
| C12 | P3 + P5 |
| C21 | P2 + P4 |
| C22 | P1 − P2 + P3 + P6 |
Finally, the four blocks are combined:
| C11 | C12 |
| C21 | C22 |
Step-by-Step Working
For two 2 × 2 matrices:
A =
[ A11 A12 ]
[ A21 A22 ]
B =
[ B11 B12 ]
[ B21 B22 ]
The complete process is:
- Divide matrices A and B into four blocks.
- Calculate the seven products P1 to P7.
- Calculate C11, C12, C21 and C22.
- Combine the four result blocks.
- For larger matrices, recursively repeat the same process.
Solved Example 1: Simple 2 × 2 Matrix
Multiply:
A =
[ 1 2 ]
[ 3 4 ]
B =
[ 5 6 ]
[ 7 8 ]
Therefore:
A11=1, A12=2, A21=3, A22=4
B11=5, B12=6, B21=7, B22=8
Step 1: Calculate P1
P1 = (A11 + A22) × (B11 + B22)
= (1 + 4) × (5 + 8)
= 5 × 13 = 65
Step 2: Calculate P2
P2 = (A21 + A22) × B11
= (3 + 4) × 5
= 7 × 5 = 35
Step 3: Calculate P3
P3 = A11 × (B12 − B22)
= 1 × (6 − 8)
= 1 × (−2) = −2
Step 4: Calculate P4
P4 = A22 × (B21 − B11)
= 4 × (7 − 5)
= 4 × 2 = 8
Step 5: Calculate P5
P5 = (A11 + A12) × B22
= (1 + 2) × 8
= 3 × 8 = 24
Step 6: Calculate P6
P6 = (A21 − A11) × (B11 + B12)
= (3 − 1) × (5 + 6)
= 2 × 11 = 22
Step 7: Calculate P7
P7 = (A12 − A22) × (B21 + B22)
= (2 − 4) × (7 + 8)
= −2 × 15 = −30
Step 8: Calculate C11
C11 = P1 + P4 − P5 + P7
= 65 + 8 − 24 − 30
= 19
Step 9: Calculate C12
C12 = P3 + P5
= −2 + 24 = 22
Step 10: Calculate C21
C21 = P2 + P4
= 35 + 8 = 43
Step 11: Calculate C22
C22 = P1 − P2 + P3 + P6
= 65 − 35 − 2 + 22
= 50
Final Answer
| 19 | 22 |
| 43 | 50 |
Therefore, AB = [19 22; 43 50]
Solved Example 2: Medium 4 × 4 Matrix
Multiply the following matrices using Strassen's method:
A =
[ 1 2 3 4 ]
[ 5 6 7 8 ]
[ 9 10 11 12 ]
[ 13 14 15 16 ]
B =
[ 16 15 14 13 ]
[ 12 11 10 9 ]
[ 8 7 6 5 ]
[ 4 3 2 1 ]
For a 4 × 4 matrix, divide each matrix into four 2 × 2 blocks.
For matrix A:
A11 =
[ 1 2 ]
[ 5 6 ]
A12 =
[ 3 4 ]
[ 7 8 ]
A21 =
[ 9 10 ]
[ 13 14 ]
A22 =
[ 11 12 ]
[ 15 16 ]
Similarly, divide B:
B11 =
[ 16 15 ]
[ 12 11 ]
B12 =
[ 14 13 ]
[ 10 9 ]
B21 =
[ 8 7 ]
[ 4 3 ]
B22 =
[ 6 5 ]
[ 2 1 ]
Now apply the seven Strassen formulas to these 2 × 2 blocks. Each multiplication of two 2 × 2 blocks is itself performed using Strassen's method.
Step 1: Calculate the Seven Block Products
P1 = (A11 + A22)(B11 + B22)
A11 + A22 =
[ 12 14 ]
[ 20 22 ]
B11 + B22 =
[ 22 20 ]
[ 14 12 ]
Therefore:
P1 =
[ 12 14 ] [ 22 20 ]
[ 20 22 ] [ 14 12 ]
= [ 460 408; 748 664 ]
Similarly:
P2 = (A21 + A22)B11
A21 + A22 =
[ 20 22 ]
[ 28 30 ]
P2 = [ 584 542; 808 753 ]
P3 = A11(B12 − B22)
B12 − B22 =
[ 8 8 ]
[ 8 8 ]
P3 = [ 24 24; 88 88 ]
P4 = A22(B21 − B11)
B21 − B11 =
[ −8 −8 ]
[ −8 −8 ]
P4 = [ −184 −184; −248 −248 ]
P5 = (A11 + A12)B22
A11 + A12 =
[ 4 6 ]
[ 12 14 ]
P5 = [ 36 26; 100 74 ]
P6 = (A21 − A11)(B11 + B12)
A21 − A11 =
[ 8 8 ]
[ 8 8 ]
B11 + B12 =
[ 30 28 ]
[ 22 20 ]
P6 = [ 416 384; 416 384 ]
P7 = (A12 − A22)(B21 + B22)
A12 − A22 =
[ −8 −8 ]
[ −8 −8 ]
B21 + B22 =
[ 14 12 ]
[ 6 4 ]
P7 = [ −160 −128; −160 −128 ]
Step 2: Calculate the Four Result Blocks
C11 = P1 + P4 − P5 + P7
= [ 80 70; 240 190 ]
C12 = P3 + P5
= [ 60 50; 188 162 ]
C21 = P2 + P4
= [ 400 358; 560 505 ]
C22 = P1 − P2 + P3 + P6
= [ 316 274; 444 383 ]
Final Answer
| 80 | 70 | 60 | 50 |
| 240 | 190 | 188 | 162 |
| 400 | 358 | 316 | 274 |
| 560 | 505 | 444 | 383 |
Solved Example 3: Advanced 8 × 8 Concept
For an 8 × 8 matrix, Strassen's Algorithm is applied recursively. The important point is that the algorithm does not directly multiply all 64 × 64 individual elements using the conventional method.
The 8 × 8 matrix is first divided into four 4 × 4 blocks:
| A11 (4 × 4) | A12 (4 × 4) |
| A21 (4 × 4) | A22 (4 × 4) |
The same division is made for matrix B. The seven block products are then formed:
P1 = (A11 + A22)(B11 + B22)
P2 = (A21 + A22)B11
P3 = A11(B12 − B22)
P4 = A22(B21 − B11)
P5 = (A11 + A12)B22
P6 = (A21 − A11)(B11 + B12)
P7 = (A12 − A22)(B21 + B22)
Each of these seven 4 × 4 multiplications is again divided into four 2 × 2 blocks. The same seven-product procedure is then applied recursively.
Thus, the recursion can be represented as:
8 × 8 → 4 × 4 → 2 × 2 → 1 × 1
At the 1 × 1 level, ordinary scalar multiplication is performed. The results are then combined while returning from the recursive calls.
This example demonstrates the most important feature of Strassen's Algorithm: the same seven-product method is repeatedly applied to smaller matrices.
How Recursion Works
For an n × n matrix, the algorithm divides the problem into seven subproblems of size n/2 × n/2.
T(n) = 7T(n/2) + O(n2)
The 7T(n/2) term represents the seven recursive multiplications, while O(n2) represents the matrix additions and subtractions.
The recursion stops when the matrix reaches the selected base-case size, commonly 1 × 1 or a small threshold such as 2 × 2, depending on the implementation.
Handling Matrix Sizes That Are Not Powers of 2
The simplest recursive implementation works most naturally when the matrix dimension is a power of 2, such as:
2, 4, 8, 16, 32, 64, ...
If the matrix size is not a power of 2, such as 3 × 3 or 5 × 5, the matrices can be padded with zeros until the required dimension is reached.
For example, a 3 × 3 matrix can be converted into a 4 × 4 matrix by adding a row and column containing zeros:
| a | b | c | 0 |
| d | e | f | 0 |
| g | h | i | 0 |
| 0 | 0 | 0 | 0 |
After multiplication, the extra row and column are discarded and the original 3 × 3 result is retained.
Strassen's Algorithm
Strassen(A, B)
Step 1 : If the matrix size is the base case, perform ordinary multiplication.
Step 2 : Divide A into A11, A12, A21 and A22.
Step 3 : Divide B into B11, B12, B21 and B22.
Step 4 : Calculate P1 to P7.
Step 5 : Calculate C11, C12, C21 and C22.
Step 6 : Combine the four result blocks.
Step 7 : Return the resultant matrix.
Pseudocode
Strassen(A, B)
if size(A) == 1
return A × B
Divide A into A11, A12, A21, A22
Divide B into B11, B12, B21, B22
P1 = Strassen(A11 + A22, B11 + B22)
P2 = Strassen(A21 + A22, B11)
P3 = Strassen(A11, B12 - B22)
P4 = Strassen(A22, B21 - B11)
P5 = Strassen(A11 + A12, B22)
P6 = Strassen(A21 - A11, B11 + B12)
P7 = Strassen(A12 - A22, B21 + B22)
C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 - P2 + P3 + P6
Combine C11, C12, C21, C22
return C
Complexity Analysis
| Method | Time Complexity | Multiplication Subproblems |
|---|---|---|
| Conventional Matrix Multiplication | O(n3) | 8 recursive multiplications |
| Strassen's Algorithm | O(nlog27) ≈ O(n2.807) | 7 recursive multiplications |
The improvement comes from reducing the number of recursive multiplications from 8 to 7. However, Strassen's method performs more additions and subtractions and requires additional temporary storage.
Advantages of Strassen's Algorithm
- Uses seven recursive multiplications instead of eight.
- Has a theoretical time complexity of approximately O(n2.807).
- Uses the Divide and Conquer technique.
- Can provide performance benefits for sufficiently large matrices.
- Can be applied recursively to large square matrices.
Limitations of Strassen's Algorithm
- The algorithm is more complicated than conventional matrix multiplication.
- It requires additional matrix additions and subtractions.
- It requires additional temporary storage.
- Recursive overhead makes it less attractive for small matrices.
- Numerical errors can be more noticeable for floating-point computations because of the additional additions and subtractions.
- For practical implementations, a conventional multiplication method is often used below a selected matrix-size threshold.
Applications of Strassen's Algorithm
| Area | Use |
|---|---|
| Scientific Computing | Efficient multiplication of large matrices. |
| Machine Learning | Matrix operations used in computational models. |
| Computer Graphics | Matrix-based transformations and calculations. |
| Image Processing | Large-scale matrix computations. |
| Numerical Computing | Efficient computation involving large dense matrices. |
Conventional Matrix Multiplication vs Strassen's Algorithm
| Feature | Conventional Method | Strassen's Algorithm |
|---|---|---|
| Technique | Direct multiplication | Divide and Conquer |
| Recursive Multiplications | 8 | 7 |
| Time Complexity | O(n3) | O(n2.807) approximately |
| Implementation | Simple | More complex |
| Extra Additions/Subtractions | Fewer | More |
| Extra Storage | Lower | Higher |
| Small Matrices | Usually preferable | Recursive overhead may dominate |
Summary
Strassen's Matrix Multiplication is a Divide and Conquer algorithm that reduces the number of recursive multiplications required for matrix multiplication from eight to seven. The algorithm divides matrices into four blocks, calculates seven carefully designed intermediate products and combines them to obtain the final matrix.
Its recurrence relation is T(n) = 7T(n/2) + O(n2), giving a theoretical time complexity of O(nlog27) ≈ O(n2.807). The method is particularly useful as a theoretical and practical technique for large dense matrix multiplication, although the extra additions, temporary storage and recursive overhead mean that it is not necessarily the best choice for every matrix size.