0% found this document useful (0 votes)
55 views

Generating Functions

Math

Uploaded by

hanakaleed1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
55 views

Generating Functions

Math

Uploaded by

hanakaleed1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
Chapter 17 Generating Functions Generating functions are one of the most surprising, useful, and clever inventions in dis- crete math. Roughly speaking, generating functions transform problems about sequences into problems about real-valued functions. This is great because we've got piles of math- ‘ematical machinery for manipulating real-valued functions. Thanks to generating func- tions, we can apply all that machinery to problems about sequences. In this way, we can use generating functions to solve all sorts of counting problem. There is a huge chunk of mathematics concerning generating functions, so we will only get a taste of the subject In this lecture, we'll put sequences in angle brackets to more clearly distinguish them from the many other mathemtical expressions floating around. 17.1 Generating Functions ‘The ordinary generating function (OGP) for the infinite sequence (go, 91,92 93 ---) is the formal power series: G(@) = 0+ 18 + G22? + 90? + A generating function is a “formal” power series in the sense that we usually regard x as a placeholder rather than a number. Only in rare cases will we let x be a real number and actually evaluate a generating function, so we can largely forget about questions of convergence. Not all generating functions are ordinary, but those are the only kind we'll consider here. ‘Throughout the lecture, we'll indicate the correspondence between a sequence and its generating function with a double-sided arrow as follows: (Gos 8109268a1-6-) —> Go gre + gaa? + gua + 218 Generating Punctions For example, here are some sequences and their generating functions: {0,0,0,0,...) » 0400+ 0x7+00% +... =0 (1,0,0,0,...) > 1400 +02? +028 +++ = 1 B,2,1,0,...) > 84204 I? +s? +. = 3+ 20407 ‘The pattern here is simple: the i-th term in th in the generating function. sequence (indexing from 0) is the coefficient Recall that the sum of an i finite geometric series is: Lestat This equation does not hold when |2| > 1, but once again we won't worry about conver- gence issues. This formula gives closed-form generating functions for a whole range of sequences, For example: GULL...) oo lteteteatt = Qy-Ll-b..) mo loetat-dtatie = (aa?a’,...) 9 Tbar ate? + a8e3 + {1,0,1,0,1,0,...) + la? +2440 4 17.2. Operations on Generating Functions The magic of generating functions is that we can carry out all sorts of manipulations on sequences by performing mathematical operations on their associated generating func- tions. Let’s experiment with various operations and characterize their effects in terms of sequences. 17.2.1 Scaling Multiplying a generating function by a constant scales every term in the associated se- quence by the same constant. For example, we noted above that: (1,0,1,0,1,0,...) —> 1ta%+at+2%4 Generating Functions 219 Multiplying the generating function by 2 gives a = De Oa Da ie which generates the sequence: Hl (2,0,2,0,2,0...) Rule 12 (Scaling Rule). If (fos fisfar---) ++ F(z), then (efo. Cf ofa, -..) —> &- F(a), Proof. (cfoschis char.) > cfy + ofia-+ ofa? + (Jot fit + fat? +--+) F(z) 17.2.2 Addition “Adding generating functions corresponds to adding the two sequences term by term, For ‘example, aclding two of our earlisr examples gives: (Hei PIES aGeeste yee 4h = a 1 ) - — Lh =k, ae (2% 0% 0 % 0, eee et % % 0% 0, mtn We've now derived two different expressions that both generate the sequence (2, 0,2,0,..-) Not surprisingly, they turn out to be equal: 1 1 _ (+a)+(1-2) 2 =a ive (i-s)G@ta) i-# Rule 13 (Addition Rule). If (fos fis fan.) —> PC), and (90: 91,925---) —> Gle), then (fot 9 fit gn. fot ges ---) —> F(x) + G(2), 220 Generating Functions Proof. (fot 90 fit gts fat 9m -.) > Sn + gn) c= = F(e)+@(e) 17.2.3 Right Shifting ‘Let's start over again with a simple sequence and its generating function: (4,11, Now let's right-shift the sequence by adding k leading zeros: Oe Q I Tdi) oe thee peg atte eae = he trtat tat ye) Evidently, adding k leading zeros to the sequence corresponds to multiplying the gener- ating function by :c*, This holds true in general. Rule 14 (Right-Shift Rule). If (fo, fa, Joy...) —> Fle), then: Qo 105 for fas fay.) —> F(a) Proof. eres (0,0, 750, fos Fs fase=2) fo + fiat + far? ah (f+ fut + fox? + fan® +--+) = &- P(x) Generating Functions 221 17.2.4 Differentiation What happens if we take the derivative of a generating function? As an example, let's differentiate the now-familiar generating function for an infinite sequence of 1's, a giteteraeats. eS 1+ Qe + 3a? + 40° + = (1,284...) We found a generating function for the sequence (1, 2,3,4,..-)! In general, differentiating a generating function has two effects on the corresponding sequence: each term is multiplied by its index and the entire sequence is shifted left one place Rule 15 (Derivative Rule). If (fos fs far fay) — Fl), then (fis 2fa 3fa,--.) —> P'@) Proof. (fil, Blas) = Sr Bf + Bfoa? + = 2 (fo fax + fox? + fax? + a = Fe) a The Derivative Rule is very useful. In fact, there is frequent, independent need for each of differentiation’s two effects, multiplying terms by their index and left-shifting one place. Typically, we want just one effect and must somehow cancel out the other. For ex- ample, let's try to find the generating function for the sequence of squares, (0, 1, 4,9, 16, ...) Ifwe could start with the sequence (1, 1,1, 1,...) and multiply each term by its index two times, then we'd have the desired result (0-0, 1-1, 2-2, 8-3, ...) = (01,49...) A challenge is that differentiation not only multiplies each term by its index, but also shifts the whole sequence left one place. However, the Right-Shift Rule 14 tells how to cancel out this unwanted left-shift: multiply the generating function by = 202 i Generating Functions Our procedure, therefore, is to begin with the generating function for (1,1,1,1,...), differentiate, multiply by x, and then differentiate and multiply by «once more. (LATA...) oe (12,84, ...) 1,2,3,...) — @ WG-2)” G-ayP i d z lta ~ ad (ap Wap lte2 _ 2(1 ese Gea aa (1,4,9,16,...) (0,1,4,9, ‘Thus, the generating function for squares is: 17.3. The Fibonacci Sequence Sometimes we can find nice generating functions for more complicated sequences. For example, here is a generating function for the Fibonacci numbers: (0,1,1,2,3,5,8,18,21,...) — p= The Fibonacci numbers are a fairly nasty bunch, but the generating function is simple! ‘We're going to derive this generating function and then use it to find a closed form for the n-Fibonacci number. Of course, we already have a closed form for Fibonacci numbers, obtained from the cookbook procedure for solving linear recurrences. But there are a couple reasons to cover the same ground again. First, we'll gain some insight into why the cookbook method for linear recurrences works. And, second, the techniques we'll use are applicable to a large class of recurrence equations, including some that we have no other way to tackle. 17.3.1 Finding a Generating Function Let's begin by recalling the definition of the Fibonacci numbers: fo=0 fist fu featfr2 (forn> 2) Generating Functions 23 We can expand the final clause into an infinite sequence of equations. Thus, the Fibonacci numbers are defined by: Now the overall plan is to define a function F(:i) that generates the sequence on the left side of the equality symbols, which are the Fibonacci numbers. Then we derive a function that generates the sequence on the right side. Finally, we equate the two and solve for F(a). Let's try this, First, we define: P(e) = fot far + far? + far? + faa + Now we need to derive a generating function for the sequence: (0.1, fit fos fet fry fat fy ---) One approach is to break this into a sum of three sequences for which we know generating functions and then apply the Addition Rule: (0 4, 0, 0, 0, yo (0, fo, Sy ty Say do + (0, 0, os fu fy +») CO 1th th Ath hth ) — 2+aF@)+eF@) This sequence is almost identical to the right sides of the Fibonacci equations. The one blemish is that the second term is 1 + fo instead of simply 1. However, this amounts to nothing, since fy ~ 0 anyway. Now if we equate F'(r) with the new function «4x F() +.2?F(c), then we're implicitly writing down all of the equations that define the Fibonacci numbers in one fell swoop: F(z) = ft fi tt fp P+ fp Pt fe ate i a) " i fl cteF(x)+2F(2) = OF tf)ot(ht het (hth) + (ft hott Solving for F(x) gives the generating function for the Fibonacei sequence: F(a) = 0+ 2F(c) +2°F(e) > F@)= Sure enough, this is the simple generating function we claimed at the outset! 224 Generating Functions 17.32 Finding a Closed Form Why should one care about the generating function for a sequence? There are several answers, but here is one: if we can find a generating function for a sequence, then we can often find a closed form for the n-th coefficient— which can be pretty useful! For example, a closed form for the coefficient of x” in the power series for «/(1—2 — ®) would be an explicit formula for the n-th Fibonacci number. So our next task is to extract coefficients from a generating function. There are sev- eral approaches. For a generating function that is a ratio of polynomials, we can use the method of partial fractions, which you learned in calculus. Just as the terms in a par- tial fractions expansion are easier to integrate, the coefficients of those terms are easy to compute, Let's try this approach with the generating function for Fibonacci numbers. First, we factor the denominator: 1-2— 22 = (1— a2)(t — ant) where oy = 3(1 + V5) and ay = $(1— v5). Next, we find Ai and Ay which satisfy: w Ar Aa =a- Toa! ioe ‘We do this by plugging in various values of « to generate linear equations in A and Az We can then find A; and As by solving a linear system. This gives: Substituting into the equation above gives the partial fractions expansion of F(s.): © 1 1 1 I-2 ve \i= ae T= ane Each term in the partial fractions expansion has a simple power series given by the geo- metric sum formula: Lt ae+alet +. Trae arta + Lt one + ale? +. Toage 1) + O24 02 Generating Functions 225 Substituting in these series gives a power series for the generating function: 1 Fey- (5 ae 1 aa) =3 Foye + ode? + ++) — (1+ aget ode? + -++)) 5a ne FF “all *y) ‘This is the same scary formula for the n-th Fibonacci number that we found using the method for solving linear recurrences. And this alternate approach sheds some light on that method. In particular, the strange rules involving repeated roots of the characteristic equation are reflections of the rules for finding a partial fractions expansion! 17.4 Counting with Generating Functions Generating functions are particularly useful for solving counting problems. In particular, problems involving choosing items from a set often lead to nice generating functions. When generating functions are used in this way, the coefficient of 2” is the number of ‘ways to choose n items. 17.4.1 Choosing Distinct Items from a Set The generating function for binomial coefficients follows directly from the Binomial The- orem: RY fk) (R ke K K RY oo R\ 5 CC Genoa) > (the er Ghee (De = (+z ‘Thus, the coefficient of x” in (1 -+ «)* is the number of ways fo choose n distinct items from a k-element set. For example, the coefficient of 2? is (5), the number of ways to choose 2 items from a k-element set. Similarly, the coefficient of x** is the number of ways to choose ft + 1 items from a h-element set, which is zero. 17.4.2 Building Generating Functions that Count Often we can translate the description of a counting problem directly into a generating function for the solution. For example, we could figure out that (1 +c) generates the 226 Generating Functions number of ways to select n distinct items from a k-element subset without resorting to the Binomial Theorem or even fussing with binomial coefficients! Here is how. First, consider a single-element set {a1}. The generating function for the number of ways to choose n elements from this set is simply 1 +2: we have 1 way to choose zero elements, 1 way to choose one element, and 0 ways to choose more than one element. Similarly, the number of ways to choose n elements from the set {a2} is also given by the generating function | + «. The fact that the elements differ in the two cases is irrelevant. Now hereis the the main trick: the generating function for choosing elements from a union of disjoint sets is the product of the generating functions for choosing from each set. We'll justify this ina moment, but let's first look at an example. According to this principle, the generating function for the number of ways to choose n elements from the {a1, a2} is: (t+) + (+2) = (14a)? =1420+2 ae OGE for OGFfor — OGE for far} {ax} fanaa} Sure enough, for the set {a1, a2}, we have 1 way to choose zero elements, 2 ways to choose one element, 1 way to choose two elements, and 0 ways to choose more than two ele- ments. Repeated application of this rule gives the generating function for choosing n items from a k-element set {a1,42,...,a4): (Gta) + (+2) Gia) = (tah wer ae OGFfor OGEfor — OGF for OGF for {a} {a2} {ax} {tr @25.-- 5 e) This is the same generating function that we obtained by using the Binomial Theorem. But this time around we translated directly from the counting problem to the generating function. We can extend these ideas to a general principle: Rule 16 (Convolution Rule). Let A(z) be the generating function for selecting items fromt set A, and let B(sr) 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\ B is the product A(x) - B(x) This rule is rather ambiguous: what exactly are the rules governing the selection of items from a set? Remarkably, the Convolution Rule remains valid under samy inter- pretations of selection. For example, we could insist that distinct items be selected or ‘we might allow the same item to be picked a limited number of times or any number of times, Informally, the only restrictions are that (1) the order in which items are selected is disregarded and (2) restrictions on the selection of items from sets A and B also apply in selecting items from AU 8. (Formally, there must be a bijection between n-element selections from AU B and ordered pairs of selections from A and B containing a total of nelements.) Generating Functions 27 Proof. Define: Ale) = A(z): Ble) = > =o i Vane, Ble) = s bya, Oa) = Let's first evaluate the product A(z)- B(x) and express the coefficient cin terms of the aand b coefficients. We can tabulate all of the terms in this product in a table: byt” by) bax’ ayn} aabyx® —agbat! —agba® —anbaar® mr) aybor" ayb,2? aybox? aye? | agbyx ybyx* asx?) ayy" Notice that all terms involving the same power of « lie on a /-sloped diagonal. Collecting these terms together, we find that the coefficient of 2” in the product is: Ca = aby + arbnn + aban +++ + dnbo Now we must show that this is also the number of ways to select n items from AUB. In general, we can select a total of n items from AU B by choosing j items from A and n—j items from B, where j is any number from 0 ton. This can be done in ajby_; ways. ‘Summing over all the possible values of j gives a total of, pba + OPy y+ dadua + + dnbo ways to select n items from AU B. This is precisely the value of c, computed above. The expression cn = dobn+aibn-1+arb cessing course; the sequence (cu, ¢1, ¢2, and (bo, bib2,-+-)- 2++++anby may be familiar from a signal pro- is the convolution of sequences (¢,41,@2,---) ) 17.4.3 Choosing Items with Repetition ‘The first counting problem we considered asked for the number of ways toselect a dozen doughnuts when there were five varieties available. We can generalize this question as follows: in how many ways can we select & items from an n-element set if we're allowed 228 Generating Functions to pick the same item multiples times? In these terms, the doughnut problem asks in how many ways we can select a dozen doughnuts from the set: {chocolate, lemon-iilled, sugar, glazed, plain} if we're allowed to pick several doughnuts of the same variety. Let’s approach this ques- nn from a generating functions perspective. Suppose we choose 1 items (with repetition allowed) from a set containing a single item. Then there is one way to choose zero items, one way to choose one item, one way to choose two items, etc. Thus, the generating function for choosing n elements with repetition from a 1-clement set is: Gyj111,...) — l+ctatsacte 1 rs ‘The Convolution Rule says that the generating function for selecting items from a union of disjoint sets is the product of the generating functions for selecting items from h set each set 4 1. es OGFfor OGFfor — OGF for OGF for {ai} {aa} {ay} {airs aas-++@n} ‘Therefore, the generating function for selecting items from a n-element set with repetition allowed is 1/(1— 2)" Now we need to find the coefficients of this generating function. We could try to use partial fractions, but (1 — 7)" has a nasty repeated root at 1. An alternative is to use Taylor’s Theorem: ‘Theorem 81 (Taylor’s Theorem). $70) 2 5 £0) 5 4g LO Wy, 2 3! Hl F(a) = FO) + FO}e + This theorem says that the k-th coefficient of 1/(1 —:x)" is equal to its k-th derivative evaluated at 0 and divided by k!. And computing the i-th derivative turns out not to be very difficult. Let ‘Then we have: G@'@) =n - 2) Mx) = n(n 41) — 2)? G@"(z) = n(n + 1}(n + 2)(2 = 2-88 GMa) = n(n +1) m+k-1L Generating Functions 229 ‘Thus, the coefficient of 2" in the generating function is: con —™ (ntk-1t ~ Ga et _ (thot a k Therefore, the number of ways to select k items from an n-element set with repetition allowed is: nt k= 1 k This makes sense, since there is a bijection between such selections and (n + k ~ 1)-bit sequences with f zeroes (representing the items) and n — 1 ones (separating the n different types of item), 175 An “Impossible” Counting Problem So far everything we've done with generating functions we could have done another way. But here is an absurd counting problem— really over the top! In how many ways can we fill a bag with n fruits subject to 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: Apples |6 4 4.22.00 Bananas |0 0 0.00 5 5 Oranges }0 2.143 10 Pears |0 010101 ‘These constraints are so complicated that the problem seems hopeless! But let's see what generating functions reveal. Let's first construet a generating function for selecting apples. We can select a set of O apples in one way, a set of 1 apples in zero ways (since the number of apples must be 230 Generating Functions even), a set of 2 applies in one way, a set of 3 apples in zero ways, and so forth. So we have: Ala) = 102428 4a 4. Similarly, the generating function for selecting bananas is: Bix) =1Lt e+e 4a + Now, we can select a set of 0 oranges in one way, a set of 1 orange in one ways, and so on. However, we can not select more than four oranges, so we have the generating function: 1-a* O(z) =1l+a+27 425454 Here we're using the geometric sum formula. Finally, we can select only zero or one pear, so we have: P(r)=1+e2 ‘The Convolution Rule says that the generating function for selecting from among all four kinds of fruit is: Ale) B(2)0(2) P(x) (~2P 4+ 2n+ 3a? + 48 4 Almost everything cancels! We're left with 1/(1 —2)?, which we found a power series for tarlier: the coefficient of 2” is simply n+ 1. Thus, the number of ways to form a bag of n fruits is just n_+ 1. This is consistent with the example we worked out, since there were 7 different fruit bags containing 6 fruits. Amazing!

You might also like