0% found this document useful (0 votes)
81 views23 pages

4.recurrences: Hsu, Lih-Hsing

The document discusses different methods for solving recurrence relations, including the substitution method, recursion-tree method, and master method. The substitution method involves guessing the form of the solution and using mathematical induction to prove it is correct. The recursion-tree method works by drawing out the recursion tree to determine the overall cost. The master method provides a general way to solve recurrences of the form T(n) = aT(n/b) + f(n). Examples are provided to illustrate how to apply each method.

Uploaded by

huma Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views23 pages

4.recurrences: Hsu, Lih-Hsing

The document discusses different methods for solving recurrence relations, including the substitution method, recursion-tree method, and master method. The substitution method involves guessing the form of the solution and using mathematical induction to prove it is correct. The recursion-tree method works by drawing out the recursion tree to determine the overall cost. The master method provides a general way to solve recurrences of the form T(n) = aT(n/b) + f(n). Examples are provided to illustrate how to apply each method.

Uploaded by

huma Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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 ) ( cn / 2 b ) ( cn / 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 +17n 34

n
n (2 +17) 21/a21/2 1.5 n 12

34a-2b -bb 34a


c > 0-c > -2c
T(n) anlgn - blgn - cT(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
T (n) cn cn cn ...
2
cn 2 (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 2
cn nlog 3 4

i 0 16


1
1 (3 / 16)
cn 2 n log 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
3T ( n / 4) cn 2
T ( n)
3d n / 42 cn 2

3d (n / 4) 2 cn 2

3 2
dn cn 2

16
dn2 ,

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


Theorem 4.1 (Master theorem)
Let a 1 and b 1 be constants, let f ( n ) be a function, and T ( n )
be defined on the nonnegative integers by the recurrence
T ( n ) aT ( n / b ) f ( n )
where we interpret n / b mean either n / b or n / b .
1. If f ( n ) O( n logb a ) for some constant 0 , then T ( n ) ( nlogb a ) .
2. If f ( n ) O( n logb a ) then T ( n ) ( nlogb a log n ) .
3. If f ( n ) O( n logb a ) for some constant 0 and if af ( n / b ) cf ( n )
for some constant c 1 and all sufficiently large n, then T ( n ) ( f ( n )) .
Proof. (In section 4.4 by recursive tree)

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,
b=2, f(n)= n lgn, and n
log b a
n. It might
seem that case 3 should apply, since f(n)=
n lgn is asymptotically larger than n log b a
n.
The problem is that it is not polynomially
larger.

Chapter 4 P.23

You might also like