Master Method for Analyzing Recurrences in Algorithms, Study notes of Computer Science

The master method for analyzing recurrences in algorithms, specifically in the context of divide and conquer algorithms. It covers three cases based on the relationship between 'a' and 'b' in the recurrence relation t(n) = at(n/b) + cn, and provides examples and explanations for each case.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-d1i
koofers-user-d1i 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10/3/2002 CSE 202 - Recurrences
CSE 202 - Algorithms
Recurrences
Master Method
CSE 202 - Recurrences2
Your turn ...
For one of the following recursion trees ...
1. T(n) = 3T(n/4) + 5n for n = 256
2. T(n) = 2T(n/2) + 5n for n = 32
3. T(n) = 3T(n/2) + 5n for n = 32
4. T(n) = 3T(n/2) + n2for n = 32
5. T(n) = 4T(n/2) + n2for n = 32
...figure out the total “combine time” (the last
term) needed at each level.
Answer should be a sequence of 5 or 6 numbers.
pf3
pf4

Partial preview of the text

Download Master Method for Analyzing Recurrences in Algorithms and more Study notes Computer Science in PDF only on Docsity!

10/3/2002 CSE 202 - Recurrences

CSE 202 - Algorithms

Recurrences

Master Method

Your turn ...

For one of the following recursion trees ...

1. T(n) = 3T(n/4) + 5n for n = 256

2. T(n) = 2T(n/2) + 5n for n = 32

3. T(n) = 3T(n/2) + 5n for n = 32

4. T(n) = 3T(n/2) + n

2

for n = 32

5. T(n) = 4T(n/2) + n

2

for n = 32

...figure out the total “combine time” (the last

term) needed at each level.

Answer should be a sequence of 5 or 6 numbers.

3 CSE 202 - Recurrences

Recursion Tree for T(n) = aT(n/b) + cn

c n

c n/b c n/b c n/b

c n/b^2 c n/b^2 c n/b^2 c n/b^2

1 depth-0 node

a depth-1 nodes

... a^2 depth-2 nodes

a

logbn

nodes at

depth log b n

... ...

... (^) c c (^) c

... ...

c (^) c c c (^) c c

T(n) < c ( 1 + a(n/b) + a^2 (n/b 2 ) + ... + a logbn (n/ b logbn ) )

< cn ( 1 + a/b + (a/b) 2

  • ... + (a/b) logbn ).

How does your tree grow?

What’s T(n) = cn ( 1 + a/b + (a/b)^2 + ... + (a/b)logbn^ )?

The largest term of a geometric series “dominates”.

If a/b < 1, the first term dominates

Thus, T(n) e Q(n).

If a/b > 1, the last term dominates

So T(n) e Q(n(a/b) logbn^ ) = Q(n(a logbn /b logbn ) ) = Q(n(a logbn /n) ) = Q(a logbn ) = Q(n logba ).

If a/b = 1, all terms are equal. There are log b n terms.
So T(n) e Q(n log b n ) = Q(n lg n).

7 CSE 202 - Recurrences

Previous slide is “Master Method”

Slight differences:

Book’s condition on case 1, “f(n) is O(n logba-e )”, is

slightly more general.

It allows f(n) to be less uniform.

Case 2 – remember a logbn^ = n logba.

Book has (unnecessary) extra condition in case 3

“ f(n) is W(n logba+e )” is implied by “f(n) > c a f(n/b), c>1”

Master method can “interpret n/b to mean either

n/b or n/b”

Other recurrences

Master Method doesn’t always apply:

What if, in MergeSort, we divided 75% - 25%?

T(n) = T(3n/4) + T(n/4) + c n.

Or we divided into 1000 and n-1000 sized pieces?

T(n) = T(1000) + T(n-1000) + cn (for n>1000).

Or consider:

T(n) = 2T(n/2) + n lg n.