Salta al contenuto
Note per Studenti Recurrence Relations - Recursion Tree and General Formula Report

Recurrence Relations - Recursion Tree and General Formula

In questa pagina 5

Unfolding (Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →) works case by case. Here it is organized into a general formula obtained by reading the recurrence tree level by level.

Call tree and recurrence tree

For worst-case analysis the recurrence tree has the same shape as the call tree of the worst instance of size nn. In each node write: for an internal node the size of the subinstance and, beside it, the total cost of ADA_D and ACA_C on that instance; for a leaf the base size and the cost of direct resolution. This cost is the node's work. T(n)T(n) is the sum of the works of all nodes.

The family of recurrences

Let s,f:N→Ns,f:\mathbb N\to\mathbb N and w:N→R+∪{0}w:\mathbb N\to\mathbb R^+\cup\{0\} be non-decreasing:

  • s(n)s(n): number of subinstances generated by a non-base instance of size nn;
  • f(n)f(n): size of those subinstances; must be a contraction, f(n)<nf(n)<n for non-base sizes;
  • w(n)w(n): work (divide plus conquer) of a non-base instance of size nn.

Base instances have size ≤n0\le n_0 and T0T_0 bounds the direct-resolution cost: T(n)={T0n≤n0s(n)⋅T(f(n))+w(n)n>n0.(∗)T(n)=\begin{cases}T_0&n\le n_0\\ s(n)\cdot T\bigl(f(n)\bigr)+w(n)&n>n_0.\end{cases}\tag{$\ast$} If s,f,ws,f,w are only upper bounds, the solution is an upper bound to the complexity.

Example (merge sort, n=2kn=2^k, only key comparisons cost): n0=1n_0=1, T0=0T_0=0, s=2s=2, f=n/2f=n/2, w(n)=n−1w(n)=n-1 (Divide splits without comparing; the ordered merge costs at most n−1n-1). So T(1)=0T(1)=0, T(n)=2T(n/2)+n−1T(n)=2T(n/2)+n-1.

Iterates of ff

Sizes at the various levels: f(0)(n)=nf^{(0)}(n)=n, f(ℓ)(n)=f(f(ℓ−1)(n))f^{(\ell)}(n)=f(f^{(\ell-1)}(n)).

f(n)f(n) form of nn f(ℓ)(n)f^{(\ell)}(n)
n/bn/b n=bkn=b^k, b>1b>1 n/bℓn/b^\ell
n\sqrt n n=22kn=2^{2^k} n1/2ℓn^{1/2^\ell}
n1/bn^{1/b} n=abkn=a^{b^k} n1/bℓn^{1/b^\ell}
n−1n-1 n>0n>0 n−ℓn-\ell

Define f∗(n,n0)=max⁡{k:f(k)(n)>n0}f^\ast(n,n_0)=\max\{k:f^{(k)}(n)>n_0\}: the last level of non-base instances; leaves are at level f∗+1f^\ast+1. For n≤n0n\le n_0 set f∗=−1f^\ast=-1 (the only leaf is the root, at level 00). Notable cases:

  • f=n/bf=n/b, n/n0n/n_0 a power of bb: f∗(n,n0)=log⁡bnn0−1f^\ast(n,n_0)=\log_b\frac n{n_0}-1, so f∗(n,1)=log⁡bn−1f^\ast(n,1)=\log_bn-1.
  • f=nf=\sqrt n, n=22kn=2^{2^k}: f∗(n,2)=log⁡2log⁡2n−1f^\ast(n,2)=\log_2\log_2n-1 (from log⁡2n2k>1  ⟺  k<log⁡2log⁡2n\frac{\log_2n}{2^k}>1\iff k<\log_2\log_2n).
  • f=n−1f=n-1: f∗(n,1)=n−2f^\ast(n,1)=n-2 (from n−k>1n-k>1).

Analysis of the tree

At level ℓ\ell (0≤ℓ≤f∗0\le\ell\le f^\ast) every node has work w(f(ℓ)(n))w(f^{(\ell)}(n)); every leaf (level f∗+1f^\ast+1) has work T0T_0. A node at level ℓ\ell has s(f(ℓ)(n))s(f^{(\ell)}(n)) children, so the number of nodes at level ℓ\ell is ∏j=0ℓ−1s(f(j)(n))\prod_{j=0}^{\ell-1}s(f^{(j)}(n)) (empty sum =0=0, empty product =1=1).

LEVEL          SIZE         NODES                            WORK/NODE
0              f^(0)(n)     1                                w(f^(0)(n))
l (0<=l<=f*)   f^(l)(n)     prod_{j=0}^{l-1} s(f^(j)(n))     w(f^(l)(n))
f*+1 (leaves)  <= n0        prod_{j=0}^{f*} s(f^(j)(n))      T0

General formula. T(n)=∑ℓ=0f∗(n,n0)[∏j=0ℓ−1s(f(j)(n))]w(f(ℓ)(n))+[∏j=0f∗(n,n0)s(f(j)(n))]T0.T(n)=\sum_{\ell=0}^{f^\ast(n,n_0)}\left[\prod_{j=0}^{\ell-1}s\bigl(f^{(j)}(n)\bigr)\right]w\bigl(f^{(\ell)}(n)\bigr)+\left[\prod_{j=0}^{f^\ast(n,n_0)}s\bigl(f^{(j)}(n)\bigr)\right]T_0.

Proof (induction on nn). [B] n≤n0n\le n_0: f∗=−1f^\ast=-1, empty sum and empty product, T(n)=T0T(n)=T_0. [HP] true for sizes <n<n. [TH] T(n)=s(n)T(f(n))+w(n)T(n)=s(n)T(f(n))+w(n); with f(j)(f(n))=f(j+1)(n)f^{(j)}(f(n))=f^{(j+1)}(n) and f∗(f(n),n0)=f∗(n,n0)−1f^\ast(f(n),n_0)=f^\ast(n,n_0)-1, and F=f∗(n,n0)F=f^\ast(n,n_0), multiplying the formula for T(f(n))T(f(n)) by s(n)s(n) (the factor j=0j=0 of every product) and shifting indices (ℓ′=ℓ+1\ell'=\ell+1, j′=j+1j'=j+1) gives the levels 1..F1..F and the leaf term; adding w(n)w(n), the level-00 term, gives the formula for T(n)T(n). □\square

Worked examples

Example 1: T(1)=4T(1)=4, T(n)=6T(n/3)+n(n−1)T(n)=6T(n/3)+n(n-1), n=3kn=3^k. s=6s=6, f=n/3f=n/3, w=n2−nw=n^2-n, f∗=log⁡3n−1f^\ast=\log_3n-1, level ℓ\ell has 6ℓ6^\ell nodes of work n29ℓ−n3ℓ\frac{n^2}{9^\ell}-\frac n{3^\ell}: T(n)=∑ℓ=0log⁡3n−16ℓ(n29ℓ−n3ℓ)⏟A+6log⁡3n⋅4⏟B.T(n)=\underbrace{\sum_{\ell=0}^{\log_3n-1}6^\ell\Bigl(\tfrac{n^2}{9^\ell}-\tfrac n{3^\ell}\Bigr)}_{A}+\underbrace{6^{\log_3n}\cdot4}_{B}. Using alog⁡cb=blog⁡caa^{\log_cb}=b^{\log_ca} and log⁡36=1+log⁡32\log_36=1+\log_32: B=4n1+log⁡32B=4n^{1+\log_32}. With 6ℓ/9ℓ=(2/3)ℓ6^\ell/9^\ell=(2/3)^\ell and 6ℓ/3ℓ=2ℓ6^\ell/3^\ell=2^\ell: A=n21−(2/3)log⁡3n1/3−n1−2log⁡3n−1=3n2(1−nlog⁡32−1)+n(1−nlog⁡32)=3n2−4n1+log⁡32+n.A=n^2\frac{1-(2/3)^{\log_3n}}{1/3}-n\frac{1-2^{\log_3n}}{-1}=3n^2\bigl(1-n^{\log_32-1}\bigr)+n\bigl(1-n^{\log_32}\bigr)=3n^2-4n^{1+\log_32}+n. So T(n)=A+B=3n2+nT(n)=A+B=3n^2+n. Check: T(n)=6(3n29+n3)+n2−n=3n2+nT(n)=6\bigl(3\frac{n^2}9+\frac n3\bigr)+n^2-n=3n^2+n.

Example 2: T(2)=1T(2)=1, T(n)=2T(n)+1T(n)=2T(\sqrt n)+1, n=22kn=2^{2^k}. s=2s=2, w=1w=1, f∗=log⁡2log⁡2n−1f^\ast=\log_2\log_2n-1, 2ℓ2^\ell nodes at level ℓ\ell: T(n)=∑ℓ=0log⁡2log⁡2n−12ℓ+2log⁡2log⁡2n=(log⁡2n−1)+log⁡2n=2log⁡2n−1.T(n)=\sum_{\ell=0}^{\log_2\log_2n-1}2^\ell+2^{\log_2\log_2n}=(\log_2n-1)+\log_2n=2\log_2n-1. Check: T(4)=3T(4)=3, T(16)=7T(16)=7, T(256)=15T(256)=15.

Example 3 (merge sort, exact): level ℓ\ell has 2ℓ2^\ell nodes of work n2ℓ−1\frac n{2^\ell}-1, i.e. n−2ℓn-2^\ell per level: T(n)=∑ℓ=0log⁡2n−1(n−2ℓ)=nlog⁡2n−n+1(n=8: 17).T(n)=\sum_{\ell=0}^{\log_2n-1}(n-2^\ell)=n\log_2n-n+1\qquad(n=8:\ 17).

Example 4 (contraction n−1n-1): T(1)=1T(1)=1, T(n)=2T(n−1)+1T(n)=2T(n-1)+1 (Towers of Hanoi): f∗=n−2f^\ast=n-2, 2ℓ2^\ell nodes per level: T(n)=∑ℓ=0n−22ℓ+2n−1=2n−1.T(n)=\sum_{\ell=0}^{n-2}2^\ell+2^{n-1}=2^n-1. The tree is very deep (n−1n-1 levels) with 2n−12^{n-1} leaves: exponential. With s=1s=1, e.g. T(n)=T(n−1)+nT(n)=T(n-1)+n, T(1)=1T(1)=1: ∑ℓ=0n−2(n−ℓ)+1=n(n+1)2\sum_{\ell=0}^{n-2}(n-\ell)+1=\frac{n(n+1)}2.

Leaves of size n0>1n_0>1 (hybrid algorithms). If recursion stops at n0>1n_0>1 with f=n/bf=n/b and n/n0n/n_0 a power of bb: f∗=log⁡bnn0−1f^\ast=\log_b\frac n{n_0}-1 and there are alog⁡b(n/n0)=(n/n0)log⁡baa^{\log_b(n/n_0)}=(n/n_0)^{\log_ba} leaves of work T0T_0, which may depend on n0n_0 (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 →). Example a=b=2a=b=2, w=nw=n, n0=4n_0=4, T0=5T_0=5: T(n)=∑ℓ=0log⁡2n−32ℓn2ℓ+n4⋅5=n(log⁡2n−2)+5n4(T(8)=18).T(n)=\sum_{\ell=0}^{\log_2n-3}2^\ell\frac n{2^\ell}+\frac n4\cdot5=n(\log_2n-2)+\frac{5n}4\qquad(T(8)=18).

Typical pitfalls. Not rewriting terms in standard form (powers of nn with logarithmic exponents) so cancellations are missed; miscounting levels (non-base levels are 0..f∗0..f^\ast, leaves at f∗+1f^\ast+1 cost T0T_0, not ww); using s(n)s(n) instead of s(f(j)(n))s(f^{(j)}(n)) in the product; forgetting the leaf term [∏s]T0\bigl[\prod s\bigr]T_0; applying the formula to nn not of the required form (bkb^k, abka^{b^k}).

The Master Theorem (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 →) is the order-of-growth shortcut for s=as=a, f=n/bf=n/b.

Versione ripasso

  • Recurrence tree: shape of the call tree of the worst instance; internal node = size + work (Divide+Conquer), leaf = direct cost. T(n)T(n) = sum of all works.
  • Family: T(n)=T0T(n)=T_0 for n≤n0n\le n_0, T(n)=s(n) T(f(n))+w(n)T(n)=s(n)\,T(f(n))+w(n) for n>n0n>n_0, with s,f,ws,f,w non-decreasing, f(n)<nf(n)<n. If s,f,ws,f,w are upper bounds then TT is an upper bound. Merge sort: s=2,f=n/2,w=n−1,T0=0s=2,f=n/2,w=n-1,T_0=0.
  • Iterates: f(0)=nf^{(0)}=n, f(ℓ)=f(f(ℓ−1))f^{(\ell)}=f(f^{(\ell-1)}): n/b→n/bℓn/b\to n/b^\ell; n→n1/2ℓ\sqrt n\to n^{1/2^\ell}; n−1→n−ℓn-1\to n-\ell. f∗(n,n0)=max⁡{k:f(k)(n)>n0}f^\ast(n,n_0)=\max\{k:f^{(k)}(n)>n_0\} (last non-base level, −1-1 if n≤n0n\le n_0): log⁡bnn0−1\log_b\frac n{n_0}-1 for n/bn/b; log⁡2log⁡2n−1\log_2\log_2n-1 for n\sqrt n, n0=2n_0=2; n−n0−1n-n_0-1 for n−1n-1.
  • Levels: level ℓ\ell has size f(ℓ)(n)f^{(\ell)}(n), ∏j<ℓs(f(j)(n))\prod_{j<\ell}s(f^{(j)}(n)) nodes, work w(f(ℓ)(n))w(f^{(\ell)}(n)) each; leaves at level f∗+1f^\ast+1 with work T0T_0. Empty sum 00, empty product 11.
  • General formula: T(n)=∑ℓ=0f∗[∏j=0ℓ−1s(f(j)(n))]w(f(ℓ)(n))+[∏j=0f∗s(f(j)(n))]T0.T(n)=\sum_{\ell=0}^{f^\ast}\Bigl[\prod_{j=0}^{\ell-1}s(f^{(j)}(n))\Bigr]w(f^{(\ell)}(n))+\Bigl[\prod_{j=0}^{f^\ast}s(f^{(j)}(n))\Bigr]T_0. Proof by induction on nn, using f∗(f(n),n0)=f∗(n,n0)−1f^\ast(f(n),n_0)=f^\ast(n,n_0)-1 and shifting indices.
  • Examples: 6T(n/3)+n(n−1)6T(n/3)+n(n-1), T(1)=4T(1)=4: 3n2+n3n^2+n (part A=3n2−4n1+log⁡32+nA=3n^2-4n^{1+\log_32}+n, part B=4n1+log⁡32B=4n^{1+\log_32}, using alog⁡cb=blog⁡caa^{\log_cb}=b^{\log_ca}); 2T(n)+12T(\sqrt n)+1, T(2)=1T(2)=1: 2log⁡2n−12\log_2n-1; merge sort: level cost n−2ℓn-2^\ell, T=nlog⁡2n−n+1T=n\log_2n-n+1; 2T(n−1)+12T(n-1)+1, T(1)=1T(1)=1: 2n−12^n-1; T(n−1)+nT(n-1)+n: n(n+1)2\frac{n(n+1)}2.
  • Hybrid (n0>1n_0>1): f∗=log⁡bnn0−1f^\ast=\log_b\frac n{n_0}-1, leaves (n/n0)log⁡ba(n/n_0)^{\log_ba} of cost T0T_0 (possibly depending on n0n_0): 2T(n/2)+n2T(n/2)+n, n0=4n_0=4, T0=5T_0=5 gives n(log⁡2n−2)+5n4n(\log_2n-2)+\frac{5n}4.
  • Calculating f∗f^\ast: f=n/bf=n/b: bk<n/n0⇒k<log⁡bnn0b^k<n/n_0\Rightarrow k<\log_b\frac n{n_0}; f=nf=\sqrt n: log⁡2n2k>log⁡2n0⇒k<log⁡2log⁡2n−log⁡2log⁡2n0\frac{\log_2n}{2^k}>\log_2n_0\Rightarrow k<\log_2\log_2n-\log_2\log_2n_0; f=n−1f=n-1: n−k>n0⇒k<n−n0n-k>n_0\Rightarrow k<n-n_0.
  • Common mistakes: not using standard form before summing; wrong level count (leaves at f∗+1f^\ast+1 cost T0T_0); s(n)s(n) in place of s(f(j)(n))s(f^{(j)}(n)); forgetting the leaf term.

Teoria collegata