Docsity

Solving Recurrences using Sequence Operators and Annihilators, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Algorithms Pre-lecture R: Solving Recurrences
R Solving Recurrences
“. . . 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]
Linear Recurrences (Annihilators)
R.1 Fun with Fibonacci numbers
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.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Solving Recurrences using Sequence Operators and Annihilators and more Study notes Algorithms and Programming in PDF only on Docsity!

R Solving Recurrences

“... 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]

Linear Recurrences (Annihilators)

R.1 Fun with Fibonacci numbers

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.

  • Suppose the first bounce is on the boundary between the two panes. After the bounce, the light either leaves the class immediately (so n = 1), or bounces again off the top of the upper pane. After the second bounce, if any, the path is equivalent to a path that enters from the top and bounces n − 2 times.
  • Suppose the first bounce is not at the boundary between the two panes. Then either there are no bounces at all (so n = 0) or the first bounce is off the bottom pane. After the first bounce, the path is equivalent to a path that enters from the bottom and bounces n − 1 times. Entering through the bottom pane is the same as entering through the top pane (but flipped over).

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.

R.2 Sequences, sequence operators, and annihilators

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:

  • ( E − 1 ) annihilates any constant sequence 〈 α 〉.
  • ( E − 1 )^2 annihilates any arithmetic sequence 〈 α + β i 〉.
  • ( E − 1 )^3 annihilates any quadratic sequence 〈 α + β i + γi^2 〉.
  • ( E − 3 )( E − 2 )( E − 1 ) annihilates any sequence 〈 α + β 2 i^ + γ 3 i^ 〉.
  • ( E − 3 )^2 ( E − 2 )( E − 1 ) annihilates any sequence 〈 α + β 2 i^ + γ 3 i^ + δi 3 i^ 〉.

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

  1. Write down the annihilator. Since ri + 3 − 7 ri + 2 + 16 ri + 1 − 12 ri = 0, the annihilator is E^3 − 7 E^2 + 16 E − 12.
  2. Factor the annihilator. E^3 − 7 E^2 + 16 E − 12 = ( E − 2 )^2 ( E − 3 ).
  3. Determine sequences annihilated by each factor. ( E − 2 )^2 annihilates 〈( αi + β ) 2 i^ 〉 for any constants α and β , and ( E − 3 ) annihilates 〈 γ 3 i^ 〉 for any constant γ.
  4. Combine the sequences. ( E − 2 )^2 ( E − 3 ) annihilates 〈( αi + β ) 2 i^ + γ 3 i^ 〉 for any constants α , β , γ.
  5. Solve for the constants. The base cases give us three equations in the three unknowns α , β , γ : r 0 = 1 = ( α · 0 + β ) 20 + γ · 30 = β + γ r 1 = 5 = ( α · 1 + β ) 21 + γ · 31 = 2 α + 2 β + 3 γ r 2 = 17 = ( α · 2 + β ) 22 + γ · 32 = 8 α + 4 β + 9 γ

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:

  1. Write down the homogeneous annihilator, directly from the recurrence
  2. ‘Multiply’ by the annihilator for the residue
  3. Factor the annihilator
  4. Determine what sequence each factor annihilates
  5. Add these sequences together to form the generic solution
  6. Solve for constants of the solution by using initial conditions

R.3.3 Some more examples

In each example below, we use the base cases a 0 = 0 and a 1 = 1.

  • an = an − 1 + an − 2 + 2 - The homogeneous annihilator is E^2 − E − 1. - The residue is the constant sequence 〈2, 2, 2,.. .〉, which is annihilated by E − 1. - Thus, the annihilator is ( E^2 − E − 1 )( E − 1 ). - The annihilator factors into ( E − φ )( E − φ ˆ)( E − 1 ). - Thus, the generic solution is an = αφn^ + β φ ˆ n^ + γ. - The constants α , β , γ satisfy the equations

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 "

  • an = an − 1 + an − 2 + n^2 − 2 n The homogeneous annihilator ( E^2 − E − 1 ) leaves the residue 〈( i + 2 )^2 − 2 i −^2 〉. The quadratic part of the residue is annihilated by ( E − 1 )^3 , and the exponential part is annihilated by ( E − 2 ). Thus, the annihilator for the whole recurrence is ( E^2 − E − 1 )( E − 1 )^3 ( E − 2 ), and so the generic solution is an = αφn^ + β φ ˆ n^ + γ + δn + "n^2 + η 2 i^. The constants α , β , γ , δ , " , η satisfy a system of six equations in six unknowns determined by a 0 , a 1 ,... , a 5.
  • an = an − 1 + an − 2 + φn The annihilator is ( E^2 − E − 1 )( E − φ ) = ( E − φ )^2 ( E − φ ˆ), so the generic solution is an = αφn^ + β nφn^ + γ φ ˆ n. (Other recurrence solving methods will have a “interference” problem with this equation, while the operator method does not.) Our method does not work on recurrences like an = an − 1 + (^1) n or an = an − 1 + lg n , because the

functions (^1) n and lg n do not have annihilators. Our tool, as it stands, is limited to linear recurrences.

Divide and Conquer Recurrences (Recursion Trees)

R.4 Fun with Mergesort

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

+ T

b n/ 2 c

  • Θ( n ) for all n ≥ 2.

This doesn’t look anything like the recurrences that we can solve with annihilators, so we need a different solution technique.

R.5 Recursion Trees

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 ) =

∑^ L

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.

  • Mergesort (simplified): If we ignore the floor and ceiling in our mergesort recurrence, as well as the Θ() constant, we obtain the simplified recurrence T ( n ) = 2 T ( n/ 2 ) + n , which is in exactly the right form to apply recursion trees.

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.

  • Randomized quicksort: T ( n ) = T ( 3 n/ 4 ) + T ( n/ 4 ) + n This recurrence isn’t in the standard form described earlier, but we can still solve it using recursion trees. Now modes in the same level of the recursion tree have different values, and different leaves are at different levels. However, the nodes in any complete level ( i.e. , above any of the leaves) sum to n. Moreover, every leaf in the recursion tree has depth between log 4 n and log 4 / 3 n. To derive an upper bound, we overestimate T ( n ) by ignoring the base cases and extending the tree downward to the level of the deepest leaf. Similarly, to derive a lower bound, we overestimate T ( n ) by counting only nodes in the tree up to the level of the shallowest leaf. These observations give us the upper and lower bounds n log 4 n ≤ T ( n ) ≤ n log 4 / 3 n. Since these bounds differ by only a constant factor, we have T ( n ) = Θ( n log n ).
  • Deterministic selection: T ( n ) = T ( n/ 5 ) + T ( 7 n/ 10 ) + n Again, we have a lopsided recursion tree. If we look only at complete levels of the tree, we find that the level sums form a descending geometric series T ( n ) = n + 9 n/ 10 + 81 n/ 100 + · · · , so we have a descending geometric series. We can get an upper bound by ignoring the base cases entirely and growing the tree out to infinity, and we can get a lower bound by only counting nodes in complete levels. Either way, the geometric series is dominated by its largest term, so T ( n ) = Θ( n ).
  • T ( n ) =

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 ).

  • T ( n ) = 4 p n · T ( p n ) + n We still have at most lg lg n levels, but now the nodes in level i sum to 4 i^ n. We have an increasing geometric series of level sums, so T ( n ) is dominated by the value of the deepest level: T ( n ) = Θ( 4 lg lg^ nn ) = Θ( n log^2 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.

  • If a f ( n/b ) = κ f ( n ) for some constant κ < 1, then T ( n ) = Θ( f ( n )).
  • If a f ( n/b ) = K f ( n ) for some constant K > 1, then T ( n ) = Θ( n log b^ a ).
  • If a f ( n/b ) = f ( n ), then T ( n ) = Θ( f ( n ) log b n ).
  • If none of these three cases apply, you’re on your own.

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 Domain Transformations

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

  • n ≤ 2 T ( n/ 2 + 1 ) + n.

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

F 1

φ^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