Master Theorem

What is the Master Theorem?

A technique to determine the time complexity (big-o) of a recursive algorithm.

Procedure

  1. Find recurrence relation
  2. Identify the appropriate case of the Master Theorem
  3. Solve for Big-o

Example: Binary Search

Recurrence Relation

Master Theorem Definition



Image taken from Data Structures and Algorithm Analysis by Weiss

Coefficients

Master Theorem "Template": \( T(n) = a T(n/b) + \theta(n^k log^p(n)) \)


Binary Search: \( T(n) = T(n/2) + \theta(1) \)


Fitting the Template

Master Theorem "Template": \( T(n) = a T(n/b) + \theta(n^k log^p(n)) \)


Binary Search: \( T(n) = T(n/2) + \theta(1) = T(n/2) + \theta(n^0 log^0(p)) \)


Determining the Alignment

Master Theorem "Template": \( T(n) = a T(n/b) + \theta(n^k log^p(n)) \)


Binary Search: \( T(n) = T(n/2) + \theta(n^0 log^0(n)) \)


Alignment: \( a=1 \), \(b = 2\), \(k = 0\), \( p=0 \)

Determining the Relationship

Alignment: \( a=1 \), \(b = 2\), \(k = 0\), \( p=0 \)

Compare: \( a \) vs \( b^k \), i.e. \( \lt \), \( \gt\), or \( = \)

\( a=1 \), \(b^k = 2^0 = 1\)

Result: \( a = b^k \)

Determining the Case

Since \(a = b^k \)

Solution

Alignment: \( a=1 \), \(b = 2\), \(k = 0\), \( p=0 \)


Case: \( T(n) = O(n^k log^{p+1}(n)) \)


Simplification: \( T(n) = O( n^0 log^{0+1}(n) ) = O(log(n)) \)

The End

The Big-O time complexity of binary search is

\( O(log(n)) \)