Download presentation
Presentation is loading. Please wait.
1
Recursion-tree method
A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion-tree method can be unreliable. The recursion-tree method promotes intuition, however.
2
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
3
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: T(n)
4
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 T(n/4) T(n/2)
5
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 T(n/16) T(n/8) T(n/8) T(n/4)
6
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 … Q(1)
7
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 … Q(1)
8
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 … Q(1)
9
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 … … Q(1)
10
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 … … Q(1) Total = = Q(n2) geometric series
11
Appendix: geometric series
for x ¹ 1 for |x| < 1
12
The master method The master method applies to recurrences of the form
T(n) = a T(n/b) + f (n) , where a ³ 1, b > 1, and f(n) (nd), d 0
13
Idea of master theorem Recursion tree: f (n) f (n) a f (n/b)
… h = logbn a … f (n/b) f (n/b) f (n/b) a … f (n/b2) f (n/b2) f (n/b2) … #leaves = ah = alogbn = nlogba nlogbaT (1) T (1)
14
Three common cases Compare a with bd: If a > bd, logba > d
nlogba > nd We also know that f(n) (nd) f (n) grows polynomially slower than nlogba. Solution: T(n) = Q(nlogba) .
15
Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b)
h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. nlogbaT (1) T (1) Q(nlogba)
16
Three common cases Compare a with bd: If a = bd, logba = d Nlogba = nd
We also know that f(n) (nd) f (n) and nlogba grow at similar rates. Solution: T(n) = Q( f(n) log n) .
17
Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b)
h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 2: The weight is approximately the same on each of the logbn levels. nlogbaT (1) T (1) Q(nlogbalg n)
18
Three common cases (cont.)
Compare a with bd: If a < bd, logba < d nlogba < nd We also know that f(n) (nd) f (n) grows polynomially faster than nlogba. Solution: T(n) = Q( f (n) ) = Q(nd) .
19
Idea of master theorem Recursion tree: f (n) f (n) a … f (n/b) f (n/b)
h = logbn … a2 f (n/b2) f (n/b2) f (n/b2) f (n/b2) … … CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. nlogbaT (1) T (1) Q( f (n))
20
Examples Ex. T(n) = 4T(n/2) + n
a = 4, b = 2, d = 1 nlogba = n2; f (n) = n. (a=4) >( bd =21) CASE 1: T(n) = Q(n2). Ex. T(n) = 4T(n/2) + n2 a = 4, b = 2, d=2 nlogba = n2; f (n) = n2. (a =4) = (bd=22) CASE 2: T(n) = Q(n2log n).
21
Examples Ex. T(n) = 4T(n/2) + n3
a = 4, b = 2, d=3 nlogba = n2; f (n) = n3. (a=4) < (bd = 23) CASE 3: T(n) = Q(nd) = Q(n3).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.