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 . In each node write: for an internal node the size of the subinstance and, beside it, the total cost of and on that instance; for a leaf the base size and the cost of direct resolution. This cost is the node's work. is the sum of the works of all nodes.
The family of recurrences
Let and be non-decreasing:
- : number of subinstances generated by a non-base instance of size ;
- : size of those subinstances; must be a contraction, for non-base sizes;
- : work (divide plus conquer) of a non-base instance of size .
Base instances have size and bounds the direct-resolution cost: If are only upper bounds, the solution is an upper bound to the complexity.
Example (merge sort, , only key comparisons cost): , , , , (Divide splits without comparing; the ordered merge costs at most ). So , .
Iterates of
Sizes at the various levels: , .
| form of | ||
|---|---|---|
| , | ||
Define : the last level of non-base instances; leaves are at level . For set (the only leaf is the root, at level ). Notable cases:
- , a power of : , so .
- , : (from ).
- : (from ).
Analysis of the tree
At level () every node has work ; every leaf (level ) has work . A node at level has children, so the number of nodes at level is (empty sum , empty product ).
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)) T0General formula.
Proof (induction on ). [B] : , empty sum and empty product, . [HP] true for sizes . [TH] ; with and , and , multiplying the formula for by (the factor of every product) and shifting indices (, ) gives the levels and the leaf term; adding , the level- term, gives the formula for .
Worked examples
Example 1: , , . , , , , level has nodes of work : Using and : . With and : So . Check: .
Example 2: , , . , , , nodes at level : Check: , , .
Example 3 (merge sort, exact): level has nodes of work , i.e. per level:
Example 4 (contraction ): , (Towers of Hanoi): , nodes per level: The tree is very deep ( levels) with leaves: exponential. With , e.g. , : .
Leaves of size (hybrid algorithms). If recursion stops at with and a power of : and there are leaves of work , which may depend on (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 , , , :
Typical pitfalls. Not rewriting terms in standard form (powers of with logarithmic exponents) so cancellations are missed; miscounting levels (non-base levels are , leaves at cost , not ); using instead of in the product; forgetting the leaf term ; applying the formula to not of the required form (, ).
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 , .
Versione ripasso
- Recurrence tree: shape of the call tree of the worst instance; internal node = size + work (Divide+Conquer), leaf = direct cost. = sum of all works.
- Family: for , for , with non-decreasing, . If are upper bounds then is an upper bound. Merge sort: .
- Iterates: , : ; ; . (last non-base level, if ): for ; for , ; for .
- Levels: level has size , nodes, work each; leaves at level with work . Empty sum , empty product .
- General formula: Proof by induction on , using and shifting indices.
- Examples: , : (part , part , using ); , : ; merge sort: level cost , ; , : ; : .
- Hybrid (): , leaves of cost (possibly depending on ): , , gives .
- Calculating : : ; : ; : .
- Common mistakes: not using standard form before summing; wrong level count (leaves at cost ); in place of ; forgetting the leaf term.