Professional Documents
Culture Documents
6.046J/18.401J/SMA5503
Lecture 2
Prof. Erik Demaine
Solving recurrences
• The analysis of merge sort from
Lecture 1 required us to solve a
recurrence.
• Recurrences are like solving integrals,
differential equations, etc.
o Learn a few tricks.
• Lecture 3: Applications of recurrences.
Θ(1)
Θ(1)
Θ(1)
…
…
Θ(1)
…
…
Θ(1) Total = n 2
( 5 5
1 + 16 + 16 ( ) +( )
2 5 3
16
+L )
= Θ(n2) geometric series
Day 3 Introduction to Algorithms L2.17
The master method
∑ i i ) = 1.
(
k
p
a /b
i =1
Then, the answers are the same as for the
master method, but with np instead of nlogba.
(Akra and Bazzi also prove an even more
general result.)
Day 3 Introduction to Algorithms L2.23
Idea of master theorem
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) … f (n/b) a f (n/b)
h = logbn a
f (n/b2) f (n/b2) … f (n/b2) a2 f (n/b2)
…
#leaves = ah
…
…
…
C ASE 1:
CASE 1: The
The weight
weight increases
increases
geometrically
geometrically from
from the
the root
root to
to the
the nlogbaΤ (1)
Τ (1) leaves.
leaves. The
The leaves
leaves hold
hold aa constant
constant
fraction
fraction of
of the
the total
total weight.
weight. Θ(nlogba)
Day 3 Introduction to Algorithms L2.25
Idea of master theorem
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) … f (n/b) a f (n/b)
h = logbn a
f (n/b2) f (n/b2) … f (n/b2) a2 f (n/b2)
…
…
CCASEASE 2:
2: (k
(k == 0)
0) The
The weight
weight
isis approximately nlogbaΤ (1)
Τ (1) approximately the the same
same on
on
each
each ofof the
the log
logbbnn levels.
levels.
Θ(nlogbalg n)
Day 3 Introduction to Algorithms L2.26
Idea of master theorem
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) … f (n/b) a f (n/b)
h = logbn a
f (n/b2) f (n/b2) … f (n/b2) a2 f (n/b2)
…
…
C ASE 3:
CASE 3: The
The weight
weight decreases
decreases
geometrically
geometrically from
from the
the root
root to
to the
the nlogbaΤ (1)
Τ (1) leaves.
leaves. The
The root
root holds
holds aa constant
constant
fraction
fraction of
of the
the total
total weight.
weight. Θ( f (n))
Day 3 Introduction to Algorithms L2.27
Conclusion
n +1
1 − x
1 + x + x2 + L + xn = for x ≠ 1
1− x
2 1
1+ x + x +L = for |x| < 1
1− x
Return to last
slide viewed.