The Master Theorem
Learn the master theorem of the divide-and-conquer algorithm.
We'll cover the following
Divide-and-conquer algorithm
A typical divide-and-conquer algorithm solves a problem of size as follows:
Divide: Break the problem into problems of size at most and solve them recursively. We assume that and .
Conquer: Combine the answers found by recursive calls to an answer for the initial problem.
Therefore, the algorithm makes recursive calls to problems of size .
Note: does not necessarily divide , so instead of , we should usually write . Still, we write ; it simplifies the notation and does not break the analysis.
Assume that everything that is done outside of recursive calls takes time where is a non-negative constant. This includes preparing the recursive calls and combining the results.
Since this is a recursive algorithm, we also need to specify conditions under which a problem is solved directly rather than recursively (with no such base case, a recursion would never stop). This usually happens when becomes small. For simplicity, we assume that it is and that the running time of the algorithm is equal to in this case.
Therefore, by denoting the running time of the algorithm on problems of size by , we get the following recurrence relation on :
Below, we show that we can find out the growth rate of by looking at parameters , , and . Before doing this, we illustrate how this recurrence relation captures some well-known algorithms.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.