Top Banner
Recurrence Relations Recurrence Relations Analyzing the performance of recursive algorithms
27

Recurrence Relations Analyzing the performance of recursive algorithms.

Jan 19, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recurrence Relations Analyzing the performance of recursive algorithms.

Recurrence RelationsRecurrence Relations

Analyzing the performance of recursive algorithms

Page 2: Recurrence Relations Analyzing the performance of recursive algorithms.

Worst Case AnalysisWorst Case Analysis

Select a Hypothetical Set of Inputs Should be expandable to any size Make Algorithm run as long as possible Must not depend on Algorithm Behavior

– May use static behavior analysis– Inputs may not change dynamically

Page 3: Recurrence Relations Analyzing the performance of recursive algorithms.

Average Case AnalysisAverage Case Analysis

Determine the range of inputs over which the average will be taken

Select a probability distribution Characterize Amount of work required

for each input Compute the Mean Value

Page 4: Recurrence Relations Analyzing the performance of recursive algorithms.

Binary SearchBinary Search

BinS(X:Item;First,Last:Integer):Integervar Middle:Integer;begin

if First <= Last thenMiddle = (First + Last)/2;if L[Middle] = X then return Middle;if L[Middle] < X then return BinS(X,Middle+1,Last);if L[Middle] > X then return BinS(X,First,Middle-1);

elsereturn -1;

endif;end;

Page 5: Recurrence Relations Analyzing the performance of recursive algorithms.

Analysis of BinS: BasisAnalysis of BinS: Basis

If List Size = 1 then First = Last If List Size = 0 then First > Last

if First <= Last then ...else return -1;

Then W(0) = 0

Page 6: Recurrence Relations Analyzing the performance of recursive algorithms.

Analysis of BinS: Recurrsion IAnalysis of BinS: Recurrsion I

If comparison fails, find size of new list If list size is odd:

– First=1,Last=5,Middle=(1+5)/2=3

If list size is even:– First=1,Last=6,Middle=(1+6)/2=3

Page 7: Recurrence Relations Analyzing the performance of recursive algorithms.

Analysis of BinS: Recurrsion IIAnalysis of BinS: Recurrsion II

Exercise: Verify that these examples are characteristic of all lists of odd or even size

Worst case: Item X not in list, larger than any element in list.

If list is of size n, new list will be of size:n2

Page 8: Recurrence Relations Analyzing the performance of recursive algorithms.

Analysis of BinS: RecurrenceAnalysis of BinS: Recurrence

W( )0 0

W( ) W /n n 1 2

Is W(n) (n)?

Page 9: Recurrence Relations Analyzing the performance of recursive algorithms.

Solving the RecurrenceSolving the Recurrence

W( ) W /n n 1 2

We need to eliminate W from the RHS.

W( / ) W / /n n2 1 2 2

1 4W n /

W( ) W /n n 1 1 4

Page 10: Recurrence Relations Analyzing the performance of recursive algorithms.

Continuing the Exercise ...Continuing the Exercise ...

W( ) W /n n 1 2

W( ) W /n n 1 1 4

W( ) W /n n 1 1 1 8

W( ) W /n n 1 1 1 1 16

Page 11: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

Page 12: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

But, if k is big enough ...

Page 13: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

But, if k is big enough ...

Then the argument of W will be zero, and ...

Page 14: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

But, if k is big enough ...

Then the argument of W will be zero, and ...

Since W(0) = 0 ...

Page 15: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

But, if k is big enough ...

Then the argument of W will be zero, and ...

Since W(0) = 0 ...

The W on the RHS ...

Page 16: Recurrence Relations Analyzing the performance of recursive algorithms.

Ah Ha! A General Formula!Ah Ha! A General Formula!

W( ) W /n k n k 2

But, if k is big enough ...

Then the argument of W will be zero, and ...

Since W(0) = 0 ...

The W on the RHS ...

Will Disappear!

Page 17: Recurrence Relations Analyzing the performance of recursive algorithms.

When Will W disappear?When Will W disappear?

When:

Page 18: Recurrence Relations Analyzing the performance of recursive algorithms.

When Will W Disappear?When Will W Disappear?

When:

Take the Log:

Page 19: Recurrence Relations Analyzing the performance of recursive algorithms.

When Will W Disappear?When Will W Disappear?

When:

Take the Log:

Take the Floor:

Page 20: Recurrence Relations Analyzing the performance of recursive algorithms.

Finally ...Finally ...

Page 21: Recurrence Relations Analyzing the performance of recursive algorithms.

Some Other RecurrencesSome Other Recurrences

Page 22: Recurrence Relations Analyzing the performance of recursive algorithms.

Recurrences with T(1)=1Recurrences with T(1)=1

Page 23: Recurrence Relations Analyzing the performance of recursive algorithms.

The ChallengerThe Challenger

T( ) T

T( )

n n n cn 2 1

Page 24: Recurrence Relations Analyzing the performance of recursive algorithms.

The Master Theorem IThe Master Theorem IT( ) T( / ) ( )n a n b f n

Page 25: Recurrence Relations Analyzing the performance of recursive algorithms.

The Master Theorem IIThe Master Theorem IIT( ) T( / ) ( )n a n b f n

Page 26: Recurrence Relations Analyzing the performance of recursive algorithms.

The Master Theorem IIIThe Master Theorem IIIT( ) T( / ) ( )n a n b f n

(for sufficiently large n)

Page 27: Recurrence Relations Analyzing the performance of recursive algorithms.

HomeworkHomework

Page 72, 4-1 Page 73, 4-4