CSCE 222 Lecture 22

From Notes
Jump to navigation Jump to search

« previous | Wednesday, March 30, 2011 | next »


Recurrence Relations

A recurrence relation for the sequence of integers {an}n≥0 is an equation that expresses an as a function of some of the previous terms a0 to an−1 starting from an integer n0

A linear homogeneous recurrence relation of degree k with constant coefficients is of the form:

an=C1an−1+C2an−2+…+Ckan−k
linear
ai is a linear function of the terms before it (no exponents).
EX: an=an−1+an−22 is not linear.
homogeneous
every term is a multiple of ai
degree
the number of ai terms added to get the current an.

This function can be rewritten as a series:

an=rn where r is a constant.


Theorem

Let c1 and c2 be real numbers. Suppose that r2−c1r−c2=0 has 2 distinct roots r1 and r2. Then an=c1an−1+c2an−2 has a solution {an} n≥0 iff an is of the form: an=α1r1n+α2r2n for constants α1 and α2.


Example 1: Fibonacci Numbers

Given f1=1 and f2=1,

fn=fn−1+fn−2for n≥3

The Fibonacci sequence is a linear homogeneous function of degree 2

From here, we can get {1,1,2,3,5,8,13,21,34,55,89}.

Using the theorem, we can rewrite an as a degree 2 polynomial: r12−r2−1 has roots 1±52 (the golden ratio).

More complicated math can solve for the constants α1 and α2. Now we can solve the Fibonacci sequence in a single formula that runs in O(1) time:

fn=α1(1+52)n+α2(1−52)n

The sequence grows asymptotically according to Θ((1+52)n).

Example 2: Towers of Hanoi

Moving one disk at a time, move all n disks from peg 1 to peg 3. A larger disk cannot be placed on top of a smaller one.

In order to do this, the top n−1 disks have to be moved to peg 2, the largest disk is moved to peg 3, then the top n−1 disks are moved to peg 3.

Hn=Hn−1+1+Hn−1=2Hn−1+1=2n−1

The towers of hanoi function is linear, but not homogeneous since we add 1.

Given H1=1, we can find H2=3, H3=7.