Master Theorem
Tags | CS161Complexity |
---|
Recurrence relationships and motivation
We define to be the time that a program takes to run. When dealing with divide and conquer algorithms, this can be defined as a recurrent relation, like
This is reminicient of merge sort, which divides the problem into two separate parts and performs a merging operation in the order of . But how do we find a closed-form solution for ?
The old method: the tree
We might use the tree method, which is to look at how the function varies with , the depth. This depth starts at and moves down to . We know this because every time, we divide the problem by 2. In between, layer of the tree yields problems because we double the number of problems on every layer. Note that when , we get problem, which makes sense.
We also know that for every problem on layer , the size is . This is because every time, we feed in a problem that has been cut in half. Again, note how layer behaves.
Putting this together, we get that in every layer , we get problems. There are layers, so we get a . Neat!
One different example
But do all recurrence relationships behave the same way? Well, let’s try .
Here, every layer has one problem, and the problem is . When we do a geometric summation from , we get that
So it’s . Interesting!
And another different example
So we saw that behaves differently than . What about ?
Every layer has problems and steps per problem. this yields problems per step. When we compute a summation, we get
Interesting!
The upshot
So what we’ve noticed here is that with three different constants in front of the recurrence relationship, we are able to get drastically different behavior. Can we unify this behavior under a general framework?
Master Theorem: generalizing the tree
Let’s define this:
Here, can be any constant. We recognize that the base case may not always be trivial, but it will not depend on . So, is used because it can be arbitrarily large. We are also using for the polynomial term. Bear in mind that we can use , but this means that masters theorem won’t hold in big Theta. It will only hold with big O.
Do we have a general formula for this??
The derivation
At layer , we have problems, and the problems are of size . Each of these problems take time to do. There are layers. Taken together, we have that the total runtime is
There are three cases possible
Case 1:
When , the summation becomes so the final coimpilexity is on the order of . Note that we drop the base of the log because all logs are equivalent in big Theta
Case 2:
When , it means that the summation is a geometric series. We can set a lower bound as the infinity sum
And as you can see, it is because of this leading constant that doesn’t depend on .
Intuitively, you can also understand that the summation becomes (zero exponent) and the complexity is on the order of . We are legally allowed to do this because we can actually represent the sum remainder as a scaling of that is not dependent on .
Case 3:
We can also do the summation trick, although it’s easier to disregard everything except for the last term because everything is getting larger. This yields the following:
We do the classical base-exponent swap, which gets us
Now, taken together, we have
which gets us . Again, like before, we are allowed to chop up the sum like that because we can express the sum residual as a constant scaling of .
General master theorem
Good summary
Intuition
The basic idea is that we have two things in tension. If we have a thin tree, then the tasks on the top outweigh the tasks on the bottom branch. If we have a fat tree, then the tasks on the bottom outweigh the tasks on the top. Merge sort has a complexity that balances both, which leads to steps on each layer. In other cases, however, there might not be a balance, which leads to either the first or last term taking the lead.
Pendantics
We can assume that divides evenly at every time step, which is not really true. However, it can be proven that it doesn’t really matter (to do: CLRS 4.6.2)
When does it fail?
The classical failure mode is when the subproblems are different sizes, like