Lecture 5
Lecture 5
Lecture 5
Analysis of
Algorithm
Lecture 5: Recursion
Dr. Tauheed
Contents
Complexity of
recurrence
relation
2
Complexity of recurrence relation
• Iteration Method
• Recursion tree method
• Master Method
The recursion-tree
In a recursion tree, each node represents the cost of a single sub problem somewhere
in the set of recursive function invocations.
𝑇 ( 𝑛 )=3 . 𝑇 ( )
𝑛
4
+𝑐 𝑛
2
Example: Recursion Tree
𝑇 ( 𝑛 )=3 . 𝑇 ( )
𝑛
4
+𝑐 𝑛2
Master Theorem
The master method provides a “cookbook” method for solving recurrences of the form
, where, 1 and
1. if where
then
2. if
then
3. if
if for some c<1
then
Example: Master Theorem
Step 1:
Step 1:
Step 2: compute
Step 2: compute
Step 3: compute
Step 3: compute
Step 4: compare with
Step 4: compare with
Step 5: Choose the case: Case 1
Step 5: Choose the case: Case 2
Step 6:
Step 6:
Step 1:
Step 2: compute
𝑇 ( 𝑛 )= 𝜃 (𝑛𝑙𝑜𝑔 (𝑛))
Step 3: compute
Step 4: compare with
Step 5: Choose the case: Case 3
Step 6:
Example: Master Theorem
(a)
Answer: T(n) =
(b) Answer: T(n) =