Knowing what the complexity of a program is valuable information. However figuring it out for every function one writes may be exhausting. That is why researchers seek to find generalized solutions to certain types of problems. Functions with recursion are particularly interwoven and its hard to untangle their full complexity. In this lesson we will learn how, given a specific form of recursion algorithm - known as divide and conquer algorithms, we may know its complexity without having to know their full intricacies.
This will likely be the most difficult topic in this course. This lesson requires a greater amount of mathematical maturity than the rest of the course. Try and read as much of it as you can, but don't feel discouraged if the topics aren't sticking and wish to move on to something more tangible.
When we state and solve recurrences, we will omit floor, ceiling, and boundary conditions. This is because they have negligible effect on the actual complexity of the algorithm. We might get a floor or ceiling effect when n is not a power of b. However it's been shown that including these won't actually change the boundary conditions and so to simplify the solution a little bit, we will ignore it. If you wish to learn more about it, or any other part of the master theorem, please refer to the excellent textbook Introduction to algorithms 3rd edition by Leiserson, Rivest, and Stein.
We will separate this lesson into two parts. First, a definition of the theorem and its specific cases. Second, how a recursion tree might be used to prove the master theorem.
The master theorem solves recurrences of the form below.
Where a and b are constants, T might be considered total time, n is the size of the input, and f is a function.
This recurrence defines the run time of algorithms that divides a problem of size n into a subproblems, each of size n/b. f(n) includes the cost of dividing the problem and the cost of combining the results of the subproblems.
the master theorem provides bounds on T(n) under three different scenarios.
| if | Bound on | Constraints |
|---|---|---|
| 1. | ||
| 2. | ||
| 3. |
Given the recurrence relations below, we will use the master theorem to quickly solve a tight bound on it. In the next section we will verify this using recursion trees.
We see
1.
2.
3.
This recurrence relation is actually one for mergesort. We will provide a bound on mergesort in three different ways, using the master theorem as we just did, below using a recursion tree, and third using line-by-line code in the mergesort lesson.
We've been using bounds from the master theorem without any motivation as to where they came from. In this section we will attempt to elucidate an answer.
We will start by drawing a recursion tree for
A recursion tree is a tree where each node represents the cost of a single subproblem.
We can find the total cost for a recursion relation by summing its corresponding recursion tree. This total cost may then be used to define a bound.
The root has initial cost
We can make some observations from this tree. First, the height
is
The following equation represents the total cost for this recursion tree.
The
The summation of a constant is simply a product of the constant and the upper summation term.
We may now take an upper bound of this equation; also,
Great! We reached the same conclusion as the master theorem provided us. This was just one recurrence tree, what about others? We may write a generalized recursion tree for any equation that is solved by the master theorem as below.
Summing every node except the leaves, then including the leaves gives the equation below.
The summation represents the cost of dividing the problem into subproblems and then recombining them. The cost of the leaves is the cost for the base case at the end of recursion.
The master theorem may be seen to handle three cases in a recursion tree. 1) When the work is dominated by the leaves. 2) When the work is equally distributed among the levels of the tree. 3) When the work is dominated by the root.
The last step is to find a bound for this generalized equation for each of the three scenarios. However the proof is even more rigorous than anything previous and is outside the scope of this course. If one really wishes to learn about it, please refer to chapter 4.6.1 in Introduction to algorithms 3rd edition.
Use the master theorem to provide a bound on the recurrence
relation
Use the master theorem to provide a bound on the recurrence
relation