1 C 3 C 9 C _ _ _ C 3n1 D
nX1
iD0
3i D
1 3n
13
D
3n 1
2
(9.10)
If the terms in a geometric sum grow smaller, as in Equation 9.6, then the sum is
said to be geometrically decreasing. If the terms in a geometric sum grow
progressively
larger, as in Equation
which is, indeed, the case.
Equation 9.22 can be verified by induction. The base case when n D 1 is true
since we know that S1 D 1=2. The inductive step follows from Equation 9.21.
So we now know the maximum possible spread and hence the maximum possible
Definition 7.8.1. A chain is a sequence a1 _ a2 _ _ _ _ _ at , where ai aj for all
i j , such that each item is comparable to the next in the chain, and it is smaller
with respect to _. The length of the chain is t , the number of elements in the chain.
T
3
4
5
6
7
Figure 10.2 The 7-step solution to the Towers of Hanoi problem when there are
n D 3 disks.
1
2
3
Figure 10.3 A recursive solution to the Towers of Hanoi problem.
mcs-ftl 2010/9/8 0:40 page 286 #292
286 Chapter 10 Recurrences
Stage 1. Move the to
the plug-and-chug procedure.
10.3 Linear Recurrences
So far weve solved recurrences with two techniques: guess-and-verify and plugandchug. These methods require spotting a pattern in a sequence of numbers or
expressions. In this section and the next, well
iD0
xi D
1
1x
:
Proof.
1X
iD0
xi WWD
n!1
nX1
iD0
xi
lim
D lim
n!1
1 xn
1x
(by Equation 9.3)
D
1
1x
:
The final line follows from that fact that limn!1 xn D 0 when jxj < 1. _
In our annuity problem, x D 1=.1 C p/ < 1, so Theorem 9.1.1 applies, and we
get
V
25
12
:
There is good news and bad news about Harmonic numbers. The bad news is that
there is no closed-form expression known for the Harmonic numbers. The good
news is that we can use Theorem 9.3.1 to get close upper and lower bounds on Hn.
In particular
or product symbols or otherwise need those handy (but sometimes troublesome)
dots. . . . Expressions in closed form are usually easier to evaluate (it doesnt get
much simpler than 2N 1, for example) and it is usually easier to get a feel for
their magnitu
: (9.14)
It turns out that there is a way to derive these expressions, but before we explain
it, we thought it would be fun4 to show you how Gauss proved Equation 9.13 when
he was a young boy.5
Gausss idea is related to the perturbation method we used in
D 3=4:
Both cases give the same spread, albeit by different approaches. For example, see
Figure 9.10.
That was easy enough. What about S3?
S3 D max
_
1
1
6
;
3
4
C
1
6
_
D max
_
5
6
;
11
12
_
D
11
12
:
As we can see, the method provided by Case 2 is the b
chug. These methods are applicable to every recurrence, but their success requires
a flash of insightsometimes an unrealistically brilliant flash. So well also
introduce two big classes of recurrences, linear and divide-and-conquer, that often
come up in
case, the spread is maximized by having the center of mass of the top n 1 blocks
be directly over the left edge of the bottom block.
mcs-ftl 2010/9/8 0:40 page 263 #269
9.4. Hanging Out Over the Edge 263
n1
blocks
1=2 11=2n
table
Figure 9.9 A method for a
table
center of mass
of block
Figure 9.6 One block can overhang half a block length.
In general, the overhang of a stack of blocks is maximized by sliding the entire
stack rightward until its center of mass is at the edge of the table. The overhang
will t
Comparing the shaded regions in Figures 9.1 and 9.2, we see that S is at least
I plus the area of the leftmost rectangle. Hence,
S _ I C f .1/ (9.15)
This is the lower bound for S. We next derive the upper bound.
Figure 9.3 shows the curve of f .x/ from 1
x _0
T y, since _0
T is a topological sort of the partial order _0. And this
implies x _T y since _T contains _0
T.
Thus, .A;_T / is a topological sort of .A;_/. This shows that P.n/ implies
P.n C 1/ for all n _ 1. Therefore P.n/ is true for all n _ 1 by
: (9.11)
The left-hand side of Equation 9.11 is simply
nX1
iD0
d
dx
.xi / D
nX1
iD0
ixi1:
The right-hand side of Equation 9.11 is
nxn1.1 x/ .1/.1 xn/
.1 x/2
D
nxn1 C nxn C 1 xn
.1 x/2
D
1 nxn1 C .n 1/xn
.1 x/2 :
Hence, Equation 9.11 means that
nX1
iD0
ixi
Corollary 7.9.2 implies a famous result8 about partially ordered sets:
Lemma 7.9.3 (Dilworth). For all t > 0, every partially ordered set with n elements
must have either a chain of size greater than t or an antichain of size at least n=t .
Proof. By cont
x
.1 x/2 :
As a consequence, suppose that there is an annuity that pays im dollars at the
end of each year i forever. For example, if m D $50,000, then the payouts are
$50,000 and then $100,000 and then $150,000 and so on. It is hard to believe that
the v
2n D O.n/, we can say O.n/ D 2n. But n D O.n/, so we conclude that n D
O.n/ D 2n, and therefore n D 2n. To avoid such nonsense, we will never write
O.f / D g.
Similarly, you will often see statements like
Hn D ln.n/ C C O
_
1
n
_
or
n. D .1 C o.1/
p
2_n
_
n3=2 C
p
n
2
3
:
In other words, the sum is very close to 2
3n3=2.
Well be using Theorem 9.3.1 extensively going forward. At the end of this
chapter, we will also introduce some notation that expresses phrases like the sum
is very close to in a more preci
T .n/ D .n2/:
Little Omega
There is also a symbol called little-omega, analogous to little-oh, to denote that one
function grows strictly faster than another function.
Definition 9.7.14. For functions f; g W R ! R with f nonnegative, we say that
f .x/ D !
5
2
1
p
5
2
!n
D
1
p
5
1C
p
5
2
!nC1
1
p
5
1
p
5
2
!nC1
:
mcs-ftl 2010/9/8 0:40 page 298 #304
298 Chapter 10 Recurrences
It is easy to see why no one stumbled across this solution for almost six centuries!
All Fibonacci numbers are integers, but this expr
Similarly, the third payment is worth m=.1 C p/2, and the n-th payment is worth
only m=.1 C p/n1. The total value, V , of the annuity is equal to the sum of the
1Such
trading ultimately led to the subprime mortgage disaster in 20082009. Well talk more
abo
Proof. .x2C100xC10/=x2 D 1C100=xC10=x2 and so its limit as x approaches
infinity is 1C0C0 D 1. So in fact, x2C100xC10 _ x2, and therefore x2C100xC
10 D O.x2/. Indeed, its conversely true that x2 D O.x2 C 100x C 10/. _
Proposition 9.7.10 generalizes to an
ln.f .i /:
We can then apply our summing tools to find a closed form (or approximate closed
form) for ln.P / and then exponentiate at the end to undo the logarithm.
For example, lets see how this works for the factorial function n. We start by
taking the
mcs-ftl 2010/9/8 0:40 page 269 #275
9.5. Double Trouble 269
Definition 9.4.2. For functions f; g W R ! R, we say f is asymptotically equal to
g, in symbols,
f .x/ _ g.x/
if
lim
x!1
f .x/=g.x/ D 1:
Although it is tempting to write Hn _ ln.n/ C to indicate
Figure 9.3 The shaded area under the curve of f .x C1/ from 0 to n1 is I , the
same as the area under the curve of f .x/ from 1 to n. This curve is the same as the
curve in Figure 9.2 except that has been shifted left by 1.
Combining Equations 9.15 and 9.
mcs-ftl 2010/9/8 0:40 page 293 #299
10.2. Merge Sort 293
Step 1: Plug and Chug Until a Pattern Appears
First, we expand the recurrence equation by alternately plugging and chugging until
a pattern appears.
Tn D 2Tn=2 C n 1
D 2.2Tn=4 C n=2 1/ C .n 1/ plug