Recurrence Relations, Recursion Trees - Data Structures and Algorithms | CS 361L, Study notes of Computer Science

Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/22/2009

koofers-user-4ng
koofers-user-4ng 🇺🇸

4

(1)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 5
Jared Saia
University of New Mexico
Today’s Outline
Recurrence Relations
Recursion Trees
1
Alg1
Alg1 (int n){
if (n<=1) return 1;
else
return Alg1(n/2) + Alg1(n/2) + n;
}
2
Example1
Let T(n) be the run time of Alg1 on input n
Then we can write T(n) = 2T(n/2) + 1
How to solve for T(n)?
Up to this point, I’ve been supplying you with goo d “guesses”
for recurrence solutions
Q: How do we get these guesses?
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Recurrence Relations, Recursion Trees - Data Structures and Algorithms | CS 361L and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 5

Jared Saia

University of New Mexico

Today’s Outline

Recurrence Relations

Recursion Trees

Alg

Alg1 (int n){

elseif (n<=1) return 1;

return Alg1(n/2) + Alg1(n/2) + n;

Example

Let

T

n ) be the run time of Alg1 on input

n

Then we can write

T

n ) = 2

T

n/

How to solve for

T

n )?

for recurrence solutionsUp to this point, I’ve been supplying you with good “guesses”

Q: How do we get these guesses?

Getting Good Guesses (I)

Following are some good guesses for solutions to recurrences. log

(^) n

n

n n (^) log

(^) n

n 2

n 3

n

Better Techniques (II)

We will review three useful techniques:

Recursion tree method

Master Theorem

Annihilators

Recursion-tree method

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

Recursion-tree method

using substitution methodUsed to get a good guess which is then refined and verified

Best

method

(usually)

for

recurrences

where

a

term

like

T

n/c

) appears on the right hand side of the equality

Solution

T

n )

log

4 (^) n

i=0 ∑

i n 2

n 2

∑∞ i =

i

n 2

O

n 2 )

Master Theorem

recurrences of the formDivide and conquer algorithms often give us running-time

T

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.

Master Theorem

tionsUnfortunately, the Master Theorem doesn’t work for all func-

f (^) ( n )

Further many useful recurrences don’t look like

T

n )

rences when it appliesHowever, the theorem allows for very fast solution of recur-

Master Theorem

treesMaster Theorem is just a special case of the use of recursion

Consider equation

T

n ) =

a T

n/b

(^) f (^) ( n )

We start by drawing a recursion tree

The 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

Details

renceThe tree stops when we get to the base case for the recur-

We’ll assume

T

f (^) (1) = Θ(1) is the base case

Thus the depth of the tree is log

b (^) n

and there are log

b (^) n

levels

Recursion Tree

Let

T

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

L

= 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 )

A “Log Fact” Aside

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

Example

Karatsuba’s multiplication algorithm:

T

n

) = 3

T

n/

n

If we write this as

T

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

T

n ) = Θ(

n log

2 (^3) )

Example

Mergesort:

T

n ) = 2

T

n/

n

If we write this as

T

n ) =

aT

n/b

f (^) ( n ), then

a

= 2,

b

=

f (^) ( n ) =

n

Here

a f

n/b

f (^) ( n ), so

T

n ) = Θ(

n (^) log

(^) n

)

Example

T

n ) =

T

n/

n (^) log

(^) n

If we write this as

T

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

T

n ) = Θ(

n (^) log

(^) n

)

In-Class Exercise

Consider the recurrence:

T

n ) = 4

T

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?

In-Class Exercise

Consider the recurrence:

T

n ) = 2

T

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?

Take Away

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.

Todo

Read Chapter 4 (Recurrences) in text