CS112_week6
CS112_week6
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.
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
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).
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
1 + x1 + x2 + x3 + … + xn + …
⇒ 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
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
1
¢1 + ¢2X + ¢3X + ¢4X +… =
1 2 3
(1-X)3
[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) 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
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),
e.g., c5 = 6
[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 = n+1. <1,2,3,4,…>
Yet another problem...
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,0,1,0,…] ⟷ 1 + x2 + x4 + x6 +… =
Rules
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
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
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
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
length = (n – 1) : fn ways
length = (n – 2) : fn-1
ways
or
Some examples:
F2n = F1 + F3 + F5 + … + F2n-1
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
n-1
n-1
(Fn)2 = Fn-1 Fn+1 + (-1)n-1
n-2
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
0 + 1X1 + 1X2 + 2 X3 + 3 X4 + 5 X5 + 8 X6 +…
Two Representations
F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2
X2 A(X) =0 + 0 X1 + F0 X2 + F1 X3 + … + Fn-2 Xn +…
(1 – X – X2) A(X)
F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2
X
A(X) =
(1 – X – X2)
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, … ]
Procedure Fib(k)
If k=0 return 0
If k=1 return 1
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