1 Recursion Tree Method
1 Recursion Tree Method
Notes 4 Recursion-tree
1 Recursion Tree Method
The substitution method that we have seen above is good for proving the correctness of a solution.
But it requires that we have a good guess of the form of a solution. The recursion tree method
is a general method that can give complete solutions, but also, allows us to come up with good
guesses. We saw an example of the recursion tree method while analyzing the running time of
Merge-Sort function. The recursion tree method works as follows.
1. Each node of the recursion tree represents the cost of a single sub-problem.
2. We add the sum of the costs of the nodes in the tree, usually level-wise.
We will illustrate this with two examples.
Example (a). Obtain an asymptotical upper bound for the following recurrence.
T(n) = 3T(n/2) +n
2
The recursion tree for this recurrence equation is shown in Figure 1. Figure 1 shows the recursion
tree for this recurrence equation. The number of nodes at level k are 3
k
(assuming that the root
is at level 0). The size of each sub-problem at level k is n/2
k
. Hence, the sum of the cost of the
sub-problems at level k is 3
k
(n/2
k
). For now assume that n is a power of 2, so that log(n) is an
integer. Then,
T(n) =
log(n)
k=0
3
k
(n/2
k
)
2
=
log(n)
k=0
n
2
(3/4)
k
n
2
k0
(3/4)
k
=
n
2
1 3/4
= 4n
2
.
Here we have used a (common-place) trick, namely, that a geometrically decreasing sum up to a
nite number of terms is bounded by the innite terms sum, that is,
log(n)
k=0
(3/4)
k
k=0
(3/4)
k
=
4, resulting in a simpler expression.
So what happens when n is not a power of 2? One way of solving this is to use the substitution
method and show that T(n) cn
2
is a valid solution for all n 2. Another way would be to note
that T(n) is a monotonically increasing function of n and therefore, T(n) T(m), where m is the
smallest power of 2 larger than or equal to n (i.e., m = 2
log n
). Since, T(m) O(m
2
), therefore,
T(n) = O(m
2
). But for n 2, n m 2n 2, since, there must be a power of 2 between n and
2n 1. Therefore,
T(n) T(m) O(m
2
) O((2n 2)
2
) = O(n
2
)
Note that T(n) = (n
2
), since the root of the tree has cost n
2
, and therefore, T(n) = (n
2
).
Example (b). Suppose we make a small change to the recurrence equation:
T(n) = 3T(n/2) +n
1
Figure 1: Illustrating the recursion tree method for solving the recurrence equation T(n) =
3T(n/2) + n
2
. Figure (a) shows a two-level unrolling, Figure (b) shows recursion tree with 3
levels, and Figure (c) shows the general recursion tree and the cost summations per level.
2
The recursion tree for this equation is only a minor change from that of Example (a) and is shown
in Figure 2. The number of nodes at level k remain 3
k
, and the size of the problem at level k is
n/2
k
. The sum of the cost of the sub-problems at level k is 3
k
(n/2
k
). Assuming n to be a power
of 2, we have,
T(n) =
log(n)
k=0
3
k
(n/2
k
) = n(3/2)
log(n)
log(n)
l=0
(2/3)
l
n(3/2)
log(n)
l0
(2/3)
l
= (3)(3/2)
log n
= 3n
log
2
(3/2)
.
We have used another common trick here. Recognizing that the terms (3/2)
k
are increasing geo-
metrically with k, we change the order of summation to k down from log(n) to 0, or, equivalently,
change the variable in the discrete summation from k to l = log(n) k. This makes it a geomet-
rically decaying series, and our earlier summation technique now works. The last step is obtained
as follows:
(3/2)
log(n)
= 2
(log
2
(3/2))(log(n))
=
2
log(n)
log
2
(3/2)
= n
log
2
(3/2)
since, 3/2 = 2
log
2
(3/2)
and 2
log(n)
= n, by denition of logarithm.
Example (c). So far, our recurrence equations were uniform, that is they partitioned the problem
into equal sized pieces. Let us now consider a simple example of a non-uniform partition.
T(n) = T(n/3) +T(2n/3) +cn
The recursion tree for this recurrence is shown in Figure 3. The recursion tree is dierent from
the previous ones since the recursion partitions the problem of size n into 2 problems of dierent
sizes, one n/3 and another 2n/3. The number of levels of recursion is l where, (2/3)
l
n = 1, or,
l = log
3/2
(n). Let us sum the costs of the node at each level k. If all the nodes at level k exist,
then the sum is cn as shown in Figure 3. However if all the nodes at level k do no exist, and this
happens for all values of k where, (1/3)
k
n < 1, or, k > log
3
(n), then the sum is bounded above
by cn. Thus the total sum over all the levels 0 . . . l is at most (l + 1)cn = O(nlog n). [Note that
l = log
3/2
(n) = log
2
(n)/ log
2
(3/2).]
Is this estimate a good estimate? To see this, note that for k log
3
(n), the sum of the costs of the
nodes is (log
3
(n) + 1)cn and this is a lower bound on T(n). Thus, T(n) (1 + log
3
(n))cn. Since,
log
3
(n) = log
2
(n)/ log
2
3, we have, T(n) = (nlog(n)). Combining, we have T(n) = (nlog(n)).
Summary. Recursion-tree is a good way to try and understand how a recurrence unfolds and to
get a good guess at a solution that can then be proved using the substitution method.
3
Figure 2: Illustrating the recursion tree method for solving the recurrence equation T(n) =
3T(n/2) + n. Figure (a) shows a two-level unrolling and Figure (b) shows the general recursion
tree and the cost summations per level.
4
Figure 3: Recursion tree for the recurrence T(n) = T(n/3) +T(2n/3) +cn illustrating non-uniform
partition of the problem
5