Skip to main content

Master Theorem for Divide and Conquer Recurrences

Master Theorem is a popular technique to solve recurrence relations of divide and conquer algorithms. It provides a way to determine the time complexity of recursive algorithms. The theorem is used to solve recurrences of the form:

T(n)=aTnb+f(n) \boldsymbol{{T(n)} = aT \frac{n}{b} + f(n)}

where:

  • T(n)T(n) is the time complexity of the algorithm.
  • aa is the number of subproblems in the recursion.
  • nb\frac{n}{b} is the size of each subproblem.
  • f(n)f(n) is the time complexity of the work done outside the recursive calls.
  • nn is the size of the input.
  • aβ‰₯1a \geq 1 and b>1b > 1 are constants.
  • f(n)f(n) is a function that is asymptotically positive.
  • T(n)T(n) is defined on non-negative integers.
  • The recurrence relation is defined for nβ‰₯n0n \geq n_0 for some constant n0n_0.

The Master Theorem provides a way to determine the time complexity of the algorithm based on the values of a, b, and f(n).

Master Theorem Cases​

The Master Theorem has three cases based on the comparison of f(n) with n^log_b(a):

  1. Case 1: If f(n) = O(n^log_b(a - Ρ)) for some constant Ρ > 0, then T(n) = Θ(n^log_b(a)).
  2. Case 2: If f(n) = Θ(n^log_b(a)), then T(n) = Θ(n^log_b(a) * log(n)).
  3. Case 3: If f(n) = Ω(n^log_b(a + Ρ)) for some constant Ρ > 0, if a * f(n/b) <= c * f(n) for some constant c < 1 and sufficiently large n, then T(n) = Θ(f(n)).

Example​

Let's consider the recurrence relation for the Merge Sort algorithm:

T(n)=aTnb+f(n) \boldsymbol{{T(n)} = aT \frac{n}{b} + f(n)}

Here:

  • a = 2 (number of subproblems).
  • b = 2 (size of each subproblem).
  • f(n) = Θ(n) (time complexity of work done outside the recursive calls).
  • n is the size of the input.
  • n0 = 1.
  • f(n) is asymptotically positive.
  • a >= 1 and b > 1.
  • The recurrence relation is defined for n >= n0.
  • The relation is of the form T(n) = aT(n/b) + f(n).

Now, let's compare f(n) with n^log_b(a):

  • n^log_b(a) = n^log_2(2) = n.
  • f(n) = Θ(n).
  • f(n) = Θ(n) = n^log_b(a).

Since f(n) = Θ(n) = n^log_b(a), the recurrence relation falls under Case 2 of the Master Theorem. Therefore, the time complexity of the Merge Sort algorithm is T(n) = Θ(n * log(n)).

Conclusion​

The Master Theorem provides a way to determine the time complexity of divide and conquer algorithms. It simplifies the process of solving recurrence relations and helps in analyzing the time complexity of recursive algorithms. By comparing the function f(n) with n^log_b(a), the Master Theorem classifies the recurrence relation into one of the three cases and provides the time complexity of the algorithm.

Master Theorem is a powerful tool to analyze the time complexity of divide and conquer algorithms. It is widely used in the analysis of recursive algorithms to determine their time complexity.

Implementations​

 function masterTheorem(a, b, f, n) {
if (a < 1 || b < 1) {
return "Invalid input";
}

const logBaseB = Math.log(a) / Math.log(b);

if (f === n ** logBaseB) {
return `T(n) = Θ(n^${logBaseB} * log(n))`;
} else if (f < n ** logBaseB) {
return `T(n) = Θ(n^${logBaseB})`;
} else {
return "Case 3: Not covered by Master Theorem";
}
}

Live Coding Implementation​

Live Editor
function MasterTheoremExample() {
  const a = 2,
    b = 2,
    f = 2,
    n = 4;

  function MasterTheorem(a, b, f, n) {
    if (a < 1 || b < 1) {
      return `Invalid input`;
    }

    const logBaseB = Math.log(a) / Math.log(b);

    if (f === n ** logBaseB) {
      return `T(n) = Θ(n^${logBaseB} * log(n))`;
    } else if (f < n ** logBaseB) {
      return `T(n) = Θ(n^${logBaseB})`;
    } else {
      return `Case 3: Not covered by Master Theorem`;
    }
  }

  return (
    <div>
      <p>
        <b>Output:</b> {MasterTheorem(a, b, f, n)}
      </p>
    </div>
  );
}
Result
Loading...