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

Recursion Tree Method

The document discusses using recursion trees to solve recurrence relations. It provides examples of using recursion trees to determine asymptotic bounds for recurrence relations of the form T(n) = aT(n/b) + f(n). Specifically: 1) Recursion trees divide problems into smaller subproblems at each level, and the cost is the sum of subproblem costs plus the merge cost. 2) An example shows a recursion tree can provide an O(nlogn) bound for T(n) = 3T(n/4) + n^2. 3) Exercise asks to use a recursion tree to determine the bound for T(n) = 4T(n/2

Uploaded by

Saikat Mondal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Recursion Tree Method

The document discusses using recursion trees to solve recurrence relations. It provides examples of using recursion trees to determine asymptotic bounds for recurrence relations of the form T(n) = aT(n/b) + f(n). Specifically: 1) Recursion trees divide problems into smaller subproblems at each level, and the cost is the sum of subproblem costs plus the merge cost. 2) An example shows a recursion tree can provide an O(nlogn) bound for T(n) = 3T(n/4) + n^2. 3) Exercise asks to use a recursion tree to determine the bound for T(n) = 4T(n/2

Uploaded by

Saikat Mondal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Solving Recurrence using

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 )

• Indicates division of problem into three smaller problems at each step


• Each sub-problem is 1/4th the size of original problem
• Merger step takes n2 time at each step

𝑇 𝑛 = 3𝑇 𝑛/4 + 𝑐(𝑛2 )

• The recursion tree for this problem can be designed as follows


Verification using Substitution Method
𝑛 2𝑛
𝑇 𝑛 =𝑇 +𝑇 + 𝑂(𝑛)
3 3

• Each time the problem is divided asymmetrically into sub-problems of sizes


n/3 and 2n/3
• Merger steps takes O(n) time
• The equation can be re-written as:

𝑛 2𝑛
𝑇 𝑛 =𝑇 +𝑇 + 𝑐𝑛
3 3

• We use this to draw the recursion tree


• Height of the tree: longest sequence from root

⇒ (2/3)𝑘 𝑛 = 1 ⇒ 𝑘 = log 3/2 𝑛

• Root contributes cn to cost


• If each level contributes equally, total cost is bounded by 𝑂 𝑐𝑛 log 3 𝑛 ⇒
2
O(nlgn)
• Each level however does not contribute equally
• Contribution of leaves is different
• No. of leaves = 2log3/2 𝑛 ⇒ 𝑛log3/2 2
• If each leaf contributes constant time then the bound is 𝜃(𝑛log3/2 2 ) for
leaves
• log 3/2 2 is strictly greater than 1, therefore bound is 𝜔(𝑛𝑙𝑔𝑛)
Verification using Substitution Method
Exercise
• Use a recursion tree to determine a good asymptotic upper bound on
the recurrence
𝑛
𝑇 𝑛 = 4𝑇 +2 +𝑛
2
• Use the substitution method to verify your answer.
cn
𝑐𝑛

𝑛 𝑛 𝑛
𝑛
𝑐( + 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𝑖

• Cost of last level ⇒ 4𝑘 × 𝑇 1 ⇒ 4log2 𝑛 ⇒ 𝜃(𝑛2 )


Calculating total cost
𝑛
𝑇 𝑛 = 4𝑇 + 2 + 𝑛
2
𝑙𝑔𝑛−1
𝑛
𝑇 𝑛 = 𝑐𝑛 + ෍ 𝑐 4𝑖 × 𝑖 + 4𝑖 × 2 − 2 + 𝜃(𝑛2 )
2
𝑖=0

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 − 4𝑐𝑛 − 32𝑐 + 𝑛

= 𝑐𝑛2 + 1 − 4𝑐 𝑛 − 32𝑐

7
1 − 4𝑐 ≤ −6 ⇒ 𝑐 ≥
4
Example 2
𝑛
𝑇 𝑛 = 2𝑇 + 17 + 𝑛
2

• Height of the tree ⇒ 𝑘 = log 2 𝑛

𝑖 𝑛
• Total Cost at the ith level ⇒ 2 . 𝑐 + 17
2𝑖

• Cost at the last level ⇒ 2𝑖 × 𝑇 1 = 𝜃(𝑛)


cn
𝑐𝑛

𝑛
𝑛
𝑐( + 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

𝑇 𝑛 ≤ 𝑐𝑛𝑙𝑔𝑛

You might also like