maths.freeDiscrete Math & Logic › 7. Additional Topics › Generating Functions

Generating Functions

There is an extremely powerful tool in discrete mathematics used to manipulate sequences called the generating function.

Generating Functions

There is an extremely powerful tool in discrete mathematics used to manipulate sequences called the generating function. The idea is this: instead of an infinite sequence (for example: \(2, 3, 5, 8, 12, \ldots\)) we look at a single function which encodes the sequence. But not a function that gives the \(n\)th term as output. Instead, a function whose power series (like from calculus) displays the terms of the sequence. So for example, we would look at the power series \(2 + 3x + 5x^2 + 8x^3 + 12x^4 + \cdots\) which displays the sequence \(2, 3, 5, 8, 12, \ldots\) as coefficients.

An infinite power series is simply an infinite sum of terms of the form \(c_nx^n\) were \(c_n\) is some constant. So we might write a power series like this: \[\sum_{k=0}^\infty c_k x^k\]. or expanded like this \[c_0 + c_1x + c_2x^2 + c_3x^3 + c_4x^4 + c_5x^5 + \cdots\].

When viewed in the context of generating functions, we call such a power series a generating series. The generating series generates the sequence \[c_0, c_1, c_2, c_3, c_4, c_5, \ldots\].

In other words, the sequence generated by a generating series is simply the sequence of coefficients of the infinite polynomial.

Example

What sequence is represented by the generating series \(3 + 8x^2 + x^3 + \frac{x^5}{7} + 100x^6 + \cdots\)?

Solution

We just read off the coefficients of each \(x^n\) term. So \(a_0 = 3\) since the coefficient of \(x^0\) is 3 (\(x^0 = 1\) so this is the constant term). What is \(a_1\)? It is NOT 8, since 8 is the coefficient of \(x^2\), so 8 is the term \(a_2\) of the sequence. To find \(a_1\) we need to look for the coefficient of \(x^1\) which in this case is 0. So \(a_1 = 0\). Continuing, we have \(a_2 = 8\), \(a_3 = 1\), \(a_4 = 0\), and \(a_5 = \frac{1}{7}\). So we have the sequence \[3, 0, 8, 1, 0, \frac{1}{7}, 100, \ldots\].

Note that when discussing generating functions, we always start our sequence with \(a_0\).

Now you might very naturally ask why we would do such a thing. One reason is that encoding a sequence with a power series helps us keep track of which term is which in the sequence. For example, if we write the sequence \(1, 3, 4, 6, 9, \ldots, 24, 41,\ldots\) it is impossible to determine which term \(24\) is (even if we agreed that the first term was supposed to be \(a_0\)). However, if we wrote the generating series instead, we would have \(1 + 3x + 4x^2 + 6x^3 + 9x^4 + \cdots + 24 x^{17} + 41 x^{18} + \cdots\). Now it is clear that 24 is the 17th term of the sequence (that is, \(a_{17} = 24\)). Of course to get this benefit we could have displayed our sequence in any number of ways, perhaps \(\fbox{1}_0 \fbox{3}_1 \fbox{4}_2 \fbox{6}_3 \fbox{9}_4 \cdots \fbox{24}_{17}\fbox{41}_{18}\cdots\), but we do not do this. The reason is that the generating series looks like an ordinary power series (although we are interpreting it differently) so we can do things with it that we ordinarily do with power series such as write down what it converges to.

For example, from calculus we know that the power series \(1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \cdots + \frac{x^n}{n!} + \cdots\) converges to the function \(e^x\). So we can use \(e^x\) as a way of talking about the sequence of coefficients of the power series for \(e^x\). When we write down a nice compact function which has an infinite power series that we view as a generating series, then we call that function a generating function. In this example, we would say \[1, 1, \frac{1}{2}, \frac{1}{6}, \frac{1}{24}, \ldots, \frac{1}{n!}, \ldots \mbox{ has generating function } e^x\].

Building Generating Functions

The \(e^x\) example is very specific. We have a rather odd sequence, and the only reason we know its generating function is because we happen to know the Taylor series for \(e^x\). Our goal now is to gather some tools to build the generating function of a particular given sequence.

Let's see what the generating functions are for some very simple sequences. The simplest of all: 1, 1, 1, 1, 1, . What does the generating series look like? It is simply \(1 + x + x^2 + x^3 + x^4 + \cdots\). Now, can we find a closed formula for this power series? Yes! This particular series is really just a geometric series with common ratio \(x\). So if we use our multiply, shift and subtract technique from , we have \[\begin{aligned}S \amp = 1 + x + x^2 + x^3 + \cdots \\ \underline{-\qquad xS} \amp \underline{\,\, = ~~~~~~ x + x^2 + x^3 + x^4 + \cdots} \\ (1-x)S \amp = 1\end{aligned}\]

Therefore we see that \[1 + x + x^2 + x^3 \cdots = \dfrac{1}{1-x}\].

You might remember from calculus that this is only true on the interval of convergence for the power series, in this case when \(|x| \lt 1\). That is true for us, but we don't care. We are never going to plug anything in for \(x\), so as long as there is some value of \(x\) for which the generating function and generating series agree, we are happy. And in this case we are happy.

The generating function for \(1,1,1,1,1,1,\ldots\) is \(\dfrac{1}{1-x}\)

Let's use this basic generating function to find generating functions for more sequences. What if we replace \(x\) by \(-x\). We get \[\frac{1}{1+x} = 1 - x + x^2 - x^3 + \cdots \mbox{ which generates } 1, -1, 1, -1, \ldots\].

If we replace \(x\) by \(3x\) we get \[\frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots \mbox{ which generates } 1, 3, 9, 27, \ldots\].

By replacing the \(x\) in \(\frac{1}{1-x}\) we can get generating functions for a variety of sequences, but not all. For example, you cannot plug in anything for \(x\) to get the generating function for \(2,2,2,2, \ldots\). However, we are not lost yet. Notice that each term of \(2, 2, 2, 2, \ldots\) is the result of multiplying the terms of \(1, 1, 1, 1, \ldots\) by the constant 2. So multiply the generating function by 2 as well. \[\frac{2}{1-x} = 2 + 2x + 2x^2 + 2x^3 + \cdots \mbox{ which generates } 2, 2, 2, 2, \ldots\].

The generating function for \(1, 2, 3, 4, 5, \ldots\) is \(\d\frac{1}{(1-x)^2}\).

Condensed — the full section is in Levin, Discrete Mathematics: An Open Introduction.

Differencing

We have seen how to find generating functions from \(\frac{1}{1-x}\) using multiplication (by a constant or by \(x\)), substitution, addition, and differentiation. To use each of these, you must notice a way to transform the sequence \(1,1,1,1,1\ldots\) into your desired sequence. This is not always easy. It is also not really the way we have analyzed sequences. One thing we have considered often is the sequence of differences between terms of a sequence. This will turn out to be helpful in finding generating functions as well. The sequence of differences is often simpler than the original sequence. So if we know a generating function for the differences, we would like to use this to find a generating function for the original sequence.

For example, consider the sequence \(2, 4, 10, 28, 82, \ldots\). How could we move to the sequence of first differences: \(2, 6, 18, 54,\ldots\)? We want to subtract 2 from the 4, 4 from the 10, 10 from the 28, and so on. So if we subtract (term by term) the sequence \(0, 2, 4, 10, 28,\ldots\) from \(2, 4, 10, 28\ldots\), we will be set. We can get the generating function for \(0,2,4,10,28,\ldots\) from the generating function for \(2,4,10,28\ldots\) by multiplying by \(x\). Use \(A\) to represent the generating function for \(2, 4, 10, 28, 82, \ldots\) Then: \[\begin{aligned}A \amp = 2 + 4x + 10x^2 +28x^3 + 82x^4 + \cdots \\ \underline{-\qquad xA} \amp \underline{\,\,= 0 + 2x + 4x^2 + 10x^3 + 28 x^4 + 82x^5 + \cdots} \\ (1-x)A \amp = 2 + 2x + 6x^2 + 18x^3 + 54x^4 + \cdots\end{aligned}\]

While we don't get exactly the sequence of differences, we do get something close. In this particular case, we already know the generating function \(A\) (we found it in the previous section) but most of the time we will use this differencing technique to find \(A\): if we have the generating function for the sequence of differences, we can then solve for \(A\).

Now that we have a generating function for the odd numbers, we can use that to find the generating function for the squares:

Condensed — the full section is in Levin, Discrete Mathematics: An Open Introduction.

Multiplication and Partial Sums

What happens to the sequences when you multiply two generating functions? Let's see: \(A = a_0 + a_1x + a_2x^2 + \cdots\) and \(B = b_0 + b_1x + b_2x^2 + \cdots\). To multiply \(A\) and \(B\), we need to do a lot of distributing (infinite FOIL?) but keep in mind we will group like terms and only need to write down the first few terms to see the pattern. The constant term is \(a_0b_0\). The coefficient of \(x\) is \(a_0b_1 + a_1b_0\). And so on. We get: \[AB = a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + (a_0b_3 + a_1b_2 + a_2b_1 + a_3b_0)x^3 + \cdots\].

Example

Multiply the sequence \(1, 2, 3, 4, \ldots\) by the sequence \(1, 2, 4, 8, 16, \ldots\).

Solution

The new constant term is just \(1 \cdot 1\). The next term will be \(1\cdot 2 + 2 \cdot 1 = 4\). The next term: \(1 \cdot 4 + 2 \cdot 2 + 3 \cdot 1 = 11\). One more: \(1 \cdot 8 + 2 \cdot 4 + 3 \cdot 2 + 4 \cdot 1 = 26\). The resulting sequence is \[1, 4, 11, 26, 57, \ldots\].

Since the generating function for \(1,2,3,4, \ldots\) is \(\frac{1}{(1-x)^2}\) and the generating function for \(1,2,4,8, 16, \ldots\) is \(\frac{1}{1-2x}\), we have that the generating function for \(1,4, 11, 26, 57, \ldots\) is \(\frac{1}{(1-x)^2(1-2x)}\)

Consider the special case when you multiply a sequence by \(1, 1, 1, \ldots\). For example, multiply \(1,1,1,\ldots\) by \(1, 2, 3, 4, 5\ldots\). The first term is \(1\cdot 1 = 1\). Then \(1\cdot 2 + 1 \cdot 1 = 3\). Then \(1\cdot 3 + 1\cdot 2 + 1 \cdot 1 = 6\). The next term will be 10. We are getting the triangular numbers. More precisely, we get the sequence of partial sums of \(1,2,3,4,5, \ldots\). In terms of generating functions, we take \(\frac{1}{1-x}\) (generating \(1,1,1,1,1\ldots\)) and multiply it by \(\frac{1}{(1-x)^2}\) (generating \(1,2,3,4,5,\ldots\)) and this gives \(\frac{1}{(1-x)^3}\). This should not be a surprise as we found the same generating function for the triangular numbers earlier.

Condensed — the full section is in Levin, Discrete Mathematics: An Open Introduction.

Solving Recurrence Relations with Generating Functions

We conclude with an example of one of the many reasons studying generating functions is helpful. We can use generating functions to solve recurrence relations.

Example

Solve the recurrence relation \(a_n = 3a_{n-1} - 2a_{n-2}\) with initial conditions \(a_0 = 1\) and \(a_1 = 3\).

Solution

We saw in an example above that this recurrence relation gives the sequence \(1, 3, 7, 15, 31, 63, \ldots\) which has generating function \(\dfrac{1}{1 - 3x + 2x^2}\). We did this by calling the generating function \(A\) and then computing \(A - 3xA + 2x^2A\) which was just 1, since every other term canceled out.

But how does knowing the generating function help us? First, break up the generating function into two simpler ones. For this, we can use partial fraction decomposition. Start by factoring the denominator: \[\frac{1}{1-3x + 2x^2} = \frac{1}{(1-x)(1-2x)}\].

Partial fraction decomposition tells us that we can write this faction as the sum of two fractions (we decompose the given fraction): \[\frac{1}{(1-x)(1-2x)} = \frac{a}{1-x} + \frac{b}{1-2x} \text{ ~~ for some constants } a \text{ and } b\].

To find \(a\) and \(b\) we add the two decomposed fractions using a common denominator. This gives \[\frac{1}{(1-x)(1-2x)} = \frac{a(1-2x) + b(1-x)}{(1-x)(1-2x)}\]. so \[1 = a(1-2x) + b(1-x)\].

This must be true for all values of \(x\). If \(x = 1\), then the equation becomes \(1 = -a\) so \(a = -1\). When \(x = \frac{1}{2}\) we get \(1 = b/2\) so \(b = 2\). This tells us that we can decompose the fraction like this: \[\frac{1}{(1-x)(1-2x)} = \frac{-1}{1-x} + \frac{2}{1-2x}\].

This completes the partial fraction decomposition. Notice that these two fractions are generating functions we know. In fact, we should be able to expand each of them. \[\frac{-1}{1-x} = -1 - x - x^2 -x^3 - x^4 - \cdots \mbox{ which generates } -1, -1, -1, -1, -1, \ldots\]. \[\frac{2}{1-2x} = 2 + 4x + 8x^2 + 16x^3 + 32x^4 + \cdots \mbox{ which generates } 2, 4, 8, 16, 32, \ldots\].

We can give a closed formula for the \(n\)th term of each of these sequences. The first is just \(a_n = -1\). The second is \(a_n = 2^{n+1}\). The sequence we are interested in is just the sum of these, so the solution to the recurrence relation is \[a_n = 2^{n+1} - 1\].

We can now add generating functions to our list of methods for solving recurrence relations.

Practice (16)

Try each one on paper first. Reveal the answer to check; verified ones can be opened in the solver for every step.

  1. Find the generating function for each of the following sequences by relating them back to a sequence with known generating function.

    1. \(4,4,4,4,4,\ldots\)
    2. \(2, 4, 6, 8, 10, \ldots\)
    3. \(0,0,0,2,4,6,8,10,\ldots\)
    4. \(1, 5, 25, 125, \ldots\)
    5. \(1, -3, 9, -27, 81, \ldots\)
    6. \(1, 0, 5, 0, 25, 0, 125, 0, \ldots\)
    7. \(0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, \ldots\)

    Хариулт

    1. \(\dfrac{4}{1-x}\)
    2. \(\dfrac{2}{(1-x)^2}\)
    3. \(\dfrac{2x^3}{(1-x)^2}\)
    4. \(\dfrac{1}{1-5x}\)
    5. \(\dfrac{1}{1+3x}\)
    6. \(\dfrac{1}{1-5x^2}\)
    7. \(\dfrac{x}{(1-x^3)^2}\)

  2. Find the sequence generated by the following generating functions:

    1. \(\dfrac{4x}{1-x}\)
    2. \(\dfrac{1}{1-4x}\)
    3. \(\dfrac{x}{1+x}\)
    4. \(\dfrac{3x}{(1+x)^2}\)
    5. \(\dfrac{1+x+x^2}{(1-x)^2}\)

    Хариулт

    1. \(0, 4, 4, 4, 4, 4, \ldots\)
    2. \(1, 4, 16, 64, 256, \ldots\)
    3. \(0, 1, -1, 1, -1, 1, -1, \ldots\)
    4. \(0, 3, -6, 9, -12, 15, -18, \ldots\)
    5. \(1, 3, 6, 9, 12, 15, \ldots\)

  3. Show how you can get the generating function for the triangular numbers in three different ways:

    1. Take two derivatives of the generating function for \(1,1,1,1,1, \ldots\)

    2. Use differencing.

    3. Multiply two known generating functions.

  4. Use differencing to find the generating function for \(4, 5, 7, 10, 14, 19, 25, \ldots\).

    Хариулт

    Call the generating function \(A\). Compute \(A - xA = 4 + x + 2x^2 + 3x^3 + 4x^4 + \cdots\). Thus \(A - xA = 4 + \dfrac{x}{(1-x)^2}\). Solving for \(A\) gives \(\d\frac{4}{1-x} + \frac{x}{(1-x)^3}\).

  5. Find a generating function for the sequence with recurrence relation \(a_n = 3a_{n-1} - a_{n-2}\) with initial terms \(a_0 = 1\) and \(a_1 = 5\).

    Хариулт

    \(\dfrac{1+2x}{1-3x + x^2}\).

  6. Use the recurrence relation for the Fibonacci numbers to find the generating function for the Fibonacci sequence.

    Хариулт

    Compute \(A - xA - x^2A\) and then solve for \(A\). The generating function will be \(\dfrac{x}{1-x-x^2}\).

  7. Use multiplication to find the generating function for the sequence of partial sums of Fibonacci numbers, \(S_0, S_1, S_2, \ldots\) where \(S_0 = F_0\), \(S_1 = F_0 + F_1\), \(S_2 = F_0 + F_1 + F_2\), \(S_3 = F_0 + F_1 + F_2 + F_3\) and so on.

    Хариулт

    \(\dfrac{x}{(1-x)(1-x-x^2)}\).

  8. Find the generating function for the sequence with closed formula \(a_n = 2(5^n) + 7(-3)^n\).

    Хариулт

    \(\dfrac{2}{1-5x} + \dfrac{7}{1+3x}\).

  9. Find a closed formula for the \(n\)th term of the sequence with generating function \(\dfrac{3x}{1-4x} + \dfrac{1}{1-x}\).

    Хариулт

    \(a_n = 3\cdot 4^{n-1} + 1\).

  10. Find \(a_7\) for the sequence with generating function \(\dfrac{2}{(1-x)^2}\cdot\dfrac{x}{1-x-x^2}\).

    Хариулт

    Hint:

    You should multiply the two sequences.

  11. Explain how we know that \(\dfrac{1}{(1-x)^2}\) is the generating function for \(1, 2, 3, 4, \ldots\).

  12. Starting with the generating function for \(1,2,3,4, \ldots\), find a generating function for each of the following sequences.

    1. \(1, 0, 2, 0, 3, 0, 4,\ldots\)
    2. \(1, -2, 3, -4, 5, -6, \ldots\)
    3. \(0, 3, 6, 9, 12, 15, 18, \ldots\)
    4. \(0, 3, 9, 18, 30, 45, 63,\ldots\)

    Хариулт

    1. \(\frac{1}{(1-x^2)^2}\)
    2. \(\frac{1}{(1+x)^2}\)
    3. \(\frac{3x}{(1-x)^2}\)
    4. \(\frac{3x}{(1-x)^3}\)

  13. You may assume that \(1, 1, 2, 3, 5, 8,\ldots\) has generating function \(\dfrac{1}{1-x-x^2}\) (because it does). Use this fact to find the sequence generated by each of the following generating functions.

    1. \(\frac{x^2}{1-x-x^2}\)
    2. \(\frac{1}{1-x^2-x^4}\)
    3. \(\frac{1}{1-3x-9x^2}\)
    4. \(\frac{1}{(1-x-x^2)(1-x)}\)

    Хариулт

    1. \(0,0,1,1,2,3,5,8, \ldots\)
    2. \(1, 0, 1, 0, 2, 0, 3, 0, 5, 0, 8, 0, \ldots\)
    3. \(1, 3, 18, 81, 405, \ldots\)
    4. \(1, 2, 4, 7, 12, 20, \ldots\)

  14. Find the generating function for the sequence \(1, -2, 4, -8, 16, \ldots\).

  15. Find the generating function for the sequence \(1, 1, 1, 2, 3, 4, 5, 6, \ldots\).

    Хариулт

    \(\frac{x^3}{(1-x)^2} + \frac{1}{1-x}\).

  16. Suppose \(A\) is the generating function for the sequence \(3, 5, 9, 15, 23, 33, \ldots\).

    1. Find a generating function (in terms of \(A\)) for the sequence of differences between terms.

    2. Write the sequence of differences between terms and find a generating function for it (without referencing \(A\)).

    3. Use your answers to parts (a) and (b) to find the generating function for the original sequence.

Symbols used here

\sum_{k=1}^{n} a_k
summation
Add a_k for k = 1 up to n.
\infty
infinity
Not a number: "grows without bound" in limits and intervals.
e
Euler's number
2.71828…, the base whose exponential is its own derivative.
\sup,\ \inf
supremum, infimum
Least upper bound, greatest lower bound.
n!
factorial
n × (n−1) × … × 1; the number of orderings of n things. 0! = 1.
\binom{n}{k}
binomial coefficient, "n choose k"
Number of k-element subsets of n things: n!/(k!(n−k)!).
x \in A,\ A \subseteq B
element of, subset
x belongs to A; every element of A is in B.
A \cup B,\ A \cap B,\ A \setminus B
union, intersection, difference
In either; in both; in A but not B.
\emptyset,\ |A|
empty set, cardinality
The set with no elements; the number of elements of A.
\forall,\ \exists
for all, there exists
Quantifiers: every x; at least one x.
\neg,\ \wedge,\ \vee,\ \Rightarrow,\ \Leftrightarrow
not, and, or, implies, iff
Logical connectives.
\blacksquare\ \text{or}\ \square
end of proof (halmos)
Marks the point where the statement has been established.
a \equiv b \pmod n
congruent modulo n
n divides a − b; a and b have the same remainder.
O(n^2),\ \Theta,\ \Omega
big-O notation
Grows no faster than n² (up to a constant), for large n.
a \bmod n
remainder
What is left after dividing a by n.

Questions people ask

What makes mathematics "discrete"?

It deals with separate, countable objects — integers, graphs, statements — rather than continuous quantities. No limits, no infinitesimals; instead induction, counting and logic.

How does a proof by induction work?

Show the statement for the first case, then show that whenever it holds for n it holds for n + 1. Like dominoes: the first falls, and each knocks over the next.

Өөрийнхөөг турш

Parts of this page are adapted from Levin, Discrete Mathematics: An Open Introduction (CC BY-SA 4.0). Condensed and re-explained here; errors are ours.

Бүх зүйл Discrete Math & Logic