Sub topic 14
Strassen's Matrix Multiplication
This chapter assumes you know the Karatsuba chapter's main idea: if you cut the number of recursive subproblems, the exponent in the running time goes down. It also assumes you can apply the master theorem, read Python list comprehensions, and multiply two matrices by hand.
Strassen's algorithm applies Karatsuba's idea to matrices. Block matrix multiplication needs eight half-size products. Strassen gets by with seven. That one saved product changes the running time from Θ(n³) to Θ(n^log₂7), about Θ(n^2.807).
Block Multiplication and Why the Obvious Split Gains Nothing
Split each n × n matrix into four n/2 × n/2 blocks:
- A = [[A11, A12], [A21, A22]]
- B = [[B11, B12], [B21, B22]]
The product C = AB has the same block structure, and each block follows the ordinary row-times-column rule:
- C11 = A11·B11 + A12·B21
- C12 = A11·B12 + A12·B22
- C21 = A21·B11 + A22·B21
- C22 = A21·B12 + A22·B22
That gives eight recursive multiplications of half-size blocks plus four block additions, each costing Θ(n²). The recurrence is:
T(n) = 8T(n/2) + Θ(n²)
Here a = 8 and b = 2, so n^log_b a = n³. That grows faster than n², so master theorem case 1 applies and T(n) = Θ(n³). The recursion reorganizes the triple loop without making it faster. To improve the bound you have to lower a.
Seven Products Instead of Eight
In 1969 Volker Strassen found seven products of sums and differences of blocks that are enough to rebuild C:
- 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 blocks of C are then sums and differences of these products:
- C11 = M1 + M4 − M5 + M7
- C12 = M3 + M5
- C21 = M2 + M4
- C22 = M1 − M2 + M3 + M6
These identities hold for blocks because they only use the ring axioms: addition, subtraction, distributivity, and associativity. They never assume that multiplication commutes, and matrix multiplication does not commute. If you expand M1 + M4 − M5 + M7, every unwanted cross term cancels and A11·B11 + A12·B21 is left. Karatsuba's z1 trick works the same way.
You can check the formulas with scalars. Take A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]]:
| Product | Value |
|---|---|
| M1 = (1+4)(5+8) | 65 |
| M2 = (3+4)·5 | 35 |
| M3 = 1·(6−8) | −2 |
| M4 = 4·(7−5) | 8 |
| M5 = (1+2)·8 | 24 |
| M6 = (3−1)(5+6) | 22 |
| M7 = (2−4)(7+8) | −30 |
So C11 = 65 + 8 − 24 − 30 = 19, C12 = 22, C21 = 43, and C22 = 50. These match the ordinary product [[19, 22], [43, 50]].
The Recurrence
One level does 7 recursive multiplications and 18 block additions or subtractions: 10 to form the operands and 8 to combine the products. Each addition costs Θ(n²):
T(n) = 7T(n/2) + Θ(n²)
Now n^log₂7 ≈ n^2.807, which still dominates n², so case 1 gives T(n) = Θ(n^log₂7). The recursion tree shows why. Level i has 7^i nodes, and each node does Θ((n/2^i)²) work, so level i costs Θ(n²·(7/4)^i) in total. The ratio 7/4 is greater than 1, so the per-level cost grows geometrically and the leaves dominate. There are 7^log₂n = n^log₂7 leaves.
| Method | Recurrence | Running time |
|---|---|---|
| Triple loop | none | Θ(n³) |
| Block recursion, 8 products | 8T(n/2) + Θ(n²) | Θ(n³) |
| Strassen, 7 products | 7T(n/2) + Θ(n²) | Θ(n^2.807) |
An Implementation with Padding and a Cutoff
The code below handles square matrices of any size. It pads them with zeros up to the next power of two, then recurses. Below a cutoff size it switches to the naive method.
import random
def add(A, B):
return [[a + b for a, b in zip(ra, rb)] for ra, rb in zip(A, B)]
def sub(A, B):
return [[a - b for a, b in zip(ra, rb)] for ra, rb in zip(A, B)]
def naive(A, B):
n, k, m = len(A), len(B), len(B[0])
return [[sum(A[i][t] * B[t][j] for t in range(k)) for j in range(m)]
for i in range(n)]
def split(M):
h = len(M) // 2
return ([row[:h] for row in M[:h]], [row[h:] for row in M[:h]],
[row[:h] for row in M[h:]], [row[h:] for row in M[h:]])
def strassen_square(A, B, cutoff):
# A and B are n x n with n a power of two
n = len(A)
if n <= cutoff:
return naive(A, B)
A11, A12, A21, A22 = split(A)
B11, B12, B21, B22 = split(B)
M1 = strassen_square(add(A11, A22), add(B11, B22), cutoff)
M2 = strassen_square(add(A21, A22), B11, cutoff)
M3 = strassen_square(A11, sub(B12, B22), cutoff)
M4 = strassen_square(A22, sub(B21, B11), cutoff)
M5 = strassen_square(add(A11, A12), B22, cutoff)
M6 = strassen_square(sub(A21, A11), add(B11, B12), cutoff)
M7 = strassen_square(sub(A12, A22), add(B21, B22), cutoff)
C11 = add(sub(add(M1, M4), M5), M7)
C12 = add(M3, M5)
C21 = add(M2, M4)
C22 = add(sub(M1, M2), add(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 strassen(A, B, cutoff=32):
n = len(A)
size = 1
while size < n:
size *= 2
def pad(M):
return ([row + [0] * (size - n) for row in M] +
[[0] * size for _ in range(size - n)])
C = strassen_square(pad(A), pad(B), max(1, cutoff))
return [row[:n] for row in C[:n]]
print(strassen([[1, 2], [3, 4]], [[5, 6], [7, 8]], cutoff=1)) # [[19, 22], [43, 50]]
random.seed(0)
n = 50
A = [[random.randint(-9, 9) for _ in range(n)] for _ in range(n)]
B = [[random.randint(-9, 9) for _ in range(n)] for _ in range(n)]
print(strassen(A, B) == naive(A, B)) # TrueThe base case is n ≤ cutoff, and every recursive call halves n, so the recursion terminates. Correctness follows by induction on n. The base case uses the naive product. In the inductive step, each Mi is correct by the inductive hypothesis, and the block identities produce the correct C.
Gotchas and Trade-offs
The cutoff matters more than the exponent
Each level does 18 matrix additions, against 4 for the eight-product split, and it allocates many temporary matrices. For small n that overhead costs more than the multiplication it saves. Real implementations stop recursing at a crossover size, usually somewhere from a few dozen to several hundred depending on hardware, and finish with a cache-tuned standard kernel. Pure-Python lists exaggerate the overhead. In practice, a BLAS library such as the one behind NumPy's @ beats this code by orders of magnitude, even though it runs a Θ(n³) algorithm.
Odd and non-power-of-two sizes
Padding to the next power of two can almost double the dimension. A 513 × 513 input becomes 1024 × 1024, and that work counts against you. Alternatives are dynamic peeling, which handles one extra row and column separately at each level, or padding by a single row and column only when n is odd.
Numerical stability
The algorithm is exact for integers. With floating point, the subtractions in M3, M4, M6, and M7 can cancel large intermediate values. Strassen's error bound holds only in a normwise sense, so it is weaker than the elementwise bound of the standard algorithm. For most scientific workloads the result is acceptable, but some libraries avoid Strassen because of this.
It needs subtraction
The cancellation trick relies on additive inverses. Over a semiring such as (min, +), which is used for all-pairs shortest paths, no subtraction exists, so Strassen's identities cannot be applied.
Memory
A straightforward implementation allocates Θ(n²) temporary storage at each level. The total is still Θ(n²) because the sizes form a geometric series, but the constant is large. Memory-careful versions reuse buffers.
Beyond Strassen
The Winograd variant also uses seven products but needs only 15 additions per level, so it usually replaces the original in practice. Theoretical work has pushed the matrix multiplication exponent ω below 2.372. Those algorithms have constants so large that no one uses them on real inputs. In practice you get Strassen-style speedups only on very large dense matrices, and only when the implementation is carefully engineered.
Exercise
Suppose someone found a way to multiply 3 × 3 block matrices using only 22 block multiplications, with O(n²) additional work per level. Write the recurrence and compare its running time with Strassen's.
Show answer
T(n) = 22T(n/3) + Θ(n²). Since log₃22 ≈ 2.814 > 2, master theorem case 1 gives Θ(n^2.814). That is slightly worse than Strassen's n^2.807, so the 3 × 3 scheme would need 21 or fewer products to win (log₃21 ≈ 2.771).
Exercise
Using only Strassen's definitions, verify that C21 = M2 + M4 equals A21·B11 + A22·B21 without assuming that multiplication commutes.
Show answer
M2 = (A21 + A22)B11 = A21·B11 + A22·B11. M4 = A22(B21 − B11) = A22·B21 − A22·B11. Adding them cancels A22·B11 and leaves A21·B11 + A22·B21. Each product kept its left-right order, so commutativity was never used.
Exercise
Why does the recursion tree for T(n) = 7T(n/2) + cn² put most of the cost at the leaves, while T(n) = 2T(n/2) + cn for merge sort spreads it evenly across levels?
Show answer
Level i of Strassen costs cn²(7/4)^i. The ratio is above 1, so costs grow geometrically toward the leaves and the bottom level dominates. For merge sort, level i costs cn·(2/2)^i = cn, so all log n levels cost the same and the total is Θ(n log n).