For the complete documentation index, see llms.txt. This page is also available as Markdown.

Chapter 7: Permutations

Exercise 7.1 🌟

All algorithms run in Θ(N)\Theta(N) time without first trying to reconstruct the associated permutation.

  • left-to-right maxima: Traverse the inversion table from left to right and register indices where qk=0q_k=0.

  • right-to-left maxima: If pkp_k is a RTL maximum, all elements greater than pkp_k must be to its left. Therefore, a smaller qkq_k means the element itself is larger. By traversing the inversion table right-to-left, the RTL maxima correspond exactly to the indices where qkq_k achieves a new minimum.

    1. Initialize minq=\text{min$_q$} = \infty and an empty list.

    2. Traverse kk backwards from NN to 1:

      1. If qk<minqq_k < \text{min$_q$}, add kk to the list and update minq=qk\text{min$_q$}=q_k.

  • right-to-left minima: Let sk=k1qks_k = k- 1 - q_k denote the number of elements to the left of pkp_k that are smaller than pkp_k. Similar to the logic above, by traversing the inversion table right-to-left, the RTL minima correspond exactly to the indices where sks_k achieves a new minimum.

    1. Initialize mins=\text{min$_s$} = \infty and an empty list.

    2. Traverse kk backwards from NN to 1::

      1. If sk<minss_k<\text{min$_s$}, add kk to the list and update mins=sk\text{min$_s$}=s_k.

Exercise 7.2

Let mm be the number of cycles in the permutation and denote by lkl_k the length of the kkth cycle. The number of ways to write the sample permutation in cycle notation is m!k=1mlkm! \prod_{k=1}^m l_k.

For the sample permutation in Figure 7.1, we’ve 4!×6×6×2×1=17284!\times6 \times6\times2\times1=1728 equivalent ways to write it down.

Exercise 7.3

There are (2NN)((N1)!)2/2\binom{2N}{N}((N-1)!)^2/2 permutations of 2N2N elements having exactly two cycles, each of length NN.

There are (2NN)N!/2N=(2N1)!!\binom{2N}{N}N!/2^N=(2N-1)!! permutations of 2N2N elements having exactly NN cycles, each of length 2. Notice that we must divide by 2N2^N, since for a given representation all other possible ways of "flipping" elements in those NN cycles are duplicates.

Exercise 7.4

We must maximize the product from Exercise 7.2 subject to the constraint k=1mlk=N\sum_{k=1}^m l_k = N. The formula depends on the number of cycles and the product of their lengths. If we increase mm then we reduce the lengths and vice versa. Nonetheless, the contribution of mm is more important, as it grows factorially.

Let’s assume that we start with an identity permutation with m=Nm=N. This results in N!N! equivalent representations. Can we maximize this number? If we set only one cycle to length 2 and keep the others at 1, then we get (N1)!2<N!(N-1)!2<N! for N>2N>2. It turns out that we cannot attain a better result.

Therefore, the identity permutation has the greatest number of different representations with cycles. For the trivial edge cases of N=1N=1 and N=2N=2, all permutations have the exact same number of representations.

Exercise 7.5

The Python 3 program below implements the task in Θ(N2)\Theta(N^2) time.

It outputs 232 for the sample permutation from the book.

Exercise 7.6 🌟

Since FF is strictly increasing for all practical purposes, we’ve

a1<a2<a3F(a1)<F(a2)<F(a3).a_1 < a_2 < a_3 \quad \Longleftrightarrow \quad F(a_1) < F(a_2) < F(a_3).

By the probability integral transform, Ui=F(ai)U_i = F(a_i) are independent and identically distributed (i.i.d.) uniform on [0,1][0,1]. For i.i.d. uniform random variables, all 3!3! orderings are equally likely by symmetry, because the joint density is symmetric. Hence,

Pr{a1<a2<a3}=Pr{U1<U2<U3}=13!.\Pr\{a_1 < a_2 < a_3\} = \Pr\{U_1 < U_2 < U_3\} = \frac{1}{3!}.

Observe that we can easily change the relational operator, for example, from < to > and still get the same outcome. Furthermore, for NN i.i.d. continuous random variables, the probability of any fixed strict ordering is 1/N!1/N!.

Exercise 7.7

11 12 3 4 1 9 5 13 8 15 7 10 6 2 14\bold{11} \space \bold{12} \space 3 \space 4 \space 1 \space 9 \space 5 \space \bold{13} \space 8 \space \bold{15}\space 7\space 10\space 6\space 2 \space 14

Cycle leaders are shown in bold letters. Apparently, they’re left-to-right maxima.

Exercise 7.8

The Python 3 script below implements the original left-to-right minima based variant.

Exercise 7.9 🌟

For computing the inversion table corresponding to a given permutation, we scan the permutation left to right, maintaining a Fenwick tree over the values 1N1 \ldots N. When we encounter the value pip_i, we query how many values greater than pip_i have already been seen: that count is exactly qiq_i. Then we mark pip_i as seen. The usage of the Fenwick tree is important to attain an efficient linearithmic algorithm, as it natively supports range sums. Recall that marking pip_i as seen is nothing else than setting 1 for that position in the range. A naive approach would result in quadratic performance.

For computing the permutation corresponding to a given inversion table, we follow the algorithm from the book and process the entries from right to left (i=N..1i=N..1). We maintain an array of “empty slots” that is represented as a Fenwick tree having 1 at each position that is still free. To set pip_i to be the (qi+1)(q_i+1)th largest of the integers not yet used, we binary search on the Fenwick tree’s prefix sums to find the smallest position kk such that the number of free slots up to kk is iqii-q_i. Then we mark that slot as occupied (set Fenwick tree at kk to 0).

The previous algorithm uses binary search with embedded computations of prefix sums. This achieves a near optimal performance Θ(Nlog2N)\Theta(N \log^2 N). It’s possible to leverage an optimization technique called binary lifting to reduce it to Θ(NlogN)\Theta(N \log N). This actually boils down to "walking" the Fenwick tree, as it stores partial sums in powers of 2 (this is why it’s called a binary indexed tree). By examining the largest power of 2, subtracting it from kk if the sum is smaller, and narrowing the interval, we can resolve the exact index in O(logN)O(\log N) operations.

Exercise 7.10

A one-to-one correspondence between permutations and lists of NN integers q1q2qNq_1q_2 \dots q_N with 0qiNi0 ≤ q_i \le N-i is easy to establish. Given a permutation, its altered inversion table is such a list. The reconstruction algorithm proves the existence of the transformation in the opposite direction:

  1. Create an array of NN empty slots.

  2. Process the values from smallest to largest (v=1..Nv=1..N).

    1. Place vv into the (qv+1)(q_v+1)th available empty slot from the left.

    2. Mark the slot as occupied.

Observe that we can use the same Fenwick tree based approach, as in the previous exercise. At the start, we initialize it with 1s at all NN positions (representing empty slots). Using binary lifting we can find the next available free slot in O(logN)O(\log N) time.

Exercise 7.11

There are (Nk)\binom{N}{k} independent ways to pick the rows and columns for rooks. Let the sequence of selected columns be c1c2ckc_1 c_2 \dotsc c_k. For any such sequence, we have an additional k!k! possibilities to place rooks in selected rows; kk choices for c1c_1, k1k-1 choices for c2c_2, and so on. A symmetrical argument applies for permuting rows. Therefore, multiplying these numbers together gives the total to be (Nk)2k!.\binom{N}{k}^2k!.

Exercise 7.12

There are symmetries. The "above and left" gives the same number as "below and right" that equals inv(p)\text{inv}(p). Furthermore, the "above and right" gives the same number as "below and left" that equals (N2)inv(p)\binom{N}{2}-\text{inv}(p).

Exercise 7.13

An involution is its own inverse. Therefore, the original matrix (lattice representation) must equal its transpose. This is by definition a symmetric matrix.

Exercise 7.14

Exercise 7.15 🌟

Let x,y,zx,y,z be 3 consecutive values in a permutation. Looking at Figure 7.5, we can see that an inorder traversal of a HOT reproduces the underlying permutation. Now, based on relationships between these 3 values, we can decipher the required structural characterizations of properties of permutations:

  • x<y    x yx<y \implies x\overset{-}{\space}y (rise). Because xx is smaller it’s an ancestor of yy. For the inorder traversal to visit yy immediately after xx, yy cannot have a left child. On the other hand, xx must have a right child.

  • y>z    y +zy>z \implies y\overset{+}{\space}z (fall). Because zz is smaller it’s an ancestor of yy. For the inorder traversal to visit zz immediately after yy, yy cannot have a right child. On the other hand, zz must have a left child.

  • x<y<z    x y zx < y < z \implies x\overset{-}{\space}y\overset{-}{\space}z (double rise). yy cannot have a left child, but it does have a right child, since it’s an ancestor of zz. Therefore, yy only has a right child.

  • x>y>z    x +y +zx > y > z \implies x\overset{+}{\space}y\overset{+}{\space}z (double fall). yy cannot have a right child, but it does have a left child, since it’s an ancestor of xx. Therefore, yy only has a left child.

Exercise 7.16 🌟

We can count such alternating permutations thankfully to bijection with HOTs. There are two types of such permutations: down-up (starts with a fall) and up-down (starts with a rise). There is a simple one-to-one correspondence between them, so it’s enough to handle one group. It turns out that down-up permutations can be nicely described using the symbolic method.

Let’s tackle the case when NN is odd. This can be expressed via the symbolic method

A=Z+ZA2,\mathcal{A} = \mathcal{Z} + \mathcal{Z}^{\Box} \star \mathcal{A}^2,

where A\mathcal{A} is the class of odd-length down-up permutations represented as HOTs consisting of strictly degree 0 and degree 2 nodes. This can be seen by recalling that the inorder traversal of a HOT reestablishes the original permutation. Since we have a sequence starting with a fall, using the rules from the previous exercise, we can conclude that the border elements of a sequence must be leaves together with all the peaks. In the middle, we have valleys.

In a HOT, the root must always be the absolute minimum value. To capture this via the symbolic method, we must use a new operator, called the min-box product, that controls the relabeling process. The corresponding functional equation is

A(z)=1+A(z)2,A'(z) = 1 + A(z)^2,

with the initial condition A(0)=0A(0) = 0. The solution is A(z)=tan(z)A(z) = \tan(z).

To handle the case when NN is even, notice that the absolute minimum (taking up 1 node) must be the root. Picking any candidate splits the sequence into two down-up sequences of different parities (zero is regarded as even). This gives

B=ϵ+Z(BA),\mathcal{B} = \epsilon + \mathcal{Z}^{\Box} \star (\mathcal{B} \star \mathcal{A}),

where B\mathcal{B} is the class of even-length down-up permutations represented as HOTs. The corresponding functional equation is

B(z)=B(z)A(z)=B(z)tan(z),B'(z) = B(z) \cdot A(z)=B(z) \cdot \tan(z),

with the initial condition B(0)=1B(0) = 1. The solution is B(z)=sec(z)B(z)=\sec(z).

Since the two cases are disjoint, the combined result is

F(z)=2(A(z)+B(z))1z=2(tan(z)+sec(z))1z,\boxed{F(z)=2(A(z)+B(z))-1-z=2(\tan(z)+\sec(z))-1-z},

where F(z)F(z) is the EGF for alternating permutations. The coefficients can be extracted by looking at the Taylor expansions of the constituent functions. Notice that we multiply by two to include the up-down permutations, too. Finally, because up-down and down-up permutations are indistinguishable for lengths 0 and 1, multiplying by 2 overcounts them, hence, we must subtract extra terms.

Exercise 7.17

There is only one permutation (shown in Figure 7.5) associated with the given HOT, unlike BSTs (see Exercise 7.14).

Exercise 7.18

We just need to tweak a bit the symbolic method expression from Exercise 7.16 to include nodes with a single child (either left or right). This gives

K=Z+Z(2K+K2).\mathcal{K} = \mathcal{Z} + \mathcal{Z}^{\Box} \star (2\mathcal{K}+\mathcal{K}^2).

The corresponding functional equation is

K(z)=1+2K(z)+K(z)2,K'(z)=1+2K(z)+K(z)^2,

with the initial condition K(0)=0K(0) = 0.

Exercise 7.19

Let P\mathcal{P} be the set of all permutations. For each permutation pp, BST(p)\text{BST}(p) represents the corresponding BST built from pp. Recall that multiple permutations may result in the same BST. Observe that the length of the given permutation determines the size of the associated BST. This gives

C(z)=pPipl(BST(p))zpp!=pLPpRP(pL+pRpL)(ipl(BST(pL))+ipl(BST(pR))+pL+pR)zpL+pR+1(pL+pR+1)!.\begin{align*} C(z) &= \sum_{p \in \mathcal{P}} \text{ipl}(\text{BST}(p)) \frac{z^{|p|}}{|p|!} \\ &= \sum_{p_L \in \mathcal{P}} \sum_{p_R \in \mathcal{P}} \binom{|p_L| + |p_R|}{|p_L|} \Big( \text{ipl}(\text{BST}(p_L)) + \text{ipl}(\text{BST}(p_R)) + |p_L| + |p_R| \Big) \frac{z^{|p_L| + |p_R| + 1}}{(|p_L| + |p_R| + 1)!}. \end{align*}

The binomial convolution is fully justified, because even though the left and right subtrees are split based on the value of the root (first element in pp), they maintain their relative order. The number of ways to interleave those two subsequences, without impacting the final tree, is exactly driven by the binomial coefficient. This becomes clearer by changing perspectives via lattice representation of permutations, more specifically looking at Figure 7.4. As stated in the book:

Note that many permutations might correspond to the same binary search tree: interchanging columns corresponding to a node above and a node below any node will change the permutation, not the tree.

The pivot is the root of the BST (first element in the permutation). Everything above and below of it are elements comprising the subtrees. Now, the number of ways to change columns for those elements is exactly dictated by the binomial coefficient.

Differentiating both sides of the equation, as was done in the book for HOTs, we get

C(z)=pLPpRP(pL+pRpL)(ipl(BST(pL))+ipl(BST(pR))+pL+pR)zpL+pR(pL+pR)!.C'(z) = \sum_{p_L \in \mathcal{P}} \sum_{p_R \in \mathcal{P}} \binom{|p_L| + |p_R|}{|p_L|} \Big( \text{ipl}(\text{BST}(p_L)) + \text{ipl}(\text{BST}(p_R)) + |p_L| + |p_R| \Big) \frac{z^{|p_L| + |p_R|}}{(|p_L| + |p_R|)!}.

We can use H(z)H(z) to enumerate permutations, thus we get our differential equation

C(z)=2C(z)H(z)+2zH(z)H(z)=2C(z)1z+2z(1z)3.C'(z) = 2 C(z) H(z) + 2zH(z)H'(z)=\frac{2C(z)}{1-z}+\frac{2z}{(1-z)^3}.

Exercise 7.20

Employing Theorem 7.1 gives 1,576,575 permutations.

Exercise 7.21

From left to right, the frequencies are: 224, 1 and 896.

Exercise 7.22

The smallest number corresponds to a chain shape. The largest number corresponds to balanced trees. The reason is obvious, since in these trees subtree sizes drop rapidly.

Exercise 7.23

The arguments are literally the same as for the direct derivation of EGF, just that we are growing larger structures from the perspective of those larger structures. As a side note, we can immediately read out this recurrence from the differential equation for the involution EGF (presented in the book) by equating coefficients for NN on both sides of the equation.

Exercise 7.24

This is a variation of the previous exercise. We need to add cycles of length 3 into the picture, that entails a "double loop." This gives

bN+1=bN+NbN1+N(N1)bN2, for N>1 with b0=b1=1,b2=2.b_{N+1}=b_N+Nb_{N-1}+N(N-1)b_{N-2}, \quad \text{ for $N>1$ with $b_0=b_1=1,b_2=2$}.

Exercise 7.25

Our generating function is

Pk(z)=exp(j=1kzjj).P_k(z) = \exp\left( \sum_{j=1}^k \frac{z^j}{j} \right).

The radius of convergence bound is (see Section 5.5 in the book)

[zN]Pk(z)minx(0,)Pk(x)xNminx(0,)exp(xk/k)xN.[z^N]P_k(z) \le \underset{x \in (0,\infty)}{\min} \frac{P_k(x)}{x^N}\sim \underset{x \in (0,\infty)}{\min}\frac{\exp(x^k / k)}{x^N}.

Because we are looking for the behavior as NN \to \infty, we expect the minimizing value of xx to grow large. For large xx, the highest degree term in the polynomial exponent, xk/kx^k/k, completely dominates the lower-order terms.

To minimize this, we take the derivative of its logarithm with respect to xx and set it to zero

ddx(xkkNlnx)=xk1Nx=0xk=N    x=N1/k.\begin{align*} \frac{d}{dx} \left( \frac{x^k}{k} - N \ln x \right) &= x^{k-1} - \frac{N}{x} = 0 \\ &\therefore x^k = N \implies x = N^{1/k}. \end{align*}

This gives

[zN]Pk(z)Pk(N1/k)NN/keN/kNN/k.[z^N] P_k(z) \le \frac{P_k(N^{1/k})}{N^{N/k}} \sim \frac{e^{N/k}}{N^{N/k}}.

Using the Stirling's approximation N!(N/e)NN! \sim (N/e)^N, we get

N![zN]Pk(z)(N/e)N(eN/kNN/k)=NN(11/k)eN(11/k). N! [z^N] P_k(z) \sim (N/e)^N\left( \frac{e^{N/k}}{N^{N/k}} \right)= N^{N(1-1/k)} e^{-N(1-1/k)}.

Exercise 7.26

Actually, in the derivation of the EGF for the number of permutations that consist only of cycles of odd length (see Exercise 5.7), we had indirectly solved this problem, too. Thus, the EGF associated with the combinatorial class PEVENP^*_{EVEN} is

PEVEN(z)=11z2.P_{EVEN}^*(z) = \frac{1}{\sqrt{1-z^2}}.

The EGF for cycles of length divisible by tt is a simple generalization of even length cycles

Ct(z)=k1ztktk=1tk1(zt)kk=1tln11zt.C_t(z) = \sum_{k\ge1} \frac{z^{tk}}{tk}= \frac{1}{t} \sum_{k\ge1} \frac{(z^t)^k}{k} = \frac{1}{t} \ln \frac{1}{1-z^t}.

Exponentiate to find the EGF for the permutations

Pt(z)=exp(1tln11zt)=1(1zt)1/t.P_t^*(z) = \exp\left( \frac{1}{t} \ln \frac{1}{1-z^t} \right)=\frac{1}{(1-z^t)^{1/t}}.

Exercise 7.27

Following the instruction from the book gives

D(z)+(1z)D(z)=ezD(z)+(1z)D(z)=(1z)D(z)D(z)+D(z)zD(z)=D(z)+zD(z)D(z)zD(z)=zD(z).\begin{align*} -D(z) + (1 - z)D'(z) &= -e^{-z} \\ -D(z) + (1 - z)D'(z) &= -(1 - z)D(z) \\ -D(z) + D'(z) - zD'(z) &= -D(z) + zD(z) \\ D'(z) - zD'(z) &= zD(z). \end{align*}

Let dNd_N be the number of derangements of size NN. Now, we extract the coefficient of zNN!\frac{z^N}{N!} using Table 3.4 from the book

dN+1NdN=NdN1    dN+1=N(dN+dN1)for N>1,d_{N+1} - N d_N = N d_{N-1} \implies d_{N+1} = N (d_N + d_{N-1}) \qquad \text{for $N>1$},

with d1=0d_1=0 and d2=1d_2=1.

Exercise 7.28 🌟

Slow Symbolic Manipulation

The next Python 3 script uses SymPy to evaluate the EGF according to Table 7.5. Compare this method with the "manual" approach in Exercise 6.63. Nevertheless, don’t try to run this with k>8k>8, as it requires an eternity to finish.

Fast Combinatorial Approach

Instead of expanding the generating function, we can build the permutations directly via the proper combinatorial recurrence (see the previous exercise and Exercise 7.24). Let pNp_N be the number of valid permutations of size NN. Consider the element NN. In a valid permutation, it must belong to a cycle of some length jj. Because of our restriction, kjNk \le j \le N. For any valid cycle length jj:

  • We need to pick j1j-1 other elements from the remaining N1N-1 elements. There are (N1j1)\binom{N-1}{j-1} ways to do this.

  • There are (j1)!(j-1)! ways to arrange these jj elements into a valid cycle.

  • There are pNjp_{N-j} ways to arrange the remaining elements into valid cycles.

Multiplying these together and summing over all possible allowed cycle lengths jj, gives our recurrence

pN=j=kN(N1j1)(j1)!pNj=j=kN(N1)!(Nj)!pNj.p_N = \sum_{j=k}^N \binom{N-1}{j-1} (j-1)! \, p_{N-j}= \sum_{j=k}^N \frac{(N-1)!}{(N-j)!} p_{N-j}.

The next Python 3 script employs dynamic programming and achieves stunning performance compared to the previous variant. It handles much larger problem sizes than required for this exercise.

Exercise 7.29 🌟

There are (Nk)\binom{N}{k} ways to form a subset of size kk from NN elements. In an arrangement, the order matters, so we must multiply this by k!k!. In other words, the product is a total number of ways to form kk-tuples from NN elements. Finally, we must sum over all kk. We can translate this in terms of an EGF

A(z)=N0aNzNN!=N0(k=0N(Nk)k!)zNN!=ez1z.A(z)=\sum_{N\ge 0}a_N\frac{z^N}{N!}=\sum_{N\ge 0} \left(\sum_{k=0}^N\binom{N}{k}k!\right)\frac{z^N}{N!}=\frac{e^z}{1-z}.

The RHS follows from the definition of a binomial sum of factorials (see Tables 3.3 and 3.4 in the book).

As a side note, we can also derive the EGF using the symbolic method. An arrangement takes NN labels and partitions them into two distinct groups:

  • The primary ordered group defining the kk-tuples of selected elements.

  • The leftover unordered group consisting of unselected elements.

The number of ways to distribute NN labels onto kk items is (Nk)\binom{N}{k}. Therefore,

Arrangements=SEQ(Z)SET(Z).\text{Arrangements} = \text{SEQ}(\mathcal{Z}) \star \text{SET}(\mathcal{Z}).

Exercise 7.30

The hint gives the answer, since there is a bijection between the original permutations and their complements. Furthermore, the number of rises in a permutation pp equals the number of falls in its complement qq, and vice versa. For example, let pi<pi+1p_i<p_{i+1} be a rise in pp. It becomes a fall in q, because

N+1pi>N+1pi+1    pi>pi+1.N+1-p_i>N+1-p_{i+1} \implies -p_i>-p_{i+1}.

Thankfully, due to this bijection, the average number of rises and the average number of falls must be identical. Their total is N1N-1, so the result follows immediately.

Exercise 7.31 🌟

A(z,u)=pPuruns(p)zpp!=1+pP(runs(p)uruns(p)+(p+1runs(p))uruns(p)+1)zp+1(p+1)!Az(z,u)=pP(runs(p)uruns(p)+puruns(p)+1+uruns(p)+1runs(p)uruns(p)+1)zpp!Az(z,u)=uAu(z,u)+uzAz(z,u)+uA(z,u)u2Au(z,u)Az(z,u)=11uz(uA(z,u)+u(1u)Au(z,u)).\begin{align*} A(z,u) &= \sum_{p \in \mathcal{P}} u^{runs(p)}\frac{z^{|p|}}{|p|!} \\ &= 1 + \sum_{p \in \mathcal{P}} \Big( \text{runs}(p) u^{\text{runs}(p)} + (|p| + 1 - \text{runs}(p)) u^{\text{runs}(p)+1} \Big) \frac{z^{|p|+1}}{(|p|+1)!} \\ A_z(z,u) &= \sum_{p \in \mathcal{P}} \Big( \text{runs}(p) u^{\text{runs}(p)} + |p|u^{\text{runs}(p)+1} + u^{\text{runs}(p)+1} - \text{runs}(p) u^{\text{runs}(p)+1} \Big) \frac{z^{|p|}}{|p|!} \\ A_z(z,u) &= uA_u(z,u) + uzA_z(z,u) + uA(z,u) - u^2A_u(z,u) \\ A_z(z,u) &= \frac{1}{1-uz} \Big( uA(z,u) + u(1-u)A_u(z,u) \Big). \end{align*}

Exercise 7.32

A(z,u)=1u1uez(1u)=(1u)m0(uez(1u))m=m0um(1u)ezm(1u).\begin{align*} A(z,u) &= \frac{1-u}{1 - u e^{z(1-u)}} \\ &= (1-u) \sum_{m \ge 0} \left( u e^{z(1-u)} \right)^m \\ &= \sum_{m \ge 0} u^m (1-u) e^{zm(1-u)}. \end{align*}

We can also expand the exponential into a Taylor series

ezm(1u)=N0(zm(1u))NN!=N0zNN!mN(1u)N.e^{zm(1-u)} = \sum_{N \ge 0} \frac{\big(zm(1-u)\big)^N}{N!} = \sum_{N \ge 0} \frac{z^N}{N!} m^N (1-u)^N.

Substitute that expanded exponential back into our equation

A(z,u)=m0um(1u)(N0zNN!mN(1u)N)=N0zNN!(m0mNum(1u)N+1)pN(u).\begin{align*} A(z,u) &= \sum_{m \ge 0} u^m (1-u) \left( \sum_{N \ge 0} \frac{z^N}{N!} m^N (1-u)^N \right) \\ &= \sum_{N \ge 0} \frac{z^N}{N!} \underbrace{\left( \sum_{m \ge 0} m^N u^m (1-u)^{N+1} \right)}_{p_N(u)}. \end{align*}

Thus, ANk=[uk]pN(u)A_{Nk}=[u^k]p_N(u). Again, we continue expanding terms, this time the binomial

(1u)N+1=j=0N+1(N+1j)(u)j=j0(1)j(N+1j)uj.(1-u)^{N+1} = \sum_{j=0}^{N+1} \binom{N+1}{j} (-u)^j = \sum_{j \ge 0} (-1)^j \binom{N+1}{j} u^j.

Now substitute this back into our polynomial expression

pN(u)=(m0mNum)(j0(1)j(N+1j)uj).p_N(u) = \left( \sum_{m \ge 0} m^N u^m \right) \left( \sum_{j \ge 0} (-1)^j \binom{N+1}{j} u^j \right).

The indices in the sums must satisfy the following equations:

m+j=k    m=kjm0    kj0    jk.m + j = k \implies m = k - j \\ m \ge 0 \implies k-j \ge 0 \implies j \le k.

Substituting m=kjm=k-j we get

ANk=0jk(1)j(N+1j)(kj)N.A_{Nk} = \sum_{0 \le j \le k} (-1)^j \binom{N+1}{j} (k-j)^N.

Exercise 7.33

The equation in this exercise is known as Worpitzky's identity. We’ve already done bulk of the work in the previous exercise. The idea is to start with pN(u)p_N(u) and run it in "reverse" (focus on the underlined part in the expression for A(z,u)A(z,u)).

x0xNux=pN(u)(1u)N+1=(k=1NANkuk)1(1u)N+1=(k=1NANkuk)(j0(N+jN)uj).\begin{align*} \sum_{x \ge 0} x^N u^x &= \frac{p_N(u)}{(1-u)^{N+1}} \\ &= \left( \sum_{k=1}^N A_{Nk} u^k \right) \frac{1}{(1-u)^{N+1}} \\ &= \left( \sum_{k=1}^N A_{Nk} u^k \right) \left( \sum_{j \ge 0} \binom{N+j}{N} u^j \right) . \end{align*}

Substituting j=xkj=x-k into the binomial coefficient gives us the coefficient for the RHS

xN=k=1NANk(N+xkN).x^N = \sum_{k=1}^N A_{Nk} \binom{N+x-k}{N}.

A permutation with kk runs has k1k-1 falls and NkN-k rises. Based on Exercise 7.30, the number of rises equals the number of falls in a complementary permutation, so

xN=k=1NANk(x+k1N).x^N = \sum_{k=1}^N A_{Nk} \binom{x +k-1}{N}.

Exercise 7.34

Instead of looking at a finished permutation and trying to find the subsequences, let's build the permutations around a specific increasing subsequence of length kk. This is what the book’s hint is trying to tell. To force an increasing subsequence of length kk to exist in a permutation of length NN, we make the following choices:

  • Pick kk slots out of the NN available positions where our subsequence will live. There are (Nk)\binom{N}{k} ways to do this.

  • Pick kk labels for our subsequence. There are (Nk)\binom{N}{k} ways to choose these values.

  • Because this must be an increasing subsequence, the kk chosen labels must be placed into the kk chosen slots in exactly one specific order (sorted from smallest to largest).

  • We have NkN-k leftover labels and NkN-k leftover slots. We can arrange these however we want. There are (Nk)!(N-k)! ways to place them.

Multiplying these choices together and simplifying, we get

SNk=(Nk)(Nk)(Nk)!=N!(Nk)1k!.S_{Nk} =\binom{N}{k} \binom{N}{k} (N-k)!= N!\binom{N}{k} \frac{1}{k!}.

Summing SNkS_{Nk} over all kk we get SNS_N

SN=N!k=0N(Nk)1k!.S_N = N! \sum_{k=0}^N \binom{N}{k} \frac{1}{k!}.

Exercise 7.35

In the previous exercise, we’ve already found the total number of increasing subsequences of length kk across all permutations of length NN.

Now, we plug this directly into the definition of an exponential CGF

Ck(z)=NkSNkzNN!=Nk(N!(Nk)1k!)zNN!=1k!Nk(Nk)zN=zkk!(1z)k+1.C_k(z) = \sum_{N \ge k} S_{Nk} \frac{z^N}{N!} = \sum_{N \ge k} \left( N! \binom{N}{k} \frac{1}{k!} \right) \frac{z^N}{N!}= \frac{1}{k!} \sum_{N \ge k} \binom{N}{k} z^N= \frac{z^k}{k! (1 - z)^{k+1}}.

From Exercise 4.4 we get that SNk/N!Nk(k!)2S_{Nk}/N!\sim \frac{N^k}{(k!)^2}.

Exercise 7.36

Combining the formula for the grand total (see the book) with the one from the previous exercise, we get

C3(z)=11zexp(z1z)(11z+z(1z)2+z22(1z)3).C_{\ge 3}(z) = \frac{1}{1-z} \exp\left( \frac{z}{1-z} \right) - \left( \frac{1}{1-z} + \frac{z}{(1-z)^2} + \frac{z^2}{2(1-z)^3} \right).

Observe that the average number asymptotically doesn’t depend on polynomial terms stemming from the short subsequences. This is expected, since for large NN, extremely small sequences are asymptotically negligible, although their numbers can be significant in an absolute sense. Therefore, the estimate from Theorem 7.6 still applies.

Exercise 7.37

According to Theorem 7.7, the average number of all mentioned node types is N/3\approx N/3. The expected total storage cost is the sum of the expected costs of each node type. Therefore, the storage requirement is (c0+c1+c2)N/3\sim (c_0+c_1+c_2)N/3.

Exercise 7.38

This is a corollary of the proof given in Exercise 7.30. Namely, peaks in the original permutation turns into valleys in the complement, and vice versa. So, due to this bijection, valleys and peaks have the same distribution for random permutations.

As Anscombe's quartet demonstrates, it would be a blunder to quickly jumpt to a conclusion about identical distributions of parameters purely based on equality of their averages and/or variances. Proving a bijection is one reliable way to establish distributional alignment.

Exercise 7.39

Based on the corollary of Theorem 6.7, the average number of leaves in a random binary Catalan tree is N/4\sim N/4. Exercise 6.40 shows that the average number of unary nodes in a random binary Catalan tree is 2N/4=N/2\sim 2N/4=N/2. Therefore, the average number of binary nodes is also N/4\sim N/4, so the storage requirement is (c0+2c1+c2)N/4\sim (c_0+2c_1+c_2)N/4.

Exercise 7.40 🌟

Rises and falls depend solely on relative of order of values in a sequence. Thus, a sequence of NN random real numbers between 0 and 1 (uniformly and independently generated) may be regarded as a random permutation of NN elements. Based on the Theorem 7.7 from the book, in a random permutation of NN elements, the average numbers of double rises and double falls are N/6\sim N/6 each.

Let X1,X2,,XNX_1, X_2, \dots, X_N be a sequence of independent, uniformly distributed continuous random variables on the interval [0,1][0, 1]. A double fall occurs starting at index ii if

Xi>Xi+1>Xi+2.X_i > X_{i+1} > X_{i+2}.

Because these variables are continuous and independent, the probability of any two being exactly equal is zero. Therefore, any specific triplet (Xi,Xi+1,Xi+2)(X_i, X_{i+1}, X_{i+2}) must fall into one of 3!=63! = 6 possible strict orderings. By symmetry, since they are drawn from the exact same uniform distribution, every single one of those 6 orderings is equally likely. Only exactly one of those 6 orderings is a strictly descending sequence. Therefore, the probability of a double fall occurring at any specific index ii is exactly 1/6.

Let BiB_i be a Bernoulli random variable, with success probability p=1/6p=1/6, indicating if there is a double fall starting at index ii. There are N2N-2 possible starting positions, so the total expectation for the number of double falls is (N2)/6(N-2)/6 (due to linearity of expectations). The number of double rises follows by symmetry. This concludes the continuous-model proof of the above asymptotic result.

Exercise 7.41

We apply the symbolic method for labelled objects with parameters. We must mark any root that has a right-child (either right-branching node and/or binary node in an HOT). This gives (notice the usage of the boxed operator, see Exercise 7.16)

K=Z+ZK+uZK+uZK2.\mathcal{K}=Z+\mathcal{Z}^{\Box}\star\mathcal{K}+u\mathcal{Z}^{\Box}\star\mathcal{K}+u\mathcal{Z}^{\Box}\star\mathcal{K}^2.

Differentiating both sides of the corresponding functional equation with respect to zz (effectively taking out the root)

Kz(z,u)=1+(1+u)K(z,u)+uK2(z,u).K_z(z,u)=1+(1+u)K(z,u)+uK^2(z,u).

Recall, that the boxed product is translated into an integral over zz. Differentiation "annihilates" integration.

Let K=K(z,u)K=K(z,u) be an abbreviated form for the EGF. We need to integrate

dK(1+K)(1+uK)=dz.\frac{dK}{(1+K)(1+uK)} = dz.

Using partial fractions

11u(11+Ku1+uK)dK=dz11u(ln(1+K)ln(1+uK))=z+C.\begin{align*} \frac{1}{1-u} \int \left( \frac{1}{1+K} - \frac{u}{1+uK} \right) dK &= \int dz \\ \frac{1}{1-u} \left( \ln(1+K) - \ln(1+uK) \right) &= z + C. \end{align*}

Since an empty tree has size 0, K(0,u)=0K(0,u) = 0, which means C=0C = 0. Thus,

ln(1+K1+uK)=z(1u)    1+K1+uK=ez(1u)    K(z,u)=1e(u1)ze(u1)zu.\ln \left( \frac{1+K}{1+uK} \right) = z(1-u) \implies \frac{1+K}{1+uK} = e^{z(1-u)} \implies \boxed{K(z,u) = \frac{1 - e^{(u-1)z}}{e^{(u-1)z} - u}}.

It’s easy to verify that the formula is correct by computing A(z,u)=1+uK(z,u)A(z,u) = 1 + uK(z,u).

Exercise 7.42

To have one inversion, exactly one pair of neighboring elements must be swapped. There are N1N-1 such candidate pairs.

To have two inversions, exactly two pairs must swapped, which can happen in (N12)\binom{N-1}{2} ways. But for neighboring pairs the order of swaps also matters. This adds N2N-2 additional choices. So, the total is (N12)+(N2)\binom{N-1}{2}+(N-2).

For three inversions, we can select 3 pairs, but for neighboring pairs the order matters. Furthermore, if we have a neighboring triplet, then the order of swaps of the first and third pairs also matters. Besides, we can also pick pairs of elements at distance 2. For example, a,b,ca,b,c turned into c,b,ac,b,a produces 3 inversions. Combined this gives

(N13)+(N2)(N3)+(N3)+(N2)=(N13)+(N2)2+N3.\binom{N-1}{3}+(N-2)(N-3)+(N-3)+(N-2)=\binom{N-1}{3}+(N-2)^2+N-3.

Exercise 7.43

The next snippet is an expanded version of Program 7.2 from the book to also produce the inversion table qq.

Exercise 7.44

We can virtually follow the same combinatorial reasoning from the book (see the direct solution with CGFs) to derive the recurrence. The base cases are p00=1p_{00}=1 and pNk=0p_{Nk}=0 for k<0k < 0 or k>(N2)k>\binom{N}{2}. Otherwise,

pNk=1N0j<Np(N1)(kj).p_{Nk}=\frac{1}{N}\sum_{0 \le j < N} p_{(N-1)(k-j)}.

Exercise 7.45 🌟

From Table 7.5 in the book, the EGF for involutions is I(z)=exp(z+z22).I(z) = \exp\left(z + \frac{z^2}{2}\right).

Every inversion (i,j)(i, j) in an involution can be uniquely classified as either an internal inversion (where ii and jj belong to the same 2-cycle) or a cross-inversion (where ii and jj belong to different cycles). Since expectation is linear, we can calculate the toll (see below) of each cycle configuration independently and sum them.

To find the CGF C(z)C(z), we use the framework of additive parameters over sets: we isolate the specific cycle components that interact to create inversions, specify a toll function E(z)E(z), multiply by the standard EGF kernel component zkk!\frac{z^k}{k!}, and then multiply the result by I(z)I(z). Notice that we work at the level of cycles and let the machinery of GFs handle the translation of costs in terms of element wise inversions. We all the time operate at the same abstraction level, where cycles are the basic ingredients.

Internal Inversions (1 cycle of size 2)

Every 2-cycle creates exactly 1 internal inversion. Therefore, E1(z)=z2/2E_1(z)=z^2/2.

Cross-Inversions (One 1-cycle, One 2-cycle)

Suppose an inversion is formed by interactions between a 1-cycle and a 2-cycle. Suppose we have 3 elements (1, 2, 3). Cycles (1, 3)(2) yield the permutation 3, 2, 1. There are 2 cross-inversions. 1 is inverted relative to 2 and 2 is inverted relative to 3. Therefore, E2(z)=2z33!=z33E_2(z)= 2 \cdot \frac{z^3}{3!} =\frac{z^3}{3}.

Cross-Inversions (Two 2-cycles)

Suppose we have 4 elements (1, 2, 3, 4). We know each 2-cycle inherently contains exactly 1 internal inversion. Therefore, any configuration of two 2-cycles will always have exactly 2 internal inversions. We can find the cross-inversions by looking at the total inversions of the sub-permutations:

  • Cycles (1, 4)(2, 3) yield the permutation 4, 3, 2, 1. This has 4 cross-inversions. Elements 2 and 3 are inverted relative to 4, and 1 is inverted relative to 2 and 3. Again, we don't care about total number of inversions; we’re counting purely inversions crossing the borders.

  • Cycles (1, 3)(2, 4) yields the permutation 3, 4, 1, 2. This has 2 cross-inversions. 1 is inverted relative to 4 and 2 is inverted relative to 3. These are the only pairs crossing the borders.

Therefore, E3(z)=6z4/4!=z4/4E_3(z)=6z^4/4!=z^4/4.

Total Cost and Average

By the fundamental rules of additive parameters over sets, the total CGF is simply the sum of all the isolated toll-generating components, multiplied by the counting function I(z)I(z). This gives

C(z)=(E1(z)+E2(z)+E3(z))I(z)=(z22+z33+z44)I(z).C(z) =(E_1(z)+E_2(z)+E_3(z))I(z)= \left( \frac{z^2}{2} + \frac{z^3}{3} + \frac{z^4}{4} \right) I(z).

The average number of inversions in an involution is

N![zN]C(z)N![zN]I(z)=12[zN2]I(z)+13[zN3]I(z)+14[zN4]I(z)[zN]I(z)=N(N1)2IN2IN+N(N1)(N2)3IN3IN+N(N1)(N2)(N3)4IN4INN24.\begin{align*} \frac{N! [z^N] C(z) }{N! [z^N] I(z) } &= \frac{ \frac{1}{2} [z^{N-2}] I(z) + \frac{1}{3} [z^{N-3}] I(z) + \frac{1}{4} [z^{N-4}] I(z) }{[z^N] I(z)} \\ &= \frac{N(N-1)}{2} \frac{I_{N-2}}{I_N} + \frac{N(N-1)(N-2)}{3} \frac{I_{N-3}}{I_N} + \frac{N(N-1)(N-2)(N-3)}{4} \frac{I_{N-4}}{I_N} \\ &\sim \frac{N^2}{4}. \end{align*}

The last line follows from the known ratio for the number of involutions

INk/INNk/2.I_{N-k} / I_N \sim N^{-k/2}.

Even though involutions are a highly restricted, symmetric subset of permutations (composed entirely of 1-cycles and 2-cycles), they are just as hard a nut to crack for insertion sort as unrestricted permutations!

Exercise 7.46 🌟

We proceed to prove by induction on kk that I(N,k)=N!pNkI(N,k)=N!\,p_{Nk} is a fixed polynomial in NN for any fixed kk, when N>kN > k. Definitely this covers the phrase "for sufficiently large NN."

The base case k=0k=0 is satisfied, since only the identity permutation has no inversions, so I(N,0)=1I(N,0)=1 for all NN. This is a fixed polynomial.

For the inductive step, assume that I(N,i)I(N,i) is a fixed polynomial in NN for all fixed i<ki<k, when N>iN > i. We use the standard "largest" construction for inversion numbers. Placing NN in a position that creates jj new inversions (0jN10\le j\le N-1) and adding the inversions of the smaller permutation gives (see also Exercise 7.44)

I(N,k)=j=0min(N1,k)I(N1,kj).I(N,k)=\sum_{j=0}^{\min(N-1,k)} I(N-1,k-j).

Observe that the sum cannot have more than kk summands. Let’s pull out the first term j=0j=0 and "move" it to the LHS

I(N,k)I(N1,k)=j=1kI(N1,kj).I(N, k) - I(N-1, k) = \sum_{j=1}^{k} I(N-1, k-j).

kk is fixed and by the inductive hypothesis all subordinate items are fixed polynomials on the RHS, hence the whole sum is also a fixed polynomial. By the standard properties of the finite difference operator (see the remark below), we can conclude that I(N,k)=N!pNkI(N,k)=N!p_{Nk} is a fixed polynomial in NN for any fixed kk when NN is large enough.

Any standard polynomial of degree dd can be rewritten as a linear combination of falling factorials up to degree dd (using Stirling numbers of the second kind). The "anti-difference" (which is just a finite sum) perfectly mimics continuous integration and gives back the original sequence:

xmδx=xm+1m+1+C.\sum x^{\underline{m}} \delta x = \frac{x^{\underline{m+1}}}{m+1} + C.

Exercise 7.47

Observe that the identity permutation, regard it as a sorted sequence of numbers from 1 to 2N2N, has 0 inversions. It’s lattice path is exactly that "down-right-down-right..." diagonal. Any departure from this ideal line, either going above or below, means that some larger numbers were skipped during the traversal. Thus, cells between the actual path and the main diagonal denote inversions.

Exercise 7.48 🌟

To prove this combinatorially, we just need to verify two things: that paths can be concatenated perfectly, and that the inversions add up cleanly without any "cross-contamination."

The Consequence of a Diagonal Touch

What does it actually mean when a lattice path touches the main diagonal at some point (k,k)(k, k)? If the path hits (k,k)(k, k), it means we have taken exactly kk Right steps and kk Down steps. Consequently, the first 2k2k values (the numbers 1,2,,2k1, 2, \dots, 2k) have perfectly filled exactly kk Odd indices and kk Even indices. Because we process values in strictly increasing order, the path after (k,k)(k,k) will purely consist of the values 2k+1,,2N2k+1, \dots, 2N.

The Additivity of Inversions

Can an element placed after the diagonal touch form an inversion with an element placed before the diagonal touch? The answer is no! Therefore, no "cross-contamination" may occur. All values placed after the touch are strictly larger than all values placed before the touch.

Therefore, if a path splits at the diagonal into Path AA and Path BB, the total number of inversions is strictly additive

inv(AB)=inv(A)+inv(B).\text{inv}(A \Join B) = \text{inv}(A) + \text{inv}(B).

Furthermore, because the values are perfectly partitioned into these blocks, there are no binomial choices to make about relabeling paths. This means we are strictly in the realm of OGFs. The paths concatenate directly like blocks. This additive nature of the problem permits clean decomposition into subproblems (reminiscent of the divide-and-conquer paradigm); recall the difficulties in analyzing tree height, where this wasn’t possible.

Proving the First Equation

Let P\mathcal{P} be the class of all 2-ordered permutation lattice paths. Let Q\mathcal{Q} be the analogous class for paths that never touch the diagonal except at the endpoints. Every single lattice path that returns to the diagonal can be uniquely decomposed into a sequence of these basic Q\mathcal{Q} paths, just by chopping it at every single diagonal touch. This gives

P=SEQ(Q)    P(z,u)=11Q(z,u).\mathcal{P} = \text{SEQ}(\mathcal{Q}) \implies P(z,u) = \frac{1}{1 - Q(z,u)}.

Proving the Second Equation

The exact same logic applies to the second equation, just restricted to a subset of the space. Thus,

S=SEQ(T)    S(z,u)=11T(z,u).\mathcal{S} = \text{SEQ}(\mathcal{T}) \implies S(z,u) = \frac{1}{1 - T(z,u)}.

Exercise 7.49 🌟

The two equations in this exercise are direct translations of the physical geometry of the lattice paths (see also the previous two exercises).

The Elevation Principle

We can build path T\mathcal{T} of length NN from path S\mathcal{S} of length N1N-1 by

T=RightSDown.\mathcal{T}=\text{Right} \Join \mathcal{S} \Join \text{Down}.

Notice that T\mathcal{T} only touches the diagonal at the endpoints. Geometrically, this envelope (Right...Down) shifts the entire inner path S\mathcal{S} up and right by one grid unit. The initial move adds 1 inversion together with the extra N1N-1 inversions due to moving away S\mathcal{S} from the main diagonal.

Recall the definition of the BGF: the exponent of uu designates the number of inversions, whilst zz reflects the size. To encode the increase +1 in inversions over the whole length of the current path S\mathcal{S}, we simply embellish the GF by passing uzuz instead of zz into S(z,u).S(z,u). In the same manner, the first Right move increases both the number of inversions and size by one, hence we just multiply the GF by uzuz. Therefore, T(z,u)=uzS(uz,u)T(z, u) = uz S(uz, u).

The Asymmetry Principle

A path Q\mathcal{Q} may be above or below the diagonal except at the endpoints. A path T\mathcal{T} is restricted to always stay above the diagonal except at the endpoints. Let path T\mathcal{T}^- represent the mirrored path T\mathcal{T} around the diagonal (stays always below it except at the endpoints), where all Right moves were replaced by Down moves, and vice versa. Clearly, Q=T+T\mathcal{Q}=\mathcal{T}+\mathcal{T^-}. Just by looking at Figure 7.10, we see that the number of inversions in T\mathcal{T} and T\mathcal{T}^- are generally different.

The path of the identity permutation starts with a Down move. Thus, the mirrored path T\mathcal{T}^- is more aligned with this reference path than T\mathcal{T}, since it also starts with a Down move. The initial difference of -1 is maintained throughout the whole length of T\mathcal{T}. Therefore, T\mathcal{T} has NN more inversions than T\mathcal{T}^-. This gives

TN(u)=uNTN(u)    T(z,u)=T(z/u,u)    T(uz,u)=T(z,u).T^-_N(u) = u^{-N} T_N(u) \implies T^-(z,u)=T(z/u,u) \implies T^-(uz,u)=T(z,u).

Therefore,

Q(z,u)=T(z,u)+T(zu,u)    Q(uz,u)=T(uz,u)+T(z,u).Q(z,u) = T(z,u) + T\left(\frac{z}{u}, u\right) \implies Q(uz, u) = T(uz, u) + T(z, u).

Exercise 7.50

S(z,u)=11T(z,u)=S(z,u)T(z,u)+1=uzS(z,u)S(uz,u)+1.\begin{align*} S(z, u) &= \frac{1}{1 – T(z, u)} \\ &= S(z,u)T(z,u)+1 \\ &= uzS(z,u)S(uz,u)+1. \end{align*}
P(z,u)=11Q(z,u)=Q(z,u)P(z,u)+1=(T(z,u)+T(z/u,u))P(z,u)+1=(uzS(uz,u)+zS(z,u))P(z,u)+1.\begin{align*} P(z, u) &= \frac{1}{1 – Q(z, u)} \\ &= Q(z,u)P(z,u)+1 \\ &= (T(z, u) + T(z/u, u))P(z,u) + 1 \\ &= (uzS(uz,u)+zS(z,u))P(z,u) + 1. \end{align*}

Exercise 7.51

Let W=14zW = \sqrt{1 - 4z}. We know our base functions are:

  • S(z,1)=1W2zS(z, 1) = \frac{1 - W}{2z}, since path S\mathcal{S} reflects the lattice path of a binary tree. This provides the identity 12zS(z,1)=W1 - 2zS(z, 1) = W.

  • P(z,1)=1WP(z, 1) = \frac{1}{W}.

To save space, let write SS for S(z,1)S(z,1) and SS_* for S(z,1)S_*(z,1).

S(z,u)=uzS(z,u)S(uz,u)+1Su(z,u)=zS(z,u)S(uz,u)+uzSu(z,u)S(uz,u)+uzS(z,u)[zSz(uz,u)+Su(uz,u)]Su=zS2+zSuS+zS(zSz+Su)(evaluate at u=1)=zS2+2zSSu+z2SSzSu(12zS)=zS2+z2SSz    SuW=zS2+z2SSz.\begin{align*} S(z, u) &= uz S(z, u)S(uz, u) + 1 \\ \hline S_u(z, u) &= z S(z,u)S(uz,u) + uz S_u(z,u)S(uz,u) + uz S(z,u) \left[ z S_z(uz,u) + S_u(uz,u) \right] \\ \hline S_u &= z S^2 + z S_u S + z S (z S_z + S_u) && \text{(evaluate at $u=1$)} \\ &= z S^2 + 2z S S_u + z^2 S S_z \\ &\therefore S_u (1 - 2z S) = z S^2 + z^2 S S_z \implies S_u W = z S^2 + z^2 S S_z. \end{align*}

We can also find Sz=S2WS_z = \frac{S^2}{W} by differentiating S(z,1)=zS(z,1)2+1S(z,1) = zS(z,1)^2 + 1 with respect to zz.

P(z,u)=(uzS(uz,u)+zS(z,u))P(z,u)+1Pu(z,u)=[zS(uz,u)+uz(zSz(uz,u)+Su(uz,u))+zSu(z,u)]P(z,u)+[uzS(uz,u)+zS(z,u)]Pu(z,u)Pu=[zS+z2Sz+2zSu]P+2zSPu(evaluate at u=1)=1W2[zS+z2Sz+2zSu].\begin{align*} P(z, u) &= (uz S(uz, u) + z S(z, u)) P(z, u) + 1 \\ \hline P_u(z, u) &= \left[ z S(uz, u) + u z (z S_z(uz, u) + S_u(uz, u)) + z S_u(z, u) \right] P(z,u) + \left[ u z S(uz, u) + z S(z, u) \right] P_u(z, u) \\ \hline P_u &= \left[ z S + z^2 S_z + 2z S_u \right] P + 2z S P_u && \text{(evaluate at $u=1$)} \\ &= \frac{1}{W^2} \left[ z S + z^2 S_z + 2z S_u \right]. \end{align*}

Now we just evaluate the three terms in that bracket using zS=1W2zS = \frac{1-W}{2} and our derivatives from step 1. The goal is to express everything on the RHS in terms of WW. It helps to remember that 1W2=4z1-W^2 = 4z. We’ve

zS=1W2z2Sz=z2S2W=(zS)2W=(1W2)2W=12W+W24W2zSu=2zzS2(1+W)2W2=(zS)2(1+W)W2=(1W2)2(1+W)W2=(1W)2(1+W)4W2=(1W)(1W2)4W2=(1W)(4z)4W2=z(1W)W2.\begin{align*} z S &= \frac{1-W}{2} \\ \hline z^2 S_z &= z^2 \frac{S^2}{W} \\ &= \frac{(zS)^2}{W} \\ &= \frac{(\frac{1-W}{2})^2}{W} \\ &= \frac{1 - 2W + W^2}{4W} \\ \hline 2z S_u &= 2z \frac{z S^2 (1+W)}{2 W^2} \\ &= \frac{(zS)^2 (1+W)}{W^2} \\ &= \frac{(\frac{1-W}{2})^2 (1+W)}{W^2} \\ &= \frac{(1-W)^2(1+W)}{4W^2} \\ &= \frac{(1-W)(1-W^2)}{4W^2} \\ &= \frac{(1-W)(4z)}{4W^2} \\ &= \frac{z(1-W)}{W^2}. \end{align*}

The biggest simplification happens in

zS+z2Sz=1W2+12W+(14z)4W=2W2W24W+22W4z4W=22W24z4W=4z4W=zW.\begin{align*} zS + z^2 S_z &= \frac{1-W}{2} + \frac{1 - 2W + (1-4z)}{4W} \\ &= \frac{2W - 2W^2}{4W} + \frac{2 - 2W - 4z}{4W} \\ &= \frac{2 - 2W^2 - 4z}{4W} \\ &= \frac{4z}{4W} = \frac{z}{W}. \end{align*}

Plugging everything back into the formula for PuP_u and further simplifying, we get

Pu(z,1)=zW4=z(14z)2.P_u(z,1) = \frac{z}{W^4}= \frac{z}{(1-4z)^2}.

Exercise 7.52

A 3-ordered permutation of total length 3N3N consists of exactly 3 perfectly sorted sublists, each of length NN, interleaved together. Because the individual sublists are already sorted, inversions can only occur between elements of two different sublists. There are 3 pairs of such sublists.

Let XijX_{ij} be a random variable for the number of inversions between 2 ordered sublists. We already know the expectation of this variable from Theorem 7.9. By the linearity of expectation, we have

E[X12+X13+X23]=E[X12]+E[X13]+E[X23]π48(3N)3/2.E[X_{12}+X_{13}+X_{23}]= E[X_{12}]+E[X_{13}]+E[X_{23}]\sim\sqrt{\frac{\pi}{48}} (3N)^{3/2}.

Wikipedia has a detailed coverage of shellsort including answers related to this exercise.

Exercise 7.53

Let CNC_N be the average number of comparisons to sort an array of size NN using this hybrid method. The divide-and-conquer recurrence is (see also Theorem 7.9 from the book)

CN2CN/2+π128N3/2.C_N \sim 2C_{N/2} + \sqrt{\frac{\pi}{128}} N^{3/2}.

According to Theorem 2.5 from the book, the performance is commensurate with the driving function c1π128N3/2\sim c_1\sqrt{\frac{\pi}{128}} N^{3/2} (see Exercise 2.68 for the value of c1c_1). We know that the quicksort implementation from Chapter 1 takes about 2NlnN\sim 2N \ln N comparisons. The objective is to find the threshold value below which the hybrid algorithm is faster. This gives

(2+2)πN128<2lnN    N550.(2+\sqrt 2)\sqrt{\frac{\pi N}{128}}< 2 \ln N \implies N \lesssim 550.

This is significantly larger than the one from Exercise 1.19. At any rate, the exact threshold isn’t that important. The key takeaway is that specialized sorting algorithms may play an important optimization role despite being asymptotically inferior compared to mainstream algorithms.

Exercise 7.54

The recurrence directly follows from Theorem 7.10 in the book and Exercise 3.71

pNk=N1Np(N1)k+1Np(N1)(k1),p_{Nk} = \frac{N-1}{N} p_{(N-1)k} + \frac{1}{N} p_{(N-1)(k-1)},

with the boundary conditions:

  • p00=1p_{00}=1,

  • pNk=0p_{Nk}=0 for k>Nk > N,

  • pN0=0p_{N0}=0 for N>0N>0.

Exercise 7.55

The counting GF is CN(u)=N!PN(u)=k[Nk]ukC_N(u)=N!P_N(u)=\sum_k \left[ N \atop k \right] u^k. Let’s employ the double counting technique (see Exercise 2.34). The first way is

CN(u)=dduk[Nk]uk=kk[Nk]uk1    CN(1)=kk[Nk].C_N'(u) = \frac{d}{du} \sum_k \left[ N \atop k \right] u^k = \sum_k k \left[ N \atop k \right] u^{k-1} \implies C_N'(1) = \sum_k k \left[ N \atop k \right].

By Theorem 7.10, we also know that

ln(CN(u))=ln(u)+ln(u+1)+ln(u+2)++ln(u+N1).\ln(C_N(u)) = \ln(u) + \ln(u+1) + \ln(u+2) + \dots + \ln(u+N-1).

Taking derivates of both sides gives

CN(u)CN(u)=1u+1u+1+1u+2++1u+N1.\frac{C_N'(u)}{C_N(u)} = \frac{1}{u} + \frac{1}{u+1} + \frac{1}{u+2} + \dots + \frac{1}{u+N-1}.

Recall that CN(1)=N!PN(1)=N!C_N(1)=N!P_N(1)=N! and the RHS evaluated at u=1u=1 is simply HNH_N. Equating both ways of computing CN(1)C'_N(1) gives

kk[Nk]=N!HN. \sum_k k \left[ N \atop k \right] =N!H_N.

Exercise 7.56

Assume for convenience that NN is even and that the array is a random permutation of NN elements. Let ii be the index of the smallest number and jj be the index of the second smallest number. Initialize these indices based on the relative order of a0a_0 and a1a_1. The algorithm proceeds as follows:

  1. The loop variable k=2..N1k=2..N-1 points to the current pair of elements. It increases in increments of 2.

    1. If ak<ak+1a_k < a_{k+1} then

      1. If ak<aia_k< a_i then

        1. If ak+1<aia_{k+1}<a_i then

          1. Set j=k+1j=k+1

        2. Else

          1. Set j=ij=i

        3. Set i=ki=k

      2. Else If ak<aja_k<a_j then

        1. Set j=kj=k

    2. Else perform the above checks just with kk and k+1k+1 swapped.

The above algorithm performs exactly 32N\frac{3}{2}N comparisons in every single case.

The index variable ii is set HNH_N times, the average number of left-to-right minima in a permutation. The index variable jj is always updated whenever ii is altered, plus anytime a candidate for the second smallest appears. In a random permutation, the probability that the kkth element is the minimum of the first kk elements is 1/k1/k. The book explains the reason. But the probability that it’s exactly the second smallest of the first kk elements is also 1/k1/k using the same argumentation. Therefore, the total number of updates to index variables is 3HN\sim 3H_N.

The presented solution and analysis is fully aligned with the theme of the book. Nonetheless, the asymptotically optimal solution is based on a tournament tree.

Exercise 7.57

Assume that an “exchange” costs twice as much as a “record access.” We need to find the threshold value for which selection sort is a better choice than insertion sort. The model is based on statements about these sorting methods from the book

N22+200N<N24+200N24    N5.\frac{N^2}{2}+200N <\frac{N^2}{4}+200\frac{N^2}{4} \implies N \ge 5.

Exercise 7.58

We need to find the threshold value for which selection sort is a better choice than quicksort. The model is based on statements about these sorting methods from the book

N22+200N<2NlnN+200N3lnN    22N433.\frac{N^2}{2}+200N <2N\ln N+200\frac{N}{3}\ln N \implies 22 \le N \le 433.

Exercise 7.59

The asymptotic behavior is identical to the standard array version given in Theorem 7.11 from the book. Without swaps, the remaining sequence is intrinsically uniformly random, although not independent from the starting permutation. Therefore, computing the average still works via linearity of expectation, but finding the variance requires advanced methods.

To understand where dependence comes from, take a look at all permutations of N=3N=3 elements and count the number of index updates for each of them (in both passes). The marginal probability that the second pass requires 1 update is 1/2. Nonetheless, if we know that we had 2 index updates in the first pass (these are associated with permutations (2, 1, 3), (3, 1, 2) and (2, 3, 1)) then Pr{Pass2=1Pass1=2}=2/31/2\Pr\{Pass_2 = 1 \mid Pass_1 = 2\} = 2/3 \neq 1/2.

The permutation pattern concept sheds more light onto why those leftover permutations may be regarded as uniformly random.

Exercise 7.60

Let VV be the total volume of input data. Since there are NN records, and each record has NN words, the total input data is V=N2V=N^2.

Notice that just reading the input requires Ω(V)\Omega(V) time. Interestingly, selection sort turns out to have an optimal performance, since its number of compares and total data movement cost are both O(V)O(V). Recall that only the first word is used as a key. All in all, it definitely has a total of Θ(V)=Θ(N2)\Theta(V)=\Theta(N^2) runtime.

Observe also that no other enlisted sorting method achieves this goal, as their data movement costs are higher than linear in terms of input size.

The moral of the story is that, depending on the context, even a generally "inferior" algorithm may become an undisputed winner. This is why machine and inputs models are crucial in evaluating algorithms.

Exercise 7.61

Similarly as in Theorem 7.13 from the book, we want to get

[ujzN]e(u1)zk/k1z=[ujzN]euzk/kezk/k1z.[u^jz^N] \frac{e^{(u-1)z^k/k}}{1-z}=[u^jz^N] \frac{e^{u z^k / k} e^{-z^k / k}}{1-z}.

Let’s first extract the component depending on uu, whilst treating the rest as "constant." This gives

[uj]euzk/k=(zk/k)jj!=zkjj!kj.[u^j] e^{u z^k / k} = \frac{(z^k / k)^j}{j!} = \frac{z^{kj}}{j! k^j}.

Substitute this back into our initial formula, and our extraction problem becomes

[zN](zkjj!kjezk/k1z)=1j!kj[zNkj]ezk/k1z.[z^N] \left( \frac{z^{kj}}{j! k^j} \frac{e^{-z^k/k}}{1-z} \right)=\frac{1}{j! k^j} [z^{N-kj}] \frac{e^{-z^k/k}}{1-z}.

The Taylor series for ezk/ke^{-z^k/k} is

m0(1)mzkmm!km.\sum_{m\ge0} \frac{(-1)^m z^{km}}{m! k^m}.

The division by 1z1-z means that we must take a partial sum of this expansion upto NkjN-kj. For a fixed kk and jj, as NN \to \infty this is equivalent to

limN[zNkj]ezk/k1z=m0(1/k)mm!=e1/k.\lim_{N \to \infty} [z^{N-kj}] \frac{e^{-z^k/k}}{1-z} = \sum_{m\ge0} \frac{(-1/k)^m}{m!}=e^{-1/k}.

If we plug this asymptotic limit back into the expression from the beginning, we get

Probability1j!kje1/k=eλλjj!.\text{Probability} \sim \frac{1}{j! k^j} e^{-1/k} = \frac{e^{-\lambda} \lambda^j}{j!}.

Setting λ=1\lambda=1 we get the result from Theorem 7.13 in the book.

Exercise 7.62

The question, translated into the language of cycles in a permutation of length 100, is "What is the probability that a random permutation of length 100 contains no cycles of length strictly greater than 50?"

If k>50k > 50, it is physically impossible to have more than one cycle of length kk when N=100N=100. Theorem 7.13 in the book already provides the average number of such cycles; here, it’s the same as the probability of having a cycle of that length. Since the events are mutually exclusive, the total probability of having any cycle larger than 50 is just the sum of the individual probabilities. Taking the complementary event, we get

P{max cycle50}=1k=511001k30%.P\{\text{max cycle} \le 50\} = 1 - \sum_{k=51}^{100} \frac{1}{k} \approx 30\%.

Exercise 7.63

Let b(p)b(p) be the number of executions of the instruction k=p[k], where pp denotes a random permutation of length NN.

Let BN(z)B_N(z) be the PGF for b(p)b(p) on permutations of size NN. Obviously, B0(z)=1B_0(z)=1. We can decompose a random permutation pp based on the position of its leader (following Knuth's paper cited in the book related to this exercise). The first cycle always starts at index 1, so one leader is predefined. It helps to look at Figure 7.1 (and Foata's correspondence) to recognize leaders of cycles. Let’s focus on this particular leader, to understand the decomposition.

Because 1 is guaranteed to be the minimum of its cycle, the inner loop will fully traverse its cycle of length kk. Consequently, the target instruction runs k1k-1 times. For all the other members of the same cycle, the loop will abort earlier, effectively simulating the algorithm on the remaining elements. They physically split into two independent permutations qq (the rest of the cycle) and rr (the elements outside the cycle): one of size k1k-1 and one of size NkN-k, respectively. Because the other leaders are equally likely to be anywhere, this yields the recursive relationship (see also Exercise 7.59)

b(p)=b(q)+b(r)+k1.b(p) = b(q) + b(r) + k-1.

Translating this into generating functions, we get the recurrence

BN(z)=1Nk=0N1zkBk(z)BN1k(z).B_N(z) = \frac{1}{N} \sum_{k=0}^{N-1} z^k B_k(z) B_{N-1-k}(z).

Multiplying both sides by NuN1N u^{N-1} and sum over all N1N \ge 1 gives

N1NBN(z)uN1=N1(k=0N1(zkBk(z))BN1k(z))uN1=N0(k=0N(zkBk(z))BNk(z))uN.\sum_{N \ge 1} N B_N(z) u^{N-1} = \sum_{N \ge 1} \left( \sum_{k=0}^{N-1} (z^k B_k(z)) B_{N-1-k}(z) \right) u^{N-1}= \sum_{N \ge 0} \left( \sum_{k=0}^{N} (z^k B_k(z)) B_{N-k}(z) \right) u^{N}.

This factors perfectly into our target functional equation

Bu(z,u)=B(z,zu)B(z,u).B_u(z, u) = B(z, zu)B(z, u).

Computing the mean and variance proceeds similarly to the steps laid out in Exercise 3.67 and will not be repeated here. The mean and variance are:

μN=(N+1)HNNσN2=2N2+4N(N+1)2HN(2)(N+1)HN.\mu_N = (N+1)H_N - N \\[0.3cm] \sigma_N^2 = 2N^2 + 4N - (N+1)^2 H_N^{(2)} - (N+1)H_N.

Exercise 7.64

The inner loop of Program 7.6 scans the array from left-to-right and bubbles up the current maximum in the corresponding subarray to its final position. This shifts the entries in the inversion sequence one position to the left and decreases their values by one.

If the next scan is done in the opposite direction, the current minimum is bubbled to its final position at the front. Because this displaces elements to the right, it shifts their entries in the inversion sequence one position to the right. However, because the sweeping element is smaller than the displaced elements, their inversion values stay the same (they do not increase), while the minimum element's inversion count drops to zero.

Wikipedia has more details about this bidirectional bubble sort.

Exercise 7.65

The formula for extracting coefficients is based on a similar approach as for the longest cycle calculation from the book (see also Theorem 7.2)

E[SN]=k=0N1Pr{SN>k}=k=0N1[zN]11zexp(i=1kzii),E[S_N] = \sum_{k=0}^{N-1} \Pr\{S_N > k\} = \sum_{k=0}^{N-1} [z^N] \frac{1}{1-z} \exp\left( - \sum_{i=1}^k \frac{z^i}{i} \right),

where SNS_N denotes a random variable for the length of the shortest cycle in a random permutation of NN elements. The Python script below "implements" this formula.

It outputs

If you multiply each entry by N!N!, then you get back the OEIS sequence A028417.

Last updated