Recursion Recursive Algorithms: Sections 7.1 - 7.2 of Rosen
Recursion Recursive Algorithms: Sections 7.1 - 7.2 of Rosen
Recursion
A recursive algorithm is one in which objects are defined in terms
of other objects of the same type.
Slides by Christopher M. Bourke
Instructor: Berthe Y. Choueiry Advantages:
I Simplicity of code
I Easy to understand
Fall 2007 Disadvantages:
I Memory
I Speed
Computer Science & Engineering 235
Introduction to Discrete Mathematics
I Possibly redundant work
Sections 7.1 - 7.2 of Rosen
Tail recursion offers a solution to the memory problem, but really,
[email protected]
do we need recursion?
Example
Consider the recurrence relation: an = 2an−1 − an−2 .
The Fibonacci numbers are defined by the recurrence,
It has the following sequences an as solutions:
F (n) = F (n − 1) + F (n − 2)
1. an = 3n, F (1) = 1
2. an = n + 1, and F (0) = 1
3. an = 5.
The solution to the Fibonacci recurrence is
Initial conditions + recurrence relation uniquely determine the √ !n √ !n
sequence. 1 1+ 5 1 1− 5
fn = √ −√
5 2 5 2
The initial conditions specify the value of the first few necessary
More generally, recurrences can have the form terms in the sequence. In the Fibonacci numbers we needed two
initial conditions, F (0) = F (1) = 1 since F (n) was defined by the
T (n) = αT (n − β) + f (n), T (δ) = c two previous terms in the sequence.
Initial conditions are also known as boundary conditions (as
or
n opposed to the general conditions).
T (n) = αT + f (n), T (δ) = c
β From now on, we will use the subscript notation, so the Fibonacci
numbers are
Note that it may be necessary to define several T (δ), initial fn = fn−1 + fn−2
conditions. f1 = 1
f0 = 1
Definition
Second Order Linear Homogeneous Recurrences Second Order Linear Homogeneous Recurrences
Example
A second order linear homogeneous recurrence is a recurrence of
the form Example
an = c1 an−1 + c2 an−2
Find a solution to
an = 5an−1 − 6an−2
Theorem (Theorem 1, p462)
with initial conditions a0 = 1, a1 = 4
r2
Let c1 , c2 ∈ R and suppose that − c1 r − c2 = 0 is the
characteristic polynomial of a 2nd order linear homogeneous I The characteristic polynomial is
recurrence which has two distinct1 roots, r1 , r2 .
r2 − 5r + 6
Then {an } is a solution if and only if
I Using the quadratic formula (or common sense), the root can
an = α1 r1n + α2 r2n be found;
r2 − 5r + 6 = (r − 2)(r − 3)
for n = 0, 1, 2, . . . where α1 , α2 are constants dependent upon the
initial conditions. so r1 = 2, r2 = 3
1
we discuss how to handle this situation later.
Second Order Linear Homogeneous Recurrences Second Order Linear Homogeneous Recurrences
Example Continued Example Continued
Example
I By Theorem 2, we have that the solution is of the form
What is the solution to the recurrence relation
an = α1 4n + α2 n4n
an = 8an−1 − 16an−2
I Using the initial conditions, we get a system of equations;
with initial conditions a0 = 1, a1 = 7?
a0 = 1 = α1
a1 = 7 = 4α1 + 4α2
I The characteristic polynomial is
3
I Solving the second, we get that α2 = 4
r2 − 8r + 16 I And so the solution is
I Factoring gives us 3
an = 4n + n4n
4
r2 − 8r + 16 = (r − 4)(r − 4)
I We should check ourselves. . .
so r0 = 4
General Linear Homogeneous Recurrences General Linear Homogeneous Recurrences I
Distinct Roots
There is a straightforward generalization of these cases to higher Let c1 , . . . , ck ∈ R. Suppose that the characteristic equation
order linear homogeneous recurrences.
rk − c1 rk−1 − · · · − ck−1 r − ck = 0
Essentially, we simply define higher degree polynomials.
The roots of these polynomials lead to a general solution. has k distinct roots, r1 , . . . , rk . Then a sequence {an } is a solution
of the recurrence relation
The general solution contains coefficients that depend only on the
initial conditions. an = c1 an−1 + c2 an−2 + · · · + ck an−k
In the general case, however, the coefficients form a system of if and only if
linear equalities.
an = α1 r1n + α2 r2n + · · · + αk rkn
Theorem (Continued)
For recursive algorithms, cost functions are often not homogenous Here, f (n) represents a non-recursive cost. If we chop it off, we
because there is usually a non-recursive cost depending on the are left with
input size.
an = c1 an−1 + c2 an−2 + · · · + ck an−k
Such a recurrence relation is called a linear nonhomogeneous
recurrence relation. which is the associated homogenous recurrence relation.
Such functions are of the form Every solution of a linear nonhomogeneous recurrence relation is
the sum of a particular solution and a solution to the associated
an = c1 an−1 + c2 an−2 + · · · + ck an−k + f (n)
linear homogeneous recurrence relation.
Linear Nonhomogeneous Recurrences Linear Nonhomogeneous Recurrences
Theorem (Continued)
Theorem (Theorem 6, p469)
When s is not a root of the characteristic equation of the
Suppose that {an } satisfies the linear nonhomogeneous recurrence associated linear homogeneous recurrence relation, there is a
relation particular solution of the form
an = c1 an−1 + c2 an−2 + · · · + ck an−k + f (n) (pt nt + pt−1 nt−1 + · · · + p1 n + p0 ) · sn
where c1 , . . . , ck ∈ R and
When s is a root of this characteristic equation and its multiplicity
f (n) = (bt nt + bt−1 nt−1 + · · · + b1 n + b0 ) · sn is m, there is a particular solution of the form
The examples in the text are quite good (see pp467–470) and We briefly describe two “unfolding” methods that work for a lot of
illustrate how to solve simple nonhomogeneous relations. cases.
We may go over more examples if you wish. Backward substitution – this works exactly as its name implies:
starting from the equation itself, work backwards, substituting
Also read up on generating functions in section 7.4 (though we values of the function for previous ones.
may return to this subject).
Recurrence Trees – just as powerful but perhaps more intuitive,
However, there are alternate, more intuitive methods. this method involves mapping out the recurrence tree for an
equation. Starting from the equation, you unfold each recursive
call to the function and calculate the non-recursive cost at each
level of the tree. You then find a general formula for each level and
take a summation over all such levels.
Backward Substitution Backward Substitution
Example Example – Continued
Example If we continue to do this, we get the following.
Give a solution to T (n) = T (n − 2) + 2(n − 1) + 2n
= T (n − 3) + 2(n − 2) + 2(n − 1) + 2n
T (n) = T (n − 1) + 2n
= T (n − 4) + 2(n − 3) + 2(n − 2) + 2(n − 1) + 2n
..
where T (1) = 5. .
= T (n − i) + i−1
P
j=0 2(n − j)
We begin by unfolding the recursion by a simple substitution of the
function values.
Observe that I.e. this is the function’s value at the i-th iteration. Solving the
sum, we get
T (n − 1) = T ((n − 1) − 1) + 2(n − 1) = T (n − 2) + 2(n − 1)
(i − 1)(i − 1 + 1)
T (n) = T (n − i) + 2n(i − 1) − 2 + 2n
2
Substituting this into the original equation gives us
T (n) = T (n − 2) + 2(n − 1) + 2n
Iteration Cost The total value of the function is the summation over all levels of
0 T (n) f (n)
the tree:
logβ n
X n
T (n) = αi · f
βi
“ ”
T (n/β) ··· α ··· T (n/β) α·f n
1 β i=0
2
„
n
« We consider the following concrete example.
2 T (n/β 2 ) · · · α · · · T (n/β 2 ) T (n/β 2 ) · · · α · · · T (n/β 2 ) α · f
β2
. .
.
.
.
.
Example
„ «
i n
i α ·f
βi
. . n
. .
. . T (n) = 2T + n, T (1) = 4
logβ n α
logβ n
· T (δ)
2
Recurrence Trees Recurrence Trees
Example – Continued Example – Continued
Iteration Cost
0 T (n) n
1 T (n/2) T (n/2) n + n
2 2
The value of the function then, is the summation of the value of
all levels. We treat the last level as a special case since its
non-recursive cost is different.
n
„ «
2 T (n/4) T (n/4) T (n/4) T (n/4) 4·
4
(log2 n)−1
X n
T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) 8·
„
n
«
T (n) = 4n + 2i = n(log n) + 4n
3 8
2i
i=0
. .
. .
. .
0 1
i 2i · @ n A
2i
. .
. .
. .
In the previous example we make the following assumption: that n Maple and other math tools are great resources. However, they are
was a power of two; n = 2k . This was necessary to get a nice not substitutes for knowing how to solve recurrences yourself.
depth of log n and a full tree. As such, you should only use Maple to check your answers.
We can restrict consideration to certain powers because of the Recurrence relations can be solved using the rsolve command
smoothness rule, which is not studied in this course. For more and giving Maple the proper parameters.
information about the smoothness rule, please consult pages The arguments are essentially a comma-delimited list of equations:
481–483 in the textbook “The Design & Analysis of Algorthims” general and boundary conditions, followed by the “name” and
by Anany Levitin. variable of the function.