Home / Computer Science / Algorithms / Recurrence Relation (Master Theorem)

Formula & Calculator

Recurrence Relation (Master Theorem)

The master theorem is a shortcut for analysing the running time of divide-and-conquer algorithms. These algorithms solve a problem by breaking it into a smaller subproblems of size n/b, solving them recursively, and then combining the results. The recurrence T(n) = aT(n/b) + f(n) captures this pattern. The theorem gives you the time complexity directly from a, b, and f(n) without drawing out the full recursion tree. It is used to analyse merge sort, binary search, and many other algorithms.

Computer Science Algorithms Recurrence
VORZEP · ALGORITHMS & COMPLEXITY TOOLS

Master Theorem Calculator

Solve the divide-and-conquer recurrence T(n) = a·T(n/b) + f(n) instantly. Enter a, b, and f(n) = np·logkn, and the calculator determines which of the three Master Theorem cases applies and returns the asymptotic complexity.

T(n) = a · T(n/b) + f(n) · compare logba vs p
Quick examples
a — number of subproblems How many subproblems each recursive call spawns. For merge sort a = 2 (two halves); for Strassen's matrix multiplication a = 7; for binary search a = 1 (only one half is followed). Must be a positive integer.
subproblems
a = 2 means each call splits into 2 recursive subproblems.
b — input reduction factor Each subproblem is of size n/b. For merge sort and binary search b = 2 (halved). For Strassen's matrix multiplication b = 2. Must be > 1; fractional b is allowed but rarely useful.
n / b
each subproblem has size n / 2
p — exponent of n in f(n) f(n) is taken to be of the form n^p · log^k n. The exponent p can be any real number: positive, zero, or negative. For merge sort f(n) = n so p = 1; for binary search f(n) = 1 so p = 0; for Strassen f(n) = n² so p = 2.
exponent
f(n) = n^1.00 · log^0 n = n
k — exponent of log n in f(n) Optional logarithmic factor: f(n) = n^p · log^k n. Usually k = 0 (no log factor). Set k = 1 for f(n) = n^p · log n (common in extended Master Theorem cases).
log^k n
f(n) = n^1.00 · log^0 n = n
Recurrence: T(n) = a · T(n/b) + n^p · log^k n
ASYMPTOTIC COMPLEXITY
Θ(n log n)
Case 2 · log₂(2) = 1.000 · f(n) = n matches n^c
CASE 2 — BALANCED
T(n) = 2·T(n/2) + n → Θ(n log n)
Step-by-step solution
f(n) vs. nlogba
f(n) = np nlogba Result class
The three Master Theorem cases
Case 1 — Root heavy
p < logba
Recursion dominates → Θ(nlogba)
Case 2 — Balanced
p = logba
Both matter → Θ(nc logk+1 n)
Case 3 — Leaf heavy
p > logba
f(n) dominates → Θ(f(n))
Companion metrics
Recursion tree — branching, depth, and work per level
Root: cost f(n) Interior: ai nodes at level i, each costing (n/bi)p Leaves: alogbn = nlogba base cases
What the Master Theorem really measures
abranching factor
vs
bpper-level work shrinkage
→
T(n)asymptotic class

Compare a to bp, not a to p

The Master Theorem is often misremembered as "compare logba with p", which is correct but hides the intuition. What you are really comparing is the branching factor a with the per-level work shrinkage factor bp. If the recursion multiplies faster than the work per call shrinks (a > bp), the leaves dominate. If it shrinks faster (a < bp), the root dominates. When they match (a = bp), every level contributes equally and you get an extra log factor.

The recursion tree is the proof

Level i of the recursion tree has ai nodes, each processing a subproblem of size n/bi, so each contributes (n/bi)p = np/bip. Total work at level i is ai · np/bip = np · (a/bp)i. Summing over i gives a geometric series whose behaviour is entirely determined by the ratio a/bp. This is why the Master Theorem works — it is not a magic formula but the closed form of a geometric sum.

Case 3 has a regularity condition

Case 3 requires a regularity condition: a·f(n/b) ≤ c·f(n) for some constant c < 1 and all sufficiently large n. For f(n) = np, this holds automatically whenever p > logba. The condition rules out pathological f(n) that oscillate or fail to shrink when applied recursively. In practice, for polynomial f(n) the condition is always satisfied and you can apply Case 3 without worry.

When the Master Theorem doesn't apply

The Master Theorem fails for recurrences where the subproblems have different sizes (like T(n) = T(n/3) + T(2n/3) + n), where a is not constant, where f(n) is not polynomially bounded (like f(n) = 2n), or where the gap between p and logba is not polynomial (like f(n) = n log n vs n). For those, use the Akra–Bazzi method or the substitution method. This calculator handles the standard polynomial-times-log form, which covers the overwhelming majority of textbook recurrences.

How to Use This Calculator

Enter the four parameters of your divide-and-conquer recurrence and the calculator applies the Master Theorem to find the asymptotic complexity.

1. Enter a and b

a is the number of subproblems (must be a positive integer). b is the divisor on the input size (must be > 1). For T(n) = 2T(n/2) + n, a = 2 and b = 2.

2. Enter p and k for f(n)

Express the non-recursive part as f(n) = np · logk n. For f(n) = n, p = 1, k = 0. For f(n) = n² log n, p = 2, k = 1. For f(n) = 1, p = 0, k = 0.

3. Read the case and result

The result card shows which of the three Master Theorem cases applies (root heavy, balanced, or leaf heavy) and the resulting Θ() complexity.

4. Use the preset examples

Click a preset to load a classic recurrence: merge sort, binary search, Strassen's algorithm, Karatsuba, and others. Useful as a quick sanity check for the tool itself.

5. Check the recursion tree

The tree diagram shows the branching factor a and the depth logbn. When the leaves dominate you see the tree widening; when the root dominates you see the work concentrating at the top.

6. Verify with the comparison chart

The first chart overlays np and nlogba on a log-log plot. Whichever curve is on top tells you which term dominates — and therefore which case applies.

What This Tool Offers

Built around the Master Theorem for divide-and-conquer recurrences — the standard tool for analysing any algorithm that recursively splits its input.

Instant case detection

Automatically identifies Case 1, 2, or 3 from the comparison of logba with p, with a numerical tolerance that handles fractional exponents cleanly.

Full f(n) form

Handles f(n) = np · logk n for any real p and any non-negative integer k, including the extended Case 2 with logarithmic factors.

Recursion tree diagram

A visual of the branching and depth of the recursion, showing why the root, the leaves, or every level contributes the dominant cost.

Side-by-side comparison chart

Plots f(n) and nlogba on the same log-log axes so you can see exactly which one grows faster.

Preset recurrences

Merge sort, binary search, Strassen, Karatsuba, and other textbook algorithms load instantly with a single click.

Reference tables

Common recurrences and their solutions, the three cases side by side, and a variable reference with the formal conditions.

Reference Tables

Common recurrences and their solutions, plus a compact reference to the three Master Theorem cases.

Algorithm / Recurrenceabf(n)logbaCaseResult
Merge sort22n1.0002Θ(n log n)
Binary search1210.0002Θ(log n)
Strassen (matrix mult.)72n²2.8071Θ(n2.807)
Karatsuba (integer mult.)32n1.5851Θ(n1.585)
Standard matrix mult.82n²3.0001Θ(n³)
Linear recursion (find max)12n0.0003Θ(n)
Recursive power1210.0002Θ(log n)
Closest pair (brute halving)22n log n1.0002Θ(n log² n)
CaseConditionDominant levelResult
Case 1p < logbaLeavesΘ(nlogba)
Case 2 (k = 0)p = logbaEvery level equallyΘ(nlogba · log n)
Case 2 (k > 0)p = logbaEvery level equallyΘ(nlogba · logk+1 n)
Case 3p > logbaRootΘ(np · logk n)

What Does the Master Theorem Measure?

The Master Theorem gives the asymptotic solution to a divide-and-conquer recurrence of the form T(n) = a · T(n/b) + f(n), where a ≥ 1 is the number of recursive subproblems, b > 1 is the factor by which the input size shrinks, and f(n) is the non-recursive cost of splitting the problem and combining the results. It is the single most useful tool in algorithm analysis, because almost every classic algorithm — sorting, searching, matrix multiplication, fast Fourier transform, closest pair — has a recurrence of this exact shape.

The Master Theorem

Let c = logba. Then the recurrence T(n) = a·T(n/b) + f(n) has the following solution:

Case 1: if f(n) = O(nc−ε) for some ε > 0, then T(n) = Θ(nc)
Case 2: if f(n) = Θ(nc · logk n), then T(n) = Θ(nc · logk+1 n)
Case 3: if f(n) = Ω(nc+ε) for some ε > 0, and a·f(n/b) ≤ c'·f(n) for some c' < 1, then T(n) = Θ(f(n))

When f(n) has the form np · logk n (which covers almost every practical case), the conditions simplify dramatically:

p < logba → Case 1: T(n) = Θ(nlogba)
p = logba → Case 2: T(n) = Θ(np · logk+1 n)
p > logba → Case 3: T(n) = Θ(np · logk n)

This is exactly what the calculator implements. The only comparison that matters is p vs. logba.

The Recursion Tree Proof

The Master Theorem is not magic — it falls out of a simple geometric series. Level i of the recursion tree has ai nodes, and each node processes a subproblem of size n/bi. If the cost at a node of size m is mp, then the cost at level i is:

Level i cost = ai · (n/bi)p = np · (a / bp)i

The ratio a/bp is the per-level growth factor. If it is less than 1, the level costs form a decreasing geometric series and the total is dominated by level 0 — the root. If it equals 1, every level costs the same and there are logbn levels, so the total is np · logbn. If it is greater than 1, the series increases and the total is dominated by the last level — the leaves.

Comparing a/bp with 1 is the same as comparing logba with p, because a = blogba. This is the equivalence the calculator checks.

Master Theorem Variables

SymbolMeaningTypical values
aNumber of recursive subproblems1, 2, 3, 7, 8
bInput reduction factor (n/b per subproblem)2, 3, 4, 10
pExponent of n in f(n) = np · logk n0, 1, 2, 3
kLog factor exponent in f(n)0 (usually), 1, 2
c = logbaCritical exponent — the tiebreaker0, 1, 1.585, 2.807, 3
f(n)Non-recursive cost of split + combine1, n, n², n log n

Worked Examples

Merge sort: T(n) = 2·T(n/2) + n
a = 2, b = 2, p = 1, k = 0
log₂(2) = 1 → p = c → Case 2
T(n) = Θ(n1 · log1 n) = Θ(n log n)
Strassen: T(n) = 7·T(n/2) + n²
a = 7, b = 2, p = 2, k = 0
log₂(7) = 2.807 → p < c → Case 1
T(n) = Θ(n2.807) = Θ(n2.807)
Linear recursion (find maximum): T(n) = T(n/2) + n
a = 1, b = 2, p = 1, k = 0
log₂(1) = 0 → p > c → Case 3
T(n) = Θ(n1) = Θ(n)
Karatsuba: T(n) = 3·T(n/2) + n
a = 3, b = 2, p = 1, k = 0
log₂(3) = 1.585 → p < c → Case 1
T(n) = Θ(n1.585) = Θ(n1.585)

Frequently Asked Questions

Because the per-level work in the recursion tree depends on both a and b. At level i, there are ai nodes each processing a subproblem of size n/bi. The total work at that level is ai · (n/bi)p = np · (a/bp)i. The ratio a/bp determines whether the series grows or shrinks, and a/bp < 1 is the same as a < bp, which is the same as logba < p. So logba and p are the natural quantities to compare.

In exact mathematics, Case 2 requires p = logba precisely. If p differs by any positive amount ε (no matter how small), the recurrence falls into Case 1 or Case 3. In practice, with floating-point inputs, the calculator uses a small tolerance (1e-9) to decide equality. If you are working with a fractional exponent that should mathematically equal logba — say, T(n) = 4·T(n/2) + n² where log₂(4) = 2 exactly — the tolerance catches it. If you enter a case like p = 2.0000001 with logba = 2, it correctly reports Case 3.

Yes — set p = 1 and k = 1. The calculator handles the extended form f(n) = np · logk n. A subtle point: the standard Master Theorem is often stated only for f(n) = np, and the log factor must be handled with the extended version. When p = logba and k > 0, the result is Θ(nc · logk+1 n) — the extra log factor comes from the additional level of summation that the log introduces. For example, T(n) = 2T(n/2) + n log n gives Θ(n log² n).

Four common cases: (1) the subproblems have unequal sizes, like T(n) = T(n/3) + T(2n/3) + n — use Akra–Bazzi instead; (2) the branching factor a is not constant; (3) f(n) is not polynomially bounded, like f(n) = 2n or f(n) = n!; (4) the gap between p and logba is not polynomial — for example, T(n) = 2T(n/2) + n log n technically has p = 1 and c = 1, but the standard theorem still handles it via the extended form with k = 1. When the theorem does not apply, fall back to the recursion-tree method, the substitution method, or the Akra–Bazzi formula.

Because when a = bp, the per-level work ai · (n/bi)p simplifies to np at every level. There is no geometric decay to make one level dominate, so every level contributes equally. Since there are logbn levels and each contributes np work, the total is np · logbn = Θ(np log n). The base of the logarithm is absorbed by the Θ notation, since changing base only changes the log by a constant factor.

Case 3 requires a·f(n/b) ≤ c·f(n) for some constant c < 1 and all sufficiently large n. This condition ensures that the work at deeper levels actually shrinks, so the sum of the series is dominated by the root. For f(n) = np, the condition is a·(n/b)p ≤ c·np, which simplifies to a/bp ≤ c. Since we are already in Case 3, p > logba means a/bp < 1, so we can always choose c = a/bp + ε < 1. The regularity condition is therefore automatically satisfied for polynomial f(n), which is why most textbooks do not mention it. It matters only for exotic f(n) functions that oscillate.

Calculating...

Variables

Symbol Quantity Unit
a Number of subproblems
b Factor by which input size is divided
f(n) Cost of dividing and combining

What it means

The master theorem is a mathematical tool for analyzing the time complexity of divide‑and‑conquer algorithms. Given a recurrence of the form T(n) = a T(n/b) + f(n), where a ≥ 1, b > 1, and f(n) is the cost of dividing and combining, the theorem provides three cases to determine the asymptotic bound. Case 1: if f(n) = O(n^(log_b a - ε)) for some ε>0, then T(n) = Θ(n^(log_b a)). Case 2: if f(n) = Θ(n^(log_b a) log^k n), then T(n) = Θ(n^(log_b a) log^(k+1) n). Case 3: if f(n) = Ω(n^(log_b a + ε)) and the regularity condition holds, then T(n) = Θ(f(n)). This theorem is essential for quickly deriving complexities of algorithms like merge sort (T(n)=2T(n/2)+n → Θ(n log n)), binary search (T(n)=T(n/2)+1 → Θ(log n)), and Strassen’s matrix multiplication. It simplifies the analysis, saving time over iterative expansion. The master theorem is a staple in algorithm analysis and is typically taught in computer science curricula.

Worked example

Master Theorem – Two Examples

Real‑World
Scenario 1 – Recursive Algorithm: A recursive algorithm divides the problem into 2 subproblems of half size and does O(n) work to combine. Recurrence: T(n) = 2T(n/2) + n.
ParameterValue
a2
b2
f(n)n
logbalog₂2 = 1
1Compare f(n) = n with nlogba = n¹
2Case 2 applies: f(n) = Θ(nlogba)
3Solution: T(n) = Θ(n log n)
Result T(n) = Θ(n log n) ✓ Merge sort
Scenario 2 – Divide by 2, 4 Subproblems: Recurrence: T(n) = 4T(n/2) + n. Find the complexity.
ParameterValue
a4
b2
f(n)n
logbalog₂4 = 2
1nlogba = n²
2f(n) = n = O(n²) with ε = 1
3Case 1 applies: T(n) = Θ(n²)
Result T(n) = Θ(n²) ✓ Polynomial time
Key insight: Master theorem provides a quick way to determine the complexity of divide‑and‑conquer recurrences.

Common mistakes

  • Applying to non‑divide‑and‑conquer recurrences: The master theorem only applies to recurrences of the form T(n)=aT(n/b)+f(n) with constant a≥1, b>1.
  • Case selection: Identify which of the three cases applies based on f(n) relative to n^(log_b a) – mis‑classification leads to wrong Θ.
  • Polynomial vs. non‑polynomial: If f(n) is not polynomially comparable, the theorem does not apply; use other methods (e.g., recursion tree).
  • Base case ignored: The theorem gives asymptotic bounds; the base case affects only constants.
  • Regularity condition: For case 3, check that a·f(n/b) ≤ c·f(n) for some c<1 and large n – otherwise the theorem fails.

Applications

The master theorem provides a systematic way to solve recurrence relations of the form T(n) = aT(n/b) + f(n), which arise from divide‑and‑conquer algorithms. It gives asymptotic bounds (Θ, O, Ω) for algorithms like merge sort, binary search, and matrix multiplication. This theorem is a cornerstone of algorithm analysis, enabling engineers to quickly determine the efficiency of recursive algorithms without deriving the full solution. Its applications extend to designing efficient algorithms for problems such as maximum subarray, closest pair of points, and fast Fourier transforms. In practice, understanding the master theorem helps in comparing algorithm alternatives, optimising parameters (e.g., choosing the base case size), and predicting performance on large inputs. It is an essential tool for any computer scientist or software engineer working on complex computational problems.

  • Analysis of divide‑and‑conquer algorithms (merge sort, quick sort)
  • Algorithm design and optimisation
  • Comparison of recursive strategies
  • Theoretical computer science and complexity classes
  • Performance prediction for large‑scale data processing