Algorithm Search⌘K

Proofs: Strassen's algorithm

These are the human proofs of Strassen's algorithm, at full length.

Strassen's seven products give the matrix product full

This proof shows: correctness, termination.

Let RR be a ring (an additive abelian group with an associative multiplication that distributes over addition). Commutativity of the multiplication is not used. For matrices over RR of matching shapes, the matrix ring laws hold: X(Y+Z)=XY+XZX(Y + Z) = XY + XZ, (X+Y)Z=XZ+YZ(X + Y)Z = XZ + YZ, X(−Y)=−(XY)=(−X)YX(-Y) = -(XY) = (-X)Y, and block multiplication: if AA and BB are split into 2×22 \times 2 blocks with matching inner splits, then (AB)rs=Ar1B1s+Ar2B2s(AB)_{rs} = A_{r1} B_{1s} + A_{r2} B_{2s}.

Lemma (the four identities). For any blocks of matching shapes, with M1,…,M7M_1, \ldots, M_7 defined as in lines L4 to L10 but with exact products,

M1+M4−M5+M7=A11B11+A12B21,M3+M5=A11B12+A12B22,M2+M4=A21B11+A22B21,M1−M2+M3+M6=A21B12+A22B22.\begin{aligned} M_1 + M_4 - M_5 + M_7 &= A_{11} B_{11} + A_{12} B_{21}, &\quad M_3 + M_5 &= A_{11} B_{12} + A_{12} B_{22}, \\ M_2 + M_4 &= A_{21} B_{11} + A_{22} B_{21}, &\quad M_1 - M_2 + M_3 + M_6 &= A_{21} B_{12} + A_{22} B_{22}. \end{aligned}

Proof. Expand each product by distributivity, with the AA factor kept on the left:

  • M1=A11B11+A11B22+A22B11+A22B22M_1 = A_{11}B_{11} + A_{11}B_{22} + A_{22}B_{11} + A_{22}B_{22}
  • M2=A21B11+A22B11M_2 = A_{21}B_{11} + A_{22}B_{11}
  • M3=A11B12−A11B22M_3 = A_{11}B_{12} - A_{11}B_{22}
  • M4=A22B21−A22B11M_4 = A_{22}B_{21} - A_{22}B_{11}
  • M5=A11B22+A12B22M_5 = A_{11}B_{22} + A_{12}B_{22}
  • M6=A21B11+A21B12−A11B11−A11B12M_6 = A_{21}B_{11} + A_{21}B_{12} - A_{11}B_{11} - A_{11}B_{12}
  • M7=A12B21+A12B22−A22B21−A22B22M_7 = A_{12}B_{21} + A_{12}B_{22} - A_{22}B_{21} - A_{22}B_{22}

First identity. In M1+M4−M5+M7M_1 + M_4 - M_5 + M_7, the term A11B22A_{11}B_{22} appears in M1M_1 and in −M5-M_5 and cancels. A22B11A_{22}B_{11} appears in M1M_1 and with a minus sign in M4M_4. A22B22A_{22}B_{22} appears in M1M_1 and with a minus sign in M7M_7. A22B21A_{22}B_{21} appears in M4M_4 and with a minus sign in M7M_7. A12B22A_{12}B_{22} appears in −M5-M_5 with a minus sign and in M7M_7 with a plus sign. What is left is A11B11+A12B21A_{11}B_{11} + A_{12}B_{21}.

Second identity. M3+M5=A11B12−A11B22+A11B22+A12B22=A11B12+A12B22M_3 + M_5 = A_{11}B_{12} - A_{11}B_{22} + A_{11}B_{22} + A_{12}B_{22} = A_{11}B_{12} + A_{12}B_{22}.

Third identity. M2+M4=A21B11+A22B11+A22B21−A22B11=A21B11+A22B21M_2 + M_4 = A_{21}B_{11} + A_{22}B_{11} + A_{22}B_{21} - A_{22}B_{11} = A_{21}B_{11} + A_{22}B_{21}.

Fourth identity. In M1−M2+M3+M6M_1 - M_2 + M_3 + M_6: A11B11A_{11}B_{11} cancels between M1M_1 and M6M_6; A11B22A_{11}B_{22} cancels between M1M_1 and M3M_3; A22B11A_{22}B_{11} cancels between M1M_1 and −M2-M_2; A21B11A_{21}B_{11} cancels between −M2-M_2 and M6M_6; A11B12A_{11}B_{12} cancels between M3M_3 and M6M_6. What is left is A22B22+A21B12A_{22}B_{22} + A_{21}B_{12}. Addition is commutative, so this is the claimed right side. □\square

Lemma (padding). Let A′A' be AA with zero rows added at the bottom and zero columns added at the right, and B′B' be BB with zero rows added at the bottom (as many as the zero columns of A′A') and zero columns at the right. Then the top left n×pn \times p block of A′B′A'B' is ABAB.

Proof. For i<ni < n and j<pj < p, (A′B′)[i][j]=∑k<mA[i][k]B[k][j]+∑k≥mA′[i][k]B′[k][j](A'B')[i][j] = \sum_{k < m} A[i][k]B[k][j] + \sum_{k \ge m} A'[i][k] B'[k][j], and each term of the second sum has A′[i][k]=0A'[i][k] = 0. □\square

Theorem. For every n0≥1n_0 \ge 1 and all A∈Rn×mA \in R^{n \times m}, B∈Rm×pB \in R^{m \times p}, STRASSEN(A,B)(A, B) stops and returns ABAB.

Proof. Let μ=max⁡(n,m,p)\mu = \max(n, m, p). We use strong induction on μ\mu, and inside one value of μ\mu, we first treat the calls of line L3 (all sides even) and then the calls of line L2.

Line L1. If min⁡(n,m,p)≤n0\min(n, m, p) \le n_0, the result is the classical product, which is correct by the triple loop proof. This covers every μ≤n0\mu \le n_0 and every input with a side 00.

All sides even, min⁡(n,m,p)>n0≥1\min(n, m, p) > n_0 \ge 1. Then all sides are at least 2. Line L3 splits each side dd into two halves of length d/2<dd/2 < d. Each of the seven calls of lines L4 to L10 has the shape (n/2)×(m/2)(n/2) \times (m/2) times (m/2)×(p/2)(m/2) \times (p/2), with max⁡<μ\max < \mu, so by the hypothesis it returns the exact product of its two operands. By the first lemma, lines L11 to L14 compute the four blocks (AB)rs(AB)_{rs}, and by block multiplication the matrix of line L15 is ABAB.

Some side odd, min⁡(n,m,p)>n0\min(n, m, p) > n_0. Line L2 pads each odd side by one, so the padded sides are even and at most μ+1\mu + 1. The call on the padded matrices passes line L1 (its sides are at least the old ones) and goes to line L3 with all sides even. Its seven calls have sides at most ⌈μ/2⌉\lceil \mu / 2 \rceil, which is less than μ\mu since μ≥2\mu \ge 2, so by the hypothesis and the case above, the padded call returns A′B′A'B'. By the padding lemma, removing the padding gives ABAB.

Termination. Each call either returns at line L1, or calls itself on sides at most ⌈μ/2⌉<μ\lceil \mu/2 \rceil < \mu after at most one padding step. So μ\mu drops along each path of the recursion, and the depth is O(log⁡μ)O(\log \mu). ■\blacksquare

Back to Strassen's algorithm