Professional Documents
Culture Documents
AA03 GFs
AA03 GFs
PART ONE
3. Generating Functions
http://aofa.cs.princeton.edu
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3a.GFs.OGFs
Ordinary generating functions
Definition. !
( )= is the ordinary generating function (OGF)
≥
sequence OGF
!
1, 1, 1, 1, 1, ... =
−
≥
!
1, 1/2, 1/6, 1/24, ... = [zN ]ez = 1/N !
!
≥
sequence OGF
!
1, 1, 1, 1, 1, ... =
−
≥
!
1, 2, 4, 8, 16, 32, ... = [ ] =
− −
≥
4
Operations on OGFs: Addition
!
If ( )= is the OGF of , , ,..., ,...
≥
!
and ( )= is the OGF of , , ,..., ,...
≥
Example:
sequence OGF
!
1, 1, 1, 1, 1, ... =
−
≥
!
1, 2, 4, 8, 16, 32, ... =
−
≥
OGF sequence
!
= 1, 1, 1, 1, 1, ...
−
≥
!
= 0, 1, 2, 3, 4, 5, ...
( − )
≥
!" #
= 0, 0, 1, 3, 6, 10, ...
( − )
≥
!" #
+
= 0, ..., 1, M+1, (M+2)(M+1)/2, ...
( − )
≥
+
+
= 1, M+1, (M+2)(M+1)/2, ...
( )
6
Operations on OGFs: Integration
!
If ( )= is the OGF of , , ,..., ,...
≥
! " − −
then () = is the OGF of , , , ,..., ,...
≥
!
= 1, 1, 1, 1, 1, ...
−
≥
!
ln = 0, 1, 1/2, 1/3, 1/4, 1/5, ...
−
≥
7
Operations on OGFs: Partial sum
!
If ( )= is the OGF of , , ,..., ,...
≥
Proof. ! !
( )=
−
≥ ≥
!!
+
Distribute
=
≥ ≥
!!
Change n to n−k = −
≥ ≥
!" ! #
Switch order of summation. = −
≥ ≤ ≤
!" ! #
Change k to n−k =
≥ ≤ ≤
8
Operations on OGFs: Partial sum
!
If ( )= is the OGF of , , ,..., ,...
≥
( )=
=
+
!
= 1, 1, 1, 1, 1, ...
−
≥
!
ln = 0, 1, 1/2, 1/3, 1/4, 1/5, ...
−
≥
9
Operations on OGFs: Convolution
!
If ( )= is the OGF of , , ,..., ,...
≥
Proof.
( ) ( )=
+
Distribute =
Change n to n−k =
10
Operations on OGFs: Convolution
!
If ( )= is the OGF of , , ,..., ,...
≥
!
= 1, 1, 1, 1, 1, ...
−
≥
!
= ( + ) 1, 2, 3, 4, 5, ...
( − )
≥
11
Expanding a GF (summary)
= + + + + + ...
! ! !
Example.
! Integrate to get ln
− −
[ ] ln = =
( − ) −
≤ ≤ then convolve with ln
− −
12
In-class exercise
ln
( − ) −
13
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3a.GFs.OGFs
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3b.GFs.recurrences
Solving recurrences with OGFs
General procedure:
sequence
16
Solving recurrences with GFs
For linear recurrences with constant coefficients, the GF equation is a polynomial, so
the general procedure is a algorithm.
Example 4 from previous lecture.
= = =
Solve. ( )=
+
Use partial fractions: ( )= +
solution must be of the form
+ =
Solve for coefficients.
+ =
Expand. =
17
Solving linear recurrences with GFs
For linear recurrences with constant coefficients, the GF equation is a polynomial, so
the general procedure is a algorithm.
Example with multiple roots.
= + = , = =
Solve. ( )=
+
( )
Simplify. ( )= =
( )( ) ( )
= + = , = =
( )=
Solve. +
Simplify. ( )= =
( )( + ) ( + )
Expand. = ( +( ) )= ( +( ) )
Solution to = − + − + ... + −
Solution is
! ! !
β + β + ... + β t terms
≤< ≤< ≤<
= + +
homogeneous equation
Evaluate sums to get an ( ) ( )= ( )/( )
ordinary differential equation ( )= +
( ) solution (integration factor)
( ) = /( )
Solve the ODE. (( ) ( )) = ( ) ( ) ( ) ( )
( )
=( ) ( ) =
Integrate. ( )= ln
( )
Expand. =[ ] ln = ( + )( + )
( )
21
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3b.GFs.recurrences
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3c.GFs.Catalan
Catalan numbers
Tk TN−1−k
T0 = 1 T1 = 1
T2 = 2
T3 = 5
= +
<
T4=14
24
Catalan numbers
+-+-+--
+-++---
- +-- +-+--
++-+---
++---
T0 = 1 T1 = 1
Tk TN−1−k T2 = 2
++--+--
+++----
T3 = 5
= +
<
T4=14
25
Catalan numbers
Tk TN−1−k
= +
<
26
Catalan numbers
Tk TN−1−k
= +
<
27
Solving the Catalan recurrence with GFs
!
Recurrence that holds for all N. = − − +δ
≤ <
! ! !
Multiply by zN and sum. ( )≡ = − − +
≥ ≥ ≤ <
!!
Switch order of summation ( )= + − −
≥ >
convolution
+ + (backwards)
Change N to N+k+1 ( )= +
Distribute. ( )= +
( )= + ( )
28
Common-sense rule for working with GFs
It is always worthwhile to check your math with your computer.
( )= + + + + + ...
Check:
( )= + ( )
29
Solving the Catalan recurrence with GFs (continued)
Functional GF equation. ( )= + ( )
+
Set coefficients equal = ( )
+
+
( )( )...( )( )
Expand via definition. =
( + )!
· · ···( )·
Distribute (−2)N among factors. =
( + )!
· · ···( ) · · ···
Substitute (2/1)(4/2)(6/3)... for 2N. =
+ ! · · ···
=
+
30
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3c.GFs.Catalan
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3d.GFs.EGFs
Exponential generating functions (EGFs)
Definition. !
( )= is the exponential generating function (EGF)
!
≥
sequence EGF
!
1, 1, 1, 1, 1, ... =
!
≥
!
1, 2, 4, 8, 16, 32, ... =
!
≥
!
1, 1, 2, 6, 24, 120 ... ! =
! −
≥
33
Operations on EGFs: Binomial convolution
and ( )=
!
is the EGF of , , ,..., ,... 𝚺
0≤k≤n
Proof.
( ) ( )=
! !
+
Distribute. =
! !
Change n to n−k =
!( )!
Choice of EGF vs. OGF is typically dictated naturally from the problem.
Example. =
( / )
Distribute. ( )=
! ! convergence
not assured
+ / + / + / +...
Evaluate and telescope. ( )= ( / )= =
Check.
Expand. =
=
35
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3d.GFs.EGFs
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3e.GFs.counting
Counting with generating functions
Example.
T ≡ set of all binary trees
|t | ≡ number of internal nodes in t ∈ T
TN ≡ number of t ∈ T with |t | = N
! !
||
( )≡ =
∈T ≥
!!
| |+| |+
Decompose from definition ( )= + tL tR
∈ ∈
!" #!" # tL ●nodes tR ●nodes
| | | |
Distribute = +
∈ ∈
= + ( )
38
Combinatorial view of Catalan GF
( )= + + + + + + + + + ...
= + + + + ...
( )= + ( )
= + ( + + + + . . .)( + + + + . . .)
= + + + + + + + + + ...
39
Values of parameters ("costs")
are often the object of study in the analysis of algorithms.
leaf
40
Computing expected costs by counting
An alternative (combinatorial) view of probability
• Define a class of combinatorial obects.
• Model: All objects of size N are equally likely
P
| | P
P | |=
( )
P | |=
P
| | P
P | |=
( )
! !
| |
Counting GF ( )≡ =
∈P ≥
! !!
| |
Cumulative cost GF ( )≡ ( ) =
∈P ≥ ≥
| |
Cumulative cost GF. ( )= ( )
0 b’
1 b’
| |+
= + · ( )
= ( )+ ( )
= !
( − ) = ( )
( − )
≥
−
[ ] ( )
Average # 1 bits in a random
bitstring of length N. [ ] ( )
= = ✓
43
Leaves in binary trees
are internal nodes whose children are both external.
Definitions:
TN is the # of binary trees with N nodes.
tNk is the # of N-node binary trees with k leaves
CN is the average # of leaves in a random N-node binary tree
C0 = 0
t11=1
C1 = 1
t21=2
C2 = 1
t41=8
t42=6
t31=4 C4 = 1.42857
t32=1
C3 = 1.2
||
Counting GF. ( )= = =
+
T
||
Cumulative cost GF. ( )= ()
||
CGF. ( )= ()
tL tR
tL nodes tR nodes
leaves(tL) leaves leaves(tR) leaves
| |+| |+
( )= + ( )+ ( )
! ! ! !
| | | | | | | |
= + ( ) + ( )
∈ ∈ ∈ ∈
= + ( ) ( )
46
How many leaves in a random binary tree?
||
CGF. ( )= ()
| |+| |+
Decompose from definition. ( )= + ( )+ ( )
= + ( ) ( )
( + )· ·
Compute average number of leaves. / = = /
+
( )
47
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
• OGFs
• Solving recurrences
• Catalan numbers
OF
• EGFs
• Counting with GFs
http://aofa.cs.princeton.edu
3e.GFs.counting
Exercise 3.20
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
49
Exercise 3.28
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
50
Assignments for next lecture
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
51
A N A LY T I C C O M B I N AT O R I C S
PART ONE
3. Generating Functions
http://aofa.cs.princeton.edu