1. Gyanastra
  2. Algorithms
  3. Strassen, Karatsuba and Closest Pair of Points
  4. Strassen's Matrix Multiplication
Free lesson, no account needed 7 min read
Asked in past papers2 times
  • GATE CSE1990
  • UGC NET2013

Algorithms · Strassen, Karatsuba and Closest Pair of Points

Strassen's Matrix Multiplication

This chapter is about divide-and-conquer tricks that beat the "obvious" algorithm by reducing the number of recursive multiplications. Three classic results:

  1. Strassen's matrix multiplication — multiply two n×nn\times n matrices in Θ(nlog⁡27)≈Θ(n2.81)\Theta(n^{\log_2 7})\approx\Theta(n^{2.81}) instead of Θ(n3)\Theta(n^3).
  2. Karatsuba's integer multiplication — multiply two nn-digit numbers in Θ(nlog⁡23)≈Θ(n1.585)\Theta(n^{\log_2 3})\approx\Theta(n^{1.585}) instead of Θ(n2)\Theta(n^2).
  3. Closest pair of points — find the closest pair among nn points in the plane in O(nlog⁡n)O(n\log n) instead of O(n2)O(n^2).

All three share one idea: split the problem in half, and make fewer recursive calls than the naive divide-and-conquer would need. The Master Theorem then converts "fewer calls" into "smaller exponent."


1.1 The naive (schoolbook) method

To multiply two n×nn\times n matrices AA and BB to get C=ABC=AB:

Cij=∑k=1nAik BkjC_{ij}=\sum_{k=1}^{n} A_{ik}\,B_{kj}

Each entry needs nn multiplications and n−1n-1 additions. There are n2n^2 entries, so:

  • Scalar multiplications: n3n^3
  • Scalar additions: n2(n−1)=n3−n2n^2(n-1)=n^3-n^2

So the schoolbook method is Θ(n3)\Theta(n^3).

Divide-and-conquer the obvious way. Split each n×nn\times n matrix into four (n/2)×(n/2)(n/2)\times(n/2) blocks:

A=(A11A12A21A22),B=(B11B12B21B22)A=\begin{pmatrix}A_{11}&A_{12}\\A_{21}&A_{22}\end{pmatrix},\quad B=\begin{pmatrix}B_{11}&B_{12}\\B_{21}&B_{22}\end{pmatrix}

Then

C11=A11B11+A12B21,C12=A11B12+A12B22C_{11}=A_{11}B_{11}+A_{12}B_{21},\quad C_{12}=A_{11}B_{12}+A_{12}B_{22} C21=A21B11+A22B21,C22=A21B12+A22B22C_{21}=A_{21}B_{11}+A_{22}B_{21},\quad C_{22}=A_{21}B_{12}+A_{22}B_{22}

That is 8 block multiplications and 4 block additions. Recurrence:

T(n)=8T(n/2)+Θ(n2)T(n)=8T(n/2)+\Theta(n^2)

By the Master Theorem: a=8a=8, b=2b=2, f(n)=Θ(n2)f(n)=\Theta(n^2). Since log⁡ba=log⁡28=3\log_b a=\log_2 8=3 and f(n)=O(n3−ε)f(n)=O(n^{3-\varepsilon}), this is Case 1, giving T(n)=Θ(n3)T(n)=\Theta(n^3). No gain — as expected.

1.2 Strassen's idea: 7 multiplications instead of 8

Strassen found a way to compute the same four blocks using only 7 block multiplications, at the cost of more additions. Define:

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})

Then the four blocks of CC are formed by additions/subtractions only:

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

Verify one of them (this is the kind of check a good exam answer includes). Expand C11C_{11}:

  • M1=A11B11+A11B22+A22B11+A22B22M_1 = A_{11}B_{11}+A_{11}B_{22}+A_{22}B_{11}+A_{22}B_{22}
  • M4=A22B21−A22B11M_4 = A_{22}B_{21}-A_{22}B_{11}
  • −M5=−A11B22−A12B22-M_5 = -A_{11}B_{22}-A_{12}B_{22}
  • M7=A12B21+A12B22−A22B21−A22B22M_7 = A_{12}B_{21}+A_{12}B_{22}-A_{22}B_{21}-A_{22}B_{22}

Sum: the A11B22A_{11}B_{22} terms cancel (+A11B22−A11B22+A_{11}B_{22}-A_{11}B_{22}), the A22B11A_{22}B_{11} terms cancel (+A22B11−A22B11+A_{22}B_{11}-A_{22}B_{11}), the A22B22A_{22}B_{22} terms cancel (+A22B22−A22B22+A_{22}B_{22}-A_{22}B_{22}), the A12B22A_{12}B_{22} terms cancel (−A12B22+A12B22-A_{12}B_{22}+A_{12}B_{22}), and the A22B21A_{22}B_{21} terms cancel (+A22B21−A22B21+A_{22}B_{21}-A_{22}B_{21}). What remains is exactly A11B11+A12B21=C11A_{11}B_{11}+A_{12}B_{21}=C_{11}. ✓

1.3 Recurrence and complexity

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

Master Theorem: a=7a=7, b=2b=2, f(n)=Θ(n2)f(n)=\Theta(n^2). Compute log⁡ba=log⁡27≈2.807\log_b a=\log_2 7\approx 2.807. Since f(n)=O(n2.807−ε)f(n)=O(n^{2.807-\varepsilon}), this is Case 1:

T(n)=Θ(nlog⁡27)=Θ(n2.807…)≈Θ(n2.81)T(n)=\Theta(n^{\log_2 7})=\Theta(n^{2.807\ldots})\approx\Theta(n^{2.81})

Why the Θ(n2)\Theta(n^2) term? The 7 products M1,…,M7M_1,\dots,M_7 are computed recursively; the additions/subtractions of (n/2)×(n/2)(n/2)\times(n/2) blocks cost Θ(n2)\Theta(n^2) at each level (a constant number of block additions, each Θ(n2)\Theta(n^2)). So the non-recursive work is Θ(n2)\Theta(n^2).

1.4 Counting scalar multiplications

The whole point of Strassen is that the number of scalar multiplications drops. If M(n)M(n) = number of scalar multiplications to multiply two n×nn\times n matrices:

M(n)=7 M(n/2),M(1)=1M(n)=7\,M(n/2),\qquad M(1)=1

Solving: M(n)=7log⁡2n=nlog⁡27M(n)=7^{\log_2 n}=n^{\log_2 7}.

Matrix size nnStrassen multiplications 7log⁡2n7^{\log_2 n}Schoolbook n3n^3
2×22\times 271=77^1=788
4×44\times 472=497^2=496464
8×88\times 873=3437^3=343512512
16×1616\times 1674=24017^4=240140964096

So for 2×22\times2: 7 multiplications; 4×44\times4: 49; 8×88\times8: 343. (These are the numbers GATE asks for.)

Additions count. At each level Strassen forms the 10 sums/differences that feed M1,…,M7M_1,\dots,M_7 and then assembles CC with 3+1+1+3=83+1+1+3=8 more: 18 block additions, each of (n/2)2=n2/4(n/2)^2=n^2/4 scalar additions, i.e. 18⋅n2/4=4.5 n218\cdot n^2/4=4.5\,n^2 per level (the 8-product divide-and-conquer needs only 4 block additions per level). The addition count therefore satisfies A(n)=7A(n/2)+4.5 n2A(n)=7A(n/2)+4.5\,n^2, A(1)=0A(1)=0, which solves to A(n)=6 (nlog⁡27−n2)=Θ(nlog⁡27)A(n)=6\,(n^{\log_2 7}-n^2)=\Theta(n^{\log_2 7}). Check n=2n=2: 6(7−4)=186(7-4)=18 additions (versus 44 for the schoolbook 2×22\times2 product). So for one 2×22\times2 step Strassen trades more additions (18 vs 4) for fewer multiplications (7 vs 8); asymptotically both counts are Θ(n2.81)\Theta(n^{2.81}), below the schoolbook Θ(n3)\Theta(n^3). The heavy additions and memory traffic are why it only wins for large nn in practice (the crossover is machine-dependent and not something to quote as a fixed exam fact).

1.5 Comparison table

MethodRecurrenceMultiplications for n×nn\times nTime complexity
Schoolbook (8-product D&C)T(n)=8T(n/2)+Θ(n2)T(n)=8T(n/2)+\Theta(n^2)n3n^3Θ(n3)\Theta(n^3)
Strassen (7-product D&C)T(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2)nlog⁡27n^{\log_2 7}Θ(nlog⁡27)≈Θ(n2.81)\Theta(n^{\log_2 7})\approx\Theta(n^{2.81})

Convention note. Some texts write the Strassen recurrence with the additive term as Θ(n2)\Theta(n^2) (block additions), others as O(n2)O(n^2); both give the same answer. Some write T(n)=7T(n/2)+cn2T(n)=7T(n/2)+cn^2. The exponent log⁡27\log_2 7 is convention-independent. GATE uses log⁡27≈2.81\log_2 7\approx 2.81.


Pages 1–2 of 2