The Master Theorem¶
The Master Theorem (From Discrete mathematics)
Let \(f(n),g(n)\) be a functions that satisfies
whenever \(n=b^k\), where \(k\) is a positive integer, \(a\ge 1,b>1\) are constants, and \(c>0\) and \(d\ge0\) are real numbers.
Then
Example
The time complexity of the merge sort algorithm is \(O(n\log n)\).
proof
Let \(T(n)\) be the time required to sort \(n\) numbers using merge sort. Then it is easy to see that
By the Master Theorem, since \(\log_2 2=1\), \(T(n)=O(n\log n)\).
The Master theorem (From wikipedia and some modified notations)
Let \(T(n)\) denote the total time for the algorithm on an input size of \(n\), and let \(f(n)\) denote the amount of time taken at the top level of the recurrence, then
where \(a\ge 1\) and \(b>1\). \(a\) is the number of subproblems in the recursion, and \(b\) is the size of the subproblem. The reason \(b>1\) is that we have to make the problem become smaller to eventually hit the base case.
Let \(d:=\log_b a\). Then