Generating Functions and Counting Trees

 
Generating Functions
and Counting Trees
 
Today’s Plan
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
5.
Catalan number
Generating Functions
a sequence of numbers
a function
a polynomial
 
Through this mapping, we can apply our techniques for manipulating functions.
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
Ordinary Generating Functions
Given a sequence <g0,g1,g2,g3,………>
 
the 
ordinary generating function
 is:
 
We use a double sided arrow to indicate the correspondence.
Simple Examples
The pattern here is simple:
the i-th term in the sequence (indexing from 0) is
the coefficient of x
i
 in the generating function.
What is the generating function for <1,1,1,1,1,1,1,………………………>?
Geometric Series
What is the closed form expression of G
n
?
 
G
n
xG
n
=
 
1
……
……
……
+ ……
+ ……
+ ……
More Examples
These are all 
closed form
 generating functions.
 
Today’s Plan
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
5.
Catalan number
Operations on Generating Functions
manipulations on sequences
manipulations on functions
 
There are a few basic operations we’ll learn.
 
1.
Scaling
2.
Addition
3.
Right shift
4.
Differentiation
5.
Product
 
We can use these operations to get new sequences from known sequences,
and new generating functions from known generating functions.
Scaling
Multiplying a generating function by a constant
=> scales every term in the associated sequence by the same constant.
Multiply the generating function by 2 gives
which generates the sequence:
Addition
Adding generating functions corresponds to adding sequences term by term.
The same result as in the previous slide.
Right Shift
 
How to generate the sequence   <0, 0, …, 0, 1, 1, 1, 1, 1…>?
 
k zeros
 
k zeros
Adding 
k
 zeros 
 multiplying x
k
 on the generating function.
Differentiation
How to generate the sequence   <1, 2, 3, 4, 5, …>?
The generating function is
How to obtain a closed form of this function?
We found a generating function for the sequence <1,2,3,…> of positive integers!
More Differentiation
How to generate the sequence   <1, 4, 9, 16, 25, …>?
Nice idea.  But not what we want.
More Differentiation
How to generate the sequence   <1, 4, 9, 16, 25, …>?
Product
What is the sequence corresponds to the polynomial C(x) = A(x)B(x)?
 
Today’s Plan
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
5.
Catalan number
Counting with Generating Functions
General strategy:
coefficient of x
n
 = number of ways to choose n items.
 
A simple example:
the coefficient of x
n
 in (1 + x)
k
 is the number of ways
to choose n distinct items from a set of size k.
Convolution Rule
Let A(x) be the generating function for selecting items from set A.
 Let B(x) be the generating function for selecting items from set B.
If A and B are disjoint,
then the generating function for selecting items from the union A U B
is the product A(x) · B(x).
Choosing Subsets
Choose n items from k distinct elements {a1, a2, …, ak}
How many ways to choose from single element set {a1}?
 
There is one way to choose 0 item, one way to choose 1 element.
 
So the generating function for {a1} is (1+x)
 
So the generating function for {a2} is (1+x)
 
……………
 
By convolution rule, the generating function for
choosing items in a k-element set {a1,a2,…,ak} is:
Choosing Doughnuts
How many ways can we select n doughnuts with k varieties?
Suppose there is only chocolate doughnuts.
How many ways can we select n doughnuts?
 
Well there is only one way to choose zero, one, two, three, ……, chocolate doughnuts.
So the generating function for choosing chocolate doughnuts is:
By convolution rule:
The generating function for choosing doughnuts with k varieties is:
By convolution rule:
The generating function for choosing doughnuts with k varieties is:
Choosing Doughnuts
Now what?  How do we obtain the answer?
Taylor’s Theorem
where f
(n)
(x) is the n-th derivative of f(x).
Taylor’s Theorem
Choosing Doughnuts
The generating function for choosing doughnuts with k varieties is:
 
……
Choosing Doughnuts
The generating function for choosing doughnuts with k varieties is:
The number of ways to choose n doughnuts with k varieties is:
This is what we get before.  Now there is a general method to derive it.
Choosing Fruits
This is an “impossible” counting problem…
How many ways can we fill a bag with n fruits with the following constraints?
 
• The number of apples must be even.
• The number of bananas must be a multiple of 5.
• There can be at most four oranges.
• There can be at most one pear.
For example, there are 7 ways to form a bag with 6 fruits
Choosing Fruits
• The number of apples must be even.
• The number of bananas must be a multiple of 5.
• There can be at most four oranges.
• There can be at most one pear.
GF for apples:
GF for bananas:
GF for oranges:
GF for pears:
GF for fruits:
By convolution rule
Choosing Fruits
Generating function for fruits:
How many ways can we fill a bag with n fruits with the following constraints?
The answer is exactly n+1!
 
We solve an impossible counting problem in a routine way…
 
Today’s Plan
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
5.
Catalan number
Solving Recurrences with Generating Functions
The Rabbit Population
 
A mature boy/girl rabbit pair reproduces every month.
Rabbits mature after one month.
  
w
n
::= # ne
w
born pairs after n months
  
r
n
::= # 
r
eproducing pairs after n months
Start with a newborn pair:   
w
0 
=1, 
r
0 
= 0
 
w
n
::= # ne
w
born pairs after n months
r
n
::= # 
r
eproducing pairs after n  months
                      
r
1
 
=
 1
                      
r
n
 
=
 r
n-1
 + 
w
n-1
                      
w
n
 = 
r
n-1   
so
                      r
n
 
=
 r
n-1
 + 
r
n-2
It was 
Fibonacci
 who was studying rabbit population growth.
Rabbit Populations
How many rabbits after n months?
Fibonacci Sequence
The Fibonacci sequence we want to analyze is:
Define a generating function for this sequence:
 
Remember
First we want to obtain a closed form for R(x)
 
R(x)::= r
0
+r
1
x+r
2
x
2 
+r
3
x
3
+…
-xR(x)  =
 
-
 
r
0
x-r
1
x
2 
-r
2
x
3
 
-…
-x
2
R(x)  =      -r
0
x
2
-r
1
x
3
-…
 
0
Remember
Generating Function for Rabbits
 
Generating Function for Rabbits
 
R(x)::= r
0
+r
1
x+r
2
x
2 
+r
3
x
3
+
-xR(x)  =
 
-
 
r
0
x-r
1
x
2 
-r
2
x
3
 
-
-x
2
R(x) =       -r
0
x
2
-r
1
x
3  
-
 
0
 
0      
Generating Function for Rabbits
R(x)::= r
0
+r
1
x
-xR(x)  =
 
-
 
r
0
x
-x
2
R(x) =       
 
R(x)-xR(x)-x
2
R(x) =
            r
0
+r
1
x-r
0
x = x
Closed Form for R(x)
What is the closed form of r
n
?
So r
n 
= coefficient of x
n
 in R(x)
Closed Form for Coefficients
So r
n 
= coefficient of x
n
 in R(x)
 
Move
1,2
(n)::= Move
1,3
(n-1);
                    big disk 1
2;
                    Move
3,2
(n-1)
Tower of Hanoi
http://www.mazeworks.com/hanoi/
 
s
n
::=# steps by Move
1,2
(n)
   s
n
 = 2s
n-1
 + 1
   s
0
 = 0
Generating Function
 
The sequence we want to analyze is:
Define a generating function for this sequence:
First we want to obtain a closed form for S(x)
 
S(x)::= s
0
+  s
1
x+  s
2
x
2 
+ s
3
x
3
+…
-2xS(x)= -2s
0
x-2s
1
x
2 
-2s
2
x
3
-…
-x/(1-x)=   -1
¢
x - 1
¢
x
2  
-  1
¢
x
3
-…
 
0
 
0
 
0     …
Generating Function
 s
n
 = 2s
n-1
 + 1
S(x) - 2xS(x)
 
- x/(1-x) = s
0
 = 0
Closed Form for S(x)
What is the closed form of s
n
?
so    
s
n
 = 2
n
 - 1
 
Today’s Plan
 
1.
Generating functions for basic sequences
2.
Operations on generating functions
3.
Counting
4.
Solve recurrences
5.
Catalan number
Catalan Number
Catalan number can be defined recursively by
 
We are going to show this is equal to
Catalan Number
Consider the generating function r(x) = r
0
 + r
1
x + r
2
x
2
 + …
 
Recall that
How to generate the right hand side?
 
This is just the convolution rule!
r
0
 + r
1
x + r
2
x
2
 + …    = 1 + x(r
0
 + r
1
x + r
2
x
2
 + … )(r
0
 + r
1
x + r
2
x
2
 + … )
 
Notice that by the recursive formula, LHS = RHS!
Catalan Number
r
0
 + r
1
x + r
2
x
2
 + …    = 1 + x(r
0
 + r
1
x + r
2
x
2
 + … )(r
0
 + r
1
x + r
2
x
2
 + … )
 
Let R(x) = r
0
 + r
1
x + r
2
x
2
 + …
 
Then the above equation implies that
 
R(x) = 1 + x(R(x))
2
 
Solving the quadratic equation
 
x(R(x))
2 
- R(x) + 1 = 0
 
We get R(x) =
Catalan Number
We get R(x) = 
 
We know that when x tends to 0, then R(x) should tend to r
0
 = 1.
 
So we must have R(x) =
 
Now it remains to calculate the coefficients of this polynomial.
 
Note that
Catalan Number
 
Therefore R(x) =
Note that 
 
So r
n
 =
Slide Note
Embed
Share

Today's plan involves generating functions for basic sequences, operations on generating functions, counting, solving recurrences, and exploring Catalan numbers. Learn how to manipulate functions, find closed-form expressions, and perform various operations on sequences and functions.

  • Mathematics
  • Generating Functions
  • Sequences
  • Recurrences
  • Catalan Numbers

Uploaded on Feb 20, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Generating Functions and Counting Trees

  2. Todays Plan 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences 5. Catalan number

  3. Generating Functions a sequence of numbers a function a polynomial Through this mapping, we can apply our techniques for manipulating functions. 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences

  4. Ordinary Generating Functions Given a sequence <g0,g1,g2,g3, > the ordinary generating function is: We use a double sided arrow to indicate the correspondence.

  5. Simple Examples The pattern here is simple: the i-th term in the sequence (indexing from 0) is the coefficient of xiin the generating function. What is the generating function for <1,1,1,1,1,1,1, >?

  6. Geometric Series G ::= 1+ x+ x + +x +x 2 n-1 n + n What is the closed form expression of Gn? G ::= = 1+ x+ x + x+x +x + +x +x +x + x 2 n-1 n + n xG 2 3 n n+1 + n Gn xGn= 1 1-x 1-x n+1 G = n

  7. More Examples These are all closed form generating functions.

  8. Todays Plan 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences 5. Catalan number

  9. Operations on Generating Functions manipulations on sequences manipulations on functions There are a few basic operations we ll learn. 1. Scaling 2. Addition 3. Right shift 4. Differentiation 5. Product We can use these operations to get new sequences from known sequences, and new generating functions from known generating functions.

  10. Scaling Multiplying a generating function by a constant => scales every term in the associated sequence by the same constant. Multiply the generating function by 2 gives which generates the sequence:

  11. Addition Adding generating functions corresponds to adding sequences term by term. The same result as in the previous slide.

  12. Right Shift How to generate the sequence <0, 0, , 0, 1, 1, 1, 1, 1 >? k zeros k zeros Adding k zeros multiplying xkon the generating function.

  13. Differentiation How to generate the sequence <1, 2, 3, 4, 5, >? The generating function is How to obtain a closed form of this function? We found a generating function for the sequence <1,2,3, > of positive integers!

  14. More Differentiation How to generate the sequence <1, 4, 9, 16, 25, >? Nice idea. But not what we want.

  15. More Differentiation How to generate the sequence <1, 4, 9, 16, 25, >?

  16. Product What is the sequence corresponds to the polynomial C(x) = A(x)B(x)?

  17. Todays Plan 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences 5. Catalan number

  18. Counting with Generating Functions General strategy: coefficient of xn= number of ways to choose n items. A simple example: the coefficient of xnin (1 + x)kis the number of ways to choose n distinct items from a set of size k.

  19. Convolution Rule Let A(x) be the generating function for selecting items from set A. Let B(x) be the generating function for selecting items from set B. If A and B are disjoint, then the generating function for selecting items from the union A U B is the product A(x) B(x).

  20. Choosing Subsets Choose n items from k distinct elements {a1, a2, , ak} How many ways to choose from single element set {a1}? There is one way to choose 0 item, one way to choose 1 element. So the generating function for {a1} is (1+x) So the generating function for {a2} is (1+x) By convolution rule, the generating function for choosing items in a k-element set {a1,a2, ,ak} is:

  21. Choosing Doughnuts How many ways can we select n doughnuts with k varieties? Suppose there is only chocolate doughnuts. How many ways can we select n doughnuts? Well there is only one way to choose zero, one, two, three, , chocolate doughnuts. So the generating function for choosing chocolate doughnuts is: The generating function for choosing doughnuts with k varieties is: By convolution rule:

  22. Choosing Doughnuts The generating function for choosing doughnuts with k varieties is: By convolution rule: Now what? How do we obtain the answer? Taylor s Theorem where f(n)(x) is the n-th derivative of f(x).

  23. Taylors Theorem

  24. Choosing Doughnuts The generating function for choosing doughnuts with k varieties is:

  25. Choosing Doughnuts The generating function for choosing doughnuts with k varieties is: The number of ways to choose n doughnuts with k varieties is: This is what we get before. Now there is a general method to derive it.

  26. Choosing Fruits This is an impossible counting problem How many ways can we fill a bag with n fruits with the following constraints? The number of apples must be even. The number of bananas must be a multiple of 5. There can be at most four oranges. There can be at most one pear. For example, there are 7 ways to form a bag with 6 fruits

  27. Choosing Fruits The number of apples must be even. The number of bananas must be a multiple of 5. There can be at most four oranges. There can be at most one pear. GF for apples: GF for bananas: GF for oranges: By convolution rule GF for pears: GF for fruits:

  28. Choosing Fruits Generating function for fruits: How many ways can we fill a bag with n fruits with the following constraints? The answer is exactly n+1! We solve an impossible counting problem in a routine way

  29. Todays Plan 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences 5. Catalan number

  30. Solving Recurrences with Generating Functions The Rabbit Population A mature boy/girl rabbit pair reproduces every month. Rabbits mature after one month. wn::= # newborn pairs after n months rn::= # reproducing pairs after n months Start with a newborn pair: w0 =1, r0 = 0

  31. Rabbit Populations wn::= # newborn pairs after n months rn::= # reproducing pairs after n months r1= 1 rn= rn-1+ wn-1 wn= rn-1 so rn= rn-1+ rn-2 How many rabbits after n months? It was Fibonacci who was studying rabbit population growth.

  32. Fibonacci Sequence The Fibonacci sequence we want to analyze is: Define a generating function for this sequence: Remember First we want to obtain a closed form for R(x)

  33. Generating Function for Rabbits R(x)::= r0+r1x+r2x2 +r3x3+ -xR(x) =-r0x-r1x2 -r2x3- -x2R(x) = -r0x2-r1x3- 0 Remember

  34. Generating Function for Rabbits R(x)::= r0+r1x+r2x2 +r3x3+ -xR(x) =-r0x-r1x2 -r2x3- -x2R(x) = -r0x2-r1x3 - 0 0

  35. Generating Function for Rabbits R(x)::= r0+r1x -xR(x) =-r0x -x2R(x) = R(x)-xR(x)-x2R(x) = r0+r1x-r0x = x

  36. Closed Form for R(x) ? ? ? = 1 ? ?2 What is the closed form of rn? So rn = coefficient of xnin R(x)

  37. Closed Form for Coefficients ? ? ? = 1 ? ?2 ? =1 + 5 ? =1 5 2 2 So rn = coefficient of xnin R(x)

  38. Tower of Hanoi Post #1 Post #2 Post #3 Move1,2(n)::= Move1,3(n-1); big disk 1 2; Move3,2(n-1) http://www.mazeworks.com/hanoi/

  39. Generating Function sn::=# steps by Move1,2(n) sn= 2sn-1+ 1 s0= 0 The sequence we want to analyze is: Define a generating function for this sequence: First we want to obtain a closed form for S(x)

  40. Generating Function S(x)::= s0+ s1x+ s2x2 + s3x3+ -2xS(x)= -2s0x-2s1x2 -2s2x3- -x/(1-x)= -1 x - 1 x2 - 1 x3- 0 0 0 sn= 2sn-1+ 1

  41. Closed Form for S(x) S(x) - 2xS(x) - x/(1-x) = s0= 0 What is the closed form of sn? so sn= 2n- 1

  42. Todays Plan 1. Generating functions for basic sequences 2. Operations on generating functions 3. Counting 4. Solve recurrences 5. Catalan number

  43. Catalan Number Catalan number can be defined recursively by We are going to show this is equal to

  44. Catalan Number Consider the generating function r(x) = r0+ r1x + r2x2+ Recall that How to generate the right hand side? This is just the convolution rule! r0+ r1x + r2x2+ = 1 + x(r0+ r1x + r2x2+ )(r0+ r1x + r2x2+ ) Notice that by the recursive formula, LHS = RHS!

  45. Catalan Number r0+ r1x + r2x2+ = 1 + x(r0+ r1x + r2x2+ )(r0+ r1x + r2x2+ ) Let R(x) = r0+ r1x + r2x2+ Then the above equation implies that R(x) = 1 + x(R(x))2 Solving the quadratic equation x(R(x))2 - R(x) + 1 = 0 We get R(x) =

  46. Catalan Number We get R(x) = We know that when x tends to 0, then R(x) should tend to r0= 1. So we must have R(x) = Now it remains to calculate the coefficients of this polynomial. Note that

  47. Catalan Number Note that Therefore R(x) = So rn=

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#