The master theorem
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Recursive algorithms that split their input into equal pieces have step counts like T(n) = 2T(n/2) + n. Draw the recursion tree of a short Python function for n = 8, add up each level, and watch which levels hold the work. The master theorem turns that picture into a rule with three cases: the leaves win, it is a tie, or the root wins. The Software lessons "How fast is fast? Big O" and "Recursion" and the Maths lessons "Logarithms and exponents" and "Recurrences: describing a value by smaller cases" cover counting steps, recursive calls, log₂ n and unrolling a recurrence.
A function that splits in two
This Python function counts the steps of a divide-and-conquer algorithm. A call on size 1 costs 1 step. A bigger call makes two calls on halves, then does n steps of its own. So T(n) = 2T(n/2) + n, with T(1) = 1.
the recurrence, read off the code: n == 1 return 1 T(1) = 1 two calls cost(n // 2) × 2 2·T(n/2) own work + n n T(n) = 2T(n/2) + n like merge sort: two halves, then a merge of n items
اقرأ الدرس خطوة بخطوة
A function that splits in two
This Python function counts the steps of a divide-and-conquer algorithm. A call on size 1 costs 1 step. A bigger call makes two calls on halves, then does n steps of its own. So T(n) = 2T(n/2) + n, with T(1) = 1.
the recurrence, read off the code: n == 1 return 1 T(1) = 1 two calls cost(n // 2) × 2 2·T(n/2) own work + n n T(n) = 2T(n/2) + n like merge sort: two halves, then a merge of n itemsThe root: cost(8)
Call cost(8). Leave its two calls aside for now: on its own, the call does 8 steps, the + n. Draw it as a block 8 units wide. It is the root of a recursion tree, and level 0 costs 8 steps.
cost(8) own work: + n = 8 steps still to come: cost(4), cost(4) level 0: 1 call × 8 = 8 block width = steps the call does itselfLevel 1: two halves
The root’s two calls are cost(4) and cost(4). Each does 4 steps of its own, so each child block is half as wide. Level 1 has 2 calls: 2 × 4 = 8 steps, the same as level 0.
cost(8) → cost(4), cost(4) level 0: 1 call × 8 = 8 level 1: 2 calls × 4 = 8 twice the calls, half the work eachLevel 2: four quarters
Each cost(4) calls cost(2) twice. Level 2 has 4 calls doing 2 steps each: 4 × 2 = 8 again. Going down a level, the number of calls doubles while each call’s own work halves.
each cost(4) → cost(2), cost(2) level 0: 1 call × 8 = 8 level 1: 2 calls × 4 = 8 level 2: 4 calls × 2 = 8Level 3: the leaves
Each cost(2) calls cost(1) twice. Now n == 1, so these calls stop and return 1. They are the leaves: 8 × 1 = 8 steps. Halving 8 reaches 1 after 3 splits, because log₂ 8 = 3.
each cost(2) → cost(1), cost(1) cost(1) hits n == 1: returns 1, no more calls level 0: 1 call × 8 = 8 level 1: 2 calls × 4 = 8 level 2: 4 calls × 2 = 8 level 3: 8 calls × 1 = 8 8 → 4 → 2 → 1: 3 halvings = log₂ 8Add up the levels
Add the levels: 4 levels of 8 steps, so T(8) = 32, which is what cost(8) returns. For any power of 2 there are log₂ n + 1 levels of n steps: T(n) = n log₂ n + n = Θ(n log n).
T(8) = 8 + 8 + 8 + 8 = 32 cost(8) returns 32 in general (n a power of 2): log₂ n + 1 levels, n steps each T(n) = n log₂ n + n n = 1024: 10·1024 + 1024 = 11264 T(n) = Θ(n log n)One shape, three numbers
Many recursive algorithms share one shape: T(n) = a·T(n/b) + f(n). A call makes a calls on pieces of size n/b, plus f(n) steps of its own. Our function has a = 2, b = 2 and f(n) = n.
T(n) = a·T(n/b) + f(n) a calls each call makes 2 b how much the size shrinks 2 f(n) the call’s own steps n level i: a^i calls, each f(n / b^i)Master theorem: a tie
The master theorem compares f(n) with the number of leaves, n^(log_b a). Here that is n¹: 8 leaves, and f(n) = n grows just as fast. Every level costs the same, so T(n) = Θ(n log n).
leaves = a^(log_b n) = n^(log_b a) 2^3 = 8 = n¹ f(n) = n¹: same exponent → balanced level 0: 1 call × 8 = 8 level 1: 2 calls × 4 = 8 level 2: 4 calls × 2 = 8 level 3: 8 calls × 1 = 8 total = 32 T(n) = Θ(n^(log_b a) · log n) = Θ(n log n)Four calls: the leaves win
Make four calls instead: a = 4. Now there are 4³ = 64 leaves, that is n², which grows faster than f(n) = n. Level costs double: 8, 16, 32, 64. The leaves win: T(n) = Θ(n²).
T(n) = 4T(n/2) + n leaves = 4^3 = 64 = n^(log₂ 4) = n² f(n) = n¹, exponent 1 < 2 → leaves dominate level 0: 1 call × 8 = 8 level 1: 4 calls × 4 = 16 level 2: 16 calls × 2 = 32 level 3: 64 calls × 1 = 64 total = 120 T(n) = Θ(n^(log_b a)) = Θ(n²)Bigger own work: the root wins
Back to two calls, but now f(n) = n². Level costs halve: 64, 32, 16, 8, so the root’s own work wins: T(n) = Θ(n²). This case also needs a·f(n/b) ≤ c·f(n) for some c < 1; here c = ½.
T(n) = 2T(n/2) + n² leaves = 2^3 = 8 = n¹ f(n) = n², exponent 2 > 1 → root dominates check: 2·(n/2)² = ½·n², so c = ½ < 1 level 0: 1 call × 64 = 64 level 1: 2 calls × 16 = 32 level 2: 4 calls × 4 = 16 level 3: 8 calls × 1 = 8 total = 120 T(n) = Θ(f(n)) = Θ(n²)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In the recursion tree of cost(16) (T(n) = 2T(n/2) + n, T(1) = 1), how many levels are there and what does each cost?
16 → 8 → 4 → 2 → 1 is 4 halvings, so log₂ 16 + 1 = 5 levels. Level i has 2^i calls doing 16/2^i steps each: 16 steps per level, T(16) = 80.
02Which recurrence describes merge sort, which sorts both halves and then merges n items?
Two recursive calls (a = 2) on halves (b = 2), plus about n steps to merge (f(n) = n). That is the lesson's worked example, so merge sort takes Θ(n log n) steps.
03For T(n) = 4T(n/2) + n, which part of the recursion tree does most of the work?
There are 4^(log₂ n) = n² leaves, which grows faster than f(n) = n. Level costs double going down (8, 16, 32, 64 for n = 8), so the leaves dominate and T(n) = Θ(n²).
04Binary search makes one call on half the list and does a constant amount of work: T(n) = T(n/2) + 1. What does the master theorem give?
a = 1, b = 2, so n^(log₂ 1) = n⁰ = 1, the same growth as f(n) = 1. A tie: every level costs 1 and there are about log₂ n levels, so T(n) = Θ(log n).
05Why can't the master theorem solve T(n) = T(n − 1) + n?
The theorem needs pieces of size n/b. Here the tree is a chain of n levels costing n, n − 1, ..., 1, which adds up to n(n + 1)/2 = Θ(n²): the recursion tree still works when the theorem does not.
المسرد
- Recurrence
- An equation that gives T(n) in terms of T of smaller inputs, plus the starting value, such as T(1) = 1.
- Recursion tree
- A drawing of every call a recursive function makes: the first call at the top, the calls it makes below it, and so on down to the base case.
- Level
- One row of the recursion tree. Level i holds the calls made after i splits.
- Leaf
- A call that hits the base case and makes no more calls. In the lesson's example, the calls cost(1).
- a, b and f(n)
- In T(n) = a·T(n/b) + f(n): a is how many calls each call makes, b is how much the size shrinks, and f(n) is the work a call does itself.
- n^(log_b a)
- The number of leaves in the recursion tree, because a^(log_b n) = n^(log_b a). The master theorem compares f(n) with it.
- Master theorem
- A rule that solves T(n) = a·T(n/b) + f(n): the leaves dominate, the work is spread evenly over about log n levels, or the root dominates.
- Regularity condition
- The extra check in the root case: a·f(n/b) ≤ c·f(n) for some constant c < 1, so level costs really shrink going down.
الملاحظات والمصادر
- The rule used here is the simple form from MIT 6.006 for T(n) = a·T(n/b) + Θ(n^c): if c < log_b a the leaves dominate and T(n) = Θ(n^(log_b a)); if c = log_b a every level costs about the same and T(n) = Θ(n^c log n); if c > log_b a the root dominates and T(n) = Θ(n^c). CLRS §4.5 states the full theorem for any f(n).
- The root case also needs the regularity condition a·f(n/b) ≤ c·f(n) for some constant c < 1, so the level costs really shrink. For polynomial f(n) = n^c with c > log_b a it always holds.
- The theorem needs equal-size pieces n/b with a constant b > 1. T(n) = T(n − 1) + n shrinks by subtraction, not division, so it does not fit; draw its recursion tree instead (it is a chain of n levels, Θ(n²) in total).
- The figure uses n = 8, a power of 2, so every halving is exact and n // 2 equals n/2. For other n the pieces differ by at most 1 and the Θ answers stay the same.
- The code is real Python 3: cost(8) returns 32 and cost(1024) returns 11264. Each call really makes two smaller calls, so the recursion tree drawn is the real call tree.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Recitation 3: Recurrences and the Master Theorem
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 3: Sorting (merge sort's recursion tree)
- Sedgewick and Wayne, Algorithms 4th ed., §2.2 Mergesort
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), §4.4 and §4.5
- Python Language Reference: Binary arithmetic operations (floor division //)
اختبار يتاح بعد الخطوة الأخيرة.