Sub topic 14
Strassen's Matrix Multiplication
This chapter assumes you know the master theorem, the Karatsuba idea of trading multiplications for additions, and how ordinary matrix multiplication works.
The Naive Divide-and-Conquer Baseline
Split two n×n matrices A and B into four n/2 × n/2 blocks each. The product C = AB then follows the same rule as 2×2 multiplication, with blocks in place of numbers:
- C11 = A11·B11 + A12·B21
- C12 = A11·B12 + A12·B22
- C21 = A21·B11 + A22·B21
- C22 = A21·B12 + A22·B22
That makes eight half-size multiplications plus Θ(n²) work for the block additions. The recurrence is T(n) = 8T(n/2) + Θ(n²). Here log₂8 = 3, so case 1 of the master theorem gives Θ(n³). Splitting the matrices alone gains nothing over the triple loop.
Seven Products Instead of Eight
In 1969, Volker Strassen found seven block products that are enough to rebuild C. The cost is extra additions and subtractions:
| Product | Definition |
|---|---|
| M1 | (A11 + A22)(B11 + B22) |
| M2 | (A21 + A22) B11 |
| M3 | A11 (B12 − B22) |
| M4 | A22 (B21 − B11) |
| M5 | (A11 + A12) B22 |
| M6 | (A21 − A11)(B11 + B12) |
| M7 | (A12 − A22)(B21 + B22) |
The four result blocks are built from these products:
- C11 = M1 + M4 − M5 + M7
- C12 = M3 + M5
- C21 = M2 + M4
- C22 = M1 − M2 + M3 + M6
The new recurrence is T(n) = 7T(n/2) + Θ(n²). Since log₂7 ≈ 2.807 > 2, case 1 still applies, and the running time is Θ(n^2.807).
Implementation
The code below assumes n is a power of two.
def add(X, Y):
return [[a + b for a, b in zip(r, s)] for r, s in zip(X, Y)]
def sub(X, Y):
return [[a - b for a, b in zip(r, s)] for r, s in zip(X, Y)]
def strassen(A, B):
n = len(A)
if n == 1:
return [[A[0][0] * B[0][0]]]
h = n // 2
def quad(M, i, j):
return [row[j * h:(j + 1) * h] for row in M[i * h:(i + 1) * h]]
A11, A12, A21, A22 = quad(A, 0, 0), quad(A, 0, 1), quad(A, 1, 0), quad(A, 1, 1)
B11, B12, B21, B22 = quad(B, 0, 0), quad(B, 0, 1), quad(B, 1, 0), quad(B, 1, 1)
M1 = strassen(add(A11, A22), add(B11, B22))
M2 = strassen(add(A21, A22), B11)
M3 = strassen(A11, sub(B12, B22))
M4 = strassen(A22, sub(B21, B11))
M5 = strassen(add(A11, A12), B22)
M6 = strassen(sub(A21, A11), add(B11, B12))
M7 = strassen(sub(A12, A22), add(B21, B22))
C11 = add(sub(add(M1, M4), M5), M7)
C12 = add(M3, M5)
C21 = add(M2, M4)
C22 = add(add(sub(M1, M2), M3), M6)
top = [r1 + r2 for r1, r2 in zip(C11, C12)]
bottom = [r1 + r2 for r1, r2 in zip(C21, C22)]
return top + bottom
def naive(A, B):
return [[sum(A[i][k] * B[k][j] for k in range(len(B)))
for j in range(len(B[0]))] for i in range(len(A))]
A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
B = [[2, 0, 1, 3], [1, 4, 0, 2], [3, 1, 2, 0], [0, 2, 1, 1]]
print(strassen(A, B) == naive(A, B)) # TruePractical Caveats
- If n is not a power of two, pad the matrices with zeros, or peel off an odd row and column and handle them separately.
- Library routines for dense floating-point matrices, such as the BLAS used by NumPy, mostly use tuned cubic algorithms because they make better use of the cache.
- Algorithms with better exponents than Strassen's exist, currently around 2.37. Their constant factors are so large that they are never used in practice.
Exercise
Suppose someone found a way to multiply 2×2 block matrices with 6 half-size products and Θ(n²) extra work. What would the running time be, and how does it compare with Strassen's?
Show answer
The recurrence would be T(n) = 6T(n/2) + Θ(n²). Since log₂6 ≈ 2.585 > 2, case 1 of the master theorem gives Θ(n^2.585), which is asymptotically faster than Strassen's Θ(n^2.807).
Exercise
Why does setting the base case to n = 1 make the code above slow in practice, even though the asymptotic bound is correct?
Show answer
Near the bottom of the recursion, the time goes to list slicing, building temporary matrices, and the 18 additions per call, not to useful multiplication. Switching to naive multiplication below a cutoff size removes that overhead.