The Recursion-Tree Method For Solving Recurrences
The Recursion-Tree Method For Solving Recurrences
solving recurrences
• In a recursion tree, each node represents the cost of a
single sub-problem somewhere in the set of recursive
function invocations
• We sum the costs within each level of the tree to
obtain a set of per-level costs, and then we sum all the
per-level costs to determine the total cost of all levels
of the recursion
• A recursion tree would provide a good guess for the
recurrence
• Consider the recurrence T(n) = 3T(|_n/4_|) + Ѳ(n^2)
• We start by focusing on finding an upper bound for the
solution. Because we know that floors and ceilings
usually do not matter when solving, we create a
recursion tree for the recurrence T(n) = 3T(|_n/4_|) +
Ѳ(n^2) having written out the implied constant
coefficient c > 0.
• Because sub-problem sizes decrease by a
factor of 4 each time we go down one level,
we eventually must reach a boundary
condition. How far from the root do we reach
one?
• The sub-problem size for a node at depth i is
n/4^i . Thus, the sub-problem size hits n = 1
when n/4^I = 1 or, equivalently, when i = log 4
base n
• Thus, the tree has log 4 base n + 1 levels (at
depths 0, 1, 2,….., log 4 base n)
• Next we determine the cost at each level of the tree.
• Each level has three times more nodes than the level
above, and so the number of nodes at depth i is 3^i .
• Because sub-problem sizes reduce by a factor of 4 for
each level we go down from the root, each node at
depth i, for i = 0, 1, 2, ……., log 4 base n - 1, has a cost
of c(n/4^i) ^2.
• Multiplying, we see that the total cost over all nodes at
depth i, for
• i = 0, 1, 2, ……., log 4 base n - 1, is 3^i .c(n/4^i) ^ 2 =
(3/16)^i.cn^2
• The bottom level, at depth log 4 base n, has 3 ^ (log 4
base n) = n ^ ( log 4 base 3) nodes, each contributing
cost T(1), for a total cost of n ^ (log 4 base 3) T(1),
which is Ѳ(n ^ (log 4 base 3), since we assume that T(1)
is a constant
Now we add up the costs over all levels to
determine the cost for the entire tree:
This last formula looks somewhat messy until we realize that we can
again take advantage of small amounts of sloppiness and use an
infinite decreasing geometric series as an upper bound. Backing up
one step and applying equation previous equation, we have
Assignment
T(n) = 3T(n/4)
= 3.3T(n/16)
No. of Elements
level 0 1 n = n/ (4^0)
level 1 3 n/4 = n/(4^1)
level 2 9 n/16 = n/(4^2)
level 3 27 n/64 = n/(4^3)
level 4 81 n/16 = n/(4^4)
i-th level n/(4^i)
.
.
.
d-th level n/(4^d)
n/(4^d) = 1
4^d = n
d = log 4 base n