Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generating Functions and Counting Trees. Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve.

Similar presentations


Presentation on theme: "Generating Functions and Counting Trees. Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve."— Presentation transcript:

1 Generating Functions and Counting Trees

2 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

3 Generating Functions a sequence of numbersa 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 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 x i in the generating function. What is the generating function for ?

6 Geometric Series What is the closed form expression of G n ? G n  xG n =1 …… + ……

7 More Examples These are all closed form generating functions.

8 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

9 Operations on Generating Functions manipulations on sequencesmanipulations 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 ? k zeros Adding k zeros  multiplying x k on the generating function.

13 Differentiation How to generate the sequence ? The generating function is How to obtain a closed form of this function? We found a generating function for the sequence of positive integers!

14 More Differentiation How to generate the sequence ? Nice idea. But not what we want.

15 More Differentiation How to generate the sequence ?

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

17 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

18 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.

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: By convolution rule: The generating function for choosing doughnuts with k varieties is:

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).

23 Taylor’s 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: GF for pears: GF for fruits: By convolution rule

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 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

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

31 w n ::= # newborn pairs after n months r n ::= # reproducing 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?

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 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

34 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 …

35 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

36 Closed Form for R(x) What is the closed form of r n ? So r n = coefficient of x n in R(x)

37 Closed Form for Coefficients So r n = coefficient of x n in R(x)

38 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/

39 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)

40 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 00 … Generating Function s n = 2s n-1 + 1

41 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

42 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

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) = 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!

45 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) =

46 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

47 Catalan Number Therefore R(x) = Note that So r n =

48 Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve recurrences 5.Catalan number 6.Counting Spanning Trees

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?

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)

51 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? 1.If n = 2, stop—the code is the empty sequence. 2.Otherwise, find the largest numbered leaf v. 3.Write down the only neighbour of v. 4.Delete v, and continue the process on the resulting smaller tree.

52 1.If n = 2, stop—the code is the empty sequence. 2.Otherwise, find the largest numbered leaf v. 3.Write down the only neighbour of v. 4.Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 345 Tree Code 43 2

53 1.If n = 2, stop—the code is the empty sequence. 2.Otherwise, find the largest numbered leaf v. 3.Write down the only neighbour of v. 4.Delete v, and continue the process on the resulting smaller tree. Counting Spanning Trees (Optional) 1 2 3 4 5 Tree Code 65622 6 7 One tree maps to one code.

54 How to map a sequence back to a tree? Counting Spanning Trees (Optional) Code 65622 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.


Download ppt "Generating Functions and Counting Trees. Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve."

Similar presentations


Ads by Google