Docsity

Master Theorem for Solving Recurrences, Study notes of Computer Science

Toggle sidebar
bg1
1
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
Sofya Raskhodnikova
Algorithm Design and Analysis
L
ECTURE 11
Solving Recurrences
•Master Theorem
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
Review Question: Exponentiation
Problem: Compute a
b
, where b
∈
is nbits long.
Question: How many multiplications?
a
b
=a
b/2
×a
b/2
if bis even;
a
(b–1)/2
×a
(b–1)/2
×aif bis odd.
Divide-and-conquer algorithm:
T(b) = T(b/2) + Θ(1) T(b) = Θ(log b) = Θ(n) .
Naive algorithm: Θ(b)= Θ(2
n
) (exponential
in the input length!)
Naive algorithm:
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
So far: 2 recurrences
•Mergesort; Counting Inversions
T(n) = 2 T(n/2) + Θ(n) = Θ(n log n)
•Binary Search; Exponentiation
T(n) = 1 T(n/2) + Θ(1) = Θ(log n)
Master Theorem: method for solving recurrences.
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
The master method
The master method applies to recurrences of
the form
T(n) = a T(n/b) + f(n) ,
where a≥1, b> 1, and fis asymptotically
positive, that is f (n) >0 for all n > n
0
.
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
Three common cases
Compare f(n)with n
logba
:
1. f(n) = O(n
logba–ε
)for some constant ε> 0.
•f(n) grows polynomially slower than n
logba
(by an n
ε
factor).
Solution: T(n) = Θ(n
logba
).
9/21/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith
Three common cases
Compare f(n)with n
logba
:
1. f(n) = O(n
logba–ε
)for some constant ε> 0.
•f(n) grows polynomially slower than n
logba
(by an n
ε
factor).
Solution: T(n) = Θ(n
logba
).
2. f(n) = Θ(n
logba
lg
k
n)for some constant k≥0.
•f(n) and n
logba
grow at similar rates.
Solution: T(n) = Θ(n
logba
lg
k+1
n).
pf3

Partial preview of the text

Download Master Theorem for Solving Recurrences and more Study notes Computer Science in PDF only on Docsity!

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

Sofya Raskhodnikova

Algorithm Design and Analysis

LECTURE 11

Solving Recurrences

  • Master Theorem

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

Review Question: Exponentiation

Problem: Compute a b , where b ∈  is n bits long. Question: How many multiplications?

a b^ =

a b/^2 × a b/^2 if b is even; a ( b– 1) /^2 × a ( b– 1) /^2 × a if b is odd.

Divide-and-conquer algorithm:

T ( b ) = T ( b /2) + Θ(1)  T ( b ) = Θ(log b ) = Θ(n).

Naive algorithm: Θ( b ) = Θ( 2 n ) (exponential in the input length!)

Naive algorithm:

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

So far: 2 recurrences

  • Mergesort; Counting Inversions T ( n ) = 2 T ( n /2) + Θ( n ) = Θ( n log n )
  • Binary Search; Exponentiation T ( n ) = 1 T ( n /2) + Θ(1) = Θ(log n )

Master Theorem: method for solving recurrences.

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

The master method

The master method applies to recurrences of the form T ( n ) = a T ( n / b ) + f ( n ) , where a ≥ 1 , b > 1, and f is asymptotically positive, that is f ( n ) >0 for all n > n 0.

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

Three common cases

Compare f ( n ) with n log ba :

  1. f ( n ) = O ( n log ba^ –^ ε) for some constant ε > 0.
    • f ( n ) grows polynomially slower than n log ba (by an n ε^ factor). Solution: T ( n ) = Θ( n log ba ).

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

Three common cases

Compare f ( n ) with n log ba :

  1. f ( n ) = O ( n log ba^ –^ ε) for some constant ε > 0.
    • f ( n ) grows polynomially slower than n log ba (by an n ε^ factor). Solution: T ( n ) = Θ( n log ba ).
  2. f ( n ) = Θ( n log ba^ lg kn ) for some constant k ≥ 0.
    • f ( n ) and n log ba^ grow at similar rates. Solution: T ( n ) = Θ( n log ba^ lg k +1 n ).

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

Three common cases (cont.)

Compare f ( n ) with n log ba :

  1. f ( n ) = Ω( n log ba^ +^ ε) for some constant ε > 0.
    • f ( n ) grows polynomially faster than n log ba^ (by an n ε^ factor), and f ( n ) satisfies the regularity condition that a f ( n/b ) ≤ c f ( n ) for some constant c < 1. Solution: T ( n ) = Θ( f ( n )).

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

f ( n/b )

Idea of master theorem

f ( n/b ) f ( n/b )

Recursion tree:

f ( n ) (^) a

f ( n/b^2 )^ f^ ( n/b^2 )… f^ ( n/b^2 )

a

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

f ( n/b )

Idea of master theorem

f ( n/b ) f ( n/b )

Recursion tree:

f ( n ) (^) a

f ( n/b^2 ) f ( n/b^2 )… f ( n/b^2 )

a

f ( n )

a f ( n/b )

a^2 f ( n/b^2 )

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

f ( n/b )

Idea of master theorem

f ( n/b ) f ( n/b )

Recursion tree:

f ( n ) (^) a

f ( n/b^2 ) f ( n/b^2 )… f ( n/b^2 )

h = log bn^ a

f ( n )

a f ( n/b )

a^2 f ( n/b^2 )

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

n log ba Τ (1)

f ( n/b )

Idea of master theorem

f ( n/b ) f^ ( n/b )

Recursion tree:

f ( n ) (^) a

f ( n/b^2 ) f ( n/b^2 )… f ( n/b^2 )

h = log bn^ a

f ( n )

a f ( n/b )

a^2 f ( n/b^2 )

#leaves = ah = a log bn = n log ba

9/21/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith

f ( n/b )

Idea of master theorem

f ( n/b ) f^ ( n/b )

Recursion tree:

f ( n ) (^) a

f ( n/b^2 ) f ( n/b^2 )… f ( n/b^2 )

h = log bn^ a

f ( n )

a f ( n/b )

a^2 f ( n/b^2 )

CASE 1 : The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight.

CASE 1 : The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Θ( n log ba )

n log ba Τ (1)