Recursion Tree Method
Recursion Tree Method
Recursion Tree
Concepts
• Serves as a straight forward to making a good guess for many
problems
• Each node represents the cost of a single sub-problem in the set of
recursive function invocations
• Cost of each sub-problem is summed to get cost-per level
• Cost of each level is summed to get the total cost of recursion
• While using recursion tree to generate a good guess minor variations
in the boundary conditions are tolerable
Example
𝑇 𝑛 = 3𝑇 𝑛/4 + 𝜃(𝑛2 )
𝑇 𝑛 = 3𝑇 𝑛/4 + 𝑐(𝑛2 )
𝑛 2𝑛
𝑇 𝑛 =𝑇 +𝑇 + 𝑐𝑛
3 3
𝑛 𝑛 𝑛
𝑛
𝑐( + 2)
𝑐( + 2)
2
𝑐( + 2)
2
𝑐( + 2)
2
𝑛
2 𝑐(4. + 4.2)
2
𝑛 𝑛 𝑛
𝑐( + 2) 𝑐( + 2) 𝑛
4 4 𝑐( + 2) 𝑐( + 2) 𝑛 𝑛 𝑛
4 4 𝑐( + 2) 𝑐( + 2) 𝑛 𝑛 …
4 4 𝑐( + 2)
4
𝑐( + 2)
4 𝑐(42 . + 42 . 2)
22
𝑙𝑔 𝑛
Analysis
𝑛
• Height of tree ⇒ = 1 ⇒ 𝑘 = log 2 𝑛
2𝑘
𝑛
• Total cost at ith level ⇒ 𝑐(4𝑖 × + 4𝑖 × 2)
2𝑖
2
= 𝑐𝑛 + 𝑐𝑛 2𝑙𝑔𝑛−1 − 1 + 𝑐 4𝑙𝑔𝑛−1 + 𝜃 𝑛2 − 2
3
𝑛 2 𝑛2
= 𝑐𝑛 − 1 + 𝑐 + 𝜃 𝑛2 + 𝑐𝑛 − 2
2 3 4
1
= 𝑐𝑛2 − cn + c 𝑛2 + 𝜃 𝑛2 − 2
6
⇒ 𝑂(𝑛2 )
Verification using Substitution Method
• For this and all such problems, we will use the method of subtracting
a lower order term from the equation for proving the bound
• Suppose, we guess the solution to be 𝑂 𝑛2
• Then, we have to prove that 𝑇 𝑛 ≤ 𝑐. 𝑛2 − 6n
• Let us assume that the bound holds for m = n/2+2
• Then,
𝑛 𝑛2 𝑛
𝑇 +2 ≤𝑐 + 4 + 2n − 6 × +2
2 4 2
𝑛2
⇒ 𝑇 𝑛 ≤ 4. 𝑐 + 4 + 2n − 3𝑛 − 12 + 𝑛
4
= 𝑐𝑛2 + 1 − 4𝑐 𝑛 − 32𝑐
7
1 − 4𝑐 ≤ −6 ⇒ 𝑐 ≥
4
Example 2
𝑛
𝑇 𝑛 = 2𝑇 + 17 + 𝑛
2
𝑖 𝑛
• Total Cost at the ith level ⇒ 2 . 𝑐 + 17
2𝑖
𝑛
𝑛
𝑐( + 17)
𝑐( + 17)
2
𝑛
2 𝑐(2. + 2.17)
2
𝑛
𝑛
𝑐( + 17)
𝑛
𝑛 𝑛 𝑐(22 . + 22 . 17)
4
𝑐( + 17)
4 𝑐( + 17) 𝑐( + 17) 22
4 4
𝑙𝑔 𝑛
log2 𝑛−1
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 2𝑖 × 17 − 17 + 𝜃 𝑛
𝑖=0
𝑐 𝑛
𝑇 𝑛 = 𝑐𝑛 + 𝑛𝑙𝑜𝑔𝑛 + 17 − 1 − 17 + 𝜃 𝑛
2 2
𝑇 𝑛 ≤ 𝑐𝑛𝑙𝑔𝑛