CS205M Recursion
CS205M Recursion
CS205M Recursion
4
Recurrence Relations
• Definition: A recurrence relation for a sequence
{an} is an equation that expresses an in terms of one
or more of the previous terms in the sequence:
a0, a1, a2, …, an-1
for all integers nn0 where n0 is a nonnegative
integer.
• A sequence is called a solution of a recurrence if its
terms satisfy the recurrence relation
5
Recurrence Relations: Solutions
• Consider the recurrence relation an=2an-1-an-2
• It has the following sequences an as solutions
– an= 3n
– an= n+1
– an=5
• The initial conditions + recurrence relation
uniquely determine the sequence
6
Recurrence Relations: Example
• The Fibonacci numbers are defined by the recurrence
F(n) = F(n-1) +F(n-2)
F(1) = 1
F(0) = 1
• The solution to the Fibonacci recurrence is
fn = 1/5 ((1+5)/2)n – 1/5 ((1-5)/2)n
7
Recurrence Relations: General Form
• More generally, recurrences can have the form
T(n) = T(n-) + f(n), T() = c
or
T(n) = T(n/) + f(n), T() = c
8
Recurrence Relations: Initial Conditions
10
Solving Recurrences
• There are several methods for solving
recurrences
– Characteristic Equations
– Forward Substitution
– Backward Substitution
– Recurrence Trees
– Generating functions …
11
Linear Homogeneous Recurrences
• Definition: A linear homogeneous recurrence relation of
degree k with constant coefficients is a recurrence relation of
the form
an = c1an-1 + c2an-2 + … + ckan-k
with c1, c2, …, ckR, ck 0.
• Linear: RHS is a sum of multiples of previous terms of the
sequence (linear combination of previous terms). The
coefficients are all constants (not functions depending on n)
• Homogeneous: no terms occur that are not multiples of aj’s
• Degree k: an is expressed in terms of (n-k)th term of the
sequence
12
Linear Homogeneous Recurrences: Examples
14
Second Order Linear Homogeneous Recurrences
19
Single Root Case
• We can apply the theorem if the roots are distincts, i.e. r1r2
• If the roots are not distinct (r1=r2), we say that one
characteristic root has multiplicity two. In this case, we apply
a different theorem
• Theorem (Theorem2)
Let c1, c2R and suppose that r2 - c1r - c2 = 0 has only one
distinct root, r0, then {an} is a solution to an = c1an-1+ c2an-2 if
and only if
an= 1r0n + 2nr0n
for n=0,1,2,… where 1, 2 are constants depending upon the
initial conditions
20
Single Root Case: Example (1)
• What is the solution to the recurrence relation
an = 8an-1 - 16an-2
with initial conditions a0= 1, a1= 7?
• The characteristic equation is:
r2 – 8r + 16 = 0
• Factoring gives us:
r2 – 8r + 16 = (r-4)(r-4), so r0=4
• Applying the theorem we have the solution:
an= 1(4)n + 2n(4)n
21
Single Root Case: Example (2)
• Given: an= 1(4)n + 2n(4)n
• Using the initial conditions, we get:
a0= 1 = 1(4)0 + 20(4)0 = 1
a1= 7 = 1(4) + 21(4)1 = 41 + 42
• Thus: = 1 = 1, 2 = 3/4
• The solution is
an= (4)n + ¾ n (4)n
• Always check yourself…
22
General Linear Homogeneous Recurrences
• There is a straightforward generalization of these cases to
higher-order linear homogeneous recurrences
• Essentially, we simply define higher degree polynomials
• The roots of these polynomials lead to a general solution
• The general solution contains coefficients that depend only on
the initial conditions
• In the general case, the coefficients form a system of linear
equalities
23
General Linear Homogeneous Recurrences:
Distinct Roots
• Theorem (Theorem 3)
Let c1,c2,..,ck R and suppose that the characteristic equation
rk - c1rk-1 - c2rk-2 - … - ck = 0
has k distinct roots r1,r2, …,rk. Then a sequence {an} is a
solution of the recurrence relation
an = c1an-1 + c2an-2 + … + ckan-k
if and only if
an = 1r1n + 2r2n + … + krkn
for n=0,1,2,… where 1,2,…,k are constants depending upon
the initial conditions
24
Higher Order Linear Homogeneous
Recurrences: Example
• Find a solution to
an = 6an-1 - 11an-2 + 6an-3
with initial conditions a0=2, a1=5, a2= 15
• The characteristic equation is
r3 – 6r2 + 11r - 6 = 0
• The roots are r1=1, r2=2 and r3=3
26
General Linear Homogeneous Recurrences:
Any Multiplicity
• Theorem (Theorem 4)
Let c1,c2,..,ck R and suppose that the characteristic equation
rk - c1rk-1 - c2rk-2 - … - ck = 0
has t roots with multiplicities m1,m2, …,mt. Then a sequence
{an} is a solution of the recurrence relation
an = c1an-1 + c2an-2 + … + ckan-k
if and only if an = (1,0 + 1,1n + … + 1,m1-1nm1-1) r1n +
(2,0 + 2,1n + … + 2,m2-1nm2-1) r2n + ...
(t,0 + t,1n + … + t,mt-1nmt-1) rtn
for n=0,1,2,… where i,j are constants for 1 i t and
0 jmi-1 depending upon the initial conditions
27
General Linear Homogeneous Recurrences:
Any Multiplicity
28
General Linear Homogeneous Recurrences:
Any Multiplicity
• Find a solution to an = -3an-1 - 3an-2 - an-3
with initial conditions a0=1, a1=-2, a2= -1
• The characteristic equation is r3 + 3r2 + 3r + 1 = 0 => (r + 1)3 = 0
• It has single root r = -1 with multiplicity 3.
• Hence the solution is an = (1,0 + 1,1n + 1,2n2) (-1)n
• Given the solution we plug in the three initial conditions to get a system of
linear equation 1 = 1,0
-2 = - 1,0 - 1,1 - 1,2
-1 = - 1,0 + 21,1 + 41,2
• Solving, we get 1,0 = 1, 1,1 = 3, and 1,2= -2
• Putting it back together, we have
an = (1 + 3n - 2n2)(-1)n
29
Linear NonHomogeneous Recurrences
• For recursive algorithms, cost function are often not homogeneous
because there is usually a non-recursive cost depending on the input size
• Such a recurrence relation is called a linear nonhomogeneous recurrence
relation
• Such functions are of the form
an = c1an-1 + c2an-2 + … + ckan-k + f(n)
• f(n) represents a non-recursive cost. If we chop it off, we are left with
an = c1an-1 + c2an-2 + … + ckan-k
which is the associated homogeneous recurrence relation
• Every solution of a linear nonhomogeneous recurrence relation is the sum
of
– a particular solution and
– a solution to the associated linear homogeneous recurrence relation
30
Solving Linear NonHomogeneous Recurrences (1)
• Theorem (Theorem 5)
If {an(p)} is a particular solution of the
nonhomogeneous linear recurrence relation with
constant coefficients
an = c1an-1 + c2an-2 + … + ckan-k + f(n)
then every solution is of the form {an(p) + an(h)} where
{an(h)} is a solution of the associated homogeneous
recurrence relation
an = c1an-1 + c2an-2 + … + ckan-k
31
Solving Linear NonHomogeneous Recurrences (2)
• Theorem (Theorem 6)
Suppose {an} satisfies the linear nonhomogeneous
recurrence relation
an = c1an-1 + c2an-2 + … + ckan-k + f(n)
where c1,c2,..,ck R and
f(n) = (btnt + bt-1nt-1 + .. + b1n + b0) sn
where b0,b1,..,bn,s R
… continues
33
Solving Linear NonHomogeneous Recurrences (4)
34
Linear NonHomogeneous Recurrences: Examples
35
Other Methods
• When analyzing algorithms, linear homogeneous recurrences
of order greater than 2 hardly ever arise in practice
• We briefly describe two unfolding methods that work for a lot
of cases
– Backward substitution: this works exactly as its name suggests.
Starting from the equation itself, work backwards, substituting values
of the function for previous ones
– Recurrence trees: just as powerful, but perhaps more intuitive, 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.
Then, you find a general formula for each level and take a summation
over all such levels
36
Backward Substitution: Example (1)
• Give a solution to
T(n)= T(n-1) + 2n
where T(1)=5
• We observe that
T(n-1) = T((n-1) -1) + 2(n-1) = T(n-2) + 2 (n-1)
38
Backward Substitution: Example (3)
• We want to get rid of the recursive term
T(n) = T(n-i) + 2n(i-1) – i2 + i + 2n
• To do that, we need to know at what iteration we reach our
based case, i.e. for what value of i can we use the initial
condition T(1)=5?
• We get the base case when n-i=1 or i=n-1
• Substituting in the equation above we get
T(n) = 5 + 2n(n-1-1) – (n-1)2 + (n-1) + 2n
T(n) = 5 + 2n(n-2) – (n2-2n+1) + (n-1) + 2n = n2 + n + 3
39
Recurrence Trees (1)
• When using recurrence trees, we graphically represent the
recursion
• Each node in the tree is an instance of the function. As we
progress downward, the size of the input decreases
• The contribution of each level to the function is equivalent to
the number of nodes at that level times the non-recursive
cost on the size of the input at that level
• The tree ends at the depth at which we reach the base case
• As an example, we consider a recursive function of the form
T(n) = T(n/) + f(n), T() = c
40
Recurrence Trees (2)
Iteration
Cost
0 TT(n)
f(n)
1
T(n/) T(n/) T(n/) f(n/)
41
Recurrence Trees (3)
• The total value of the function is the
summation over all levels of the tree
42
Recurrence Tree: Example (2)
Iteration
Cost
0 T(n)
n
1 T(n/2) T(n/2)
n/2 +n/2
2
8.n/8
. T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)
.
.
.
i
2i(n/2i)
.
.
.
.
log2 n
43
Recurrence Trees: Example (3)
• The value of the function 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
44
Smoothness Rule
• In the previous example, we make the following assumption
n has a power of two (n=2k)
This assumption is necessary to get a nice depth of log(n) and
a full tree
• We can restrict consideration to certain powers because of
the smoothness rule, which is not studied in this course.
• For more information about that rule, consult textbooks on
Algorithms (The Design & Analysis of Algorithms by A. Levitin)
45