Skip to content
Ahmet Çelik

Chapter 4: Matrix Multiplication and Strassen

COMP305 7 minute read

Source files used: Matrix Multiplication (Recitation 1, Sections 2 and 3.2); textbook Chapter 4 (chap4, Sections 4.1 and 4.2) as support. No worked examples are included.

1. Big Picture

Strassen’s algorithm is the recitation’s showcase for divide and conquer: a constant saving per level (7 multiplications instead of 8) changes the exponent of the running time.

The Convex Hull guide asked: how do you make the merge step cheap? This guide asks: how do you make the recursion tree less bushy?

Trade one recursive multiplication for extra additions: 8T(n/2)→7T(n/2).\boxed{\text{Trade one recursive multiplication for extra additions: }8T(n/2)\to7T(n/2).}

New ideas:

  • standard multiplication is Θ(n3)\Theta(n^3),
  • naive block divide and conquer is still Θ(n3)\Theta(n^3),
  • Strassen’s seven products M1,…,M7M_1,\dots,M_7,
  • T(n)=7T(n/2)+Θ(n2)=Θ(nlg⁡7)≈Θ(n2.807)T(n)=7T(n/2)+\Theta(n^2)=\Theta(n^{\lg7})\approx\Theta(n^{2.807}).

2. Core Ideas

2.1 Standard Multiplication

Entry (i,j)(i,j) of C=ABC=AB is row ii of AA times column jj of BB. For square n×nn\times n matrices this takes Θ(n3)\Theta(n^3): n2n^2 entries, each a sum of nn products.


2.2 Size Is the Dimension nn

Running time is measured in terms of the dimension nn, not the number of entries n2n^2. Splitting into four blocks halves nn, so subproblems are T(n/2)T(n/2), not T(n/4)T(n/4).

This is a common exam trap when writing the recurrence.


2.3 Addition Is Cheap, Multiplication Is Expensive

Adding two n×nn\times n matrices costs Θ(n2)\Theta(n^2); multiplying costs much more. So saving a multiplication at the cost of a constant number of extra additions pays off.

Textbook analogy: x2−y2=(x+y)(x−y)x^2-y^2=(x+y)(x-y) uses one multiplication instead of two, at the cost of an extra addition. For scalars this gains nothing; for large matrices it does.


2.4 Padding to a Power of 2

The recitation makes AA and BB each 2k×2k2^k\times2^k by filling the extra rows and columns with 0. This is allowed because:

  • each dimension grows by less than a factor of 2,
  • even with Θ(n3)\Theta(n^3) multiplication, that costs less than a factor of 8, a constant.

Strip the extra zero rows and columns from CC at the end. Powers of 2 are needed (not just even sizes) because the recursion halves the dimension at every level.


2.5 Strassen Is Not the Fastest Known

Strassen was a powerful early application of divide and conquer and was the fastest method when discovered. Later work by Coppersmith, Winograd, Stothers, and Vassilevska Williams gives faster methods.


3. Main Governing Equations and Formulas

3.1 Entry Formula

cij=∑k=1naikbkjc_{ij}=\sum_{k=1}^{n}a_{ik}b_{kj}

Three nested loops, Θ(n3)\Theta(n^3).


3.2 Block Partition

A=[A11A12A21A22],B=[B11B12B21B22],C=[C11C12C21C22]A=\begin{bmatrix}A_{11}&A_{12}\\A_{21}&A_{22}\end{bmatrix},\quad B=\begin{bmatrix}B_{11}&B_{12}\\B_{21}&B_{22}\end{bmatrix},\quad C=\begin{bmatrix}C_{11}&C_{12}\\C_{21}&C_{22}\end{bmatrix}

Each block has dimension n/2n/2.


3.3 Block Product: 8 Multiplications

C11=A11B11+A12B21C_{11}=A_{11}B_{11}+A_{12}B_{21} C12=A11B12+A12B22C_{12}=A_{11}B_{12}+A_{12}B_{22} C21=A21B11+A22B21C_{21}=A_{21}B_{11}+A_{22}B_{21} C22=A21B12+A22B22C_{22}=A_{21}B_{12}+A_{22}B_{22}

Recurrence (textbook, partitioning by index calculation in Θ(1)\Theta(1)):

T(n)=8T(n/2)+Θ(1)=Θ(n3)T(n)=8T(n/2)+\Theta(1)=\Theta(n^3)

No improvement over three loops. Eight children per node makes a very bushy recursion tree.


3.4 Strassen’s Seven Products (Recitation Form)

M1=(A11+A22)(B11+B22)M_1=(A_{11}+A_{22})(B_{11}+B_{22}) M2=(A21+A22)B11M_2=(A_{21}+A_{22})B_{11} M3=A11(B12−B22)M_3=A_{11}(B_{12}-B_{22}) M4=A22(B21−B11)M_4=A_{22}(B_{21}-B_{11}) M5=(A11+A12)B22M_5=(A_{11}+A_{12})B_{22} M6=(A21−A11)(B11+B12)M_6=(A_{21}-A_{11})(B_{11}+B_{12}) M7=(A12−A22)(B21+B22)M_7=(A_{12}-A_{22})(B_{21}+B_{22})

Each MiM_i is computed recursively with Strassen.


3.5 Recombining into CC

C11=M1+M4−M5+M7C_{11}=M_1+M_4-M_5+M_7 C12=M3+M5C_{12}=M_3+M_5 C21=M2+M4C_{21}=M_2+M_4 C22=M1−M2+M3+M6C_{22}=M_1-M_2+M_3+M_6

Correctness follows from expanding the products. Memory aid: C12C_{12} and C21C_{21} are each a sum of two products; C11C_{11} and C22C_{22} each use M1M_1 plus three others.

The textbook writes the same algorithm with ten sums S1,…,S10S_1,\dots,S_{10} and products P1,…,P7P_1,\dots,P_7. The count is identical; use the recitation’s M1,…,M7M_1,\dots,M_7 form.


3.6 Operation Count per Level

7 multiplications+18 additions/subtractions7\text{ multiplications}+18\text{ additions/subtractions}

(10 additions inside the MiM_i, 8 in the recombination.)

For small matrices this is worse than 8 multiplications: splitting a 2-element matrix into 1-element blocks is over triple the work. In practice, switch to the standard method below some size.


3.7 Strassen Recurrence

T(n)=7T(n/2)+18 Θ(n2)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+18\,\Theta(n^2)=7T(n/2)+\Theta(n^2)

Watershed nlog⁡27n^{\log_2 7} is polynomially larger than n2n^2, so case 1:

T(n)=Θ(nlog⁡27)≈Θ(n2.8074)T(n)=\Theta(n^{\log_2 7})\approx\Theta(n^{2.8074})

The textbook writes this as O(n2.81)O(n^{2.81}).


3.8 Beating Strassen with a Different Split

For a D&C method with aa subproblems of dimension n/bn/b and Θ(n2)\Theta(n^2) combine work, the time is Θ(nlog⁡ba)\Theta(n^{\log_b a}) whenever log⁡ba>2\log_b a>2. It beats Strassen if and only if

log⁡ba<lg⁡7\log_b a<\lg 7

Example (textbook Exercise 4.5-2): splitting into n/4n/4 blocks beats Strassen only if a<4lg⁡7=49a<4^{\lg 7}=49, so at most 48 subproblems.

Use for “could this design be faster than Strassen?” questions.


4. Problem-Solving Workflow

  1. Write the cost of the standard method: Θ(n3)\Theta(n^3).
  2. If nn is not a power of 2, pad with zeros to 2k2^k (constant-factor cost).
  3. Partition into four n/2×n/2n/2\times n/2 blocks.
  4. Compute M1,…,M7M_1,\dots,M_7 recursively (Section 3.4).
  5. Combine into C11,C12,C21,C22C_{11},C_{12},C_{21},C_{22} with additions only (Section 3.5).
  6. Strip the padding.
  7. Write the recurrence in terms of the dimension: T(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2).
  8. Solve with case 1 of the master theorem: Θ(nlg⁡7)\Theta(n^{\lg 7}).

5. Decision Rules / Decision Trees

5.1 Which Recurrence?

Naive block method (8 products)? → T(n)=8T(n/2)+Θ(1)=Θ(n3)T(n)=8T(n/2)+\Theta(1)=\Theta(n^3)

Strassen (7 products)? → T(n)=7T(n/2)+Θ(n2)=Θ(nlg⁡7)T(n)=7T(n/2)+\Theta(n^2)=\Theta(n^{\lg7})

Subproblem written as T(n/4)T(n/4) because there are 4 blocks? → wrong: size is the dimension, use T(n/2)T(n/2)

5.2 Does the Combine Work Matter?

Combine work Θ(n2)\Theta(n^2), watershed nlog⁡ban^{\log_b a} with log⁡ba>2\log_b a>2? → combine work does not affect the answer (case 1)

Watershed equals n2n^2? → case 2, extra lg⁡n\lg n factor

5.3 Strassen in Practice

Small matrices? → standard method; 18 additions dominate

Large matrices? → Strassen; the saved multiplication dominates


6. Important Tables / Correlations Needed

6.1 Methods Compared

MethodProducts per levelRecurrenceTime
Three nested loops(not recursive)noneΘ(n3)\Theta(n^3)
Block D&C88T(n/2)+Θ(1)8T(n/2)+\Theta(1)Θ(n3)\Theta(n^3)
Strassen77T(n/2)+Θ(n2)7T(n/2)+\Theta(n^2)Θ(nlg⁡7)≈n2.807\Theta(n^{\lg7})\approx n^{2.807}

6.2 Strassen Cheat Sheet

ProductFormulaUsed in
M1M_1(A11+A22)(B11+B22)(A_{11}+A_{22})(B_{11}+B_{22})C11C_{11}, C22C_{22}
M2M_2(A21+A22)B11(A_{21}+A_{22})B_{11}C21C_{21}, C22C_{22}
M3M_3A11(B12−B22)A_{11}(B_{12}-B_{22})C12C_{12}, C22C_{22}
M4M_4A22(B21−B11)A_{22}(B_{21}-B_{11})C11C_{11}, C21C_{21}
M5M_5(A11+A12)B22(A_{11}+A_{12})B_{22}C11C_{11}, C12C_{12}
M6M_6(A21−A11)(B11+B12)(A_{21}-A_{11})(B_{11}+B_{12})C22C_{22}
M7M_7(A12−A22)(B21+B22)(A_{12}-A_{22})(B_{21}+B_{22})C11C_{11}

7. Key Takeaways

  • Standard matrix multiplication is Θ(n3)\Theta(n^3).
  • Measure size by the dimension nn: four blocks means T(n/2)T(n/2), not T(n/4)T(n/4).
  • Naive block D&C: 8T(n/2)+Θ(1)=Θ(n3)8T(n/2)+\Theta(1)=\Theta(n^3), no gain.
  • Strassen: 7 multiplications and 18 additions per level.
  • T(n)=7T(n/2)+Θ(n2)=Θ(nlg⁡7)≈Θ(n2.807)T(n)=7T(n/2)+\Theta(n^2)=\Theta(n^{\lg7})\approx\Theta(n^{2.807}) by case 1.
  • Pad to 2k×2k2^k\times2^k with zeros; the cost is a constant factor.
  • Memorize M1M_1 to M7M_7 and the four recombination formulas.
  • Strassen loses on small matrices because of the extra additions.
  • A design beats Strassen exactly when log⁡ba<lg⁡7\log_b a<\lg7.
  • Strassen is not the fastest known method today.