Master ‘s Method
Prepared By: Ekta Unagar
Gyanmanjari Institute of Technology, Bhavnagar
Master’s method
• The master theorem is used in calculating the time complexity of recurrence
relations (divide and conquer algorithms) in a simple and quick way.
• The recurrence relation is of the following form:
T(n) = a * T(n/b) + f(n)
a >= 1 and b >= 1
Where, a = no of subproblems
n/b = size of subproblems
f(n) = cost/ function
14-09-2024 2
Master theorem
Three cases of master theorem:
• Case 1: if f(n) ∈ O(n E-e), for e>0
then T(n) ∈ Θ (n E)
E = logb a
• Case 2: if f(n) ∈ Θ (n E)
then T(n) ∈ Θ (f(n) * logn)
• Case 3: if f(n) ∈ Ω (n E + e)
then T(n) ∈ Θ (f(n))
14-09-2024 3
Example
• Solve the following recurrence using master theorem.
T(n) = 8 * T(n/2) + Θ(n) T(n) = a * T(n/b) + f(n)
a = 8, b = 2, f(n) = Θ(n)
E = logb a
= log2 23
= 3
• Case 1: if f(n) ∈ O(n E-e), for e>0
Θ(n) ∈ O(n 3-1)
Θ(n) ∈ O(n 2) then,
T(n) ∈ Θ (n E)
T(n) ∈ Θ (n 3)
14-09-2024 4
Example
• Solve the following recurrence using master theorem.
T(n) = 7 * T(n/3) + Θ(n 2) T(n) = a * T(n/b) + f(n)
a = 7, b = 3, f(n) = Θ(n 2)
E = logb a
= log3 7
= 1.77
• Case 1: if f(n) ∈ O(n E-e), for e>0 Case 2: if f(n) ∈ Θ (n E), for e>0 Case 3: if f(n) ∈ Ω (n E + e), for e>0
Θ(n 2) ∈ O(n 1.77-1) Θ(n 2) ∈ O(n 1.77) Θ(n 2) ∈ Ω(n 1.77 + 1)
case 2 does not match case 3 is match
Θ(n 2) ∈ O(n 0.77)
then,
case 1 does not match T(n) ∈ Θ (f(n))
T(n) ∈ Θ (n 2)
14-09-2024 5
Examples
• Solve the recurrence equations
1) T(n) = 4T(n/2) + logn
2) T(n) = 4T(n/2) + n 2
3) T(n) = 3T(n/4) + n log n
14-09-2024 6
Answers
1) T(n) = Θ(n 2)
2) T(n) = Θ(n 2 log n)
3) T(n) = Θ(n log n)
14-09-2024 7
Thank You..!!
14-09-2024 8