Chapter 3: Generating Functions
Exercise 3.1 🌟
This exercise demonstrates how to convert sequences into corresponding OGFs.
In all subproblems, {ak}k≥m denotes a sequence am,am+1,…; for example, {k3}k≥2 starts with 8 (without leading zeros).
Sequence {2k+1}k≥0
We can represent {2k+1}k≥0 as 2{2k}k≥0, which is 2x an elementary OGF from Table 3.1 with c=2. Thus, 2/(1−2z)=k≥0∑2k+1zk.
Sequence {k2k+1}k≥0
We can represent {k2k+1}k≥0 as 2{k2k}k≥0, which is a scaled version of an elementary OGF from Table 3.1. Thus, 4z/(1−2z)2=k≥0∑k2k+1zk.
Sequence {kHk}k≥1
Let A(z)=∑k≥0k(Hk−1)zk and B(z)=∑k≥0kzk, so that our sequence is their left shifted sum (both summands are elementary OGFs from Table 3.1). We have
Sequence {k3}k≥2
Notice that k3=6(3k)+6(2k)+k, so that our sequence is doubly left shifted sum of the corresponding elementary OGFs from Table 3.1. We have
Exercise 3.2 🌟
This exercise demonstrates how to extract coefficients from OGFs.
The idea behind each subproblem is to find the root OGF from Table 3.1 and then see what operations have been performed on it from Table 3.2.
[zN](1−3z)41
This is a scaled elementary OGF from Table 3.1 with M=3, so
[zN](1−z)2ln(1−z)1
The difference operation is applied twice in succession on the elementary OGF. We have to be careful to avoid a division by zero error. For this reason, the first three terms are enumerated separately. We have
[zN](1−2z2)21
The starting point is the elementary OGF 1−z21=∑N≥0z2N. It is scaled by λ=2 to get 1−2z21. Finally, it is an elementary OGF from Table 3.1 with M=1, so
Exercise 3.3
The last step wasn’t really needed, since any "missing" term has a coefficient of zero.
Exercise 3.4 🌟
This exercise demonstrates the advantages of representing an entire sequence using a single mathematical construct, such as a generating function. By manipulating generating functions, we can transform sequences and derive new identities through factoring. Each factorization corresponds to the same sequence, providing multiple perspectives for describing its terms. See also the next exercise.
The solution is available as part of the course material on the book’s website (see slide 13).
Exercise 3.5
We need to prove a general identity satisfied by the harmonic numbers and binomial coefficients by factoring the initial OGF in different ways. Let’s start with
Both components are elementary OGFs from Table 3.1. We have by definition
Another possible grouping is
Again, both are essentially elementary OGFs from Table 3.1. We have by definition
Another possible grouping is
Again, both are essentially elementary OGFs from Table 3.1. We have by definition
Looking at the initial OGF, we see that it is
Therefore, [zN](A(z)B(z))=[zN−M](P(z)Q(z)). The book Concrete Mathematics: A Foundation for Computer Science (2nd ed.) contains an identity (7.43)
By combining all components, we get
Exercise 3.6
See the remark in Exercise 3.1 how to interpret the definition of the sequence. It’s a doubly left shifted self-convolution of an elementary OGF ln1−z1
Exercise 3.7 🌟
This exercise shows an advanced case study of reconstructing the corresponding OGF.
Assume the same interpretation of this definition as in Exercise 3.1. A recipe for crafting the required OGF is as follows:
Start with the elementary OGF for the harmonic numbers.
Left shift it.
Apply the index divide (integration) operation.
Left shift again.
Despite being simple at first blush, the third step requires some advanced knowledge from calculus. The first two steps are
We can’t directly integrate the LHS, since we have an improper integral (isn’t defined at zero). Let’s break it up and compute the components individually. We get
Wikipedia has more information on the dilogarithm function. For the second integral, we can use WolframAlpha.
Exercise 3.8 🌟
This exercise shows an advanced coefficient extraction case study.
Let’s tackle first the common OGF A(z)=ln21−z1 that appears in both subtasks. As usual, there are many equivalent expressions for [zN]. Let’s reuse the results from the previous two exercises
For N≤1 the coefficients are zero. Observe that our A(z) differs from the one from Exercise 3.6, in a sense, that it represents a sequence with two leading zeros. Consequently, we can cleanly perform an index divide operation on the OGF for harmonic numbers without a need for an intermediary left shift. This removes the dilogarithm complication, as we had in the previous exercise.
[zN]1−z1ln21−z1
We apply the partial sum operation on A(z) to get B(z)=A(z)/(1−z). For pedagogical reasons, we show here another derivation of the identity stated above for A(z) (see step 3). We have
The previous derivation uses the identity (6.71) from the book Concrete Mathematics: A Foundation for Computer Science (2nd ed.).
[zN]ln31−z1
We convolve A(z) with ln1−z1 to get B(z)=ln1−z1A(z). We have
Substituting our partial sums into the expression for [zN] we have
For N≤1 the coefficients are zeros.
Exercise 3.9 🌟
This exercise demonstrates how to convert sequences into corresponding EGFs.
In all subproblems, {ak}k≥m denotes a sequence am,am+1,…; for example, {k3}k≥2 starts with 8 (without leading zeros).
Sequence {2k+1}k≥0
We can represent {2k+1}k≥0 as 2{2k}k≥0, which is 2x an elementary EGF from Table 3.3 with c=2. Thus, 2e2z=k≥0∑2k+1k!zk.
Sequence {k2k+1}k≥0
We can represent {k2k+1}k≥0 as 2{k2k}k≥0. For the scaled sequence, the starting point is e2z. Next, we need to perform an index multiply operation. To get kak instead of kak−1, we must again multiply by 2, since ak=2ak−1 for k>1.. Thus, 4ze2z=k≥0∑k2k+1k!zk.
Sequence {k3}k≥2
Notice that k3=6(3k)+6(2k)+k, so that our sequence is a doubly left shifted sum of the corresponding elementary EGFs from Table 3.3. We have
Exercise 3.10
The sequence 1,3,5,7,… can be described as {2k+1}k≥0. This is basically a sum of elementary EGFs from Table 3.3, so (1+2z)ez is the matching EGF.
The sequence 0,2,4,6,… can be described as {2k}k≥0. This is 2x an elementary EGF from Table 3.3, so 2zez is the matching EGF.
Exercise 3.11 🌟
This exercise demonstrates how to extract coefficients from EGFs.
A(z)=1−z1ln1−z1
If we treat A(z) as an OGF, then the same EGF is its labelled counterpart. This immediately gives N![zN]A(z)=N!HN.
A(z)=ln21−z1
Using the same reasoing (see Exercise 3.8) we have N![zN]A(z)=2(N−1)!HN−1 for N>1. For N≤1 the coefficients are zeros.
A(z)=ez+z2
Since ez+z2=ez⋅ez2, A(z) is a binomial sum operation applied on
As in the case of the similar OGF from Table 3.1, odd entries are zero. Using the definition of a binomial sum operation from Table 3.4, we have
Exercise 3.12
Let’s follow the hint from the book. We start by differentiating (left shifting) the EGF for harmonic numbers
We can setup the required differential equation by substituting the recurrence relation HN+1=HN+N+11 into the sum. We have
To prove the statement from the book, we just need to follow the steps for solving a linear first-order differential equation with variable coefficients. Therefore,
H(z)=∑HNN!zN⟹N![zN]H(z)=HN.
Exercise 3.13
By definition A(z)=∑k≥0akk!zk. We also know that
where B(z) is the unknown EGF. The last summation suggests that it’s a binomial convolution of A(z) and 1/(1−z). Hence,
Exercise 3.14 🌟
This exercise introduces the Laplace transform for converting from the EGF for a sequence to the OGF for the same sequence.
By definition A(z)=∑k≥0akk!zk. Prove that
provided the integral exists. Check this for sequences that appear in Tables 3.1 and 3.3.
The proof revolves around computing the definite integral. We have
The second line uses the notion of a Gamma function (see also Exercise 4.60). This establishes the desired identity.
Examples
If A(z)=ecz then
Type the following snippet into Sage to evaluate the above integral:
If A(z)=zez then
Type the following snippet into Sage to evaluate the above integral:
Exercise 3.15
We need to multiply the recurrence by zn/n! and sum on n in the first step. Now, F(z)=∑k≥0Fkk!zk, hence we get the following differential equation
with initial conditions F(0)=F0=0 and F′(0)=F1=1. For the sake of variety. let’s solve it using WolframAlpha. After issuing the command y'' - y' - y = 0, y(0) = 0, y'(0) = 1 we get
Exercise 3.16 🌟
This exercise elaborates the methodology of applying generating functions to solve linear recurrences with constant coefficients.
We describe the process in detail for the first example. The extra summand, which makes a recurrence valid for all n, is the Kronecker delta function.
Performing a partial fractions expansion is easy in WolframAlpha. For example, issue Apart[z/(1 + z - 6z^2)] to simplify the polynomial expression in the 4th step (see above).
We use EGF for the next recurrence to demonstrate how it unfolds and to avoid technical difficulties in implementing partial fractions expansion due to unwieldy roots. Solving a homogeneous third-order linear ordinary differential equation with constant coefficients is essentially the same as solving the corresponding recurrence via roots of the characteristic polynomial. The resulting EGF is easily mapped to a closed-form solution.
The next two examples have complex roots.
Using the technique illustrated in Exercise 2.31, we can convert the above expression into trigonometric form. Thus, an=72n+1sin(narccos(43)).
The trigonometric form is an=32sin(narccos(21))=32sin(n3π).
Exercise 3.17 🌟
This exercise shows how to compute the numerator polynomial f(z) using a shortcut based on modular arithmetic.
The book provides a solution for different initial conditions, but we can reuse some parts of it. More specifically,
This is an elementary OGF from Table 3.1. We have an=2n.
Exercise 3.18
The resulting OGF is a convolution of basic OGFs. We have
The above sum can be computed by entering Sum[k(-1)^(N-k-1),{k,1,N-1}] in WolframAlpha.
Exercise 3.19 🌟
This exercise pinpoints an important fact about partial fractions expansion. Namely, sometimes it makes sense to work with higher degree polynomials. We wouldn’t gain anything by breaking up (1+3z2), since it’s already a scaled elementary OGF from Table 3.1.
Again, tool support is indispensable. The decomposition of a(z) was done in WolframAlpha by issuing the Apart[(z - 5 z^2 + 7 z^3)/((1 - 3 z)^2 (1 + 3 z^2))] command.
The resulting OGF is a sum of scaled and/or shifted elementary OGFs. After simplifying terms, and reusing the analysis from the book about OGFs of the form 1/(1+z2), we get
Exercise 3.20
Exercise 3.21 🌟
This exercise nicely demonstrates the power of generating functions. Imagine solving the given recurrence using "classical" methods.
Let A(z)=∑n≥0anzn represent our sequence. We know that
Looking at the form of the coefficient for n≥t, it’s definitely a convolution of some OGF B(z) with A(z). Therefore, we have the following functional equation
Observe the term zt−1, which denotes the initial value at−1=1. The convolution only covers the case when n≥t. The range of k tells that the sequence represented with B(z) has the following values:
By searching Table 3.1, it occurs that B(z)=(1−z)t−1, since it is a scaled elementary OGF with a leading zero. Thus,
Exercise 3.22 🌟
When coefficients in a recurrence are polynomials in the index n, then the implied relationship constraining the generating function is a differential equation. This exercise repeats the case study from the book.
Let a(z)=∑n≥1anzn with an implicit assumption that a0=0.
We obtain the solution to this differential equation by solving the corresponding homogeneous equation
to get an “integration factor” ϱ(z)=1−z. This gives
Integrating (for example, by using WolframAlpha), we get the result
We’ve already encountered this recurrence in Exercise 2.11. This was another way to attain the same outcome.
Exercise 3.23
Let a(z)=∑n≥0anzn. Assume that t is an integer.
Thus, a(z) is an elementary OGF, left shifted t−1 times. This gives
Observe that the formula for an nicely captures the case when t≤0 by the properties of a generalized version of the binomial theorem known as Newton’s formula. Such generalization popped out for "free" due to reliance on GFs.
Exercise 3.24
Fast forwarding the analysis from the book, our recurrence relationship corresponds to a differential equation on the generating function
We obtain the solution to this differential equation by solving the corresponding homogeneous equation
to get an “integration factor” ϱ(z)=1/(1−z)t. This gives
Integrating (for example, by using WolframAlpha), we get the result (recall that a(0)=a0=0)
The first term is a left shifted elementary OGF, so it’s coefficient is proportional to n. The second term is t−1 times left shifted elementary OGF from Table 3.1, so behaves like (t−1n+t−1)∼nt−1. We differentiate two cases:
If t=2−ϵ, then an=Θ(n)=o(nlgn).
If t=2+ϵ, then an=Θ(n1+ϵ)=ω(nlgn).
The threshold t=2 is a bifurcation point, where even a tiny disturbance entails an asymptotically different behavior.
Exercise 3.25
Expansion of sin(z)
We have to find the derivatives of sin(z):
Since sin(4)(z)=sin(z), this pattern repeats. We also know that sin(0)=0 and cos(0)=1. Thus, we get
Expansion of 2z
We already know the result for the exponential sequence. Since 2z=eln2z=ezln2, substitute z=zln2 into the formula from the book. So,
Expansion of zez
Multiply each term on the RHS of the expansion of ez with z. This gives
Exercise 3.26 🌟
This exercise introduces the general Leibniz rule for computing the nth derivative of the product of two functions.
Let’s develop some intuition about the stated recurrence for coefficients. Tools are excellent in performing exploratory tasks like this. Issuing taylor series 1/(1-az-bz^2) in WolframAlpha gives us the terms of the expansion:
Let cn denote the nth coefficient. It seems to satisfy a second-order linear recurrence with constant coefficients
Now, we’ve to show that this holds for all n>1. Let f(z)=(1−az−bz2)−1, so that
We aim to establish a relationship between the derivatives f(n)(0), f(n−1)(0) and f(n−2)(0). Let’s rewrite our original expression as
Notice that since g(z) is a polynomial of degree 2, its derivatives vanish for n>2:
Differentiating the equation f(z)g(z)=1 n times (where n≥2) gives:
Evaluated at z=0 gives
This concludes the proof about the recurrence relation for coefficients.
Exercise 3.27
As in the previous exercise, we rely on the general Leibniz rule. Our starting point is
We have to find cn=H(n)(0)/n!. Notice that g′(z)=f(z)⟹g(n−1)(0)=f(n)(0). We also know from the book that the kth derivative of f(z) is k!(1−z)−k−1. Consequently, f(k)(0)=k!. Finally, g(0)(0)=g(0)=0, so the summation in the Leibniz rule starts at index k=1. Combining all these facts, we have
Exercise 3.28 🌟
This exercise introduces lots of novelties and derives an expansion of an important generating function.
Let’s expand (1−z)−α using Newton’s formula
Observe that -1 from the negative exponent and -1 of z cancel during derivations.
According to the hint, we should differentiate both sides with respect to α. The LHS is
For the RHS, we’ll first convert the product into a sum using the natural logarithm. This gives
Therefore,
As a quick check, for α=2 we indeed get the expansion of a left shifted OGF from Table 3.1.
In our case, α=1/2. This gives
We can use the identity below, pertaining to the generalized binomial coefficients, to simplify the first term in the RHS. The book already shows the derivation of (n−21).
The digamma function provides a continuous interpolation of the harmonic numbers. As stated on Wikipedia about it’s relation to harmonic numbers, we have
After bundling together all components, we get
Exercise 3.29
We already have an expression for a generic parameter α (see the previous exercise). Thus,
Exercise 3.30
Both OGFs are elaborated in the book.
Exercise 3.31
The differential equation (3) corresponds to (1), which is the basic recurrence describing the number of comparisons used by quicksort. Multiplying (3) by (1−z)2 is equivalent to differencing the coefficients twice in succession. We get
We could have arrived at the same result iteratively by factoring (1−z)2=(1−z)(1−z). Notice that we’re referencing the (N−1)th coefficient. This is due to the simplified equation (3) in the book, where z was removed on both sides.
Exercise 3.32
Let A(z)=∑n≥0anzn. The initial differential equation corresponds to the recurrence
If we multiply both sides of the equation by (1−z) then we get
This recurrence is describing probabilities of derangements. If we substitute an=dn/n!, simplify factorials, multiply through by n!, then we get MathWorld's equation (5) with the index shifted by 1 (see the previous link). The equivalent equation (6) can be solved using the method presented in Exercise 2.14. We would arrive at the same result, as shown below.
An easier path is to directly find the OGF and expand.
This gives
Exercise 3.33 🌟
This exercise shows how new identities may improve performance.
The LHS is the convolution of two series (see Table 3.1):
The RHS is also a convolution of two series:
By equating [zn] from both sides, we obtain the identity:
What’s the benefit of this identity? Albeit both sides being semantically equivalent, the RHS effectively halves the computation time.When r and s are real numbers, then accuracy is potentially increased, as well.
Exercise 3.34
Exercise 3.35
We need to compute ∑0≤k≤NFk. The book contains the OGF for Fibonacci numbers
where [zN]F(z)=FN. Observe that the sum of Fibonacci numbers can be represented as a partial sum of F(z). This gives
Exercise 3.36
Wikipedia contains a derivation of the sum expression for −[zm] as part of the proof. The generating function in this exercise is a negative of the one associated with Bernoulli numbers.
Exercise 3.37
The idea is to employ substitution, as for Fibonacci numbers in the book, and afterward transform the substituted term with another power series. We have
If we treat the generating function from this exercise as an EGF, then it describes the ordered Bell numbers.
Exercise 3.38
We employ the same approach as in the previous exercise. This gives
The expression for the coefficient of the corresponding EGF is known as Dobiński's formula.
Exercise 3.39
Let A(z)=∑nanzn. We need to show that
To understand the last step, fix k≥n. The coefficient [zk] will "accumulate" contributions from all n≤k. Swapping the roles of n and k gives us the desired expression.
If we substitute z=y/(y−1), then we get
which, by symmetry, validates the opposite direction.
Exercise 3.40
Wikipedia contains an analysis of the binomial transform including the sketch of the proof without using generating functions. More formally, suppose we start with an arbitrary sequence {an}. We can generate another sequence {bn} from it
where Δ is the forward difference operator, as mentioned in Chapter 2 of the book, defined by Δan=an+1−an. The nth forward difference at index 0 is given by:
Multiplying both sides by (−1)n yields the previously stated equivalence.
The transformation is symmetrical and an=∑k=0n(kn)(−1)kbk. We can see this by introducing the shift operator an+1=Ean=Ian+Δan=(I+Δ)an, where I is an identity operator. We want to express an in terms of applying the shift operator n times to a0. This gives
Observe, that IkΔn−k=Δn−k. This concludes the proof.
Exercise 3.41 🌟
This exercise explains the composition of generating functions.
Wikipedia contains the formal power series version of the Faà di Bruno’s formula, based on the application of the multinomial theorem. An important detail is the precondition g(0)=0, which makes the composition valid.
Exercise 3.42
Let’s follow the hint from the book and find a differential equation satisfied by the function
Using Table 3.4, we can convert the above differential equation into recurrence relation on coefficients. This gives
Exercise 3.43
Let f(z)=∑nfnn!zn, thus f(z/2)=∑n2nfnn!zn. Then, provided that f(0)=1, we must have
Let’s dissect in detail the grouped section, to reveal the pattern. The leading term is similar to the example from the book, just with an opposite sign. This provides the first e−2z summand. The following steps are done from inside out
Continuing these iterations will result in a geometric progression that sums to e−2z. The constant terms annihilate leaving +1 at the end. This cancels the -1 in the original formula. As mentioned in the book, technically, we need to justify carrying out the iteration indefinitely, but the solution is easily verified from the original recurrence. Indeed, replacing f(z)=e2z verifies this statement.
Therefore, the recurrence relation and its solution are
Observe that -1 (from the initial expression) disappears, since it doesn’t influence coefficients for n>0. The sum evaluates to zero (recall that fk=2k) as stated on Wikipedia about partial sums of binomial coefficients.
Exercise 3.44 🌟
This exercise showcases a more intricate functional equation, where interplay of two sub-functions define the OGF.
Since the recurrence relations distinguish between even and odd indices, let’s split f(z) accordingly
We can write the odd part as
We can write the even part as
We’ve a system of equations
At this point, we should switch to Sage. The following code presents the solution for f(z).
We can solve this functional equation by iterating. The objective is to figure out the underlying pattern. This gives
Exercise 3.45
The aim is to repeat the process until a distinct pattern emerges.
We can prove the correctness of the formula by induction on n. The base case is trivially satisfied; recall that an empty product equals one. The inductive step is based on the following expansion rule
Therefore, the series in the nth iteration becomes
Exercise 3.46
Given f(z) defined by the equation f(z)=z/(1−f(z2)) we’ve to find explicit expressions for a(z) and b(z) with f(z)=a(z)/b(z). We assume that f(z) is analytic at z=0, thus f(0)=a(0)=0 and b(0)=1 (actually, any scaling factor would work).
Substituting f(z)=a(z)/b(z) into the given equation, we get
Comparing the numerators and denominators, we can set up the following system of equations:
Substituting (1) into (2), we get b(z)=b(z2)−z2b(z4). We can now solve for b(z). Let b(z)=∑n≥0bnzn, so by substituting this series into the recurrence, we get
Apparently, the RHS has only even powers, hence b2n+1=0 for n≥0.
If n is even, then bn=bn/2−b(n−2)/4. It helps to view n as a binary number, where division by 2 is right-shifting by one position. The next 4 cases illustrate what happens under the hood, as recursion unfolds until reaching the base case n=0. X2 denotes any binary prefix.
If n=X2010, then bn/2=0 (odd index) and b(n−2)/4=−bX20.
If n=X2000, then bn/2=bX200 and b(n−2)/4=0 (non-integer index).
If n=X2100, then bn/2=bX210 and b(n−2)/4=0 (non-integer index).
If n=X2110, then bn/2=0 (odd index) and b(n−2)/4=0 (odd index) .
Thus, by induction on bit position, bn is non-zero if and only if the binary expansion of n contains no consecutive 1s. Furthermore, advancing via bn/2 drops a trailing zero, whilst b(n−2)/4 removes a binary 1 and flips a sign. Thus,
where S is a sequence registered under A003714 and νN is the population count (see Chapter 2 in the book). Observe that νN=ν2N.
Exercise 3.47
Only f(z)=0 satisfies f(z)=sin(f(z)), where f(z)=∑n≥1fnzn.
Suppose, for the sake of contradiction, that there is also another power series g(z)=0 satisfying the same condition. Let gk be the lowest indexed non-zero coefficient for k≥1. Using Taylor's expansion of the sine function (see Exercise 3.25), and passing g(z) as an argument, we’ve
But this entails that
since g(z)=sin(g(z)) by definition. Subtracting g(z) from both sides, we get
The lowest term in the RHS is −61gk3z3k, since the others have higher powers of z, so gk must be zero. But this contradicts our assumption that gk=0. Therefore, f(z)=0 is a unique solution.
Exercise 3.48
The text of this exercise in the book is imprecise. The paper Singularity Analysis Of Generating Functions* contains a section about 2-3 trees, where the matching functional equation represents the number of such trees with n external nodes.
Let f(z)=∑n≥0fnzn be the OGF for the number of 2–3 trees , where fn denotes the number of trees with n external nodes. Based on the functional equation f(z)=z+f(z2+z3), we see that f0=0 and f1=1. Notice, that the second summand generates higher-order terms, so for n>1 the first summand z has no effect. We can substitute the form of OGF into the functional equation to get
To find the recurrence for fn, we equate coefficients of the same rank on both sides. This gives the following constraints on k, using the fact that 0≤j≤k, for which there is always some j such that 3k−j=n:
3k−j≤n⟹2k≤n⟹k≤⌊n/2⌋.
3k−j≥n⟹3k≥n⟹k≥⌈n/3⌉.
Thus, for any fixed n>1, we have
The next Python script computes the number of 2–3 trees of 100 external nodes.
It prints 5,520,498,313,790,316,062.
As a verification step, it’s advisable to search for the sequence in the online encyclopedia. For instance, by displaying 20 values generated by this script (including the initial terms), we can confirm that it corresponds to A014535.
Exercise 3.49 🌟
We show by induction on t that (1−z)tC(t)(z)=Ψ(Ψ+1)…(Ψ+t−1)C(z).
The book, at the time of this writing, has an error; t+1 must be replaced by t−1.
Observe, that Ψ can be manipulated as any other "symbol" in arithmetic expressions. For the sake of completeness, let’s show that (Ψ+m)(Ψ+n)F(z)=(Ψ+n)(Ψ+m)F(z) for any nonnegative integers n, m and arbitrary function F(z). We have
We proceed by induction on t .The base case is trivially satisfied, since ΨC(z)≡(1−z)C′(z). For the inductive step, we have
By successively swapping neighboring elements, we can move (Ψ+t−1) into the last position without disturbing the relative ordering of the other terms. This proves that the statement of this exercise holds for any t≥1.
Exercise 3.50
Sedgewick’s paper The Analysis of Quicksort Programs contains a full analysis of median-of-three quick-sort algorithm (see section 3.2). It derives the base formula in a more detailed manner than the book itself.
Exercise 3.51
Section 3.3 of the paper mentioned in the previous exercise shows the analysis of the median of 2t+1 quick-sort algorithm. Therefore, for a sample size of 5 we would use t=2.
Exercise 3.52 🌟
This exercise examines advanced calculus concepts and is indirectly related to the book, via linkage to generating functions. In order to encompass diverse perspectives, the answer is more focused on the methodology itself, whilst low-level details are skipped (like, step-by-step derivations of intermediate results). Otherwise, writing up everything would demand a separate chapter.
Understanding the Problem
The specific ordinary differential equation (ODE) with variable coefficients considered here is
with its inhomogeneous counterpart,
Unlike the classical Euler-Cauchy equation, where the power of the independent variable scales directly with the order of the derivative, this equation exhibits an inverse scaling relationship: as the order of the derivative j increases, the power of the coefficient (1−z)r−j decreases.
Leading-Coefficient Observation
Let’s expand the LHS
with aj(z)=(1−z)r−j. The coefficient of the highest derivative is
so the leading coefficient is nonzero at z=1. Consequently, for the homogeneous equation the point z=1 is an ordinary point in the sense that the normalized coefficients aj(z)/ar(z) are analytic at z=1. This implies homogeneous solutions are analytic at z=1 and admit Taylor expansions about that point.
The inhomogeneous term (1−z)α may be non-analytic at z=1, so requires a careful analysis. Our focus will be on the Frobenius method. While it’s traditionally taught as a tool for regular singular points, its utility extends to constructing particular solutions for inhomogeneous equations, as in our case. We’ll showcase the Frobenius method in "inverted" regime.
The method of Frobenius can be viewed as a superset of the power series method. This is another reason why it’s a natural choice for this exercise related to a chapter about generating functions.
Change of Variable
Let x=1−z, such that
so the equation becomes
for the inhomogeneous problem. Because the coefficient of f(r)(x) is (−1)rxr−r=(−1)r, a nonzero constant, the homogeneous equation has analytic coefficients at x=0. The above canonical transformation centers a potential singularity at the origin.
First-Order Case
It always make sense to start with the simplest form to develop intuition and cement understanding of the task. The transformed equation is
The closed-form solution of the homogeneous version is f(x)=Cex2/2, where C is an arbitrary constant. This is an entire function, as expected.
For solving the other version, we apply an integrating factor I(x)=exp(−2x2). Multiplying through and integrating gives
Second-Order Case
The transformed equation is
The homogeneous part can be solved using OGFs. Let fH(x)=∑n≥0anxn, so substitution yields the recurrence
with initial relations a2=0 and a3=a1/6. The recurrence links coefficients of the same parity, hence the solution space splits into an even {en} and odd {on} series. To peek into the derivation details, it’s instructive to read an analysis of the Hermite differential equation.
For finding the particular solution, we employ the Frobenius method. We start with the default ansatz
The lowest term is from fP′′(x), thus matching to xα yields k=α+2. This gives
Matching coefficients for power xα+n produces a recurrence of the form
Solving the system for n=0,1,2,3 gives
Apparently, odd terms vanish. The full solution is
where A and B are arbitrary constants.
Exceptional Case
If the denominator of any coefficient becomes zero, for instance, when α=−2, then we have to modify the ansatz. The Frobenius method mandates the inclusion of a logarithmic term
The logarithmic coefficient is determined by substituting this ansatz and solving the resulting linear system.
General Case
The transformed equation is
Again, the homogeneous part can be solved using OGFs. The recurrence relation becomes a difference equation of order r. Specifically, the coefficient of xn in the expansion involves terms from an+r down to an−r; xr−j is paired with dj/dxj, so the shift in power is always (r−j)−j=r−2j. Thus, the parity of the shifts (even vs odd) is fixed by the parity of r.
By the fundamental theorem of linear ODEs, an rth order homogeneous equation at an ordinary point is guaranteed to have exactly r linearly independent solutions. In the context of our power series f(x)=∑anxn, these r independent solutions are uniquely determined by the r arbitrary initial conditions: a0,a1,…,ar−1.
The parity of r simply dictates how these r degrees of freedom are processed by the recurrence:
When r is even: The r initial conditions are split evenly across the two independent parity subspaces (r/2 even coefficients, r/2 odd coefficients). The recurrence propagates them independently, yielding r/2 purely even solutions and r/2 purely odd solutions, totaling r independent solutions. We’ve already seen example of this with r=2.
When r is odd: All r initial conditions (a0,…,ar−1) feed into the same unified recurrence chain. By setting one of these initial coefficients to 1 and the other r−1 coefficients to 0, and repeating this for each ak (0≤k<r), the single recurrence naturally generates the full fundamental set of r linearly independent solutions.
For finding the particular solution, we employ the Frobenius method, starting with the default ansatz. The highest-derivative term f(r) produces the lowest power, thus matching to xα gives
Hence, the particular solution begins at xα+r
We must manage exceptional cases, as in the situation with r=2.
Exercise 3.53
See Exercise 3.51.
Exercise 3.54
We define T to be the set of all binary trees, and adopt the notation ∣t∣ to represent, for t∈T, the number of external nodes in t. Then we have the following derivation:
The first line is an alternative way to express T(z) from its definition. Each tree with exactly k external nodes contributes exactly 1 to the coefficient of zk, so the coefficient of zk in the sum “counts” the number of trees with k external nodes. The second line follows from the recursive definition of binary trees: either a binary tree has one external node (which accounts for the z), or it can be decomposed into two independent binary trees whose external nodes comprise the external nodes of the original tree, without the root (internal node). The third line follows because the index variables tL and tR are independent.
In this case T(0)=0. It is always worthwhile to check our the math with a computer. The next Sage snippet correctly prints the first few known initial values and T6=42=61(510):
Exercise 3.55 🌟
This exercise introduces the concept of a quasi-polynomial..
The given OGF is a rational function
The book contains a remark, that we can learn much about asymptotic behavior of coefficients by looking at singularities of the generating function. A kth root of unity ω for k∈{1,5,10,25,50} makes the denominator zero. To understand the coefficients, we look at the partial fraction decomposition of D(z) and connect it to these singularities.
The value z=1 has order 5, as it makes any factor 1−zk zero in the denominator. The matching summand in the decomposition of D(z) would participate as C/(1−z)5, where C is some constant. Looking at Table 3.1, this would be represented in expanded form as ∑N≥0(NN+4)zN. Therefore, the coefficient of zN would grow as Θ(N4). Observe, that the other roots of unity are associated with lower exponents. Nevertheless, they do introduce cyclic fluctuations to the count, since they are periodic.
Combining the polynomial growth from z=1 and the periodicity from the other roots of unity, we get that the form of [zN]D(z) is a quasi-polynomial. If we denote by r≡N(mod50), where 50 is a least common multiple of the coin denominations, then we have
where the coefficients ai(r) depend on the remainder r.
Exercise 3.56
The next Python program uses the mathematical model from the previous exercise. The idea is to precompute coefficients for r∈[0,49] and afterward evaluate the quasi-polynomial for a given N. The implementation employs dynamic programming as well as fast linear equations solver from the Numpy package. The system of linear equations is represented as a Vandermonde matrix.
Running this program produces the following output:
LeetCode also offers this problem under Coin Change II, although covers only small cases.
Exercise 3.57
This is a simple variation of Polya's problem of changing a dollar. Instead of coins, we have powers of 2 excluding 20. Let the index pi denote the number of 2i factors. This gives
Each configuration of powers of 2 that adds up to N clearly contributes exactly 1 to the coefficient of zN. The second line follows since the indices of summations are all independent. The last line is a translation based on Table 3.1 from the book.
Exercise 3.58
First, we prove by induction on t that
The base case t=1 is trivially satisfied. For t>1 we’ve
When t→∞ then 1/(1−z)=∑nzn=∏n(1+z2n), so Euler's identity follows.
The identity reflects the fact that every non-negative integer has a unique binary representation. Expanding the product is equivalent to forming exponents by summing distinct powers of 2. Since every integer n can be written as a sum of powers of 2 in exactly one way, every term zn appears exactly once in the resulting series.
Exercise 3.59
In base 2, the digits are {0,1}, so the factors were (1+z2n)=(z0⋅2n+z1⋅2n).
In base 3, the digits are {0,1,2}. This means that for each position n (representing the value 3n), we need a term that offers the choice of adding 0⋅3n, 1⋅3n, or 2⋅3n to the exponent. Therefore, the generalized identity is
All the other details are analogous to the previous exercise.
Exercise 3.60
As in Exercise 3.58, expanding the product is equivalent to forming exponents by summing distinct powers of 2. But this time the expansion also happens with a sign flip. Adding each power of 2 is equivalent to increasing the number of ones in a binary representation of N by 1. We start with the factor 1−z, hence even number of ones are designated with +1, whilst odd number of ones with -1. This gives
where νN is the population count.
Exercise 3.61
The expected value of X expressed in terms of rk is known as the tail-sum formula.
Exercise 3.62
We define a combined function R(x)=P(x)Q(x) and apply standard calculus rules. Recall that P(1)=Q(1)=1 is given as a precondition.
To prove the mean we must express R′(1) in terms of P′(1) and Q′(1). This gives
The variance is handled in a similar way, but we have to first find the second derivative of R(x).
We can now substitute R′′(1) and R′(1) into the definition of variance, thus
Exercise 3.63
The derivates of Pn(u) can be easily computed using WolframAlpha; the second derivate is one the alternative forms offered by the tool.
Using l’Hôpital’s rule we can compute the derivatives at 1. Again, WolframAlpha can help a lot in finding derivates of complicated numerators.
Exercise 3.64
Let X be the random variable that counts the number of leading zeros in a random binary string. For a string of size n with independent fair bits, the probability mass function is
The corresponding PGF is
The mean is Pn′(1)=1−2n1.
For the variance we need Pn′′(1)=2−2n−1n+1, so
The standard deviation is σX=var(X). When n→∞ then the mean is 1 and the standard deviation is 2.
Exercise 3.65
We must find the only missing piece pn′′(1). We have
After substituting all known quantities into the formula from Table 3.5, we get that the variance for the number of 1 bits in a random binary string of length n is n/4.
Exercise 3.66
Let’s focus our attention on [zn], since we want to find the mean for the binomial distribution with size n. We know that [zn]qk(z)=(kn), so 0≤k≤n. Furthermore, [zn]P(z,1)=2n. We also have
Substituting all these into the formula for the cumulated cost gives
The double sum can be easily calculated by drawing a symmetric square matrix of size (n+1)×(n+1). The kth row should be filled with (kn). All columns sum to 2n and the main diagonal also sums to 2n. Our double sum is half the total sum of all entries. There is only one caveat; we must add once again the sum of entries at the main diagonal before dividing by 2.
Finally, the mean is simply the cumulated cost divided by 2n, which results in n/2.
Exercise 3.67
The book doesn’t provide details about derivation of the functional equation that must be satisfied by the BGF. This is important for understanding this and the next exercise. So, we’ll fill the gap here.
Derivation of the Functional Equation
We are given the recurrence for the PGF
that is defining the exponential BGF
Notice that Q(z,1) represents the sum of probabilities for each N, so it must designate a sequence of all ones, hence Q(z,1)=1/(1−z).
When dealing with permutations, there is an inherent duality between an exponential BGF and a PGF, as highlighted in the book. Namely, the division by N! moves into the kernel of an EBGF, which gets "activated" once we read out the coefficients of partial derivatives evaluated at u=1. It’s unnecessary to perform the division by the number of structures, as this calculation has already been done "from" the kernel. As a matter of fact, stricly dividing by [zN]Q(z,1)=1 is redundant.
Let’s differentiate this BGF with respect to z and substitute the recurrence of QN(u) into it. We use the compact notation for partial derivatives, as introduced in the book.
The correct initial condition is Q(0,u)=Q0(u)=1, which tells that sorting an empty array has zero cost with absolute certainty (probability is 1). The book shows the arguments in permuted order.
Solution of the Exercise
We know that QN(1)=1, since QN(u) is a PGF. This gives
Notice that QN′(1) satisfies the standard quicksort recurrence that the book addressed in section 3.3, so it must have the same solution. Consequently,
To verify the expression for q[2](z) we must differentiate the functional equation that Q(z,u) must satisfy twice with respect to u. To make the exposition manageable, due to convoluted terms, let’s introduce the following substitutions:
A(z)=Q(z,1)=1/(1−z) implying that A′(z)=A2(z).
x≡uz, y≡u and B(x,y)=Q(x,y) implying that Bu=zBx+By. This follows from applying the multivariable chain rule.
Let’s differentiate Qz=u2B2 once w.r.t. u to get
Repeating the previous step once more we arrive at
Evaluated at u=1 this becomes
It remains to compute Buu(z,1). Since Bu=zQz(uz,u)+Qu(uz,u) we have
Now
We can eliminate Q from Buu
We also have
Substituting into the expression for (q[2])′(z) and simplifying yields a linear first-order ODE, with q[2](0)=0, of the form
The integrating factor is (1−z)2. The solution confirms that
Exercise 3.68
Let’s start with the sum
The following list shows the necessary coefficient extraction toolbox. The first identity is a left-shifted elementary OGF from Table 3.1. The second identity is given in Exercise 3.28. The last one is derived by differentiating both sides of the second identity w.r.t. α.
[zN](1−z)α1=(α−1N+α−1)
[zN](1−z)α1ln1−z1=(α−1N+α−1)(HN+α−1−Hα−1).
[zN](1−z)α1ln21−z1=(α−1N+α−1)((HN+α−1−Hα−1)2−(HN+α−1(2)−Hα−1(2))).
Now it’s a matter of getting coefficients for each summand and consolidating the result. Recall that variance is defined as
Using a computer algebra tool we can confirm the exact expression for the variance given in Theorem 3.9.
Exercise 3.69 🌟
This exercise expands Table 3.6 from the book with new identities.
Exercise 3.70
By matching coefficients on both sides, we obtain the desired identity.
Exercise 3.71
By matching coefficients on both sides, we obtain the desired identity.
Exercise 3.72
By matching coefficients on both sides, we obtain the desired identity.
Exercise 3.73
Setting x=0 in the definition yields
Considering the EGF of the Bernoulli polynomials for x=1 and using Table 3.6 gives
By matching coefficients on both sides, we have
B0(1)=B0
B1(1)=B1+1
Bm(1)=Bm for m>1.
Exercise 3.74
We start with the EGF of the Bernoulli numbers (see Table 3.6)
Let’s define g(z)=f(z)+z/2. It is easy to show that g(−z)=g(z), which means g(z) is even. Expanding g(z) gives
Since g(z) is even, its series expansion contains only even powers of z. Therefore, Bk is zero for k odd, k≥3. This concludes the proof.
Exercise 3.75
This exercise is a simple generalization of the DGF for even numbers from the book. Observe that a positive integer N ending in k≥0 zeros has the form N=2kM, where M≥1. This gives
Exercise 3.76
The previous exercise already provides much of the answer, we just need to sum up DGFs for all k≥1, since absence of a kernel component entails a count of zero (odd number). This gives
Exercise 3.77
We want to have kernel components only for perfect squares. Using the same idea as for even numbers from the book (see also Exercise 3.75), we have
Exercise 3.78
This directly follows from the definition of the Lambert series.
Last updated