Generating Functions and Counting Trees Today’s
DJ
Published · 54 slides · 0 views
1 / 1
Description
Generating Functions and Counting Trees Todays Plan Generating functions for basic sequences Operations on generating functions Counting Solve recurrences Catalan number Counting Spanning Trees Generating Functions a sequence of numbers a
Related Topics
Share
Embed code
Download this presentation From Below
"Generating Functions and Counting Trees Today’s" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
01
Generating Functionsand Counting Trees<br>
02
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
03
Generating Functions a sequence of numbers a function a polynomial Through this mapping, we can apply our techniques for manipulating functions. Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences<br>
Operations on generating functions
Counting
Solve recurrences<br>
04
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.<br>
05
Simple Examples The pattern here is simple:
the i-th term in the sequence (indexing from 0) is the coefficient of xi in the generating function. What is the generating function for <1,1,1,1,1,1,1,………………………>?<br>
the i-th term in the sequence (indexing from 0) is the coefficient of xi in the generating function. What is the generating function for <1,1,1,1,1,1,1,………………………>?<br>
06
Geometric Series What is the closed form expression of Gn? GnxGn= 1 …… …… …… + …… + …… + ……<br>
07
More Examples These are all closed form generating functions.<br>
08
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
09
Operations on Generating Functions manipulations on sequences manipulations on functions There are a few basic operations we’ll learn. Scaling
Addition
Right shift
Differentiation
Product We can use these operations to get new sequences from known sequences,
and new generating functions from known generating functions.<br>
Addition
Right shift
Differentiation
Product We can use these operations to get new sequences from known sequences,
and new generating functions from known generating functions.<br>
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:<br>
=> scales every term in the associated sequence by the same constant. Multiply the generating function by 2 gives which generates the sequence:<br>
11
Addition Adding generating functions corresponds to adding sequences term by term. The same result as in the previous slide.<br>
12
Right Shift How to generate the sequence <0, 0, …, 0, 1, 1, 1, 1, 1…>? k zeros k zeros Adding k zeros multiplying xk on the generating function.<br>
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!<br>
14
More Differentiation How to generate the sequence <1, 4, 9, 16, 25, …>? Nice idea. But not what we want.<br>
15
More Differentiation How to generate the sequence <1, 4, 9, 16, 25, …>?<br>
16
Product What is the sequence corresponds to the polynomial C(x) = A(x)B(x)?<br>
17
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
18
Counting with Generating Functions General strategy:
coefficient of xn = number of ways to choose n items. A simple example: the coefficient of xn in (1 + x)k is the number of ways
to choose n distinct items from a set of size k.<br>
coefficient of xn = number of ways to choose n items. A simple example: the coefficient of xn in (1 + x)k is the number of ways
to choose n distinct items from a set of size k.<br>
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).<br>
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).<br>
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:<br>
choosing items in a k-element set {a1,a2,…,ak} is:<br>
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: By convolution rule: The generating function for choosing doughnuts with k varieties is:<br>
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:<br>
22
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).<br>
23
Taylor’s Theorem<br>
24
Choosing Doughnuts The generating function for choosing doughnuts with k varieties is: ……<br>
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.<br>
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<br>
• 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<br>
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: GF for pears: GF for fruits: By convolution rule<br>
• 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<br>
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…<br>
29
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
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<br>
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<br>
31
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 It was Fibonacci who was studying rabbit population growth. Rabbit Populations How many rabbits after n months?<br>
rn::= # reproducing pairs after n months
r1 = 1
rn = rn-1 + wn-1
wn = rn-1 so
rn = rn-1 + rn-2 It was Fibonacci who was studying rabbit population growth. Rabbit Populations How many rabbits after n months?<br>
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)<br>
33
R(x)::= r0+r1x+r2x2 +r3x3+…
-xR(x) = - r0x-r1x2 -r2x3 -…
-x2R(x) = -r0x2-r1x3-… 0 Remember Generating Function for Rabbits<br>
-xR(x) = - r0x-r1x2 -r2x3 -…
-x2R(x) = -r0x2-r1x3-… 0 Remember Generating Function for Rabbits<br>
34
Generating Function for Rabbits R(x)::= r0+r1x+r2x2 +r3x3+…
-xR(x) = - r0x-r1x2 -r2x3 -…
-x2R(x) = -r0x2-r1x3 -… 0 0 …<br>
-xR(x) = - r0x-r1x2 -r2x3 -…
-x2R(x) = -r0x2-r1x3 -… 0 0 …<br>
35
Generating Function for Rabbits R(x)::= r0+r1x
-xR(x) = - r0x
-x2R(x) = R(x)-xR(x)-x2R(x) =
r0+r1x-r0x = x<br>
-xR(x) = - r0x
-x2R(x) = R(x)-xR(x)-x2R(x) =
r0+r1x-r0x = x<br>
36
Closed Form for R(x) What is the closed form of rn? So rn = coefficient of xn in R(x)<br>
37
Closed Form for Coefficients So rn = coefficient of xn in R(x)<br>
38
Move1,2(n)::= Move1,3(n-1);
big disk 12;
Move3,2(n-1) Tower of Hanoi http://www.mazeworks.com/hanoi/<br>
big disk 12;
Move3,2(n-1) Tower of Hanoi http://www.mazeworks.com/hanoi/<br>
39
sn::=# steps by Move1,2(n)
sn = 2sn-1 + 1
s0 = 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)<br>
sn = 2sn-1 + 1
s0 = 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)<br>
40
S(x)::= s0+ s1x+ s2x2 + s3x3+…
-2xS(x)= -2s0x-2s1x2 -2s2x3-…
-x/(1-x)= -1¢x - 1¢x2 - 1¢x3-… 0 0 0 … Generating Function sn = 2sn-1 + 1<br>
-2xS(x)= -2s0x-2s1x2 -2s2x3-…
-x/(1-x)= -1¢x - 1¢x2 - 1¢x3-… 0 0 0 … Generating Function sn = 2sn-1 + 1<br>
41
S(x) - 2xS(x) - x/(1-x) = s0 = 0 Closed Form for S(x) What is the closed form of sn? so sn = 2n - 1<br>
42
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
43
Catalan Number Catalan number can be defined recursively by We are going to show this is equal to<br>
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!<br>
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) =<br>
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<br>
47
Catalan Number Therefore R(x) = Note that So rn =<br>
48
Today’s Plan Generating functions for basic sequences
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
Operations on generating functions
Counting
Solve recurrences
Catalan number
Counting Spanning Trees<br>
49
Counting Spanning Trees (Optional) A numbered tree is a tree whose vertex set is {1, 2, . . . , n} for some n >= 2. Given a complete graph of n vertices, how many spanning trees are there?<br>
50
A numbered tree is a tree whose vertex set is {1, 2, . . . , n} for some n >= 2. Given a complete graph of n vertices, how many spanning trees are there? A ::= all the spanning trees of a complete graph of n vertices B::= all sequences of n-2 integers from 1 to n There is a bijective function between A and B !!! Counting Spanning Trees (Optional)<br>
51
Counting Spanning Trees (Optional) A ::= all the spanning trees of a complete graph of n vertices B::= all sequences of n-2 integers from 1 to n How to map a spanning tree to a sequence? If n = 2, stop—the code is the empty sequence.
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree.<br>
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree.<br>
52
If n = 2, stop—the code is the empty sequence.
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 3 4 5 Tree Code 4 3 2<br>
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 3 4 5 Tree Code 4 3 2<br>
53
If n = 2, stop—the code is the empty sequence.
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 3 4 5 Tree Code 6 5 6 2 2 6 7 One tree maps to one code.<br>
Otherwise, find the largest numbered leaf v.
Write down the only neighbour of v.
Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 3 4 5 Tree Code 6 5 6 2 2 6 7 One tree maps to one code.<br>
54
How to map a sequence back to a tree? Counting Spanning Trees (Optional) Code 6 5 6 2 2 Key observation: A number does not appear is a leaf! 1 2 3 4 5 6 7 Tree One code is mapped by one tree.<br>