Master Theorem
In one sentence: The master theorem tells you how long a program takes when it cuts a problem into smaller pieces, solves each piece and joins the answers.
Read it out loud: “T of n equals a times T of n divided by b, plus f of n. T of n is the running time for an input of size n. The letter a is the number of smaller pieces, b is how many times smaller each piece is, and f of n is the extra work of splitting the input and joining the answers.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Recursion and divide and conquer
A recursive method calls itself on a smaller input. Divide and conquer is the plan: cut a problem into smaller pieces, solve each piece with the same method, then combine the answers. For example, to sort 8 cards you can sort two piles of 4 cards each, then merge the two sorted piles into one.
- The running time
is the running time of the algorithm on an input of size , counted in basic steps rather than seconds. The letter stands for time. So is the running time on a smaller input of size . Counting steps means the answer does not depend on one particular computer.
- Fractions like
A fraction bar means divide. An input of size cut into equal parts gives pieces of size . For and , each half has size . Dividing by again makes each piece smaller again, which is how a recursion moves toward the smallest pieces.
- Logarithms, like
A logarithm answers the question: what power must I raise the base to, to get this number? In the small number is the base, and the answer is the power of that gives . For example , because . The notation means that 1024 can be halved 10 times to reach 1.
- Powers, like
A power is repeated multiplication. means . An exponent does not have to be a whole number: equals , and an exponent of 1.585 lies between 1 and 2, so grows faster than but more slowly than .
- The symbol (theta)
The notation , read ‘theta’, says that the running time grows at this rate, up to constant factors. For example, means the time is at most, and at least, a constant times for large inputs. It is a tight description, not only an upper limit.
- Merge sort (background)
Merge sort is a sorting method that splits a list in half, sorts each half the same way, and then merges the two sorted halves into one sorted list. Merging two sorted lists with a total of items takes about steps, because each item is looked at once.
What each symbol means
| Symbol | Meaning |
|---|---|
| The running time of the algorithm on an input of size , counted in basic steps such as comparisons and additions. It is a count, so it has no unit in this formula. For merge sort on 1024 items, the count is in the tens of thousands of steps. | |
| The size of the input, a whole number that counts items, such as the number of numbers in a list. It has no unit. Typical textbook values are 8, 1024 or a million, because the size is what makes the running time grow. | |
| The number of smaller subproblems made at each step, a whole number at least 1. A value of means each problem splits into two smaller problems. It has no unit, and it counts pieces, not time. | |
| The shrink factor: each subproblem has size , so tells you how many times smaller each piece is. It is a number greater than 1. For merge sort , which means each half is half the size of the whole list. | |
| The running time of one subproblem, which has size . It is the same kind of count as , applied to a smaller input. The formula uses it again and again, which is why the formula is called a recurrence. | |
| The extra work at one level, outside the recursive calls: splitting the input and combining the answers. It is measured in the same steps as . For merge sort, merging two halves of a list of items costs steps. |
The formula piece by piece
The size of each smaller piece. If the input has items and it is cut into equal parts, each part has items. With , a list of 1024 items splits into two lists of 512 items each.
The running time to solve one smaller piece. It is the same kind of problem with a smaller input, so the same formula applies to it again. This is why the formula refers to itself, and why it is called a recurrence.
The total time for all smaller pieces together. If , there are two pieces, so the total is two times the time for one piece. Each piece takes , so pieces take times as long.
The extra work done at this level, outside the smaller pieces: splitting the input and joining the answers. For merge sort, joining two sorted halves takes about steps, so in this case.
The work at the very bottom of the recursion, measured against the input size. It comes from counting pieces: after levels there are pieces of size 1. The theorem compares this bottom work with .
In one plain sentence: the running time on an input of size equals times the running time on a piece of size , plus the extra work at this level.
What the formula is doing, and why
What goes in and what comes out. The formula is a recurrence. It writes the time for size in terms of the time for smaller sizes. You give the master theorem three things: , and . It gives back the running time in a clear form, such as . The point is to solve the equation without drawing the whole recursion.
Why picture a tree? Start with the first problem of size at the top. After one split there are problems of size . After splits there are problems of size . The splitting stops when the pieces reach size 1, after levels. Each level does some work, and the question is which part of the tree costs the most in total.
Why three cases? The theorem compares with the bottom work . If grows clearly more slowly, the bottom work dominates and . If they grow at the same rate, each of the levels costs about the same, so . If grows clearly faster, the top level dominates and . The last case also needs a mild condition on , which holds for usual functions such as .
Why does the middle case have a log factor? When every level has the same total work, the total is the work per level times the number of levels. The number of levels is , and that is where the comes from. In merge sort each level merges a total of items, and there are levels, so the total is .
When the theorem does not apply. It covers only this exact shape: every piece has the same size , the numbers and are fixed, and is a simple function of . A recurrence such as subtracts 1 each time instead of dividing the input, so it needs another method, such as drawing the tree and adding up its levels.
An everyday picture
Think of a boss who splits a project between 2 managers. Each manager splits their part between 2 teams, and the teams split their work down to single workers. To judge where the effort goes, ask whether most of it sits in the many small jobs at the bottom, in the middle levels, or in the planning and reporting at the top. The master theorem asks this same question about the recursion tree, and it answers without drawing the whole tree.
Worked example: Merge sort: T(n) = 2T(n/2) + n
Merge sort splits a list of numbers into 2 halves, sorts each half the same way, and then merges the two sorted halves in about steps. So pieces, each of size with , and the merging work is . The recurrence is .
- Find the bottom exponent. We need , which asks: what power of 2 gives 2? The answer is 1, because . So the bottom work is .
- Compare the two growth rates. The extra work is , and the bottom work is also . Both grow in a straight line, at exactly the same rate. This is the middle case, where every level of the tree costs about the same amount.
- Apply the middle case. The middle case adds a log factor, so . In this example means , which counts how many times the list can be halved.
- Count the levels for . Halving 1024 gives 512, 256, 128, 64, 32, 16, 8, 4, 2 and finally 1. That is 10 halvings. Since , we have levels.
- Find the work per level. Every level merges all the pieces on that level, and together the pieces hold all 1024 items. Merging costs about one step per item, so each level costs about 1024 steps. Over 10 levels the total is steps, which is 10,240 steps.
- Compare with the squared count. If the work grew like , the count for 1024 items would be steps. That is a little over 100 times the 10,240 steps that merge sort needs, which is why the difference matters for long lists.
Answer: Merge sort takes time. For 1024 items that is about 10,240 steps, not the 1,048,576 steps that an method would need.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Mixing up and . The number counts the smaller subproblems, and says how many times smaller each one is. Merge sort has and , so the two numbers look the same, which makes this easy to do.
- Treating as the total running time. is only the work done at one level, outside the recursive calls. The total also includes all the smaller pieces, which the part adds.
- Stopping at the middle case too early. When grows at the same rate as , multiply by as well. The answer is , not .
- Using the theorem on a recurrence that does not fit, such as . That one subtracts 1 each time instead of dividing the input by .
Where you will meet it
- Analyzing merge sort, which splits a list in half, sorts each half and merges the results.
- Binary search, where gives time, because each step throws away half of the remaining items.
- Strassen’s matrix multiplication, where gives about time. That is faster for large matrices than the usual method, which takes about steps.
- Karatsuba multiplication of large numbers, where gives about time.
Questions people ask
Does the master theorem work for every recurrence?
No. It applies only to recurrences of the form , with a whole number , a number and a simple . Other recurrences need a recursion tree or the substitution method, which checks a guessed answer with a proof.
How do I compare $f(n)$ with $n^{\log_b a}$?
First compute the exponent . For and it is , so the bottom work is about . Then compare growth rates. The extra work grows more slowly than , so the first case applies and .
Why does the middle case have a log n factor?
The recursion has levels. When each level does about the same total work, the total is that work times the number of levels. Multiplying the work per level by the number of levels gives the extra factor.
Related formulas
- Big O Notation: Big O notation is a way to say that one function never grows faster than another, once the input is big enough.
- Sum of the First n Integers: This formula gives the total of the numbers 1, 2, 3 and so on up to in one quick calculation, with no adding one number at a time.
- Geometric Series: A geometric series adds up a list where each term is the one before it times a fixed number, and this formula finds the whole sum in one step.
- Binary Search Steps: Binary search finds an item in a sorted list by checking the middle and throwing away half, and this formula counts how many checks that takes at most.