Recurrences: describing a value by smaller cases
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Many algorithms are easiest to describe by how a big case builds on smaller ones. Write the Towers of Hanoi as a recurrence, fill its table from the base case up, spot the closed form 2ⁿ − 1 and prove it by induction. Then count merge sort's work with M(n) = 2M(n/2) + n and see why it comes to n log₂ n.
A puzzle that repeats
Towers of Hanoi: move a stack of n disks to another peg, one disk at a time, never putting a big disk on a smaller one. Call the fewest moves T(n). The table will hold T(1) to T(5).
>>> def T(n): ... if n == 1: return 1 ... return 2*T(n-1) + 1 ... >>> [T(n) for n in range(1, 6)] [1, 3, 7, 15, 31]
اقرأ الدرس خطوة بخطوة
A puzzle that repeats
Towers of Hanoi: move a stack of n disks to another peg, one disk at a time, never putting a big disk on a smaller one. Call the fewest moves T(n). The table will hold T(1) to T(5).
>>> def T(n): ... if n == 1: return 1 ... return 2*T(n-1) + 1 ... >>> [T(n) for n in range(1, 6)] [1, 3, 7, 15, 31]The rule: a recurrence
To move n disks, move the top n−1 aside, move the biggest disk, then move the n−1 back on top. So T(n) = 2T(n−1) + 1. A rule that defines a value from smaller cases of itself is a recurrence.
>>> t1 = 1 # T(1) >>> 2*t1 + 1 # T(2) = 2T(1) + 1 3The base case
The rule alone never stops: T(5) needs T(4), which needs T(3), and so on forever. A base case ends the chain. One disk takes one move, so T(1) = 1. Line 2 of the code is that base case.
>>> def T(n): ... if n == 1: return 1 # the base case ... return 2*T(n-1) + 1 ... >>> T(1), T(2) (1, 3)Unroll from the bottom
Now build upward. Feed T(1) into the rule: T(2) = 2 × 1 + 1 = 3. Feed that in: T(3) = 2 × 3 + 1 = 7. Each new value needs only the one before it.
>>> 2*1 + 1, 2*3 + 1 # T(2), T(3) (3, 7)Keep going
T(4) = 2 × 7 + 1 = 15 and T(5) = 2 × 15 + 1 = 31. The table now reads 1, 3, 7, 15, 31. This "plug and chug" works, but reaching T(64) takes 63 rounds of the rule.
>>> 2*7 + 1, 2*15 + 1 # T(4), T(5) (15, 31)Spot the pattern
Add 1 to each value: 2, 4, 8, 16, 32. Those are the powers of 2. So we guess T(n) = 2ⁿ − 1. A formula with no T on its right side is called a closed form.
>>> [t + 1 for t in [1, 3, 7, 15, 31]] [2, 4, 8, 16, 32] >>> [2**n for n in range(1, 6)] [2, 4, 8, 16, 32]Prove it: the base case
A pattern in five values is only a guess. Proof by induction has two parts. Part one, the base case: the formula gives 2¹ − 1 = 1, and T(1) = 1. They agree.
>>> 2**1 - 1 # the formula at n = 1 1Prove it: the step
Part two: assume T(n−1) = 2ⁿ⁻¹ − 1. The rule gives T(n) = 2(2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 2 + 1 = 2ⁿ − 1. Each case passes the formula to the next, so it holds for every n ≥ 1.
>>> n = 10 >>> 2*(2**(n-1) - 1) + 1 == 2**n - 1 True >>> 2**10 - 1 # T(10) without unrolling 1023Why a closed form helps
The closed form answers at once. 64 disks need 2⁶⁴ − 1, about 1.8 × 10¹⁹ moves. At one move per second that is about 585 billion years. Each extra disk doubles the work: exponential growth.
>>> 2**64 - 1 18446744073709551615 >>> (2**64 - 1) / (365.2425 * 24 * 3600) # years at 1 move/s 584554049253.8555Divide and conquer
Merge sort splits n items into two halves, sorts each half, then merges them. Count n units of work per merge: M(n) = 2M(n/2) + n. One item is already sorted, so the base case is M(1) = 0.
>>> def M(n): ... if n == 1: return 0 ... return 2*M(n//2) + n ... >>> M(1), M(2) (0, 2)Unroll by halving
Use sizes that halve cleanly: 2, 4, 8, 16. M(2) = 2 × 0 + 2 = 2, M(4) = 2 × 2 + 4 = 8, M(8) = 2 × 8 + 8 = 24, M(16) = 2 × 24 + 16 = 64.
>>> def M(n): ... if n == 1: return 0 ... return 2*M(n//2) + n ... >>> [M(n) for n in (1, 2, 4, 8, 16)] [0, 2, 8, 24, 64]Count the levels
Halving 16 reaches 1 after log₂ 16 = 4 levels, and each level does 16 work in total. So M(n) = n log₂ n: 16 × 4 = 64. That grows far slower than Hanoi's 2ⁿ − 1.
>>> import math >>> [n * int(math.log2(n)) for n in (1, 2, 4, 8, 16)] [0, 2, 8, 24, 64] >>> 16 * math.log2(16) 64.0
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A recurrence says S(1) = 2 and S(n) = S(n−1) + 3. What is S(4)?
Unroll from the base case: S(2) = 2 + 3 = 5, S(3) = 5 + 3 = 8, S(4) = 8 + 3 = 11. The closed form is S(n) = 3n − 1.
02What goes wrong if the Python function T(n) has no base case?
Without a base case T(1) calls T(0), then T(−1), and so on. Python stops it at its recursion limit with a RecursionError.
03Towers of Hanoi needs T(n) = 2ⁿ − 1 moves. How many moves for 10 disks?
2¹⁰ − 1 = 1024 − 1 = 1023. The closed form gives it at once, with no need to unroll ten times.
04In a proof by induction of T(n) = 2ⁿ − 1, what does the induction step show?
The base case shows n = 1. The step shows each case hands the truth to the next: 2(2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 1. Together they cover every n ≥ 1.
05With M(1) = 0 and M(n) = 2M(n/2) + n, what is M(32)?
M(n) = n log₂ n for powers of 2, and log₂ 32 = 5, so M(32) = 32 × 5 = 160. Check by unrolling: M(32) = 2 × M(16) + 32 = 2 × 64 + 32 = 160.
المسرد
- Recurrence
- An equation that defines each value of a sequence or function from smaller cases of itself, such as T(n) = 2T(n−1) + 1.
- Base case
- The starting value a recurrence needs, such as T(1) = 1. Without it the chain of smaller cases never ends.
- Unrolling (plug and chug)
- Working out a recurrence's values one after another, starting from the base case.
- Closed form
- A formula for the n-th value that does not refer to other values of the sequence, such as T(n) = 2ⁿ − 1.
- Proof by induction
- A proof that a statement holds for every n: show it for the first case (base case), then show that if it holds for n−1 it holds for n (induction step).
- Divide and conquer
- Solving a problem by splitting it into smaller parts of the same kind, solving those, and combining the answers. Merge sort is an example.
- Recursion
- A function that calls itself on a smaller input. A recursive function is a recurrence written as code.
الملاحظات والمصادر
- Towers of Hanoi has three pegs and n disks of different sizes, largest at the bottom. É. Lucas published it in 1883. The rule 2T(n−1) + 1 is also the fewest moves possible: before the biggest disk can move, the other n−1 disks must all sit on the third peg.
- Checking many values with a computer is evidence, not proof. Induction proves a formula for every n: the base case starts the chain, and the step shows that each case hands the truth to the next.
- Counting comparisons exactly, a merge of n items needs at most n − 1, which gives M(n) = 2M(n/2) + n − 1 and the closed form n log₂ n − n + 1. It grows at the same rate as n log₂ n.
- For sizes that are not powers of 2, the halves are unequal (n//2 rounds down in Python), so the exact counts differ a little, but the growth is still about n log₂ n.
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT 6.042J), chapter 22: Recurrences
- MIT OpenCourseWare 6.042J Mathematics for Computer Science
- Graham, Knuth & Patashnik, Concrete Mathematics, 2nd ed., section 1.1: The Tower of Hanoi
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed., sections 2.3 and 4.4
- Python tutorial: Defining functions
- Python library: math.log2
اختبار يتاح بعد الخطوة الأخيرة.