Learn to solve recurrence relations and find asymptotic complexity of decreasing and dividing functions using master theorem. See online demo.
Master theorem provides an asymptotic analysis (using Big O notation) for recurrence relations that occur in the analysis of many divide and conquer algorithms. The approach was first presented by Jon Bentley, Dorothea Haken, and James B. Saxe in 1980, where it was described as a "unifying method" for solving such recurrences. The name "master theorem" was popularized by the widely used algorithms textbook Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein.
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and let T(n) be a function over the positive numbers defined by the recurrence
T(n) = aT(n/b) + f(n) where
T(n) = aT(n/b) + f(n) where
- a ≥ 1,
- b > 1, and
- f(n) = Θ(nk logp n), where k ≥ 0 and p ≥ 0
Case 1: If logb a > k then Θ(nlogb a)
Case 2: If logb a = k then
- if p > -1 then Θ(nk logp+1 n)
- if p = -1 then Θ(nk log log n)
- if p < -1 then Θ(nk)
Case 3: If logb a < k then
- if p > 0 then Θ(nk logp n)
- if p ≤ 0 then Θ(nk)
Let a > 0 and b > 0 be constants, let f(n) be a function, and let T(n) be a function over the positive numbers defined by the recurrence
T(n) = aT(n - b) + f(n) where
T(n) = aT(n - b) + f(n) where
- a > 0,
- b > 0, and
- f(n) = Θ(nk logp n), where k ≥ 0 and p ≥ 0
Case 1: If a < 1 then Θ(f(n)) = Θ(nk logp n)
Case 2: If a = 1 then Θ(n * f(n)) = Θ(nk+1 logp n)
Case 3: If a > 1 then Θ(an/b * f(n)) = Θ(an/b nk logp n)
The code is open-sourced, licensed under the MIT license.