










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











“... O Zarathustra, who you are and must become” behold you are the teacher of the eternal recurrence – that is your destiny! That you as the first must teach this doctrine – how could this great destiny not be your greatest danger and sickness too? — Friedrich Nietzsche, Also sprach Zarathustra (1885) [translated by Walter Kaufmann]
Consider the reproductive cycle of bees. Each male bee has a mother but no father; each female bee has both a mother and a father. If we examine eight generations of ancestors for a single male bee, we see the following family tree:
♀
♀ ♂
♂
♀
♀
♀ ♂
♀ ♂
♀
♀ ♂
♂
♀
♀ ♀ ♂
♀
♀ ♂
♂
♀
♀
♀ ♂
♀ ♂ ♀ ♀ ♂
♀
♀ ♂
♂
♀
♀
♀ ♂
♀ ♂
♀
♀ ♂
♂
♀
♀ ♀ ♂ ♀ ♀ ♂ ♀ ♂
We easily see that the number of ancestors in each generation is the sum of the two generations before it. For example, our male bee has three great-grandparents, two grandparents, and one parent, and 3 = 2 + 1. Thus, the number of ancestors a bee has in generation n is defined by the following recurrence: An = 1 if n = 0 or n = 1, and An = An − 1 + An − 2 for all n ≥ 2. We can easily write out the first several values of An :
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,....
These are the well-known Fibonacci numbers. As a second example, consider light entering two adjacent planes of glass:
At any meeting surface (between the two panes of glass, or between the glass and air), the light may either reflect or continue straight through (refract). For example, here is the light bouncing seven times before it leaves the glass.
In general, how many different paths can the light take if it bounces exactly n times before leaving the glass? The answer (in case you haven’t guessed) rests with the Fibonacci sequence. We can divide the set of paths with n reflections into two subsets, depending on where the first reflection happens.
Two types of reflection.
Let Pn denote the number of paths with exactly n bounces. There is exactly one way for the light to travel with no bounces—straight through—and exactly two ways for the light to travel with only one bounce—off the bottom and off the middle. For any n > 1, there are Pn − 1 paths where the light bounces off the bottom of the glass, and Pn − 2 paths where the light bounces off the middle and then off the top. So we have the following recurrence:
P 0 = 1, P 1 = 2, Pn = Pn − 1 + Pn − 2 for all n ≥ 2.
It’s the familiar Fibonacci recurrence, but with different base cases.
We have shown that several different problems can be expressed in terms of Fibonacci sequences, but we don’t yet know how to explicitly compute the n th Fibonacci number, or even (and more importantly) roughly how big it is. We can easily write a program to compute the n th Fibonacci number, but that doesn’t help us much here. What we really want is a closed form solution for the Fibonacci recurrence—an explicit algebraic formula without conditionals, loops, or recursion. In order to solve recurrences like the Fibonacci recurrence, we first need to understand operations on infinite sequences of numbers. Although these sequences are formally defined as functions of the form A : IN → IR, we will write them either as A = 〈 a 0 , a 1 , a 2 , a 3 , a 4 ,.. .〉 when we want to emphasize the entire sequence^1 , or as A = 〈 ai 〉 when we want to emphasize a generic element. For example, the Fibonacci sequence is 〈0, 1, 1, 2, 3, 5, 8, 13, 21,.. .〉. We can naturally define several sequence operators: (^1) It really doesn’t matter whether we start a sequence with a 0 or a 1 or a 5 or even a − 17. Zero is often a convenient starting point for many recursively defined sequences, so we’ll usually start there.
We can easily prove that the operator ( E − 5 ) annihilates R :
( E − 5 )〈 ri 〉 = E 〈 ri 〉 − 5 〈 ri 〉 = 〈 ri + 1 〉 − 〈 5 ri 〉 = 〈 ri + 1 − 5 ri 〉 = 〈 0 〉
Since ( E − 5 ) is an annihilator for R , we must have the closed form solution ri = r 05 i^ = 3 · 5 i^. We can easily verify this by induction, as follows:
r 0 = 3 · 50 = 3 ÿ [definition]
ri = 5 ri − 1 [definition] = 5 · ( 3 · 5 i −^1 ) [induction hypothesis] = 5 i^ · 3 ÿ [algebra]
R.2.2 Multiple operators
An operator is a function that transforms one sequence into another. Like any other function, we can apply operators one after another to the same sequence. For example, we can multiply a sequence 〈 ai 〉 by a constant d and then by a constant c , resulting in the sequence c ( d 〈 ai 〉) = 〈 c · d · ai 〉 = ( cd )〈 ai 〉. Alternatively, we may multiply the sequence by a constant c and then shift it to the left to get E ( c 〈 ai 〉) = E 〈 c · ai 〉 = 〈 c · ai + 1 〉. This is exactly the same as applying the operators in the reverse order: c ( E 〈 ai 〉) = c 〈 ai + 1 〉 = 〈 c · ai + 1 〉. We can also shift the sequence twice to the left: E ( E 〈 ai 〉) = E 〈 ai + 1 〉 = 〈 ai + 2 〉. We will write this in shorthand as E^2 〈 ai 〉. More generally, the operator E k^ shifts a sequence k steps to the left: E k 〈 ai 〉 = 〈 ai + k 〉. We now have the tools to solve a whole host of recurrence problems. For example, what annihilates C = 〈 2 i^ + 3 i^ 〉? Well, we know that ( E − 2 ) annihilates 〈 2 i^ 〉 while leaving 〈 3 i^ 〉 essentially unscathed. Similarly, ( E − 3 ) annihilates 〈 3 i^ 〉 while leaving 〈 2 i^ 〉 essentially unscathed. Thus, if we apply both operators one after the other, we see that ( E − 2 )( E − 3 ) annihilates our sequence C. In general, for any integers a 6 = b , the operator ( E − a )( E − b ) annihilates any sequence of the form 〈 c 1 ai^ + c 2 bi^ 〉 but nothing else. We will often ‘multiply out’ the operators into the shorthand notation E^2 − ( a + b ) E + ab. It is left as an exhilarating exercise to the student to verify that this shorthand actually makes sense—the operators ( E − a )( E − b ) and E^2 − ( a + b ) E + ab have the same effect on every sequence. We now know finally enough to solve the recurrence for Fibonacci numbers. Specifically, notice that the recurrence Fi = Fi − 1 + Fi − 2 is annihilated by E^2 − E − 1:
( E^2 − E − 1 )〈 Fi 〉 = E^2 〈 Fi 〉 − E 〈 Fi 〉 − 〈 Fi 〉 = 〈 Fi + 2 〉 − 〈 Fi + 1 〉 − 〈 Fi 〉 = 〈 Fi − 2 − Fi − 1 − Fi 〉 = 〈 0 〉
Factoring E^2 − E − 1 using the quadratic formula, we obtain
E^2 − E − 1 = ( E − φ )( E − φ ˆ)
where φ = ( 1 +
p 5 ) / 2 ≈ 1. 618034 is the golden ratio and φ ˆ = ( 1 −
p 5 ) / 2 = 1 − φ = − 1 /φ. Thus, the operator ( E − φ )( E − φ ˆ) annihilates the Fibonacci sequence, so Fi must have the form
Fi = cφi^ + ˆ c φ ˆ i
for some constants c and ˆ c. We call this the generic solution to the recurrence, since it doesn’t depend at all on the base cases. To compute the constants c and ˆ c , we use the base cases F 0 = 0 and F 1 = 1 to obtain a pair of linear equations:
F 0 = 0 = c + ˆ c F 1 = 1 = cφ + ˆ c φ ˆ
Solving this system of equations gives us c = 1 / ( 2 φ − 1 ) = 1 /
p 5 and ˆ c = − 1 /
p
We now have a closed-form expression for the i th Fibonacci number:
Fi =
φi^ − φ ˆ i p 5
p 5
p 5 2
å i −
p 5
p 5 2
å i
With all the square roots in this formula, it’s quite amazing that Fibonacci numbers are integers. However, if we do all the math correctly, all the square roots cancel out when i is an integer. (In fact, this is pretty easy to prove using the binomial theorem.)
R.2.3 Degenerate cases
We can’t quite solve every recurrence yet. In our above formulation of ( E − a )( E − b ), we assumed that a 6 = b. What about the operator ( E − a )( E − a ) = ( E − a )^2? It turns out that this operator annihilates sequences such as 〈 iai^ 〉:
( E − a )〈 iai^ 〉 = 〈( i + 1 ) ai +^1 − ( a ) iai^ 〉 = 〈( i + 1 ) ai +^1 − iai +^1 〉 = 〈 ai +^1 〉
( E − a )^2 〈 iai^ 〉 = ( E − a )〈 ai +^1 〉 = 〈 0 〉
More generally, the operator ( E − a ) k^ annihilates any sequence 〈 p ( i )· ai^ 〉, where p ( i ) is any polynomial in i of degree k − 1. As an example, ( E − 1 )^3 annihilates the sequence 〈 i^2 · 1 i^ 〉 = 〈 i^2 〉 = 〈 1 , 4 , 9 , 16 , 25 ,.. .〉, since p ( i ) = i^2 is a polynomial of degree n − 1 = 2. As a review, try to explain the following statements:
R.2.4 Summary
In summary, we have learned several operators that act on sequences, as well as a few ways of combining operators.
Example: Let’s show the steps required to solve the following recurrence:
r 0 = 1 r 1 = 5 r 2 = 17 ri = 7 ri − 1 − 16 ri − 2 + 12 ri − 3
We can solve these equations to get α = 1, β = 0, γ = 1. Thus, our final solution is ri = i 2 i^ + 3 i^ , which we can verify by induction.
R.3.2 Non-homogeneous Recurrences
A height balanced tree is a binary tree, where the heights of the two subtrees of the root differ by at most one, and both subtrees are also height balanced. To ground the recursive definition, the empty set is considered a height balanced tree of height −1, and a single node is a height balanced tree of height 0. Let Tn be the smallest height-balanced tree of height n —how many nodes does Tn have? Well, one of the subtrees of Tn has height n − 1 (since Tn has height n ) and the other has height either n − 1 or n − 2 (since Tn is height-balanced and as small as possible). Since both subtrees are themselves height-balanced, the two subtrees must be Tn − 1 and Tn − 2. We have just derived the following recurrence for tn , the number of nodes in the tree Tn : t − 1 = 0 [the empty set] t 0 = 1 [a single node] tn = tn − 1 + tn − 2 + 1
The final ‘+1’ is for the root of Tn. We refer to the terms in the equation involving ti ’s as the homogeneous terms and the rest as the non-homogeneous terms. (If there were no non-homogeneous terms, we would say that the recurrence itself is homogeneous.) We know that E^2 − E − 1 annihilates the homogeneous part tn = tn − 1 + tn − 2. Let us try applying this annihilator to the entire equation:
( E^2 − E − 1 )〈 ti 〉 = E^2 〈 ti 〉 − E 〈 ti 〉 − 1 〈 ti 〉 = 〈 ti + 2 〉 − 〈 ti + 1 〉 − 〈 ti 〉 = 〈 ti + 2 − ti + 1 − ti 〉 = 〈 1 〉
The leftover sequence 〈 1 , 1 , 1 ,.. .〉 is called the residue. To obtain the annihilator for the entire recurrence, we compose the annihilator for its homogeneous part with the annihilator of its residue. Since E − 1 annihilates 〈 1 〉, it follows that ( E^2 − E − 1 )( E − 1 ) annihilates 〈 tn 〉. We can factor the annihilator into
( E − φ )( E − φ ˆ)( E − 1 ),
so our annihilator rules tell us that tn = αφn^ + β φ ˆ n^ + γ
for some constants α , β , γ. We call this the generic solution to the recurrence. Different recurrences can have the same generic solution. To solve for the unknown constants, we need three equations in three unknowns. Our base cases give us two equations, and we can get a third by examining the next nontrivial case t 1 = 2:
t − 1 = 0 = αφ −^1 + β φ ˆ−^1 + γ = α/φ + β/ φ ˆ + γ t 0 = 1 = αφ^0 + β φ ˆ^0 + γ = α + β + γ t 1 = 2 = αφ^1 + β φ ˆ^1 + γ = αφ + β φ ˆ + γ
Solving these equations, we find that α =
p 5 + 2 p 5 , β =
p 5 − 2 p 5 , and γ = −1. Thus,
tn =
p 5 + 2 p 5
p 5 2
å n
p 5 − 2 p 5
p 5 2
å n − 1
Here is the general method for non-homogeneous recurrences:
R.3.3 Some more examples
In each example below, we use the base cases a 0 = 0 and a 1 = 1.
a 0 = 0 = α + β + γ a 1 = 1 = αφ + β φ ˆ + γ a 2 = 3 = αφ^2 + β φ ˆ^2 + γ
equations: a 0 = 0 = α + β + γ a 1 = 1 = αφ + β φ ˆ + γ + δ + " a 2 = 5 = αφ^2 + β φ ˆ^2 + γ + 2 δ + 4 " a 3 = 15 = αφ^3 + β φ ˆ^3 + γ + 3 δ + 9 " a 4 = 36 = αφ^4 + β φ ˆ^4 + γ + 4 δ + 16 "
functions (^1) n and lg n do not have annihilators. Our tool, as it stands, is limited to linear recurrences.
A different type of recurrence arises in the analysis of divide and conquer algorithms. Consider for example the classical mergesort algorithm.
MERGESORT( A [1 .. n ]): if ( n > 1 ) m ← b n/ 2 c MERGESORT( A [1 .. m ]) MERGESORT( A [ m + 1 .. n ]) MERGE( A [1 .. n ], m )
MERGE( A [1 .. n ], m ): i ← 1; j ← m + 1 for k ← 1 to n if j > n B [ k ] ← A [ i ]; i ← i + 1 else if i > m B [ k ] ← A [ j ]; j ← j + 1 else if A [ i ] < A [ j ] B [ k ] ← A [ i ]; i ← i + 1 else B [ k ] ← A [ j ]; j ← j + 1 for k ← 1 to n A [ k ] ← B [ k ] Let T ( n ) denote the worst-case running time of MERGESORT when the input array has size n. Since MERGE clearly runs in Θ( n ) time, the function T ( n ) satisfies the following recurrence:
T ( 1 ) = Θ( 1 ), T ( n ) = T
d n/ 2 e
b n/ 2 c
This doesn’t look anything like the recurrences that we can solve with annihilators, so we need a different solution technique.
Divide and conquer algorithms often give us running-time recurrences of the form
T ( n ) = a T ( n/b ) + f ( n ) (1)
where a and b are constants and f ( n ) is some other function. There is a simple and general technique for solving many recurrences of this form, using a recursion tree. The root of the recursion tree is a box containing the value f ( n ); the root has a children, each of which is the root of a (recursively defined) recursion tree for the function T ( n/b ). Equivalently, a recursion tree is a complete a -ary tree where each node at depth i contains the value f ( n/bi^ ). The recursion stops when we get to the base case(s) of the recurrence. Since we’re looking for asymptotic bounds, the exact base case doesn’t actually matter; let’s just assume T ( 1 ) = Θ( 1 ). I’ll also assume for simplicity that n is an integral power of b ; we’ll see how to avoid this assumption later. Now T ( n ) is just the sum of all values stored in the recursion tree. For each i , the i th level of the tree contains ai^ nodes, each with value f ( n/bi^ ). Thus,
T ( n ) =
i = 0
ai^ f ( n/bi^ ) (Σ)
where L is the depth of the recursion tree. We easily see that L = log b n , because n/bL^ = 1. The base
case f ( 1 ) = Θ( 1 ) implies that the last non-zero term in the summation is Θ( aL^ ) = Θ( a log b^ n ) = Θ( n log b^ a ). For most divide-and-conquer recurrences encountered in practice, the level-by-level sum ( ?? ) is a geometric series—consecutive terms are larger or smaller by a constant factor. In this case, only the largest term in the geometric series matters; all of the other terms are swallowed up by the Θ(·) notation.
f(n/b)
f(n) a
a
f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L) f(n/bf(n/b f(n/bL)L) f(n/bf(n/bf(n/bL)L)L) f(n/bf(n/bL)L)L)
f(n/b f(n/b²)²)
f(n/b f(n/b²)²)
f(n/b) a
f(n/b f(n/b²)²)
f(n/b f(n/b²)²)
f(n/b) a
f(n/b f(n/b²)²)
f(n/b f(n/b²)²)
f(n/b) a
f(n/b f(n/b²)²)
f(n/b f(n/b²)²)
f(n)
a ⋅ f(n/b)
a² ⋅ f(n/b²)
aL ⋅ f(n/bL)
+
+
+
+
A recursion tree for the recurrence T ( n ) = a T ( n/b ) + f ( n )
Here are a few examples of the recursion-tree technique in action.
The last step uses the fact that
i = 1 j/^2
j (^) = 2. Although this is not quite a geometric series, it is still dominated by its largest term.
p n · T (
p n ) + n In this case, we have a complete recursion tree, but the degree of the nodes is no longer constant. We have to be a bit more careful, but the same basic technique still applies. It’s not hard to see that the nodes in any level sum to n. The depth L satisfies the identity n^2
− L = 2 (we can’t get all the way down to 1 by taking square roots), so L = lg lg n and T ( n ) = Θ( n lg lg n ).
Most (but certainly not all) recurrences encountered in practice have the ‘standard form’ T ( N ) = aT ( n/b ) + f ( n ), where a and b are constants and f ( n ) is a polynomial function. For recurrences in this standard form, the solution is characterized by the following theorem.
The Master Theorem. The recurrence T ( n ) = aT ( n/b ) + f ( n ) can be solved as follows.
Proof: If f ( n ) is a constant factor larger than a f ( b/n ), then by induction, the sum is a descending geometric series. The sum of any geometric series is a constant times its largest term. In this case, the largest term is the first term f ( n ).
If f ( n ) is a constant factor smaller than a f ( b/n ), then by induction, the sum is an ascending geometric series. The sum of any geometric series is a constant times its largest term. In this case, this is the last term, which by our earlier argument is Θ( n log b^ a ). Finally, if a f ( b/n ) = f ( n ), then by induction, each of the L + 1 terms in the summation is equal to f ( n ). É
Although the Master Theorem leads to shorter solutions when it can be applied, many people (including Jeff) find it harder to remember the Master Theorem than to apply the recursion technique directly. Your mileage may vary.
R.6.1 Removing floors and ceilings Previously we gave the recurrence for mergesort as T ( n ) = 2 T ( n/ 2 ) + n , and obtained the solution T ( n ) = Θ( n log n ) using recursion trees (or the Master Theorem if you bother to remember it). This is fine is n is a power of two, but for other values values of n , this recurrence is incorrect. When n is odd, then the recurrence calls for us to sort a fractional number of elements! Worse yet, if n is not a power of 2, we will never reach the base case T ( 1 ) = 0. To get a recurrence that’s valid for all integers n , we need to carefully add ceilings and floors:
T ( n ) = T (d n/ 2 e) + T (b n/ 2 c) + n.
We have almost no hope of getting an exact solution here; the floors and ceilings will eventually kill us. So instead, let’s just try to get a tight asymptotic upper bound for T ( n ) using a technique called domain transformation. A domain transformation rewrites a function T ( n ) with a difficult recurrence as a nested function S ( f ( n )), where f ( n ) is a simple function and S () has an easier recurrence. First we overestimate the time bound, once by pretending that the two subproblem sizes are equal, and again to eliminate the ceiling:
T ( n ) ≤ 2 T
d n/ 2 e
Now we define a new function S ( n ) = T ( n + α ), where α is a unknown constant, chosen so that S ( n ) satisfies the Master-Theorem-ready recurrence S ( n ) ≤ 2 S ( n/ 2 ) + O ( n ). To figure out the correct value of α , we compare two versions of the recurrence for the function T ( n + α ):
S ( n ) ≤ 2 S ( n/ 2 ) + O ( n ) =⇒ T ( n + α ) ≤ 2 T ( n/ 2 + α ) + O ( n ) T ( n ) ≤ 2 T ( n/ 2 + 1 ) + n =⇒ T ( n + α ) ≤ 2 T (( n + α ) / 2 + 1 ) + n + α
For these two recurrences to be equal, we need n/ 2 + α = ( n + α ) / 2 + 1, which implies that α = 2. The Master Theorem now tells us that S ( n ) = O ( n log n ), so
T ( n ) = S ( n − 2 ) = O (( n − 2 ) log( n − 2 )) = O ( n log n ).
A similar argument gives a matching lower bound T ( n ) = Ω( n log n ). So T ( n ) = Θ( n log n ) after all, just as though we had ignored the floors and ceilings from the beginning! Domain transformations are useful for removing floors, ceilings, and lower order terms from the arguments of any recurrence that otherwise looks like it ought to fit either the Master Theorem or the recursion tree method. But now that we know this, we don’t need to bother grinding through the actual gory details!
It looks like T ( n ) = 2 n^ − 1 might be the right answer. Let’s check.
T ( 0 ) = 0 = 20 − 1 ÿ
T ( n ) = 2 T ( n − 1 ) + 1 = 2 ( 2 n −^1 − 1 ) + 1 [induction hypothesis] = 2 n^ − 1 ÿ [algebra]
We were right! That’s it; we’re done.
R.6.4 Fibonacci numbers
Let’s try a less trivial example: the Fibonacci numbers Fn = Fn − 1 + Fn − 2 with base cases F 0 = 0 and F 1 = 1. There is no obvious pattern (besides the obvious one) in the first several values, but we can reasonably guess that Fn is exponential in n. Let’s try to prove that Fn ≤ a · cn^ for some constants a > 0 and c > 1 and see how far we get.
Fn = Fn − 1 + Fn − 2 ≤ a · cn −^1 + a · cn −^2 ≤ a · cn^ ???
The last inequality is satisfied if cn^ ≥ cn −^1 + cn −^2 , or more simply, if c^2 − c − 1 ≥ 0. The smallest value of c that works is φ = ( 1 +
p 5 ) / 2 ≈ 1. 618034; the other root of the quadratic equation is negative, so we can ignore it. So we have most of an inductive proof that Fn ≤ a · φn^ for any constant a. All that we’re missing are the base cases, which (we can easily guess) must determine the value of the coefficient a. We quickly compute F 0 φ^0
= 0 and
φ^1
φ
so the base cases of our induction proof are correct as long as a ≥ 1 /φ. It follows that Fn ≤ φn −^1 for all n ≥ 0. What about a matching lower bound? Well, the same inductive proof implies that Fn ≥ b · φn^ for some constant b , but the only value of b that works for all n is the trivial b = 0. We could try to find some lower-order term that makes the base case non-trivial, but an easier approach is to recall that Ω() bounds only have to work for sufficiently large n. So let’s ignore the trivial base case F 0 = 0 and assume that F 2 = 1 is a base case instead. Some more calculation gives us
F 2 φ^2
φ^2
φ
Thus, the new base cases of our induction proof are correct as long as b ≤ 1 /φ^2 , which implies that Fn ≥ φn −^2 for all n ≥ 1. Putting the upper and lower bounds together, we correctly conclude that Fn = Θ( φn^ ). It is possible to get a more exact solution by speculatively refining and conforming our current bounds, but it’s not easy; you’re better off just using annihilators.
R.6.5 An ugly divide-and-conquer example
Consider the divide-and-conquer recurrence T ( n ) =
p n · T (
p n ) + n. It doesn’t fit into the form required by the Master Theorem, but it still sort of resembles the Mergesort recurrence—the total size of the subproblems at the first level of recursion is n —so let’s guess that T ( n ) = O ( n log n ), and then try to
prove that our guess is correct. Specifically, let’s conjecture that T ( n ) ≤ a n lg n for all sufficiently large n and some constant a to be determined later:
T ( n ) =
p n · T (
p n ) + n ≤
p n · a
p n lg
p n + n [induction hypothesis] = ( a/ 2 ) n lg n + n [algebra] ≤ an lg n ÿ
The last inequality assumes only that 1 ≤ ( a/ 2 ) log n ,or equivalently, that n ≥ 22 /a. In other words, the induction proof is correct if n is sufficiently large. So we were right! But before you break out the champagne, what about the multiplicative constant a? The proof worked for any constant a , no matter how small. This strongly suggests that our upper bound T ( n ) = O ( n log n ) is not tight. Indeed, if we try to prove a matching lower bound T ( n ) ≥ b n log n for sufficiently large n , we run into trouble.
T ( n ) =
p n · T (
p n ) + n ≥
p n · b
p n log
p n + n [induction hypothesis] = ( b/ 2 ) n log n + n 6 ≥ bn log n
The last inequality would be correct only if 1 > ( b/ 2 ) log n , but that inequality is false for large values of n , no matter which constant b we choose. Okay, so Θ( n log n ) is too big. How about Θ( n )? The lower bound is easy to prove directly: T ( n ) =
p n · T (
p n ) + n ≥ n ÿ
But an inductive proof of the upper bound fails.
T ( n ) =
p n · T (
p n ) + n ≤
p n · a
p n + n [induction hypothesis] = ( a + 1 ) n [algebra] 6 ≤ an
Hmmm. So what’s bigger than n and smaller than n lg n? How about n
p lg n?
T ( n ) =
p n · T (
p n ) + n ≤
p n · a
p n
p lg
p n + n [induction hypothesis] = ( a/
p 2 ) n
p lg n + n [algebra] ≤ a n
p lg n for large enough n ÿ
Okay, the upper bound checks out; how about the lower bound?
T ( n ) =
p n · T (
p n ) + n ≥
p n · b
p n
p lg
p n + n [induction hypothesis] = ( b/
p 2 ) n
p lg n + n [algebra] 6 ≥ b n
p lg n