Massachusetts Institute of Technology Lecture 17 6.042J/18.062J: Mathematics for Computer Science 30 Oct 97 Professor Tom Leighton

Lecture Notes

This is the last lecture on counting the number of items in a finite set. The topic today is generating functions. Generating functions are one of the most surprising, useful, and clever inventions in discrete math. There is a huge chunk of mathematics concerning generating functions, so we will only get a taste of the subject today. Generating functions will come up again later, though, when we cover probability.

1 Generating Functions

Here is the idea behind generating functions. We can represent a sequence of numbers as a power series. A power series defines a function. This lets us manipulate sequences of numbers with all the tools that we have for working with functions.

Definition The ordinary generating function (OGF) of the infinite sequence < g_{0}, g_{1}, g_{2}, g_{3} … > is the formal power series:

g_{0} + g_{1}x + g_{2}x^{2} + g_{3}x^{3} + …

This definition requires a few comments. All of the generating functions in this lecture will be ordinary, so this is the only definition that we will need. A generating function is a “formal” power series in that we typically regard x as a placeholder, not as a number. In particular, the convergence of the power series is almost never an issue, since we will almost never evaluate the power series for a particular x. Throughout the lecture, we will indicate the correspondence between a sequence and its generating function with a double-sided arrow as follows:

< g_{0}, g_{1}, g_{2}, g_{3}, … >↔ g_{0} + g_{1}x + g_{2}x^{2} + g_{3}x^{3} + …

Here are some basic examples of sequences and the corresponding generating functions:

< 0, 0, 0, 0, … > ↔ 0 + 0x + 0x^{2} + 0x^{3} … = 0
< 1, 0, 0, 0, … > ↔ 1 + 0x + 0x^{2} + 0x^{3} … = 1
< 1, 1, 0, 0, … > ↔ 1 + 1x + 0x^{2} + 0x^{3} … = 1 + x
< 1, 1, 1, 1, … > ↔ 1 + 1x + 1x^{2} + 1x^{3} … = \frac{1}{1 − x}

These examples follow directly from the definition; the i-th term in the sequence is the coefficient of x^{i} in the generating function. The last example has an interesting feature. Recall that 1 + x + x^{2} + x^{3} + … is an infinite geometric sequence with sum \frac{1}{1−x}. As a result, we can represent the infinite sequence of 1’s with a simple, closed-form expression! This generating function arises frequently and is good to remember. Below are some more tricky examples of sequences and their generating functions.

< 1, a, a^{2}, a^{3}, … > ↔ = 1 + ax + a^{2}x^{2} + a^{3}x^{3} …

= 1 + ax + (ax)^{2} + (ax)^{3} …

= \frac{1}{1 − ax}

< 1, −1, 1, −1, … > ↔ = \frac{1}{1 + x}
< 1, 0, 1, 0, … > ↔ = 1 + x^{2} + x^{4} + …

= \frac{1}{1 − x^{2}}

In the first and third examples, the closed forms for the power series follow from the formula for the sum of an infinite geometric sequence. The second example is a special case of the first with a = −1.

2 Operations on Sequences

The magic of generating functions is that they allow us to manipulate sequences of numbers with all the tools usually applied to functions. The sections below show how operations on sequences correspond to operations on their generating functions.

2.1 Adding Sequences

Adding sequences term-by-term corresponds to adding the generating functions. For example:

< 1, 1, 1, 1, … >  ↔ \frac{1}{1−x}
+ < 1, −1, 1, −1, … > ↔ \frac{1}{1+x}
< 2, 0, 2, 0, … >  ↔ \frac{1}{1−x} + \frac{1}{1+x} = \frac{2}{1−x^{2}}
⇒ < 1, 0, 1, 0, … >  ↔ \frac{1}{1−x^{2}}

The generating functions of the first two sequences were computed in the preceding section. On the third line, we sum the sequences and sum their generating functions. On the fourth line, we have divided every term by two, which corresponds to dividing the generating function by two. We have “derived” the generating function of the last sequence given in the preceding section! The derivation is justified by the following rule:

Rule 2.1 If < f_{0}, f_{1}, f_{2}, … >↔ F (x) and < g_{0}, g_{1}, g_{2}, … >↔ G(x), then

< αf_{0} + βg_{0}, αf_{1} + βg_{1}, αf_{2} + βg_{2}, … >↔ αF (x) + βG(x)

Proof.

< αf_{0} + βg_{0}, αf_{1} + βg_{1}, αf_{2} + βg_{2}, … > ↔ ∑_{n=0}^{∞}(αf_{n} + βg_{n})x^{n}

= α ∑_{n=0}^{∞} f_{n}x^{n} + β ∑_{n=0}^{∞} g_{n}x^{n}
= αF (x) + βG(x)

2.2 Shifting a Sequence

Suppose that we add k leading zeroes to a sequence; how does the generating function change? For example, in the first section we showed that:

< 1, 1, 1, … >↔ \frac{1}{1 − x}

If we add k leading zeroes to the sequence, we can compute the new generating function as follows:

< \underbrace{0, 0, …, 0}_{k zeroes}, 1, 1, 1, … > ↔ x^{k} + x^{k+1} + x^{k+2} + …

= x^{k}(1 + x + x^{2} + …)
= \frac{x^{k}}{1 − x}

In this example, adding k leading zeroes to the sequence corresponds to multiplying the generating function by x^{k}. In fact, this holds in general.

Rule 2.2 If < f_{0}, f_{1}, f_{2}, … >↔ F (x), then

< \underbrace{0, 0, …, 0}_{k zeroes}, f_{0}, f_{1}, f_{2}, … >↔ x^{k}F (x)

4 Lecture 17: Lecture Notes Proof.

< \underbrace{0, 0, …, 0}_{k zeroes}, f_{0}, f_{1}, f_{2}, … > ↔ f_{0}x^{k} + f_{1}x^{k+1} + f_{2}x^{k+2} + …
= x^{k}(f_{0} + f_{1}x + f_{2}x^{2} + …)
= x^{k}F (x)

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

\frac{d}{dx} (1 + x + x^{2} + x^{3} + x^{4} + …) = \frac{d}{dx} (\frac{1}{1 − x})
1 + 2x + 3x^{2} + 4x^{3} + … = \frac{1}{(1 − x)^{2}}
< 1, 2, 3, 4, … > ↔ \frac{1}{(1 − x)^{2}}

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:

1. Each term is multiplied by its index.

2. The sequence is shifted left one place. These effects are apparent in the following rule: Rule 2.3 If < f_{0}, f_{1}, f_{2}, f_{3}, … >↔ F (x), then

< f_{1}, 2f_{2}, 3f_{3}, … >↔ F′(x)

Proof.

< f_{1}, 2f_{2}, 3f_{3}, … > = f_{1} + 2f_{2}x + 3f_{3}x^{2} + …
= \frac{d}{dx} (f_{0} + f_{1}x + f_{2}x^{2} + f_{3}x^{3} + …)
= \frac{d}{dx} F (x)

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; we’ll see how to do this below and also in Section 4.2.

Suppose that we want the generating function for the sequence of squares, < 0, 1, 4, 9, 16, … >. We could start with the sequence < 1, 1, 1, … > and multiply each term by its index two times. This would give the desired result:

< 0 · 0, 1 · 1, 2 · 2, 3 · 3, … > = < 0, 1, 4, 9, … >

The problem is that differentiation not only multiplies each term by its index, but also shifts a sequence left one place. However, Rule 2.2 tells how to cancel out this unwanted left-shift; namely, multiply the generating function by x to right-shift the sequence back one place.

Our procedure, therefore, is to begin with the generating function for < 1, 1, 1, … >, differentiate, multiply by x, and then again differentiate and multiply by x.

< 1, 1, 1, 1, … > ↔ \frac{1}{1 − x}
< 1, 2, 3, 4, … > ↔ \frac{d}{dx} \frac{1}{1 − x} = \frac{1}{(1 − x)^{2}}
< 0, 1, 2, 3, … > ↔ x · \frac{1}{(1 − x)^{2}} = \frac{x}{(1 − x)^{2}}
< 1, 4, 9, 16, … > ↔ \frac{d}{dx} \frac{x}{(1 − x)^{2}} = \frac{1 + x}{(1 − x)^{3}}
< 0, 1, 4, 9, … > ↔ x · \frac{1 + x}{(1 − x)^{3}} = \frac{x(1 + x)}{(1 − x)^{3}}

The generating function for squares is \frac{x(1+x)}{(1−x)^{3}}.

3 The Fibonacci Sequence

A sequence defined by a recurrence often has a simple generating function. An example is the Fibonacci sequence:

< 0, 1, 1, 2, 3, 5, 8, 13, 21, … >↔ \frac{x}{1 − x − x^{2}}

It is amazing that such a tricky sequence could have so simple a generating function! In the next two sections, we will derive this generating function and then use it to find a closed form for the n-th Fibonacci number. The techniques used here are applicable to a large class of recurrence equations.

3.1 Finding a Generating Function

Recall the definition of the Fibonacci sequence:

f_{0} = 0, f_{1} = 1, f_{n} = f_{n−1} + f_{n−2} for n ≥ 2

We can write the generating function for the Fibonacci numbers as follows.

F (x) = ∑_{n=0}^{∞} f_{n}x^{n}

= \underbrace{0 + x}_{break out base cases} + ∑_{n=2}^{∞} \underbrace{(f_{n−1} + f_{n−2})x^{n}}_{expand other terms with the recurrence relation}

Our objective is now to write F (x) in terms of itself. We can then solve for F (x) to find a closed form for the generating function. The following sequence of equations is explained in the text that follows.

F (x) = x + ∑_{n=2}^{∞} f_{n−1}x^{n} + ∑_{n=2}^{∞} f_{n−2}x^{n}

= x + x ∑_{n=2}^{∞} f_{n−1}x^{n−1} + x^{2} ∑_{n=2}^{∞} f_{n−2}x^{n−2}

= x + x ∑_{n=1}^{∞} f_{n}x^{n} + x^{2} ∑_{n=0}^{∞} f_{n}x^{n}

= x + x ∑_{n=0}^{∞} f_{n}x^{n} + x^{2} ∑_{n=0}^{∞} f_{n}x^{n}
= x + xF (x) + x^{2}F (x)

In the first equation, we split the previous sum into two parts. The idea behind all subsequent steps is to make each part look like F (x). In the second equation, we make subscripts of coefficients match exponents by pulling x’s out of the summations. In the third equation, we adjust the indices of summation so that subscripts and exponents are n instead of n − 1 and n − 2. In the fourth equation, the range of the first summation is extended to include n = 0; this is valid because the summand is 0 in this case anyway. Finally, in two places we substitute F (x) for its definition, ∑_{n=0}^{∞} f_{n}x^{n}.

We now have an equation in F (x). Solving for F (x) gives the generating function for Fibonacci numbers.

F (x) = x + xF (x) + x^{2}F (x)
⇒ F (x) = \frac{x}{1 − x − x^{2}}

Amazingly, the power series of F (x) really does encode all the Fibonacci numbers:

\frac{x}{1 − x − x^{2}} = 0 + x + x^{2} + 2x^{3} + 3x^{4} + 5x^{5} + 8x^{6} + …

3.2 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 term in the sequence.

For example, we found a generating function for the Fibonacci sequence. From this we can find a closed form for the n-th Fibonacci number. In particular, the n-th Fibonacci number is the coefficient of x^{n} in the power series of \frac{x}{1−x−x^{2}} . Our problem, therefore, is to extract the n-th coefficient from the power series of a generating function. We will see two ways to do this, one here and one in Section 4.2.2

The first way to extract a coefficient from a generating function works if the generating function is a ratio of polynomials. This is often true in recurrence problems. The idea is to rewrite the generating function using partial fractions; in this form, one can immediately read out the coefficients of the power series.

There are two steps in rewriting the generating function \frac{x}{1−x−x^{2}} with partial fractions. First, we factor the denominator:

1 − x − x^{2} = (1 − α_{1}x)(1 − α_{2}x)
where α_{1} = \frac{1 + \sqrt{5}}{2} , α_{2} = \frac{1 − \sqrt{5}}{2}

Second, we find A_{1} and A_{2} which satisfy the following equation:

\frac{x}{1 − x − x^{2}} = \frac{A_{1}}{1 − α_{1}x} + \frac{A_{2}}{1 − α_{2}x}

To do this, we plug in various values of x to generate linear equations in A_{1} and A_{2}. We can then find A_{1} and A_{2} by solving a linear system. In this case, we get:

A_{1} = \frac{1}{α_{1} − α_{2}} = \frac{1}{\sqrt{5}}

A_{2} = \frac{−1}{α_{1} − α_{2}} = −\frac{1}{\sqrt{5}}

Substituting into the equation above gives the partial fractions expansion of F (x):

\frac{x}{1 − x − x^{2}} = \frac{1}{\sqrt{5}} (\frac{1}{1 − α_{1}x} − \frac{1}{1 − α_{2}x})

After the generating function is written with partial fractions, reading out coefficients is easy, because each fraction has a simple power series.

\frac{1}{1 − α_{1}x} = 1 + α_{1}x + α_{1}^{2}x^{2} + …
\frac{1}{1 − α_{2}x} = 1 + α_{2}x + α_{2}^{2}x^{2} + …

We can now find the coefficient of x^{n} in the generating function:

F (x) = \frac{1}{\sqrt{5}} (\frac{1}{1 − α_{1}x} − \frac{1}{1 − α_{2}x})
= \frac{1}{\sqrt{5}} ((1 + α_{1}x + α_{1}^{2}x^{2} + …) − (1 + α_{2}x + α_{2}^{2}x^{2} + …))
⇒ f_{n} = \frac{1}{\sqrt{5}}(α_{1}^{n} − α_{2}^{n})
= \frac{1}{\sqrt{5}} ((\frac{1 + \sqrt{5}}{2})^{n} − (\frac{1 − \sqrt{5}}{2})^{n})

This is the same scary formula for the n-th Fibonacci number that we found using the method for solving linear recurrences! The closed form for Fibonacci numbers was actually first found by DeMoivre in 1718 using this technique. The same approach can be used to solve any linear recurrence and even some non-linear recurrences.

3.3 Review of the Method

Since we will see only this one example of using generating functions to solve a recurrence, let’s review the process. There are two main steps and some tricks in each. First, we find a generating function for the sequence defined by the recurrence as follows.

1. Write the generating function as F (x) = ∑_{n=0}^{∞} f_{n}x^{n}.

2. Pull out base cases, apply the recurrence equation to remaining terms.

3. Massage this formula to get an equation in F (x).

4. Solve the equation to obtain the generating function F (x).

Second, we have to extract coefficients from the generating function:

1. Expand the generating function into partial fractions.

2. Expand each fraction into a simple power series.

3. Read out the coefficients of the generating function.

4 Counting with Generating Functions

Generating functions can be used to solve counting problems of the type we encountered in the last two weeks. Problems involving choosing items from a set often lead to nice generating functions. When generating functions are used in this way, the coefficient of x^{n} is the number of ways to choose n items from a set.

4.1 Choosing Items from a Set Without Repetition

The generating function for binomial coefficients follows directly from the Binomial Theorem:

< \binom{k}{0}, \binom{k}{1}, \binom{k}{2}, …, \binom{k}{k}, 0, 0, 0, … > ↔ \binom{k}{0} + \binom{k}{1}x + \binom{k}{2}x^{2} + … + \binom{k}{k}x^{k}
= (1 + x)^{k}

This is the generating function for choosing items from a k-element set without repetition. More precisely, the coefficient of x^{n} is the number of ways to chose n items from from a k-element set with without repetition. For example, the coefficient of x^{2} is the number of ways to choose 2 items from a k-element set, which is \binom{k}{2}. Similarly, the coefficient of x^{k+1} is the number of ways to choose k + 1 items from a k-element set, which is zero.

Often we can find the generating function for such a counting problem with very little work. In fact, we could find the generating function above without worrying about the Binomial Theorem or even binomial coefficients!

Here is how. First, consider a single-element set {a_{1}}. The generating function for the number of ways to choose n elements from this set is simply 1 + x; we have one way to choose zero elements, one way to choose one element, and zero ways to choose more than one element. Similarly, the number of ways to choose n elements from the set {a_{2}} is also given by the generating function 1 + x; the fact that the element differs is irrelevant.

Here is the the main trick. The generating function for choosing elements from the union of disjoint sets is the product of the generating functions for choosing from each set. For example, the generating for the number of ways to choose n elements from the set {a_{1}, a_{2}} is:

\underbrace{(1 + x)}_{\begin{matrix}OGF for \\ \{a_{1}\} \end{matrix}} \underbrace{(1 + x)}_{\begin{matrix}OGF for \\ \{a_{2}\} \end{matrix}} = \underbrace{(1 + x)^{2}}_{\begin{matrix}OGF for \\ \{a_{1}, a_{2}\} \end{matrix}} = 1 + 2x + x^{2}

Sure enough, for the set {a_{1}, a_{2}} we have one way to choose zero elements, two ways to choose one element, one way to choose two elements, and zero ways to choose more than two elements.

Rule 4.1 Let A(x) and B(x) be ordinary generating functions for the number of ways to choose items from disjoint sets A and B, respectively. The generating function for the number of ways to choose objects from the union A ∪ B is the product A(x) · B(x).

For example, by repeated application of this rule, we find that the generating function for choosing n items from a k-element set {a_{1}, a_{2}, …, a_{k}} is:

\underbrace{(1 + x)}_{\begin{matrix}OGF for \\ \{a_{1}\} \end{matrix}} \underbrace{(1 + x)}_{\begin{matrix}OGF for \\ \{a_{2}\} \end{matrix}} … \underbrace{(1 + x)}_{\begin{matrix}OGF for \\ \{a_{k}\} \end{matrix}} = \underbrace{(1 + x)^{k}}_{\begin{matrix}OGF for \\ \{a_{1}, a_{2}, …, a_{k}\} \end{matrix}}

This is the same generating function that we obtained by using the Binomial Theorem.

Proof. Let A(x) = ∑_{n=0}^{∞} a_{n}x^{n}, let B(x) = ∑_{n=0}^{∞} b_{n}x^{n}, and let C(x) = A(x) · B(x) = ∑_{n=0}^{∞} c_{n}x^{n}.

We must show that c_{n} is equal to the number of ways to choose n items from A ∪ B. We can compute c_{n} by evaluating the product A(x) · B(x). We can compute the number of ways to choose n items from A ∪ B by a combinatorial argument. The proof is completed by showing that these two results are the same.

First, we compute c_{n} by evaluating the product A(x) · B(x). We can tabulate all of the terms in this product in a table like the one shown below.

b_{0}x^{0} b_{1}x^{1} b_{2}x^{2} b_{3}x^{3} …
a_{0}x^{0} a_{0}b_{0}x^{0} a_{0}b_{1}x^{1} a_{0}b_{2}x^{2} a_{0}b_{3}x^{3} …
a_{1}x^{1} a_{1}b_{0}x^{1} a_{1}b_{1}x^{2} a_{1}b_{2}x^{3} …
a_{2}x^{2} a_{2}b_{0}x^{2} a_{2}b_{1}x^{3} …
a_{3}x^{3} a_{3}b_{0}x^{3} …
…  …

Note that all terms containing a particular power of x lie on a /-sloped diagonal. Collecting terms, we find that the coefficient of x^{n} in the product is:

c_{n} = a_{0}b_{n} + a_{1}b_{n−1} + a_{2}b_{n−2} + … a_{n}b_{0}

Next, we compute the number of ways to choose n items from A ∪ B. We can break up the answer based on the number of items chosen from set A. In particular, suppose we choose j items from A and n − j items from B. The number of ways to choose j items from A is a_{j}, and the number of ways to choose n − j items from B is b_{n−j}. By the Product Rule, we can choose all n items in a_{j}b_{n−j} ways. Since we could choose j = 0, 1, …, n items from A, the total number of ways to choose n items from A ∪ B is the sum:

a_{0}b_{n} + a_{1}b_{n−1} + a_{2}b_{n−2} + … a_{n}b_{0}

This is equal to the value of c_{n} computed above.

In the above argument, we implicitly used the fact that A and B are disjoint sets when referring to the number of items chosen from A; were the sets intersecting, the number of items drawn from A would not be well-defined, since the same item might be in both sets.

The expression c_{n} = a_{0}b_{n} + a_{1}b_{n−1} + a_{2}b_{n−2} + … a_{n}b_{0} may be familiar from a signal processing course; the sequence < c_{0}, c_{1}, c_{2}, … > is the convolution of sequences < a_{0}, a_{1}, a_{2}, … > and < b_{0}, b_{1}, b_{2}, … >.

4.2 Choosing Items with Repetition

In the last lecture, we saw that the number of ways to choose n items with repetition from a k-element set is \binom{n+k−1}{k}. We can derive the same result using generating functions. This will introduce some new techniques that will be helpful when we do not know the answer in advance!

4.2.1 Finding a Generating Function

Suppose we choose n items with repetition 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 a two items, etc. The generating function for choosing n elements with repetition from a 1-element set is therefore:

< 1, 1, 1, 1, … > ↔ 1 + x + x^{2} + x^{3} + … = \frac{1}{1 − x}

As before, the generating function for choosing items from a union of disjoint sets is the product of the generating functions for choosing from each set.

\underbrace{\frac{1}{1 − x}}_{\begin{matrix}OGF for \\ \{a_{1}\} \end{matrix}} \underbrace{\frac{1}{1 − x}}_{\begin{matrix}OGF for \\ \{a_{2}\} \end{matrix}} … \underbrace{\frac{1}{1 − x}}_{\begin{matrix}OGF for \\ \{a_{k}\} \end{matrix}} = \underbrace{\frac{1}{(1 − x)^{k}}}_{\begin{matrix}OGF for \\ \{a_{1}, a_{2}, …, a_{k}\} \end{matrix}}

The generating function for choosing n items from a k-item set with repetition allowed is \frac{1}{(1−x)^{k}}.

4.2.2 Finding Coefficients

Now we need to find the coefficients of the generating function. In particular, the coefficient of x^{n} in the generating function \frac{1}{(1−x)^{k}} gives the number of ways to choose n items from a k-item set with replacement.

Since the generating function is a ratio of polynomials, we could try using partial fractions as before. However, the partial fractions expansion of \frac{1}{(1−x)^{k}} actually is just \frac{1}{(1−x)^{k}}; no progress there!

Another way to find coefficients of a generating function is by repeatedly differentiating and then evaluating the result at 0. Here is how this technique can be used to find the first few coefficients of a generating function G(x).

G(x) = g_{0} + g_{1}x + g_{2}x^{2} + g_{3}x^{3} + g_{4}x^{4} + …  G(0) = g_{0}
G′(x) = g_{1} + 2g_{2}x + 3g_{3}x^{2} + 4g_{4}x^{3} + … G′(0) = g_{1}
G′′(x) = 2g_{2}x + 3 · 2g_{3}x + 4 · 3g_{4}x^{2} + … G′′(0) = 2g_{2}
G′′′(x) = 3 · 2g_{3} + 4 · 3 · 2g_{4}x + … G′′′(0) = 3 · 2g_{3}

In general, after n derivatives we have G^{(n)}(0) = n!g_{n}. Therefore, the coefficient of x^{n} is G^{(n)}(0)/n!.

Recall that differentiating a generating function has two effects on the corresponding sequence: terms are multiplied by their index and then all terms are shifted left one place. In this case, we intend to left-shift n times, but must cancel out the unwanted multiplication by dividing by n!.

All that remains is to compute the n-th derivative of G(x) = \frac{1}{(1−x)^{k}}. This is not too hard:

G(x) = (1 − x)^{−k}
G′(x) = k(1 − x)^{−k−1}
G′′(x) = k(k + 1)(1 − x)^{−k−2}
G^{(n)}(x) = k(k + 1) … (k + n − 1)(1 − x)^{−k−n}

The coefficient of x^{n} in the generating function is computed below.

G^{(n)}(0)/n! = \frac{k(k + 1) … (k + n − 1)}{n!}
= \frac{(k + n − 1)!}{n! (k − 1)!}
= \binom{k + n − 1}{n}

Therefore, the number of ways to choose n items from a k-element set with repetition allowed is \binom{k+n−1}{n}. This is the same result we derived in the last lecture!

5 An “Impossible” Counting Problem

Here is an absurd counting problem. 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 2 2 0 0
Bananas 0 0 0 0 0 5 5
Oranges 0 2 1 4 3 1 0
Pears 0 0 1 0 1 0 1

This problem appears utterly hopeless for an arbitrary number of fruits, but we can actually use generating functions to find an answer.

First, we need a generating function for the number of fruit bags. Initially, we can consider each fruit separately. That is, we can count the number of fruit bags with just apples, then just bananas, petc. For example, the number of fruit bags with 0 apples is one (the empty bag), the number of bags with 1 apple is zero (the number of apples must be even), the number of bags with 2 apples is one (the bag with two apples), etc. The generating functions for the four types of fruit are worked out below.

A(x) = 1 + x^{2} + x^{4} + x^{6} + … = \frac{1}{1 − x^{2}}
B(x) = 1 + x^{5} + x^{10} + x^{15} + … = \frac{1}{1 − x^{5}}
O(x) = 1 + x + x^{2} + x^{3} + x^{4} = \frac{1 − x^{5}}{1 − x}
P (x) =  1 + x

As in previous counting problems, the generating function for picking n items from a union of disjoint sets is the product of the generating functions for picking from each set. That is, all fruit bags are counted by the generating function A(x)B(x)O(x)P (x). This function is computed below.

OGF for fruit bags = A(x)B(x)O(x)P (x)
= \frac{1}{1 − x^{2}} \frac{1}{1 − x^{5}} \frac{1 − x^{5}}{1 − x} (1 + x)
= \frac{1}{(1 − x)^{2}}
= 1 + 2x + 3x^{2} + 4x^{3} + …

The coefficient of x^{n} is simply n + 1; that is, 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 fruit bags with 6 fruits. Amazing!