Recurrence relationclass 5

13
Solving Recurrences The Master Theorem

description

 

Transcript of Recurrence relationclass 5

Page 1: Recurrence relationclass 5

Solving RecurrencesThe Master Theorem

Page 2: Recurrence relationclass 5

Recurrence Relations

Page 3: Recurrence relationclass 5

Recurrences

• The expression:

is a recurrence.– Recurrence: an equation that describes a function in

terms of its value on smaller functions

1

22

1

)(ncn

nT

nc

nT

Page 4: Recurrence relationclass 5

Recurrence Examples

0

0

)1(

0)(

n

n

nscns

0)1(

00)(

nnsn

nns

1

22

1

)(nc

nT

nc

nT

1

1

)(

ncnb

naT

nc

nT

Page 5: Recurrence relationclass 5

Solving Recurrences

• Substitution method• Iteration method• Master method

Page 6: Recurrence relationclass 5

Solving Recurrences

• The substitution method (CLR 4.1)– “Making a good guess” method– Guess the form of the answer, then use

induction to find the constants and show that solution works

– Examples:• T(n) = 2T(n/2) + (n) T(n) = (n lg n)• T(n) = 2T(n/2) + n ???

Page 7: Recurrence relationclass 5

The Master Theorem

• Given: a divide and conquer algorithm– An algorithm that divides the problem of size

n into a subproblems, each of size n/b– Let the cost of each stage (i.e., the work to

divide the problem + combine solved subproblems) be described by the function f(n)

• Then, the Master Theorem gives us a cookbook for the algorithm’s running time:

Page 8: Recurrence relationclass 5

The Master Theorem

• if T(n) = aT(n/b) + f(n) then

1

0

largefor )()/(

AND )(

)(

)(

)(

log)(

log

log

log

log

log

c

nncfbnaf

nnf

nnf

nOnf

nf

nn

n

nT

a

a

a

a

a

b

b

b

b

b

Page 9: Recurrence relationclass 5

Using The Master Method

• T(n) = 9T(n/3) + n– a=9, b=3, f(n) = n– nlogb a = nlog3 9 = (n2)– Since f(n) = O(nlog3 9 - ), where =1, case 1

applies:

– Thus the solution is T(n) = (n2)

aa bb nOnfnnT loglog )( when )(

Page 10: Recurrence relationclass 5

Simplified Masters Theorem

)(

)(

)(

log

log)(

log d

d

d

a

d

d

baif

baif

baif

nn

nn

n

nT

b

Page 11: Recurrence relationclass 5

• In divide and conquer generally, a problem instance size of n is divided into two instances of n/2. More generally an instance of size n can be divided into b instances of size n/b, with 'a' of the needing to be solved. ( hence, 'a' and b are constants; a>=1 and b>1). Assuming that size n is a power of b, by simplifying the analysis, the recurrence for the running time

»T(n)=aT(n/b)+f(n)• Where f(n) is a function that accounts for the time

spent on dividing the problem into smaller ones and on combining their solutions.

Page 12: Recurrence relationclass 5

More Examples of Master’s Theorem

• T(n) = 3T(n/5) + n• T(n) = 2T(n/2) + n• T(n) = 2T(n/2) + 1• T(n) = T(n/2) + n• T(n) = T(n/2) + 1

Page 13: Recurrence relationclass 5

When Master’s Theorem cannot be applied

• T(n) = 2T(n/2) + n logn• T(n) = 2T(n/2) + n/ logn