Recursiontree method A recursion tree models the costs

  • Slides: 21
Download presentation
Recursion-tree method • A recursion tree models the costs (time) of a recursive execution

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. L 2. 1

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: L

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: L 2. 2

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: T(n)

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: T(n) L 2. 3

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 T(n/4) T(n/2) L 2. 4

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 (n/4)2 T(n/16) T(n/8) (n/2)2 T(n/8) T(n/4) L 2. 5

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 (n/4)2 (n/8)2 (n/4)2 … (n/16)2 (n/2)2 (1) L 2. 6

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 (n/4)2 (n/8)2 (n/4)2 … (n/16)2 (n/2)2 (1) L 2. 7

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 (n/4)2 (n/8)2 (n/4)2 … (n/16)2 (n/2)2 (1) L 2. 8

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 … (n/16)2 (n/8)2 (n/2)2 (n/8)2 (n/4)2 … (n/4)2 (1) L 2. 9

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n

Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n 2: n 2 (n/2)2 (n/4)2 (n/8)2 (1) (n/4)2 … (n/8)2 … (n/16)2 Total = = (n 2) geometric series L 2. 10

Appendix: geometric series for x ¹ 1 for |x| < 1 L 2. 11

Appendix: geometric series for x ¹ 1 for |x| < 1 L 2. 11

The master method applies to recurrences of the form T(n) = a T(n/b) +

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 L 2. 12

Idea of master theorem f (n) … a f (n/b) … f (n/b) a

Idea of master theorem f (n) … a f (n/b) … f (n/b) a h = logbn f (n/b 2) … f (n/b 2) T (1) #leaves = ah = alogbn = nlogba f (n) a f (n/b) a 2 f (n/b 2) … Recursion tree: nlogba. T (1) L 2. 13

Three common cases Compare a with bd: 1. If a > bd, logba >

Three common cases Compare a with bd: 1. If a > bd, logba > d nlogba > nd 1. We also know that f(n) (nd) • f (n) grows polynomially slower than nlogba. Solution: T(n) = (nlogba). L 2. 14

Idea of master theorem f (n) … a f (n/b) … f (n/b) a

Idea of master theorem f (n) … a f (n/b) … f (n/b) a h = logbn f (n/b 2) … f (n/b 2) CASE 1: The weight increases geometrically from the root to the T (1) leaves. The leaves hold a constant fraction of the total weight. f (n) a f (n/b) a 2 f (n/b 2) … Recursion tree: nlogba. T (1) (nlogba) L 2. 15

Three common cases Compare a with bd: 2. If a = bd, logba =

Three common cases Compare a with bd: 2. If a = bd, logba = d Nlogba = nd 2. We also know that f(n) (nd) • f (n) and nlogba grow at similar rates. Solution: T(n) = ( f(n) log n). L 2. 16

Idea of master theorem Recursion tree: f (n) T (1) a f (n/b) a

Idea of master theorem Recursion tree: f (n) T (1) a f (n/b) a 2 f (n/b 2) … … a f (n/b) … f (n/b) a h = logbn f (n/b 2) … f (n/b 2) f (n) CASE 2: The weight is approximately the same on each of the logbn levels. nlogba. T (1) (nlogbalg n) L 2. 17

Three common cases (cont. ) Compare a with bd: 3. If a < bd,

Three common cases (cont. ) Compare a with bd: 3. If a < bd, logba < d nlogba < nd 3. We also know that f(n) (nd) f (n) grows polynomially faster than nlogba. 3. Solution: T(n) = ( f (n) ) = (nd). L 2. 18

Idea of master theorem f (n) … a f (n/b) … f (n/b) a

Idea of master theorem f (n) … a f (n/b) … f (n/b) a h = logbn f (n/b 2) … f (n/b 2) CASE 3: The weight decreases geometrically from the root to the T (1) leaves. The root holds a constant fraction of the total weight. f (n) a f (n/b) a 2 f (n/b 2) … Recursion tree: nlogba. T (1) ( f (n)) L 2. 19

Examples Ex. T(n) = 4 T(n/2) + n a = 4, b = 2,

Examples Ex. T(n) = 4 T(n/2) + n a = 4, b = 2, d = 1 nlogba = n 2; f (n) = n. (a=4) >( bd =21) CASE 1: T(n) = (n 2). Ex. T(n) = 4 T(n/2) + n 2 a = 4, b = 2, d=2 nlogba = n 2; f (n) = n 2. (a =4) = (bd=22) CASE 2: T(n) = (n 2 log n). L 2. 20

Examples Ex. T(n) = 4 T(n/2) + n 3 a = 4, b =

Examples Ex. T(n) = 4 T(n/2) + n 3 a = 4, b = 2, d=3 nlogba = n 2; f (n) = n 3. (a=4) < (bd = 23) CASE 3: T(n) = (nd) = (n 3). L 2. 21