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 : threshold far above the work , 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 is too high. Strassen shows that 7 calls on matrices suffice, at the price of matrix additions/subtractions of size . Assume and the scalar-operation cost model.
The formulas
Divide (14 factors, additions/subtractions, since are single quadrants):
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 |
Recurse: , (seven recursive calls).
Conquer ( additions/subtractions):
Correctness is checked by expanding the products, keeping the order of factors (matrices do not commute).
For : Pairs cancel (, , , , ), leaving . For : and with everything cancels except .
Numeric example (, scalar quadrants), , : , , , , , , . Then , , , : the same . Strassen uses operations versus : 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 CCorrectness follows from the four identities, by induction on the size.
Analysis
For : Divide , Conquer , total work :
Order of growth. Threshold : Case 1, against .
Exact solution (, , , , ; leaves ): using and . Checks: : ; : ; : .
Constants. Dominant term versus of the iterative algorithm. Strassen is better when , i.e. numerically from ; for powers of two, from (a matrix with more than a million entries).
| Strassen | iterative | |
|---|---|---|
| 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 , so in practice the crossover is much higher. Asymptotically better algorithms exist (exponent down to about ) 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 of the block product is too high (leaves dominate). Strassen: 7 calls and additions/subtractions of matrices ().
- Divide ( operations; are single quadrants):
- , ;
- , ;
- , ;
- , ;
- , ;
- , ;
- , .
- Recurse: . Conquer ( operations): , , , .
- Check: expand the products keeping the order of factors, cancel in pairs. E.g. . Example , : , , operations (iterative: ).
- Recurrence: Divide , Conquer : Threshold , Case 1: (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: . Checks: : ; : .
- Constant versus : only from , for powers of two from (; at it loses). The model ignores recursion, cache and temporaries, so in practice the threshold is higher. Better asymptotic algorithms (exponent ) are not implementable.
- Common mistakes: saying operations in Divide ( Divide, Conquer); swapping the order of factors when checking; confusing the real crossover with the one for powers of two ().