solving recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf ·...

22
Solving Recurrences Andreas Klappenecker

Upload: others

Post on 19-Jul-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Solving RecurrencesAndreas Klappenecker

Page 2: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Generating Functions

Given a sequence (a0, a1, a2, a3,...) of real numbers, one can form its generating function, an infinite series given by

The generating functions is a formal power series, meaning that we treat it as an algebraic object, and we are not concerned with convergence questions of the power series.

∞�

k=0

akxk

Page 3: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Example 1

Suppose that the sequence is given by

(k+1)k>=0

Then its generating function is given by ∞�

k=0

(k + 1)xk = 1 + 2x+ 3x2 + · · ·

Page 4: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Example 2

The generating function of the sequence (1, 1, 1, 1, ...)is given by

1 + x+ x2 + x3 + · · · = 1

1− x.

Page 5: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Example 3

The generating function of the sequence (1, a, a2, a3, . . .)is given by

1 + ax+ a2x2 + a3x3 + · · · = 1

1− ax.

Page 6: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Example 4

The generating function of (1, 1, 1, 1, 1, 1, 0, 0, 0, . . .)is given by

1 + x+ x2 + x3 + x4 + x5.

x6 − 1

x− 1= 1 + x+ x2 + x3 + x4 + x5.

Page 7: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Sum of Sequences

Let (ak)k≥0 and (bk)k≥0 be sequences with generat-ing functions a(x) and b(x), respectively.

Then (ak + bk)k≥0 has the generating function

a(x) + b(x).

Page 8: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Products

Let a(x) =∞�

k=0

akxk and b(x) =∞�

k=0

bkxk.

Then

a(x)b(x) =∞�

k=0

k�

j=0

ajbk−j

xk

Page 9: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Example

Recall that the constant sequence (1, 1, 1, 1, . . .) hasthe generating function 1/(1− x).

What is the sequence corresponding to the gen-erating function

1

(1− x)2=

1

1− x

1

1− x?

1

1− x

1

1− x=

∞�

k=0

k�

j=0

1

xk =∞�

k=0

(k + 1)xk

Page 10: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Table of Generating Functions

Study the table on page 542!

Page 11: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Solving Recurrences

Page 12: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Shifting Sequences

Let G(x) be the generating function of the sequence(ak)k. Then

xG(x) =∞�

k=0

akxk+1 =

∞�

k=1

ak−1xk.

Page 13: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Solving a RecurrenceSuppose we have the recurrence system with

initial condition a0 = 2 and

recurrence ak = 3ak−1 for k ≥ 1.

G(x)− 3xG(x) =

∞�

k=0

akxk − 3

∞�

k=1

ak−1xk

= a0 +∞�

k=1

(ak − 3ak−1)xk

= 2.

Thus, G(x)− 3xG(x) = (1− 3x)G(x) = 2. Hence,

G(x) =2

1− 3x.

Page 14: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Solving a Recurrence

Since we know that 1/(1-ax)=1+ax+a2x2+...,

we have

G(x) = 2(1+3x+32x2+...).

Therefore, a sequence solving the recurrence is given by

(2,2x3,2x32,...)=(2x3k)

Page 15: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

The Fibonacci numbers satisfy the recurrence:

f0 = 0f1 = 1fn = fn−1 + fn−2 for n ≥ 2

Page 16: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

The Fibonacci numbers satisfy the recurrence:

f0 = 0f1 = 1f2 = f1 + f0f3 = f2 + f1f4 = f3 + f2

...

Page 17: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

Let F (x) =�∞

k=0 fkxk be the generating function of

the Fibonacci sequence. The recurrence

fn = fn−1 + fn−2

orfn − fn−1 − fn−2 = 0

suggests that we should consider

F (x)− xF (x)− x2F (x)

If it were not for the initial conditions, the latter sumwould be identically 0.

Page 18: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

The value of F (x)− xF (x)− x2F (x) is

f0 + f1x+ f2x2+ f3x3

+ · · ·−(f0x+ f1x2

+ f2x3+ · · · )

−(f0x2+ f1x3

+ · · · )

f0 + (f1 − f0)x

Since f0 + (f1 − f0)x = x, we get

F (x)− xF (x)− x2F (x) = F (x)(1− x− x2) = x.

Hence,

F (x) =x

1− x− x2

Page 19: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

We found the generating function

F (x) =x

1− x− x2

This function is of a simple form.If we can find the coefficient of xn in the power

series of F (x), we have found a closed form solutionto the Fibonacci numbers.

Page 20: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

Since x/(1−x−x2) is a rational function, we canuse the method of partial fractions, which you mightknow from calculus.

Factor the denominator

1− x− x2 = (1− α1x)(1− α2x),

where

α1 =1 +

√5

2, α2 =

1−√5

2.

Then

x

1− x− x2=

A1

1− α1x+

A2

1− α2x.

Page 21: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

We can plug in various values for x to obtainlinear equations that the real numbers A1 and A2

must satisfy. Solving these equations, we get

x

1− x− x2=

A1

1− α1x+

A2

1− α2x

with

A1 =1√5, A2 = − 1√

5.

so

F (x) =x

1− x− x2=

1√5

�1

1− α1x− 1

1− α2x

Page 22: Solving Recurrences - faculty.cs.tamu.edufaculty.cs.tamu.edu/klappi/csce222-s12/recurrence2.pdf · Solving Recurrences. Shifting Sequences Let G(x) be the generating function of the

Fibonacci Numbers

We can get the closed form by observing that

F (x) = 1√5

�1

1−α1x− 1

1−α2x

= 1√5

�(1 + α1x+ α2

1x2 + · · · )− (1 + α2x+ α2

2x2 + · · · )

Therefore,

fn =1√5(αn

1−αn2 ) =

1√5

��1 +

√5

2

�n

−�1−

√5

2

�n�