




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Fall 2005;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Jared Saia
University of New Mexico
Recurrence Relations
Recursion Trees
Alg1 (int n){
elseif (n<=1) return 1;
return Alg1(n/2) + Alg1(n/2) + n;
Let
n ) be the run time of Alg1 on input
n
Then we can write
n ) = 2
n/
How to solve for
n )?
for recurrence solutionsUp to this point, I’ve been supplying you with good “guesses”
Q: How do we get these guesses?
Following are some good guesses for solutions to recurrences. log
(^) n
n
n n (^) log
(^) n
n 2
n 3
n
We will review three useful techniques:
Recursion tree method
Master Theorem
Annihilators
recursive callEach node represents the cost of a single subproblem in a
First, we sum the costs of the nodes in each level of the tree
Then, we sum the costs of all of the levels
using substitution methodUsed to get a good guess which is then refined and verified
Best
method
(usually)
for
recurrences
where
a
term
like
n/c
) appears on the right hand side of the equality
n )
log
4 (^) n
i=0 ∑
i n 2
n 2
∑∞ i =
i
n 2
n 2 )
recurrences of the formDivide and conquer algorithms often give us running-time
n ) =
a T
n/b
(^) f (^) ( n )
Where
a
and
b
are constants and
f (^) ( n ) is some other function.
for solving such recurrences whenThe so-called “Master Method” gives us a general method
f (^) ( n ) is a simple polynomial.
tionsUnfortunately, the Master Theorem doesn’t work for all func-
f (^) ( n )
Further many useful recurrences don’t look like
n )
rences when it appliesHowever, the theorem allows for very fast solution of recur-
treesMaster Theorem is just a special case of the use of recursion
Consider equation
n ) =
a T
n/b
(^) f (^) ( n )
We start by drawing a recursion tree
The root contains the value
f (^) ( n )
It has
a
children, each of which contains the value
f (^) ( n/b
Each
of
these
nodes
has
a
children,
containing
the
value
f (^) ( n/b
2 )
In general, level
i
contains
a i nodes with values
f (^) ( n/b
i )
Hence the sum of the nodes at the
i -th level is
a i f (^) ( n/b
i )
16
renceThe tree stops when we get to the base case for the recur-
We’ll assume
f (^) (1) = Θ(1) is the base case
Thus the depth of the tree is log
b (^) n
and there are log
b (^) n
levels
Let
n ) be the sum of all values stored in all levels of the
T tree: (^) ( n ) =
f (^) ( n )+
a f
n/b
a 2 f^ (^) ( n/b
2 )+
a i f^ (^) ( n/b
i )+
a L
f^ (^) ( n/b
L )
Where
= log
b (^) n
is the depth of the tree
Since
f (^) (1) = Θ(1), the last term of this summation is Θ(
a L ) =
a log
b (^) n ) = Θ(
n log
b (^) a )
It’s not hard to see that
a log
b (^) n
n log
b (^) a
a log
b (^) n
n log
b (^) a
a log
b (^) n
a log
a (^) n ∗ log
b (^) a
log
b (^) n
log
a (^) n
(^) log
b (^) a
We get to the last eqn by taking log
a
of both sides
The last eqn is true by our third basic log fact
Karatsuba’s multiplication algorithm:
n
) = 3
n/
n
If we write this as
n ) =
aT
n/b
f (^) ( n ), then
a
= 3,
b
=
f (^) ( n ) =
n
Here
a f
n/b
n/
2 is bigger than
f (^) ( n ) =
n
by a factor of
2, so
n ) = Θ(
n log
2 (^3) )
Mergesort:
n ) = 2
n/
n
If we write this as
n ) =
aT
n/b
f (^) ( n ), then
a
= 2,
b
=
f (^) ( n ) =
n
Here
a f
n/b
f (^) ( n ), so
n ) = Θ(
n (^) log
(^) n
)
n ) =
n/
n (^) log
(^) n
If we write this as
n ) =
aT
n/b
f (^) ( n ), then
a
= 1,
b
=
f (^) ( n ) =
n (^) log
(^) n
Here
a f
n/b
n/
2 log
(^) n/
2 is smaller than
f (^) ( n ) =
n (^) log
(^) n
by
a constant factor, so
n ) = Θ(
n (^) log
(^) n
)
Consider the recurrence:
n ) = 4
n/
n (^) lg
n
Q: What is
f (^) ( n ) and
a f
n/b
(whenQ: Which of the three cases does the recurrence fall under
n
is large)?
Q: What is the solution to this recurrence?
Consider the recurrence:
n ) = 2
n/
n (^) lg
n
Q: What is
f (^) ( n ) and
a f
n/b
(whenQ: Which of the three cases does the recurrence fall under
n
is large)?
Q: What is the solution to this recurrence?
many recurrencesRecursion tree and Master method are good tools for solving
guesses for recurrences likeHowever these methods are limited (they can’t help us get
f (^) ( n ) =
f (^) ( n (^) −
(^) 1) +
f (^) ( n (^) −
rences,For info on how to solve these other more difficult recur-
review the notes on annihilators on the class web
page.
Read Chapter 4 (Recurrences) in text