You are on page 1of 8

Solving Recurrences

David Luebke

01/28/15

Recurrences

The expression:

n 1

T ( n)

2T cn n 1

2
is a recurrence.

Recurrence: an equation that describes a function in


terms of its value on smaller functions

Recurrence Examples
0
n0
s ( n)
c s (n 1) n 0

s ( n)

n 1

n0

n s (n 1) n 0

n 1

T ( n)

T ( n)

2T c n 1

David Luebke

n
aT cn n 1
b

01/28/15

Solving Recurrences
Substitution method
Recursion Tree
Master method

Substitution Method

The substitution method

A.k.a. the making a good guess method


Guess the form of the answer, then use induction to
find the constants and show that solution works
Run an example: merge sort
T(n) = 2T(n/2) + cn
We guess that the answer is O(n lg n)
Prove it by induction

Can similarly show T(n) = (n lg n), thus (n lg n)

David Luebke

01/28/15

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 algorithms running time:

The Master Theorem

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

log b a
Case
1
:

T (n) Case 2 : n
log n

Case3 : f (n)

log b a

f (n) O n logb a

f ( n) n

log b a

c 1

f (n) n logb a AND

af (n / b) cf (n) for large n

Using The Master Method

T(n) = 9T(n/3) + n

a=9, b=3, f(n) = n


nlog a = nlog 9 = (n2)
Since f(n) = O(nlog 9 - ), where =1, case 1 applies:
b

T (n) n log b a when f (n) O n log b a

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

You might also like