Master Theorem and Formulas for Recurrence Relation
Hey there! Welcome to KnowledgeKnot! Don't forget to share this with your friends and revisit often. Your support motivates us to create more content in the future. Thanks for being awesome!
Master Theorem for Divide-and-Conquer Recurrence Relations
The Master Theorem is a powerful tool for analyzing the time complexity of divide-and-conquer algorithms. It provides a straightforward way to solve recurrence relations of the form:
T(n)=aT(bn)+f(n)
where a≥1 and b>1.
To apply the Master Theorem, we first identify the values of a and b from the recurrence relation. We also express f(n) in the form:
f(n)=Θ(nklogpn)
Here, k and p are constants that describe the asymptotic behavior of f(n). Specifically:
- k is the exponent of n, indicating the polynomial part of f(n).
- p is the exponent of logn, indicating the logarithmic part of f(n).
To determine the values of k and p, you need to express f(n) in its asymptotic form. For example:
- If f(n)=n3, then k=3 and p=0.
- If f(n)=nlogn, then k=1 and p=1.
- If f(n)=lognn, then k=1 and p=−1.
We then compare two key values: logba and k.
Case 1: logba>k
If logba is greater than k, the solution to the recurrence relation is:
T(n)=Θ(nlogba)
For Example:
T(n)=2T(2n)+1
Here, a=2 and b=2. So, log22=1 and k=0.
Since 1>0, this falls under Case 1:
T(n)=Θ(n)
Case 2: logba=k
If logba is equal to k, the solution depends on the value of p.
Subcase 2a: If p>−1,
T(n)=Θ(nklogp+1n)
Subcase 2b: If p=−1,
T(n)=Θ(nkloglogn)
Subcase 2c: If p<−1,
T(n)=Θ(nk)
For Example:
T(n)=2T(2n)+n
Here, a=2 and b=2. So, log22=1 and k=1.
Since p=0, this falls under Subcase 2a:
T(n)=Θ(nlogn)
Case 3: logba<k
If logba is less than k, the solution is:
T(n)=Θ(nk)
For Example:
T(n)=T(2n)+n2
Here, a=1 and b=2. So, log21=0 and k=2.
Since 0<2, this falls under Case 3:
T(n)=Θ(n2)
Additional Examples and Explanation
Let us solve a few more examples to solidify the understanding of the Master Theorem:
Example 1:
T(n)=4T(2n)+n
Here, a=4 and b=2. So, log24=2 and k=1.
Since 2>1, this falls under Case 1:
T(n)=Θ(n2)
Example 2:
T(n)=8T(2n)+n3
Here, a=8 and b=2. So, log28=3 and k=3.
Since 3=3, this falls under Case 2a (with p=0):
T(n)=Θ(n3logn)
Example 3:
T(n)=2T(2n)+lognn
Here, a=2 and b=2. So, log22=1 and k=1.
Since p=−1, this falls under Case 2b:
T(n)=Θ(nloglogn)