0% found this document useful (0 votes)
1 views78 pages

CS112_week6

The document outlines a method for solving recurrence relations using generating functions, detailing steps from defining the generating function to solving for it and expanding into a power series. An example is provided to illustrate the method, demonstrating how to derive a formula for a specific sequence defined by a recurrence relation. Additionally, the document explores various counting problems and their solutions using generating functions.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views78 pages

CS112_week6

The document outlines a method for solving recurrence relations using generating functions, detailing steps from defining the generating function to solving for it and expanding into a power series. An example is provided to illustrate the method, demonstrating how to derive a formula for a specific sequence defined by a recurrence relation. Additionally, the document explores various counting problems and their solutions using generating functions.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Spring 2025

Prof. Dr. Hasan F. ATEŞ


Özyeğin University, İstanbul, Turkey
[email protected]
The METHOD
Given: a recurrence formula that is to be solved by the method of
generating functions.
1.Make sure that the set of values of the free variable (usually n) for which
the given recurrence relation holds, is clearly defined.

2. Give a name to the generating function and write out that function in
terms of the unknown sequence (e.g., call it A(x), and define it to be: ∑
anxn ).

3. Multiply both sides of the recurrence by xn, and sum over all values of n
for which the recurrence holds.

4. Express both sides of the resulting equation explicitly in terms of your


generating function A(x).

5. Solve the resulting equation for the unknown generating function A(x).

6. If you want an exact formula for the sequence that is defined by the given
recurrence relation, then attempt to get such a formula by expanding A(x)
into a power series by any method you can think of. In particular, if A(x) is a
rational function (quotient of two polynomials), then success will result from
expanding in partial fractions and then handling each of the resulting terms
Example
an+1 = 2 an + 1 (n ≥ 0; a0 = 0 )
1. Make sure that the set of values of the free variable (usually n) for
which the given recurrence relation holds, is clearly defined.
It is defined for n ≥ 0 . It is clearly defined. OK

2. Give a name to the generating function and write out that function in
terms of the unknown sequence (e.g., call it A(x), and define it to be: ∑
anxn ).

A(x) = ∑ anxn
n≥0 by xn, and sum over all values of n
3. Multiply both sides of the recurrence
for which the recurrence holds.

an+1xn = 2 anxn + xn

∑ an+1xn = 2 ∑ anxn + ∑ xn
n≥0 n≥0 n≥0
Example
4. Express both sides of the resulting equation explicitly in terms of your
generating function A(x).

∑ an+1xn = 2 ∑ anxn + ∑ xn
n≥0 n≥0 n≥0

HS: (this isn’t automatic, requires some intuition)


an+1xn = a1 + a2x + a3x2 + a4x3 + …
0 n≥0
= (a1x + a2x2 + a3x3 + a4x4+ …)/x
= [(a0 + a1x + a2x2 + a3x3 + a4x4 + …) – a0]
= A(x) / x (since a0 = 0 )
RHS:
2 ∑ anxn + ∑ x=n 2 A(x) + 1 / (1 - x)

n≥0 n≥0
RHS = LHS ⇒
A(x) / x = 2A(x) + 1/(1 – x)
Example
5. Solve the resulting equation for the unknown generating function A(x).

A(x) / x = 2A(x) + 1/(1 – ⇒


x) A(x) = x / [(1 – x)(1 - 2x)]
6. If you want an exact formula for the sequence that is defined by the
given recurrence relation, then attempt to get such a formula by expanding
A(x) into a power series by any method you can think of. In particular, if
A(x) is a rational function (quotient of two polynomials), then success will
result from expanding in partial fractions and then handling each of the
resulting terms separately.

x K M
A(x) = = +
(1 – x)(1 – 2x) 1–x 1 – 2x
Here, by proper selection of the unknown constants K and M, we can
generate
a single x at the denominator. Again, not automatic!
x K M
A(x) = = +
) (1 – x)(1 – 2x) 1–x 1 – 2x

Numerator: K(1 – 2x) + M (1 – x) = x


⇒ (K + M) – (2K + M) x = x
⇒ 2K + M = -1
K+M=0
⇒ K = -M
-2M + M = -1
⇒ M = 1; 1
K = -1 1
A(x) = 1 – 2x - –x
1
1 1
A(x) = -
1 – 2x 1–x

1 + x1 + x2 + x3 + … + xn + …

1 + 2x1 + 22x2 + 23x3 + … + 2nxn + …

x) = (2 – 1)x+(22 – 1)x2+(23 – 1)x2 +…+(2n+1- 1)xn+1 +


= a1 x+ a2 x2 + a 3 x3+…+ an+1 xn+1 +

⇒ an = 2n - 1
Multiplication: special case

A(X) = a0 + a1 X + a2 X2 + …
1
Special case: B(X) = 1 + X + X + …=
2
1-X
multiply them together

(A*B)(X) = a0 + (a0 + a1) X + (a0 + a1 + a2) X2


+ (a0 + a1 + a2 + a3) X3 + …

it gives us partial sums!


For example…

Suppose A(X) = 1 + X + X2 + … 1
=
1-X
and B(X) = 1 + X + X2 + … = 1
1-X
then (A*B)(X) = 1 + 2X + 3X2 + 4X3 + …
1 1 1
= * =
1-X 1-X (1-X)2

Generating function for the sequence [1,2,3,4…]


What happens if we again
take prefix sums?
1
Take 1 + 2X + 3X + 4X + …=
2 3
(1-X)2

multiplying through by 1/(1-X)

1
¢1 + ¢2X + ¢3X + ¢4X +… =
1 2 3

(1-X)3

Generating function for the triangular numbers!


Triangular numbers
What’s the pattern?

[1,1,1,1,…] 1

1-X
1
[1,2,3,4,…] ⇔
(1-X)2
1
[¢1,¢2,¢3,¢4,…] ⇔
(1-X)3

??? 1

(1-X)n
What’s the pattern?

0 1 2 3 1
, , , ,… ⇔
0 0 0 0 1-X
1
[1,2,3,4,…] ⇔
(1-X)2
1
[¢1,¢2,¢3,¢4,…] ⇔
(1-X)3

??? 1

(1-X)n
What’s the pattern?

0 1 2 3 1
, , , ,… ⇔
0 0 0 0 1-X
1 2 3 4 1

1, 1 , 1 ,1 ,… (1-X)2
1
[¢1,¢2,¢3,¢4,…] ⇔
(1-X)3

??? 1

(1-X)n
What’s the pattern?

0 1 2 3 1
, , , ,… ⇔
0 0 0 0 1-X
1 2 3 4 1

1, 1 , 1 ,1 ,… (1-X)2
2 3 4 5 1
, , , ,… ⇔
2 2 2 2 (1-X)3
… …
??? 1

(1-X)n
What’s the pattern?

0 1 2 3 1
, , , ,… ⇔
0 0 0 0 1-X
1 2 3 4 1

1, 1 , 1 ,1 ,… (1-X)2
2 3 4 5 1
, , , ,… ⇔
2 2 2 2 (1-X)3
… …
n-1 n n+1 n+2 1
, , , ⇔
n-1 n-1 n-1 n-1 (1-X)n
,…
What’s the pattern?

0 1 2
x3 = 1
32
0
+0 x 0+ x +
0 1-X
+…
1 2 3 4 1
1
+1 x 1+ x2 + x3 = (1-X)2
1
+…
2 3 4 52 1
+ x+ x + x =
3
2 2 2 2 (1-X)3
+…

 k+n-1
n-1
X k =
1
(1-X)n
k=0
What’s the pattern?

 k+n-1
n-1
X k =
1
(1-X)n
k=0
What is the coefficient of Xk
in the expansion of:

( 1 + X + X 2 + X3 + X4
+ . . . . )n ?

A solution to # of solutions for:


e1 + e2 + . . . + en = k
ek≥ 0
i.e., the coeff. of xk in the open form of:
( 1 + X + X2 + X3 + X4 + . . . . )n
5 distinct pirates want to
divide 20 identical,
indivisible bars of gold. How
many different ways can
they divide up the loot?

a) 10626
b) 4845
c) 53130
Sequences with 20 G’s and 4 /’s

GG/G//GGGGGGGGGGGGGGGGG/
represents the following division among
the pirates: 2, 1, 0, 17, 0

In general, the ith pirate gets the number of G’s after


the i-1st / and before the ith /.

This gives a correspondence between divisions of the


gold and sequences with 20 G’s and 4 /’s.

We have a total of 24 positions, we have to select 4


of these to insert the /’s, order being not important
(G’s then can be inserted in the remaining 20
How many different ways to divide up the loot?
Sequences with 20 G’s and 4 /’s

Which is equivalent to:

corresponding to selecting 20 positions out of 24 to


insert G’s with order not being important (then, the
4 /’s go to the remaining 4 positions)
How many different ways can n
distinct pirates divide k
identical, indivisible bars of
gold?
Another What is the coefficient of Xk
way to in the expansion of:
see it…
( 1 + X + X 2 + X3 + X4
+
. . . . )n ?

n+k-1 n+k-1
=
n-1 k
Equivalent problems
• The number of ways to distribute k indistinguishable
balls into n distinguishable boxes (or k coins to n
kids),

• The number of lists [a1, a2, …, an] with nonnegative


integer entries satisfying:
a1 + a 2 + … + a n = k

• The number of ways to select k objects with


repetition from n different types of objects (or, k
selections to n types)
n+k-1 n+k-1
=
are all given by: n-1 k
Finally,
a different counting problem…
Let cn = number of ways to
pick exactly n fruits.

e.g., c5 = 6

What is a closed form for cn?


So if A(x), B(x), O(x) and P(x)
are the generating functions for the
number of ways to fill baskets using
only one kind of fruit

the generating function for


number of ways to fill basket using
any of these fruit is given by
C(x) = A(x)B(x)O(x)P(x)
Suppose we only pick bananas
bn = number of ways to pick n fruits, only bananas.

[1,0,1,0,1,0,…]
1
B(x) = 1 + x2 + x4 + x6 + … =
1-X2
Suppose we only pick apples
an = number of ways to pick n fruits, only apples.

[1,0,0,0,0,1,…]
1
A(x) = 1 + x5 + x10 + x15 + …
=
1-X5
Suppose we only pick oranges
on = number of ways to pick n fruits, only oranges.

[1,1,1,1,1,0,0,0,…]
1-X 5
O(x) = 1 + x + x2 + x3 + x4 =
1-X
Suppose we only pick pears
pn = number of ways to pick n fruits, only pears.

[1,1,0,0,0,0,0,…]
1-X2
P(x) = 1 + x =
1-X
Let cn = number of ways to
pick exactly n fruits of any type

 cn xn = A(x) B(x) O(x) P(x)


1 1 1-X 5
1-X 2 1
= =
1-X5 1-X2 1-X 1-X (1-X)2
Let cn = number of ways to
pick exactly n fruits of any type
1
cn is coefficient of X in
n
(1-X)2

cn = n+1. <1,2,3,4,…>
Yet another problem...

In a basketball game a team may score 1, 2, or 3 points with


each shot.
a) In how many ways can a team score a total of 6 points?
How many points could be scored using one-point shots?
0 pts or 1 pt or 2 pts or 3 pts or 4 pts or 5 pts or 6 pts
So, GF1(x) = x0 + x1 + x2 + x3 + x4 + x5 + x6

How many points could be scored using two-point shots?


0 pts or 2 pts or 4 pts or 6 pts
GF2(x) = x0 + x2 + x4 + x6

How many points could be scored using three-point shots?


0 pts or 3 pts or 6 pts
GF3(x) = x0 + x3 + x6

Multiply these generating functions together to find:


GF(x) = 1 + x + 2x2 + 3x3 + 4x4 + 5x5 + 7x6 + 7x7 + 8x8 +
8x9+
8x10 + 7x11 + 7x12 + 5x13 + 4x14 + 3x15 + 2x16 + x17 + x18
6
Indeed, there are 7 solutions:
3+3
3+2+1
3+1+1+1
2+2+2
2+2+1+1
2+1+1+1+1
1+1+1+1+1+1

b) How about the number of ways to reach 98 points?

import numpy as np

ones = np.ones(99)
onezero = np.zeros(99)
onezero[::-2] =1
one2zero = np.zeros(99)
one2zero[::-3] =1
print(np.flip(np.poly1d(ones)*np.poly1d(onezero)*np.poly1d(one2zero))
[98])
850
Recap

[1,1,…] ⟷ 1 + x + x2 +… =

[1,-1,1,-1,…] ⟷ 1 - x + x2 -… =

[1,a,a2,a3,…] ⟷ 1 + ax + a2x2 + a3x3 +… =

[1,0,1,0,…] ⟷ 1 + x2 + x4 + x6 +… =
Rules

Scaling: [f0,f1,…] ⟷ F(x) ⇒ [cf0,cf1,…] ⟷ cF(x)

Addition: [f0,f1,…] ⟷ F(x) & [g0,g1,…] ⟷ G(x)


⇒ [f0 + g0, f1 + g1, …] ⟷ F(x) + G(x)

Right shift: [f0,f1,…] ⟷ F(x)


⇒ xk⋅F(x) ⟷ [ 0, 0, …,0, f0, f1,…]
k zeroes
More operations: Differentiation

A(X) = a0 + a1 X + a2 X2 + …
differentiate it…
A’(X) = a1 + 2a2 X + 3a3 X2 …

A’(X) =  (i+1)ai+1 Xi
i=0

X A’(X) =  iai Xi
i=0
Generating Functions

It’s one of the


most important
mathematical
ideas of all
time!
Chapter 4: Fibonacci Numbers

Chapter’s Goal: Overview:


• Introduce Fibonacci • Mathematical definition of Fibonacci
numbers sequences
• recursive function • Properties of Fibonacci sequences
definitions • Programming

Some of the slides are taken from: Great


Theoretical Ideas in Computer Science
Lecture Notes of CMU by Anupam Gupta

41
Chapter 4: roadmap
4. 1 Mathematical definition of Fibonacci sequences
4. 2 Properties of Fibonacci sequences
4. 3 Programming
Inductive Definition Of Functions
Stage 0, Initial Condition, or Base Case:
Declare the value of the function on some
subset of the domain.

Inductive Rules
Define new values of the function in terms
of previously defined values of the function

F(x) is defined if and only if it is implied by


finite iteration of the rules.
Inductive Definition or Recurrence Relation for Fibonacci

Stage 0, Initial Condition, or Base Case:


Fib(1) = 1; Fib (2) = 1

Inductive Rule
For n>3, Fib(n) = Fib(n-1) + Fib(n-2)

n 0 1 2 3 4 5 6 7
1
Fib(n) % 1 1 2 3 5 8
3
Inductive Definition or Recurrence Relation for Fibonacci

Stage 0, Initial Condition, or Base Case:


Fib(0) = 0; Fib (1) = 1

Inductive Rule
For n>1, Fib(n) = Fib(n-1) + Fib(n-2)
n 0 1 2 3 4 5 6 7
Fib(n) 0 1 1 2 3 5 8 13
Example: Staircase

A staircase has n steps.


You walk up taking one or
two steps at a time. How
many ways can you go up?

# steps (n) # ways


1 1 a) 4
2 2 b) 5
3 3 c) 6
4 ? d) 7
Example: Staircase
J(n) : # of ways for climbing n steps

J(n + 1) = J(n) + J(n – 1)

if your last if your last


step is single step is double

So, J(4) = J(3) + J(2) = 3 + 2 = 5


(1) 1,1,1,1; (2) 1,2,1; (3) 1,1,2; (4) 2,1,1; (5) 2,2
Example: Staircase
J(n) : # of ways for climbing n steps

J(n + 1) = J(n) + J(n – :1)


same as Fib rule, but
J(3) = 3 ≠ Fib(3) = 2

but, J(3) = Fib(4)


J(4) = Fib(5)
J(5) = J(4) + J(3) = Fib(5) + Fib(4) = Fib(6)

so, J(n) = Fib(n + 1)
Another Example: Bee
The bee in the picture below can start at either cell 1 or cell
2 and moves only to the right (that is, only to a cell with a
higher number in it).

There is only one path to cell 1, but two ways to reach


cell 2: directly or via cell 1. For cell 3, it can go 1-2-3,
1-3, or 2-3, that is, there are three different paths. How
many paths are there from the start to cell number n?
Another Example: Bee

It can go to cell n from cells (n – 1) or (n – 2).


So, if the number of ways to go to cell n is f(n),
it is equal to the number of ways to go to cell (n – 1),
f(n – 1), plus the number of ways to go to cell number
(n –2), f (n – 2). That is,
f(n) = f(n – 1) + f(n – 2)
Since f(1) = 1 = Fib(1) = Fib(2) and f(2) = 2 = Fib(3),
the answer is: (n+1)th Fibonacci number.
Chapter 4: roadmap
4. 1 Mathematical definition of Fibonacci sequences
4. 2 Properties of Fibonacci sequences
4. 3 Programming
Visual Representation: Tiling

Let fn+1 be the number of different


ways to tile a 1 × n strip with
squares and dominoes.
square domino
Visual Representation: Tiling

Let fn+1 be the number of different


ways to tile a 1 × n strip with
squares and dominoes.
Visual Representation: Tiling

1 way to tile a strip of length 0 (i.e., f1


= 1).
1 way to tile a strip of length 1 (i.e., f2 = 1):

2 ways to tile a strip of length 2 (i.e., f3 =


2):
fn+1 = fn + fn-1

fn+1 is number of ways to tile length n.

length = (n – 1) : fn ways

fn tilings that start with a square.

length = (n – 2) : fn-1
ways

fn-1 tilings that start with a domino.


fn+1 = fn + fn-1

fn+1 is number of ways to tile length n.

or

fn tilings that ends with a square.

fn-1 tilings that ends with a


domino.
So, fn’s are
Fibonacci numbers.
Now, let’s use this
visual
representation to
prove a couple of
Fibonacci identities.
Fibonacci Identities

Some examples:

F2n = F1 + F3 + F5 + … + F2n-1

Fm+n+1 = Fm+1 Fn+1 + Fm Fn

(Fn)2 = Fn-1 Fn+1 + (-1)n-1


Fm+n+1 = Fm+1 Fn+1 + Fm F n
Fm+n+1: # of ways to tile a strip of length
m+n
There are two distinct cases:
(1) no domino at the fault line
m n

(2) a domino at the fault line


m-1 n-1
Fm+n+1 = Fm+1 Fn+1 + Fm F n

m n

m-1 n-1
(Fn)2 = Fn-1 Fn+1 + (-1)n-1
(Fn)2 = Fn-1 Fn+1 + (-1)n-1

n-1

Fn tilings of a strip of length n-1


(Fn)2 = Fn-1 Fn+1 + (-1)n-1

n-1

n-1
(Fn)2 = Fn-1 Fn+1 + (-1)n-1

(Fn)2 tilings of two strips of size n-


1
(Fn)2 = Fn-1 Fn+1 + (-1)n-1

Draw a vertical “fault


line” at the rightmost
position (<n)
possible without
cutting any
(Fn)2 = Fn-1 Fn+1 + (-1)n-1

Swap the tails at the


fault line to map to a
tiling of 2 (n-1)‘s to a
tiling of an n-2 and an
n.
(Fn)2 = Fn-1 Fn+1 + (-1)n-1
?
n

n-2

Swap the tails at the fault line to map to a


tiling of 2 (n-1)‘s to a tiling of an n-2 and an
(Fn)2 = Fn-1 Fn+1 + (-1)n-1
n
then, (n -1) is odd,
even
originals can’t be tiled
using all dominoes, but
the new ones can,
so, we overcounted 1

n odd
then, (n -1) is even,
originals can be tiled
using all dominoes,
but the new ones
can’t, so we
undercounted 1
Fibonaccis with GF

F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2

i.e., the sequence [0,1,1,2,3,5,8,13…]

is represented by the power series

0 + 1X1 + 1X2 + 2 X3 + 3 X4 + 5 X5 + 8 X6 +…
Two Representations

F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2

A(X) = 0 + 1X1 + 1X2 + 2 X3 + 3 X4 + 5 X5 + 8 X6 +…



=  Fi Xi
i=0

Can we write A(X) more succinctly?


A(X) = F0 + F1 X1 + F2 X2 + F3 X3 + … + Fn Xn +…
X A(X) =0 + F X1 + F X2 + F2 X3 + … + F Xn +…
0 1 n-1

X2 A(X) =0 + 0 X1 + F0 X2 + F1 X3 + … + Fn-2 Xn +…

(1 – X – X2) A(X)

= (F0 – 0 – 0) + (F1 – F0 – 0)X1 + 0


0 1 0
= X
X
A(X) =
(1 – X – X2)
Fibonaccis

F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2

has the generating function

X
A(X) =
(1 – X – X2)

i.e., the coefficient of Xn in A(X) is Fn


Two representations
of the same thing…

F0 = 0, F1 = 1, X
A(X) =
Fn = Fn-1 + Fn-2 (1 – X – X2)
To recap…
Given a sequence V = [ a0, a1, a2, ..., an, … ]

associate a formal power series with it



P(X) =  aiXi
i=0
This is the “generating function” for V

We just used this for the Fibonaccis…


Chapter 4: roadmap
4. 1 Mathematical definition of Fibonacci sequences
4. 2 Properties of Fibonacci sequences
4. 3 Programming
Programs to compute Fib(n)?

Bottom-Up, Iterative Program:


Fib(0) = 0; Fib(1) =1;
Input n;
For k= 2 to n do
Fib(k)=Fib(k-1)+Fib(k-2);
Return Fib(n);
Programs to compute Fib(n)?
Top-Down, Recursive Program:
Return Fib(n);

Procedure Fib(k)
If k=0 return 0
If k=1 return 1

Otherwise return Fib(k-1) + Fib(k-2);


In Python:

def fib(n):
if n<=1 :
return n
return fib(n-1) + fib(n-2)
Programs to compute Fib(n)?
fib function is not an efficient implementation. An improved function, fibgood,
may be written based on the following:

def fibgood(n):
if n<=1 :
return n,0
a,b = fibgood(n-1)
return a+b, a

def fib_wrapper(n):
a,b = fibgood(n)
return a

You might also like