0% found this document useful (0 votes)
353 views

Recursion Tree Method

The document discusses using the recursion-tree method to solve recurrences. It begins by explaining the substitution method for solving recurrences involves guessing the form of the solution and using induction to find constants. It then introduces the recursion-tree method to generate guesses for recurrence solutions. An example recurrence is solved using a recursion tree by expanding the tree levels, summing costs at each level, and applying the geometric series formula. Verification with substitution method shows the guess of O(n^2) is correct. Finally, another example recurrence is solved using a recursion tree.

Uploaded by

2009010839784
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
353 views

Recursion Tree Method

The document discusses using the recursion-tree method to solve recurrences. It begins by explaining the substitution method for solving recurrences involves guessing the form of the solution and using induction to find constants. It then introduces the recursion-tree method to generate guesses for recurrence solutions. An example recurrence is solved using a recursion tree by expanding the tree levels, summing costs at each level, and applying the geometric series formula. Verification with substitution method shows the guess of O(n^2) is correct. Finally, another example recurrence is solved using a recursion tree.

Uploaded by

2009010839784
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MCS 360 L-39

22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the recursion-tree method
1 solving recurrences
expanding the recurrence into a tree
summing the cost at each level
applying the substitution method
2 another example
using a recursion tree
MCS 360 Lecture 39
Introduction to Data Structures
Jan Verschelde, 22 November 2010
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
solving recurrences
The substitution method for solving recurrences consists of
two steps:
1 Guess the form of the solution.
2 Use mathematical induction to nd constants in the
form and show that the solution works.
In the previous lecture, the focus was on step 2.
Today we introduce the recursion-tree method to generate a
guess for the form of the solution to the recurrence.
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the recursion-tree method
1 solving recurrences
expanding the recurrence into a tree
summing the cost at each level
applying the substitution method
2 another example
using a recursion tree
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
an example
Consider the recurrence relation
T(n) = 3T(n/4) + cn
2
for some constant c.
We assume that n is an exact power of 4.
In the recursion-tree method we expand T(n) into a tree:
T(n)
E
cn
2

e
e
e
e
T(
n
4
) T(
n
4
) T(
n
4
)
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
we expand T(
n
4
)
Applying T(n) = 3T(n/4) + cn
2
to T(n/4) leads to
T(n/4) = 3T(n/16) + c(n/4)
2
, expanding the leaves:
cn
2

r
r
r
r
r
r
r
r
c(
n
4
)
2

e
e
e
e
T(
n
16
) T(
n
16
) T(
n
16
)
c(
n
4
)
2

e
e
e
e
T(
n
16
) T(
n
16
) T(
n
16
)
c(
n
4
)
2

e
e
e
e
T(
n
16
) T(
n
16
) T(
n
16
)
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
we expand T(
n
16
)
Applying T(n) = 3T(n/4) + cn
2
to T(n/16) leads to
T(n/16) = 3T(n/64) + c(n/16)
2
, expanding the leaves:
cn
2

r
r
r
r
r
r
r
r
c(
n
4
)
2

e
e
e
e
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
c(
n
4
)
2

e
e
e
e
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
c(
n
4
)
2

e
e
e
e
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
c(
n
16
)
2

f
f
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the recursion-tree method
1 solving recurrences
expanding the recurrence into a tree
summing the cost at each level
applying the substitution method
2 another example
using a recursion tree
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the cost at each level
We sum the cost at each level of the tree:
cn
2
= cn
2

r
r
r
r
r
r
r
r
c(
n
4
)
2
+

e
e
e
e
c(
n
16
)
2

f
f
c(
n
16
)
2
+

f
f
c(
n
16
)
2
+

f
f
c(
n
4
)
2
+

e
e
e
e
c(
n
16
)
2
+

f
f
c(
n
16
)
2
+

f
f
c(
n
16
)
2
+

f
f
c(
n
4
)
2
=
3
16
cn
2

e
e
e
e
c(
n
16
)
2
+

f
f
c(
n
16
)
2
+

f
f
c(
n
16
)
2
+

f
f
= (
3
16
)
2
cn
2
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
adding up the costs
T(n) = cn
2
+
3
16
cn
2
+
_
3
16
_
2
cn
2
+
= cn
2
_
1 +
3
16
+
_
3
16
_
2
+
_
The disappear if n = 16,
or the tree has depth at least 2 if n 16 = 4
2
.
For n = 4
k
, k = log
4
(n), we have:
T(n) = cn
2
log
4
(n)

i =0
_
3
16
_
i
.
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
geometric series
Consider a nite sum rst:
S
n
= 1 + r + r
2
+ + r
n
=
n

i =0
r
i
.
To nd an explicit form of the solution we do
rS
n
= r + r
2
+ + r
n
+ r
n+1
S
n
= 1 + r + r
2
+ + r
n
(r 1)S
n
= 1 + r
n+1
So the explicit sum is
S
n
=
r
n+1
1
r 1
.
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
applying the geometric sum
Applying
S
n
=
n

i =0
r
i
=
r
n+1
1
r 1
to
T(n) = cn
2
log
4
(n)

i =0
_
3
16
_
i
with r =
3
16
leads to
T(n) = cn
2
_
3
16
_
log
4
(n)+1
1
3
16
1
.
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
polishing the result
Instead of T(n) dn
2
for some constant d, we have
T(n) = cn
2
_
3
16
_
log
4
(n)+1
1
3
16
1
.
Recall
T(n) = cn
2
log
4
(n)

i =0
_
3
16
_
i
.
To remove the log
4
(n) factor, we consider
T(n) cn
2

i =0
_
3
16
_
i
= cn
2
1
3
16
1
dn
2
, for some constant d.
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the recursion-tree method
1 solving recurrences
expanding the recurrence into a tree
summing the cost at each level
applying the substitution method
2 another example
using a recursion tree
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
verifying the guess
Let us see if T(n) dn
2
is good for T(n) = 3T(n/4) + cn
2
.
Applying the substitution method:
T(n) = 3T(n/4) + cn
2
3d
_
n
4
_
2
+ cn
2
=
_
3
16
d + c
_
n
2
=
3
16
_
d +
16
3
c
_
n
2

3
16
(2d) n
2
, if d
16
3
c
dn
2
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
the recursion-tree method
1 solving recurrences
expanding the recurrence into a tree
summing the cost at each level
applying the substitution method
2 another example
using a recursion tree
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
using a recursion tree
Consider T(n) = T(n/3) + T(2n/3) +cn.
cn = cn

r
r
r
r
c
n
3

d
d
+ = cn
c
n
9

e
e
c
2n
9

e
e
c
2n
3

d
d
c
2n
9

e
e
c
4n
9

e
e
+ + + = cn
.
.
.
+
= O(n log
2
(n))
T
log
3/2
(n)
c
MCS 360 L-39
22 Nov 2010
solving
recurrences
expanding the
recurrence into a tree
summing the cost at
each level
applying the
substitution method
another
example
using a recursion
tree
Summary + Assignments
We covered 4.4 of Introduction to Algorithms, 3rd edition
by Thomas H. Cormen, Clifford Stein, Ronald L. Rivest, and
Charles E. Leiserson.
Assignments:
1 Consider T(n) = 3T(n/2) + n. Use a recursion tree to
derive a guess for an asymptotic upper bound for T(n)
and verify the guess with the substitution method.
2 Same question as before for T(n) = T(n/2) + n
2
.
3 Same question as before for T(n) = 2T(n 1) + 1.
Last homework collection on Monday 29 November:
#1 of L-30, #1 of L-31, #3 of L-32, #2 of L-33, #1 of L-34.
Final exam on Tuesday 7 December, 8-10AM in TH 216.

You might also like