Salta al contenuto
Note per Studenti Hybrid Algorithms Report

Hybrid Algorithms

Definition. Given two algorithms A1,A2A_1,A_2 for the same problem with TA1(n)=o(TA2(n))T_{A_1}(n)=o(T_{A_2}(n)) but larger multiplicative constants for A1A_1 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 (2n3−n22n^3-n^2) and Strassen's SMUL (7nlog⁡27−6n27n^{\log_27}-6n^2), 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 nn is below a threshold (e.g. 10241024) run MUL, else SMUL. The cost is the minimum of the two functions, but for n→∞n\to\infty it coincides with SMUL: the constant of nlog⁡27n^{\log_27} stays 77 and the iterative algorithm is executed at most once.

Changing the base case

Fix a threshold n0n_0 (power of two) and use the iterative algorithm at every leaf of Strassen's tree, which is cut at size n0n_0:

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 T(n,n0)={2n3−n2n≤n07 T(n2,n0)+92n2n>n0.T(n,n_0)=\begin{cases}2n^3-n^2&n\le n_0\\7\,T(\frac n2,n_0)+\frac92n^2&n>n_0.\end{cases}

Parametric analysis

Let n=2kn=2^k, n0=2k0n_0=2^{k_0}, m=k−k0=log⁡2nn0m=k-k_0=\log_2\frac n{n_0} internal levels: f∗=m−1f^\ast=m-1 and 7m7^m leaves of cost 2n03−n022n_0^3-n_0^2 (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 →): T(n,n0)=∑ℓ=0m−17ℓ⋅92(n2ℓ)2+7m(2n03−n02)=92n2⋅43((74)m−1)+7m(2n03−n02).T(n,n_0)=\sum_{\ell=0}^{m-1}7^\ell\cdot\frac92\Bigl(\frac n{2^\ell}\Bigr)^2+7^m(2n_0^3-n_0^2)=\frac92n^2\cdot\frac43\Bigl(\bigl(\tfrac74\bigr)^m-1\Bigr)+7^m(2n_0^3-n_0^2). Since 4m=(n/n0)24^m=(n/n_0)^2, (74)m=7mn02n2(\frac74)^m=\frac{7^mn_0^2}{n^2}, so T(n,n0)=6⋅7mn02−6n2+7m(2n03−n02)=7m(2n03+5n02)−6n2.T(n,n_0)=6\cdot7^mn_0^2-6n^2+7^m(2n_0^3-n_0^2)=7^m(2n_0^3+5n_0^2)-6n^2. With 7m=nlog⁡27/n0log⁡277^m=n^{\log_27}/n_0^{\log_27}: T(n,n0)=nlog⁡27⋅2n0+5n0log⁡27−2−6n2.T(n,n_0)=n^{\log_27}\cdot\frac{2n_0+5}{n_0^{\log_27-2}}-6n^2. Checks: n0=1n_0=1 gives 7nlog⁡27−6n27n^{\log_27}-6n^2 (pure Strassen); n0=nn_0=n gives 2n3−n22n^3-n^2 (iterative). Example n=16n=16, n0=8n_0=8: 7⋅960+92⋅256=78727\cdot960+\frac92\cdot256=7872, against 79367936 (iterative) and 1527115271 (pure Strassen).

Choice of n0n_0

Minimize the leading constant g(n0)=2n0+5n0log⁡27−2g(n_0)=\frac{2n_0+5}{n_0^{\log_27-2}} over powers of two. With s=log⁡27−2≈0.807s=\log_27-2\approx0.807, g′(n0)=n0−s−1(2n0−s(2n0+5))g'(n_0)=n_0^{-s-1}\bigl(2n_0-s(2n_0+5)\bigr), which is positive iff n0>5s2(1−s)=5(log⁡27−2)2(3−log⁡27)≈10.48.n_0>\frac{5s}{2(1-s)}=\frac{5(\log_27-2)}{2(3-\log_27)}\approx10.48. So gg decreases until about 10.4810.48 and then grows; among powers of two the minimum is at 88 or 1616:

n0n_0 1 2 4 8 16 32 64
g(n0)g(n_0) 7.000 5.143 4.245 3.918 3.945 4.204 4.631

Take n0=8n_0=8: the constant drops from 77 to 3.923.92, almost halved, and the hybrid beats the iterative algorithm already from n=16n=16 (not 655655). For n=1024n=1024 the cost relative to the iterative one is 0.9180.918 (n0=1n_0=1), 0.6740.674 (2), 0.5560.556 (4), 0.5130.513 (8), 0.5160.516 (16), 0.5500.550 (32). As always the model ignores recursion, memory accesses and temporaries; real implementations have a higher optimal threshold found experimentally.

Versione ripasso

  • Definition: A1A_1 asymptotically better but with larger constants than A2A_2: combine them to keep the asymptotics of A1A_1 with lower constants.
  • STUPID_HYBRID: choose by starting size only; for large nn it is SMUL, constant 77 unchanged. H_MUL: MUL for n≤n0n\le n_0 at every leaf: T(n,n0)={2n3−n2n≤n07T(n2,n0)+92n2n>n0.T(n,n_0)=\begin{cases}2n^3-n^2&n\le n_0\\7T(\frac n2,n_0)+\frac92n^2&n>n_0.\end{cases} Correctness: composition of two correct algorithms.
  • Parametric formula: m=log⁡2nn0m=\log_2\frac n{n_0} levels, (74)m=7mn02n2(\frac74)^m=\frac{7^mn_0^2}{n^2}, 7m=nlog⁡27n0log⁡277^m=\frac{n^{\log_27}}{n_0^{\log_27}}: T=92n2⋅43((74)m−1)+7m(2n03−n02)=7m(2n03+5n02)−6n2=nlog⁡272n0+5n0log⁡27−2−6n2.T=\frac92n^2\cdot\frac43\bigl((\tfrac74)^m-1\bigr)+7^m(2n_0^3-n_0^2)=7^m(2n_0^3+5n_0^2)-6n^2=n^{\log_27}\frac{2n_0+5}{n_0^{\log_27-2}}-6n^2. Checks: n0=1n_0=1 pure Strassen, n0=nn_0=n iterative. n=16n=16, n0=8n_0=8: 7⋅960+1152=7872<79367\cdot960+1152=7872<7936.
  • Choice of n0n_0: g(n0)=2n0+5n0log⁡27−2g(n_0)=\frac{2n_0+5}{n_0^{\log_27-2}}; g′≥0g'\ge0 iff n0≥5(log⁡27−2)2(3−log⁡27)≈10.48n_0\ge\frac{5(\log_27-2)}{2(3-\log_27)}\approx10.48. Powers of two: g(8)≈3.918<g(16)≈3.945g(8)\approx3.918<g(16)\approx3.945, so n0=8n_0=8 (constant from 77 to 3.923.92; for n=1024n=1024 cost 0.5130.513 times the iterative one).
  • Common mistakes: thinking the naive hybrid improves the constant; taking n0=10n_0=10 (must be a power of two: 88 or 1616); forgetting that the leaf cost T0T_0 depends on n0n_0.

Teoria collegata