Hybrid Algorithms
Definition. Given two algorithms for the same problem with but larger multiplicative constants for in the exact expression, hybridization combines them into an algorithm with the asymptotic behaviour of the better one and, if possible, lower constants.
Running example: the iterative product MUL () and Strassen's SMUL (), see 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 → and Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →.
Naive hybrid
STUPID_HYBRID: if is below a threshold (e.g. ) run MUL, else SMUL. The cost is the minimum of the two functions, but for it coincides with SMUL: the constant of stays and the iterative algorithm is executed at most once.
Changing the base case
Fix a threshold (power of two) and use the iterative algorithm at every leaf of Strassen's tree, which is cut at size :
H_MUL(A, B)
n <- rows(A)
if n <= n0 then return MUL(A, B)
{ DIVIDE, RECURSE, CONQUER as in SMUL, with recursive calls to H_MUL }Correctness is the composition of two correct algorithms (recursive frame plus correct base solver). The recurrence is
Parametric analysis
Let , , internal levels: and leaves of cost (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →): Since , , so With : Checks: gives (pure Strassen); gives (iterative). Example , : , against (iterative) and (pure Strassen).
Choice of
Minimize the leading constant over powers of two. With , , which is positive iff So decreases until about and then grows; among powers of two the minimum is at or :
| 1 | 2 | 4 | 8 | 16 | 32 | 64 | |
|---|---|---|---|---|---|---|---|
| 7.000 | 5.143 | 4.245 | 3.918 | 3.945 | 4.204 | 4.631 |
Take : the constant drops from to , almost halved, and the hybrid beats the iterative algorithm already from (not ). For the cost relative to the iterative one is (), (2), (4), (8), (16), (32). As always the model ignores recursion, memory accesses and temporaries; real implementations have a higher optimal threshold found experimentally.
Versione ripasso
- Definition: asymptotically better but with larger constants than : combine them to keep the asymptotics of with lower constants.
STUPID_HYBRID: choose by starting size only; for large it isSMUL, constant unchanged.H_MUL:MULfor at every leaf: Correctness: composition of two correct algorithms.- Parametric formula: levels, , : Checks: pure Strassen, iterative. , : .
- Choice of : ; iff . Powers of two: , so (constant from to ; for cost times the iterative one).
- Common mistakes: thinking the naive hybrid improves the constant; taking (must be a power of two: or ); forgetting that the leaf cost depends on .