Salta al contenuto
Note per Studenti Strassen's Algorithm Report

Strassen's Algorithm

In questa pagina 3

The recursive block product (Matrix Multiplication - Iterative and Recursive Block ProductCost model on n x n matrices (scalar operations, size n or N = n^2); sum in n^2 operations (optimal); iterative row-by-column product with exact cost 2n^3 - n^2; the fractal property of the block product proved on indices; recursive algorithm RMUL with T(n) = 8T(n/2) + n^2, Theta(n^3) and exact solution 2n^3 - n^2: it performs the same operations as the iterative one.Matrix Multiplication - Iterative and Recursive Block Product →) has T(n)=8T(n2)+n2T(n)=8T(\frac n2)+n^2: threshold n3n^3 far above the work n2n^2, Case 1 of the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →, leaves dominate. Arity 88 is too high. Strassen shows that 7 calls on n2×n2\frac n2\times\frac n2 matrices suffice, at the price of 1818 matrix additions/subtractions of size n2×n2\frac n2\times\frac n2. Assume n=2kn=2^k and the scalar-operation cost model.

The formulas

Divide (14 factors, 1010 additions/subtractions, since A5,A6,B4,B7A_5,A_6,B_4,B_7 are single quadrants):

ii AiA_i BiB_i
1 A12−A22A_{12}-A_{22} B21+B22B_{21}+B_{22}
2 A11+A22A_{11}+A_{22} B11+B22B_{11}+B_{22}
3 A11−A21A_{11}-A_{21} B11+B12B_{11}+B_{12}
4 A11+A12A_{11}+A_{12} B22B_{22}
5 A11A_{11} B12−B22B_{12}-B_{22}
6 A22A_{22} B21−B11B_{21}-B_{11}
7 A21+A22A_{21}+A_{22} B11B_{11}

Recurse: Pi=AiBiP_i=A_iB_i, 1≤i≤71\le i\le7 (seven recursive calls).

Conquer (88 additions/subtractions): C11=P1+P2−P4+P6,C12=P4+P5,C21=P6+P7,C22=P2−P3+P5−P7.C_{11}=P_1+P_2-P_4+P_6,\quad C_{12}=P_4+P_5,\quad C_{21}=P_6+P_7,\quad C_{22}=P_2-P_3+P_5-P_7.

Correctness is checked by expanding the products, keeping the order of factors (matrices do not commute).

P4+P5=(A11+A12)B22+A11(B12−B22)=A11B12+A12B22=C12,P_4+P_5=(A_{11}+A_{12})B_{22}+A_{11}(B_{12}-B_{22})=A_{11}B_{12}+A_{12}B_{22}=C_{12}, P6+P7=A22(B21−B11)+(A21+A22)B11=A21B11+A22B21=C21.P_6+P_7=A_{22}(B_{21}-B_{11})+(A_{21}+A_{22})B_{11}=A_{21}B_{11}+A_{22}B_{21}=C_{21}. For C11C_{11}: P1=A12B21+A12B22−A22B21−A22B22,P2=A11B11+A11B22+A22B11+A22B22,−P4=−A11B22−A12B22,P6=A22B21−A22B11.\begin{aligned}P_1&=A_{12}B_{21}+A_{12}B_{22}-A_{22}B_{21}-A_{22}B_{22},\\P_2&=A_{11}B_{11}+A_{11}B_{22}+A_{22}B_{11}+A_{22}B_{22},\\-P_4&=-A_{11}B_{22}-A_{12}B_{22},\\P_6&=A_{22}B_{21}-A_{22}B_{11}.\end{aligned} Pairs cancel (A12B22A_{12}B_{22}, A22B21A_{22}B_{21}, A22B22A_{22}B_{22}, A11B22A_{11}B_{22}, A22B11A_{22}B_{11}), leaving A11B11+A12B21A_{11}B_{11}+A_{12}B_{21}. For C22=P2−P3+P5−P7C_{22}=P_2-P_3+P_5-P_7: −P3=−A11B11−A11B12+A21B11+A21B12,P5=A11B12−A11B22,−P7=−A21B11−A22B11,\begin{aligned}-P_3&=-A_{11}B_{11}-A_{11}B_{12}+A_{21}B_{11}+A_{21}B_{12},\\P_5&=A_{11}B_{12}-A_{11}B_{22},\\-P_7&=-A_{21}B_{11}-A_{22}B_{11},\end{aligned} and with P2P_2 everything cancels except A21B12+A22B22A_{21}B_{12}+A_{22}B_{22}.

Numeric example (n=2n=2, scalar quadrants), A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix}, B=(5678)B=\begin{pmatrix}5&6\\7&8\end{pmatrix}: P1=(2−4)(7+8)=−30P_1=(2-4)(7+8)=-30, P2=(1+4)(5+8)=65P_2=(1+4)(5+8)=65, P3=(1−3)(5+6)=−22P_3=(1-3)(5+6)=-22, P4=3⋅8=24P_4=3\cdot8=24, P5=1⋅(6−8)=−2P_5=1\cdot(6-8)=-2, P6=4⋅(7−5)=8P_6=4\cdot(7-5)=8, P7=7⋅5=35P_7=7\cdot5=35. Then C11=−30+65−24+8=19C_{11}=-30+65-24+8=19, C12=22C_{12}=22, C21=43C_{21}=43, C22=65+22−2−35=50C_{22}=65+22-2-35=50: the same (19224350)\begin{pmatrix}19&22\\43&50\end{pmatrix}. Strassen uses 7+18=257+18=25 operations versus 1212: on small cases it does not pay.

Algorithm SMUL

SMUL(A, B)
  n <- rows(A)
  if n = 1 then return [a11 * b11]                   { BASE }
  * quadrants Aij, Bij *
  { DIVIDE: 10 additions/subtractions of n/2 x n/2 matrices }
  A1 <- A12 - A22;  B1 <- B21 + B22
  A2 <- A11 + A22;  B2 <- B11 + B22
  A3 <- A11 - A21;  B3 <- B11 + B12
  A4 <- A11 + A12;  B4 <- B22
  A5 <- A11;        B5 <- B12 - B22
  A6 <- A22;        B6 <- B21 - B11
  A7 <- A21 + A22;  B7 <- B11
  { RECURSE }
  for i <- 1 to 7 do Pi <- SMUL(Ai, Bi)
  { CONQUER: 8 additions/subtractions }
  C11 <- P1 + P2 - P4 + P6;   C12 <- P4 + P5
  C21 <- P6 + P7;             C22 <- P2 - P3 + P5 - P7
  return C

Correctness follows from the four identities, by induction on the size.

Analysis

For n>1n>1: Divide 10⋅n24=52n210\cdot\frac{n^2}4=\frac52n^2, Conquer 8⋅n24=2n28\cdot\frac{n^2}4=2n^2, total work 92n2\frac92n^2: T(n)={1n=17 T(n2)+92n2n>1.T(n)=\begin{cases}1&n=1\\7\,T(\frac n2)+\frac92n^2&n>1.\end{cases}

Order of growth. Threshold nlog⁡27≈n2.81>n2n^{\log_27}\approx n^{2.81}>n^2: Case 1, T=Θ(nlog⁡27)T=\Theta(n^{\log_27}) against Θ(n3)\Theta(n^3).

Exact solution (s=7s=7, f=n2f=\frac n2, w=92n2w=\frac92n^2, T0=1T_0=1, f∗=k−1f^\ast=k-1; leaves 7k=nlog⁡277^k=n^{\log_27}): T(n)=∑ℓ=0k−17ℓ⋅92(n2ℓ)2+7k=92n2⋅43(7kn2−1)+7k=7nlog⁡27−6n2,T(n)=\sum_{\ell=0}^{k-1}7^\ell\cdot\frac92\Bigl(\frac n{2^\ell}\Bigr)^2+7^k=\frac92n^2\cdot\frac43\Bigl(\frac{7^k}{n^2}-1\Bigr)+7^k=7n^{\log_27}-6n^2, using ∑ℓ<k(74)ℓ=43((74)k−1)\sum_{\ell<k}(\frac74)^\ell=\frac43((\frac74)^k-1) and (74)k=7k/n2(\frac74)^k=7^k/n^2. Checks: n=1n=1: 11; n=2n=2: 2525; n=4n=4: 247247.

Constants. Dominant term 77 versus 22 of the iterative algorithm. Strassen is better when 7nlog⁡27−6n2<2n3−n27n^{\log_27}-6n^2<2n^3-n^2, i.e. numerically from n≈655n\approx655; for powers of two, from n=1024n=1024 (a matrix with more than a million entries).

nn Strassen iterative 2n3−n22n^3-n^2
8 2017 960
64 798967 520192
512 280902385 268173312
1024 1971035287 2146435072
2048 13816121377 17175674880

The model ignores recursion, memory access patterns and the temporary matrices for Ai,Bi,PiA_i,B_i,P_i, so in practice the crossover is much higher. Asymptotically better algorithms exist (exponent down to about 2.372.37) but with enormous constants and no usable implementations. Strassen, despite its larger constant, is used in practice for large matrices, typically in hybrid form (Hybrid AlgorithmsHybridization combines two algorithms for the same problem so the result has the asymptotics of the better one and, if possible, lower constants. Choosing the algorithm only by the starting size (STUPID_HYBRID) does not help; changing the base case of the recursion (H_MUL: iterative product for n <= n0 at every leaf of Strassen's tree) gives cost n^(log2 7)(2n0+5)/n0^(log2 7 - 2) - 6n^2, minimal for n0 = 8 with leading constant about 3.92.Hybrid Algorithms →).

Versione ripasso

  • Idea: arity 88 of the block product is too high (leaves dominate). Strassen: 7 calls and 1818 additions/subtractions of n2×n2\frac n2\times\frac n2 matrices (n=2kn=2^k).
  • Divide (1010 operations; A5,A6,B4,B7A_5,A_6,B_4,B_7 are single quadrants):
    • A1=A12−A22A_1=A_{12}-A_{22}, B1=B21+B22B_1=B_{21}+B_{22};
    • A2=A11+A22A_2=A_{11}+A_{22}, B2=B11+B22B_2=B_{11}+B_{22};
    • A3=A11−A21A_3=A_{11}-A_{21}, B3=B11+B12B_3=B_{11}+B_{12};
    • A4=A11+A12A_4=A_{11}+A_{12}, B4=B22B_4=B_{22};
    • A5=A11A_5=A_{11}, B5=B12−B22B_5=B_{12}-B_{22};
    • A6=A22A_6=A_{22}, B6=B21−B11B_6=B_{21}-B_{11};
    • A7=A21+A22A_7=A_{21}+A_{22}, B7=B11B_7=B_{11}.
  • Recurse: Pi=AiBiP_i=A_iB_i. Conquer (88 operations): C11=P1+P2−P4+P6C_{11}=P_1+P_2-P_4+P_6, C12=P4+P5C_{12}=P_4+P_5, C21=P6+P7C_{21}=P_6+P_7, C22=P2−P3+P5−P7C_{22}=P_2-P_3+P_5-P_7.
  • Check: expand the products keeping the order of factors, cancel in pairs. E.g. P4+P5=A11B22+A12B22+A11B12−A11B22=A11B12+A12B22P_4+P_5=A_{11}B_{22}+A_{12}B_{22}+A_{11}B_{12}-A_{11}B_{22}=A_{11}B_{12}+A_{12}B_{22}. Example A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix}, B=(5678)B=\begin{pmatrix}5&6\\7&8\end{pmatrix}: P=(−30,65,−22,24,−2,8,35)P=(-30,65,-22,24,-2,8,35), C=(19224350)C=\begin{pmatrix}19&22\\43&50\end{pmatrix}, 2525 operations (iterative: 1212).
  • Recurrence: Divide 10n24=52n210\frac{n^2}4=\frac52n^2, Conquer 8n24=2n28\frac{n^2}4=2n^2: T(n)={1n=17T(n2)+92n2n>1.T(n)=\begin{cases}1&n=1\\7T(\frac n2)+\frac92n^2&n>1.\end{cases} Threshold nlog⁡27≈n2.81>n2n^{\log_27}\approx n^{2.81}>n^2, Case 1: T=Θ(nlog⁡27)T=\Theta(n^{\log_27}) (Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →).
  • Exact: T=92n2∑ℓ<k(74)ℓ+7k=92n2⋅43(7kn2−1)+7k=7nlog⁡27−6n2T=\frac92n^2\sum_{\ell<k}(\frac74)^\ell+7^k=\frac92n^2\cdot\frac43(\frac{7^k}{n^2}-1)+7^k=7n^{\log_27}-6n^2. Checks: n=2n=2: 2525; n=4n=4: 247247.
  • Constant 77 versus 22: 7nlog⁡27−6n2<2n3−n27n^{\log_27}-6n^2<2n^3-n^2 only from n≈655n\approx655, for powers of two from n=1024n=1024 (1971035287<21464350721971035287<2146435072; at 512512 it loses). The model ignores recursion, cache and temporaries, so in practice the threshold is higher. Better asymptotic algorithms (exponent ≈2.37\approx2.37) are not implementable.
  • Common mistakes: saying 1818 operations in Divide (1010 Divide, 88 Conquer); swapping the order of factors when checking; confusing the real crossover 655655 with the one for powers of two (10241024).

Teoria collegata