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(nb)+f(n)T(n) = aT\left(\frac{n}{b}\right) + f(n)

where a1a \geq 1 and b>1b > 1.

To apply the Master Theorem, we first identify the values of aa and bb from the recurrence relation. We also express f(n)f(n) in the form:

f(n)=Θ(nklogpn)f(n) = \Theta(n^k \log^p n)

Here, kk and pp are constants that describe the asymptotic behavior of f(n)f(n). Specifically:

  • kk is the exponent of nn, indicating the polynomial part of f(n)f(n).
  • pp is the exponent of logn\log n, indicating the logarithmic part of f(n)f(n).

To determine the values of kk and pp, you need to express f(n)f(n) in its asymptotic form. For example:

  • If f(n)=n3f(n) = n^3, then k=3k = 3 and p=0p = 0.
  • If f(n)=nlognf(n) = n \log n, then k=1k = 1 and p=1p = 1.
  • If f(n)=nlognf(n) = \frac{n}{\log n}, then k=1k = 1 and p=1p = -1.

We then compare two key values: logba\log_b a and kk.

Case 1: logba>k\log_b a > k

If logba\log_b a is greater than kk, the solution to the recurrence relation is:

T(n)=Θ(nlogba)T(n) = \Theta(n^{\log_b a})

For Example:

T(n)=2T(n2)+1T(n) = 2T\left(\frac{n}{2}\right) + 1

Here, a=2a = 2 and b=2b = 2. So, log22=1\log_2 2 = 1 and k=0k = 0.

Since 1>01 > 0, this falls under Case 1:

T(n)=Θ(n)T(n) = \Theta(n)

Case 2: logba=k\log_b a = k

If logba\log_b a is equal to kk, the solution depends on the value of pp.

Subcase 2a: If p>1p > -1,

T(n)=Θ(nklogp+1n)T(n) = \Theta(n^k \log^{p+1} n)

Subcase 2b: If p=1p = -1,

T(n)=Θ(nkloglogn)T(n) = \Theta(n^k \log \log n)

Subcase 2c: If p<1p < -1,

T(n)=Θ(nk)T(n) = \Theta(n^k)

For Example:

T(n)=2T(n2)+nT(n) = 2T\left(\frac{n}{2}\right) + n

Here, a=2a = 2 and b=2b = 2. So, log22=1\log_2 2 = 1 and k=1k = 1.

Since p=0p = 0, this falls under Subcase 2a:

T(n)=Θ(nlogn)T(n) = \Theta(n \log n)

Case 3: logba<k\log_b a < k

If logba\log_b a is less than kk, the solution is:

T(n)=Θ(nk)T(n) = \Theta(n^k)

For Example:

T(n)=T(n2)+n2T(n) = T\left(\frac{n}{2}\right) + n^2

Here, a=1a = 1 and b=2b = 2. So, log21=0\log_2 1 = 0 and k=2k = 2.

Since 0<20 < 2, this falls under Case 3:

T(n)=Θ(n2)T(n) = \Theta(n^2)

Additional Examples and Explanation

Let us solve a few more examples to solidify the understanding of the Master Theorem:

Example 1:

T(n)=4T(n2)+nT(n) = 4T\left(\frac{n}{2}\right) + n

Here, a=4a = 4 and b=2b = 2. So, log24=2\log_2 4 = 2 and k=1k = 1.

Since 2>12 > 1, this falls under Case 1:

T(n)=Θ(n2)T(n) = \Theta(n^2)

Example 2:

T(n)=8T(n2)+n3T(n) = 8T\left(\frac{n}{2}\right) + n^3

Here, a=8a = 8 and b=2b = 2. So, log28=3\log_2 8 = 3 and k=3k = 3.

Since 3=33 = 3, this falls under Case 2a (with p=0p = 0):

T(n)=Θ(n3logn)T(n) = \Theta(n^3 \log n)

Example 3:

T(n)=2T(n2)+nlognT(n) = 2T\left(\frac{n}{2}\right) + \frac{n}{\log n}

Here, a=2a = 2 and b=2b = 2. So, log22=1\log_2 2 = 1 and k=1k = 1.

Since p=1p = -1, this falls under Case 2b:

T(n)=Θ(nloglogn)T(n) = \Theta(n \log \log n)

Suggetested Articles