You are on page 1of 23

4.

Recurrences

Hsu, Lih-Hsing
Computer Theory Lab.

Recurrences -- T ( n )  aT ( n / b )  f ( n )

 Substitution method
 Recursion-tree method
 Master method

Chapter 4 P.2
Computer Theory Lab.

Technicalities
 We neglect certain technical details
when we state and solve recurrences.
A good example of a detail that is often
glossed over is the assumption of
integer arguments to functions.
Boundary conditions is ignored. Omit
floors, ceilings.

Chapter 4 P.3
Computer Theory Lab.

4.1 The substitution method


: Mathematical induction
 The substitution method for solving
recurrence entails two steps:
1. Guess the form of the solution.
2. Use mathematical induction to find the
constants and show that the solution
works.

Chapter 4 P.4
Computer Theory Lab.

Example
T ( n )  2T ( n / 2  )  n

T (1 )  1
(We may omit the initial condition later.)

Guess T (n)  O(n log n)

Assume T ( n / 2  )  c n / 2  log n / 2 

Chapter 4 P.5
Computer Theory Lab.

n
T ( n )  2( c n / 2  log n / 2  )  n  cn log  n
2
 cn log n  cn log 2  n  cn log n (if c  1.)

Initial condition 1  T (1)  cn log1  0()

However, 4  T ( 2 )  cn log 2 ( if c  4 )

Chapter 4 P.6
Computer Theory Lab.

 Making a good guess


T ( n )  2T ( n / 2   17 )  n

We guess T ( n )  O( n log n )

Making guess provides loose upper


bound and lower bound. Then improve
the gap.

Chapter 4 P.7
Computer Theory Lab.

Subtleties
T ( n )  T ( n / 2  )  T ( n / 2  )  1

 Guess T ( n )  O( n )
 Assume T ( n )  cn
T ( n )  c n / 2   c n / 2   1  cn  1  cn
 However, assume T ( n )  cn  b
T ( n )  ( c n / 2   b )  ( c n / 2   b )  1
 cn  2 b  1  cn  b ( Choose b  1 )

Chapter 4 P.8
Computer Theory Lab.

n
Show that the solution to T(n) = 2T(2  + 17) + n is O(n lg n)

Solution:
assume a > 0, b > 0, c > 0 and T(n) ≦an lg n – blg n - c
n n n
T(n) ≦2[(2 + 17)lg(2 +17) - blg(2 + 17)-c]+n

n n
≦ (an + 34a)lg(2 +17) - 2blg(2 +17) - 2c + n

n n
≦anlg(2 +17) + anlg21/a + (34a-2b)lg(2 +17) - 2c

≦anlg(n) 21/a+ (34a-2b)lg(n) - 2c

Chapter 4 P.9
Computer Theory Lab.

anlg(n) 21/a+ (34a-2b)lg(n) - 2c


n
n≧ 2 +17,n≧34

n
n≧ (2 +17) 21/a,∵21/2≦1.5∴n ≧12

34a-2b≦ -b,b ≧34a


c > 0,-c > -2c
 T(n) ≦anlgn - blgn - c,T(n) ≦anlgn
 T(n) = O(nlgn)

Chapter 4 P.10
Computer Theory Lab.

Avoiding pitfalls
T ( n )  2T ( n / 2  )  n

T (1 )  1
 Assume T ( n )  O( n )

 Hence T ( n )  cn
T (n)  2(c n / 2)  n  cn  n  O(n)
(Since c is a constant)
 (WRONG!) You cannot find such a c.

Chapter 4 P.11
Computer Theory Lab.

Changing variables
T ( n )  2T ( n )  lg n

Let m  lg n .
T ( 2 m )  2T ( 2 m / 2 )  m

Then S ( m )  2 S ( m / 2 )  m .
 S ( m )  O( m lg m )
 T ( n )  T ( 2 m )  S ( m )  O( m lg m )
 O(lg n lg lg n )

Chapter 4 P.12
Computer Theory Lab.

4.2 the Recursion-tree method


T (n)  3T ( n / 4)  (n 2 )

Chapter 4 P.13
Computer Theory Lab.

Chapter 4 P.14
Computer Theory Lab.

The cost of the entire tree


2 log 4 n 1
3 2 3 2 3
2
T (n)  cn  cn    cn  ...    cn2  (nlog 3 )4

16  16   16 

 
log 4 n 1 i
3 2
    cn   n log 4 3

i 0  16 
(3 / 16)log n  1 2 4

 cn  (nlog 3 ). 4

(3 / 16)  1

Chapter 4 P.15
Computer Theory Lab.

 
log 4 n 1 i
3 2
T ( n)     cn   n log 4 3

i 0  16 
i
 

3
    cn 2   nlog 3 4

i  0  16 


1
1  (3 / 16)
cn 2   nlog  4 3

16 2
 cn  (nlog 3 ) 4

13
 O(n 2 )
Chapter 4 P.16
Computer Theory Lab.

substitution method
We want to Show that T(n) ≤ dn2 for some constant
d > 0. using the same constant c > 0 as before,
we have
T (n)  3T ( n / 4)  cn 2
 3d n / 42  cn 2
 3d (n / 4) 2  cn 2
3 2
 dn  cn 2
16
 dn 2 ,
Where the last step holds as long as d (16/13)c.

Chapter 4 P.17
Computer Theory Lab.

T (n)  T (n / 3)  T (2n / 3)  cn

Chapter 4 P.18
Computer Theory Lab.

substitution method
T (n)  T (n / 3)  T (2n / 3)  cn
 d (n / 3) lg(n / 3)  d (2n / 3) lg(2n / 3)  cn
 (d (n / 3) lg n  d (n / 3) lg 3)  (d (2n / 3) lg n  d (2n / 3) lg(3 / 2))  cn
 dn lg n  d ((n / 3) lg 3  (2n / 3) lg(3 / 2))  cn
 dn lg n  d ((n / 3) lg 3  (2n / 3) lg 3  (2n / 3) lg 2  cn
 dn lg n  dn(lg 3  2 / 3)  cn
 dn lg n,

As long as d  c/lg3 – (2/3)).

Chapter 4 P.19
Computer Theory Lab.

4.3 The master method

Chapter 4 P.20
Computer Theory Lab.

 T ( n )  9T ( n / 3 )  n

a  9, b  3, f ( n )  n
n log3 9  n2 , f ( n )  O( n log3 9 1 )

Case 1  T ( n )  ( n2 )

 T( n )  T( 2n / 3 )  1

a  1, b  3 / 2 , f ( n )  1
n log3 / 2 1  n0  1  f ( n ),

Case 2  T ( n )  (log n )

Chapter 4 P.21
Computer Theory Lab.

 T ( n )  3T ( n / 4 )  n log n

a  3 , b  4 , f ( n )  n log n
n log4 3  n0 .793 , f ( n )  O( n log4 3   )

Case 3
Check
n n 3n
af(n/b)=3( ) log ( )  log n=cf(n)
4 4 4
3
for c= , and sufficiently large n
4
 T ( n )  ( n log n )
Chapter 4 P.22
Computer Theory Lab.

 The master method does not apply to the


recurrence T (n)  2T (n / 2)  n lg n,
even though it has the proper form: a = 2,
log b a
b=2, f(n)= n lgn, and n  n. It might
seem that case 3 should apply, since f(n)=
log b a
n lgn is asymptotically larger than n  n.
 The problem is that it is not polynomially
larger.

Chapter 4 P.23

You might also like