Professional Documents
Culture Documents
479
480 Short Tutorial on Recurrence Relations
defining the Fibonacci numbers—see Section 2.5), more than one value needs to
be specified by initial conditions.
To solve a given recurrence subject to a given initial condition means to find
an explicit formula for the generic term of the sequence that satisfies both the
recurrence equation and the initial condition or to prove that such a sequence does
not exist. For example, the solution to recurrence (B.1) subject to initial condition
(B.2) is
n(n + 1)
x(n) = for n ≥ 0. (B.3)
2
It can be verified by substituting this formula into (B.1) to check that the equality
holds for every n > 0, i.e., that
n(n + 1) (n − 1)(n − 1 + 1)
= +n
2 2
and into (B.2) to check that x(0) = 0, i.e., that
0(0 + 1)
= 0.
2
Sometimes it is convenient to distinguish between a general solution and
a particular solution to a recurrence. Recurrence equations typically have an
infinite number of sequences that satisfy them. A general solution to a recurrence
equation is a formula that specifies all such sequences. Typically, a general solution
involves one or more arbitrary constants. For example, for recurrence (B.1), the
general solution can be specified by the formula
n(n + 1)
x(n) = c + , (B.4)
2
where c is such an arbitrary constant. By assigning different values to c, we can
get all the solutions to equation (B.1) and only these solutions.
A particular solution is a specific sequence that satisfies a given recurrence
equation. Usually we are interested in a particular solution that satisfies a given
initial condition. For example, sequence (B.3) is a particular solution to (B.1)–
(B.2).
Method of Forward Substitutions Starting with the initial term (or terms) of the
sequence given by the initial condition(s), we can use the recurrence equation to
generate the few first terms of its solution in the hope of seeing a pattern that can be
Short Tutorial on Recurrence Relations 481
Comparing the three formulas for x(n), we can see the pattern arising after i such
substitutions:1
x(n) = x(n − i) + (n − i + 1) + (n − i + 2) + . . . + n.
Since initial condition (B.2) is specified for n = 0, we need n − i = 0, i.e., i = n, to
reach it:
x(n) = x(0) + 1 + 2 + . . . + n = 0 + 1 + 2 + . . . + n = n(n + 1)/2.
The method of backward substitutions works surprisingly well for a wide
variety of simple recurrence relations. You can find many examples of its successful
applications throughout this book (see, in particular, Section 2.4 and its exercises).
ar 2 + br + c = 0. (B.9)
Quadratic equation (B.9) is called the characteristic equation for recurrence
equation (B.8).
THEOREM 1 Let r1, r2 be two roots of characteristic equation (B.9) for recur-
rence relation (B.8).
1. Strictly speaking, the validity of the pattern’s formula needs to be proved by mathematical induction
on i. It is often easier, however, to get the solution first and then verify it (e.g., as we did for
x(n) = n(n + 1)/2).
Short Tutorial on Recurrence Relations 483
Case 1 If r1 and r2 are real and distinct, the general solution to recurrence
(B.8) is obtained by the formula
x(n) = αr1n + βr2n,
where α and β are two arbitrary real constants.
Case 2 If r1 and r2 are equal to each other, the general solution to recurrence
(B.8) is obtained by the formula
x(n) = αr n + βnr n,
where r = r1 = r2 and α and β are two arbitrary real constants.
Case 3 If r1,2 = u ± iv are two distinct complex numbers, the general solution
to recurrence (B.8) is obtained as
x(n) = γ n[α cos nθ + β sin nθ ],
where γ = u2 + v 2 , θ = arctan v/u, and α and β are two arbitrary real constants.
Case 1 of this theorem arises, in particular, in deriving the explicit formula for
the nth Fibonacci number (Section 2.5). First, we need to rewrite the recurrence
defining this sequence as
F (n) − F (n − 1) − F (n − 2) = 0.
Its characteristic equation is
r 2 − r − 1 = 0,
with the roots
√ √
1± 1 − 4(−1) 1 ± 5
r1,2 = = .
2 2
Since this characteristic equation has two distinct real roots, we have to use the
formula indicated in Case 1 of Theorem 1:
. √ /n . √ /n
1+ 5 1− 5
F (n) = α +β .
2 2
After some standard algebraic simplifications, we get the following system of two
linear equations in two unknowns α and β:
α+ β =0
. √ / . √ /
1+ 5 1− 5
α+ β = 1.
2 2
Since Theorem 1 gives a complete recipe for finding the general solution to a
homogeneous second-order linear equation with constant coefficients, Theorem 2
reduces the task of finding all solutions to equation (B.7) to finding just one
particular solution to it. For an arbitrary function f (n) in the right-hand side
of equation (B.7), it is still a difficult task with no general help available. For
Short Tutorial on Recurrence Relations 485
where function f (n) accounts for the time needed to reduce an instance to a
smaller one and to extend the solution of the smaller instance to a solution of
the larger instance. Applying backward substitutions to (B.12) yields
T (n) = T (n − 1) + f (n)
= T (n − 2) + f (n − 1) + f (n)
=...
n
= T (0) + f (j ).
j =1
For a specific function f (x), the sum nj=1 f (j ) can usually be either computed
exactly or its order of growth ascertained. For example, if f (n) = 1, nj=1 f (j ) = n;
if f (n) = log n, nj=1 f (j ) ∈ (n log n); if f (n) = nk , nj=1 f (j ) ∈ (nk+1). The
n
sum j =1 f (j ) can also be approximated by formulas involving integrals (see, in
particular, the appropriate formulas in Appendix A).
where b > 1 and function f (n) accounts for the time needed to reduce an instance
to a smaller one and to extend the solution of the smaller instance to a solution
of the larger instance. Strictly speaking, equation (B.13) is valid only for n = bk ,
k = 0, 1, . . .. For values of n that are not powers of b, there is typically some round-
off, usually involving the floor and/or ceiling functions. The standard approach to
such equations is to solve them for n = bk first. Afterward, either the solution is
tweaked to make it valid for all n’s (see, for example, Problem 7 in Exercises 2.4),
or the order of growth of the solution is established based on the smoothness rule
(Theorem 4 in this appendix).
By considering n = bk , k = 0, 1, . . . , and applying backward substitutions to
(B.13), we obtain the following:
Short Tutorial on Recurrence Relations 487
For a specific function f (x), the sum kj =1 f (bj ) can usually be either computed
exactly or its order of growth ascertained. For example, if f (n) = 1,
k
f (bj ) = k = logb n.
j =1
k
k
bk − 1 n−1
f (bj ) = bj = b =b .
j =1 j =1
b−1 b−1
Also, recurrence (B.13) is a special case of recurrence (B.14) covered by the Master
Theorem (Theorem 5 in this appendix). According to this theorem, in particular,
if f (n) ∈ (nd ) where d > 0, then T (n) ∈ (nd ) as well.
where a ≥ 1, b ≥ 2, and f (n) is a function that accounts for the time spent on
dividing the problem into smaller ones and combining their solutions. Recurrence
(B.14) is called the general divide-and-conquer recurrence.2
Applying backward substitutions to (B.14) yields the following:
Since a k = a logb n = nlogb a , we get the following formula for the solution to recur-
rence (B.14) for n = bk :
logb n
T (n) = nlogb a [T (1) + f (bj )/a j ]. (B.15)
j =1
Obviously, the order of growth of solution T (n) depends on the values of the
constants a and b and the order of growth of the function f (n). Under certain
assumptions about f (n) discussed in the next section, we can simplify formula
(B.15) and get explicit results about the order of growth of T (n).
Smoothness Rule and the Master Theorem We mentioned earlier that the time
efficiency of decrease-by-a-constant-factor and divide-and-conquer algorithms is
usually investigated first for n’s that are powers of b. (Most often b = 2, as it is
in binary search and mergesort; sometimes b = 3, as it is in the better algorithm
for the fake-coin problem of Section 4.4, but it can be any integer greater than or
equal to 2.) The question we are going to address now is when the order of growth
observed for n’s that are powers of b can be extended to all its values.
It is easy to check that functions which do not grow too fast, including log n,
n, n log n, and nα where α ≥ 0, are smooth. For example, f (n) = n log n is smooth
because
f (2n) = 2n log 2n = 2n(log 2 + log n) = (2 log 2)n + 2n log n ∈ (n log n).
Fast-growing functions, such as a n where a > 1 and n!, are not smooth. For exam-
ple, f (n) = 2n is not smooth because
f (2n) = 22n = 4n ∈ (2n).
THEOREM 3 Let f (n) be a smooth function as just defined. Then, for any fixed
integer b ≥ 2,
f (bn) ∈ (f (n)),
i.e., there exist positive constants cb and db and a nonnegative integer n0 such that
db f (n) ≤ f (bn) ≤ cb f (n) for n ≥ n0.
(The same assertion, with obvious changes, holds for the O and notations.)
PROOF We will prove the theorem for the O notation only; the proof of the
part is the same. First, it is easy to check by induction that if f (2n) ≤ c2 f (n) for
n ≥ n0, then
f (2k n) ≤ c2k f (n) for k = 1, 2, . . . and n ≥ n0.
The induction basis for k = 1 checks out trivially. For the general case, assuming
that f (2k−1n) ≤ c2k−1f (n) for n ≥ n0, we obtain
Hence, we can use c2k as a required constant for this value of b to complete the
proof.
The importance of the notions introduced above stems from the following
theorem.
where b ≥ 2, then
PROOF We will prove just the O part; the part can be proved by the analogous
argument. By the theorem’s assumption, there exist a positive constant c and a
positive integer n0 = bk0 such that
Hence, we can use the product ccb as a constant required by the O(f (n)) definition
to complete the O part of the theorem’s proof.
PROOF We will prove the theorem for the principal special case of f (n) = nd . (A
proof of the general case is a minor technical extension of the same argument—
see, e.g., [Cor09].) If f (n) = nd , equality (B.15) yields for n = bk , k = 0, 1, . . . ,
logb n logb n
T (n) = nlogb a [T (1) + bj d /a j ] = nlogb a [T (1) + (bd /a)j ].
j =1 j =1
logb n
(bd /a)logb n − 1
(bd /a)j = (bd /a) if bd = a
j =1
(bd /a) − 1
and
logb n
(bd /a)j = logb n if bd = a.
j =1
= (n logb a
(b /a)logb n) = (a logb n(bd /a)logb n)
d
d
= (bd logb n) = (blogb n ) = (nd ).
If a > bd , then bd /a < 1, and therefore
logb n
(bd /a)logb n − 1
(bd /a)j = (bd /a) ∈ (1).
j =1
(bd /a) − 1