The generating function for total displacement

In a 1977 paper, Diaconis and Graham studied what Knuth calls the total displacement of a permutation $w$, which is the sum of the distances $|w(i)-i|$. In recent work of the first author and Tenner, this statistic appears as twice the type $A_{n-1}$ version of a statistic for Coxeter groups called the depth of $w$. There are various enumerative results for this statistic in the work of Diaconis and Graham, codified as exercises in Knuth's textbook, and some other results in the work of Petersen and Tenner. However, no formula for the generating function of this statistic appears in the literature. Knuth comments that"the generating function for total displacement does not appear to have a simple form."In this paper, we translate the problem of computing the distribution of total displacement into a problem of counting weighted Motzkin paths. In this way, standard techniques allow us to express the generating function for total displacement as a continued fraction.


Introduction
Let S n denote the symmetric group of permutations of {1, 2, . . . , n}. Diaconis and Graham [1] studied what they called Spearman's disarray for permutations in S n . The disarray statistic, which Knuth calls total displacement [3, Problem 5.1.1.28], is defined for a permutation w as In work of the first author and Tenner [4], half of the total displacement is shown to be equal to the depth of a permutation, which is a measure of certain minimal factorizations of a permutation into transpositions. That is, define (1.1) dep(w) = min k s=1 (j s − i s ) : w = (i 1 j 1 ) · · · (i k j k ) .
The goal of this paper is to construct the generating function for the distribution of depth, or equivalently, total displacement. In Knuth's remarks on the problem, "The generating function for total displacement does not appear to have a simple form" [3, Solution for 5.1.1.28]. The generating function we produce is a continued fraction, so whether one disagrees with Knuth depends on whether one considers a continued fraction to be "simple. " We include here for reference Table 1, which contains the distribution of depths for n ≤ 8. This array can be found as entry A062869 of [5].  Table 1. The number of permutations w ∈ S n with depth dep(w) = k.

A map from permutations to Motzkin paths
We begin by translating the problem of counting permutations by depth to the problem of counting weighted Motzkin paths by area. More specifically, we will give a map from permutations to Motzkin paths which takes depth to area, and then weigh each Motzkin path by the number of permutations which map to it.
Recall that a Motzkin path of length n is a sequence of n letters from the set {U, D, H} such that the subword on the letters U and D forms a balanced parenthesization. We can draw Motzkin paths by identifying the letters U, D, and H with the segments • • , • • , and • • , respectively. Then, the balanced parenthesization condition means only that the path starts and ends at the same height, and never goes below this height. The area of a Motzkin path is the area of the region above the base line and below the path. For example, if p = UUHUDDHDH, we would draw the following picture and see that area(p) = 12: The map we are looking for is φ : S n → Motz n by φ(w) = p 1 · · · p n , where For example, if w = 3715246, we have φ(w) = UUDUDHD. We can see the correspondence more easily if we draw our permutations as follows. Write the numbers 1, . . . , n on a line, and draw an arrow from i to j if w(i) = j and i = j. Moreover, if i < j, draw the arrow above the numbers, and if i > j, draw the arrow below. Continuing with the same example of w = 3715246, we draw the following: In terms of the picture, we have p i = U if the element i has an incoming arrow from below, and an outgoing arrow above (both to its right); we have p i = D if the element i has an incoming arrow from above, and an outgoing arrow below (both to its left); and we have p i = H if i is a fixed point or if the arrows incident to it are both above or both below. Table 2 shows the correspondence for all permutations in S 4 , grouped by their associated Motzkin paths.

Area equals depth
Let us show that the map φ : S n → Motz n takes depth to area. An excedance of a permutation w is a position i such that w(i) > i. The characterization of depth in Equation 1.2 shows that depth is equal to the sum of the sizes w(i) − i of the excedances. If we draw the permutation w as in Section 2, an excedance is simply an arrow from a smaller number to a larger one, that is, an arrow drawn above the line of numbers. If i is an excedance, so that w(i) = j > i, this is drawn as: and the contribution of the excedance i to the depth of w is the length of the arrow from i to j. If j is itself an excedance, so that w(j) = k > j, the drawing looks like: and the total contribution to depth is (j −i) + (k −j) = k −i; that is, the distance from i to k is all that matters. In fact, we can compute the depth of w if we only know where each string of right arrows begins and ends. And if φ(w) = p 1 · · · p n is the Motzkin path associated to w, then i is the beginning of a string of right arrows exactly when w −1 (i) > i < w(i), i.e., p i = U, and k is the end of a string of right arrows exactly when w −1 (k) < k > w(k), i.e., p k = D. Thus, the depth can be characterized as follows.
It turns out that this number is equal to the area under the path p, as can be seen by rearranging the terms as and consulting the following diagram: Total area = 7 In general, for a Motzkin path p = p 1 · · · p n , the letters U and D in p form a balanced parenthesization, so they can be naturally paired up; furthermore, the pair of steps p i = U and p k = D contribute a horizontal strip of area k − i below the Motzkin path. Thus, we can characterize the area under p as Together with Proposition 3.1, this observation proves that the map φ takes depth to area. Proposition 3.2. For any w ∈ S n , dep(w) = area(φ(w)). Remark 3.3. In [4, Proposition 3.2], the first author and Tenner prove that for all w ∈ S n , the depth of w is at most ⌊n 2 /4⌋, and this bound is sharp. We can recover this result as a corollary of Proposition 3.2, since ⌊n 2 /4⌋ is the largest possible area for a Motzkin path with n steps, attained by the path p = U k D k if n = 2k is even, and by the path p = U k HD k if n = 2k + 1 is odd.

How many permutations map to a Motzkin path?
Having shown that the map φ : S n → Motz n encodes the depth of a permutation as the area of the associated Motzkin path, the next step in determining the distribution of the depth statistic is to compute the size of the preimage φ −1 (p) for each Motzkin path p.
Fortunately, this can be done easily, as demonstrated with the following example. As noted in Section 3, when drawing a permutation w such that φ(w) = p, each excedance of w is drawn as a right-pointing arrow above the line of numbers, and these arrows can be grouped into strings of right-pointing arrows, which start at a position i with p i = U and end at a position k with p k = D, possibly with intermediate steps at positions j with p j = H. So, to form the permutations that correspond to this path, we will first ignore the positions j with p j = H and match up all the half-arcs above the line of numbers, outgoing with incoming, to indicate the starting and ending positions of each string of right-pointing arrows. In this example, we are forced to form the pair 6 → 7, but we have two choices for pairing the outgoing half-arcs 1 → ·, 2 → · and the incoming half-arcs · → 4, · → 5; that is: Independently, we also have two choices for matching up the half-arcs below the line of numbers into (for now, single-step) strings of left-pointing arrows in the permutation diagram for w. Finally, to complete the diagram, we only need to decide what to do with the position 3, for which p 3 = H: it can either be a fixed point of w, or join one of the two strings of right-pointing arrows above it, or join one of the two strings of left-pointing arrows below it, for a total of five choices. (Note that for all matchings here, there are two strings of right-pointing arrows above position 3 and two strings of left-pointing arrows below it.) All in all, we have 2 · 2 · 5 = 20 possible diagrams (all valid) for a permutation w such that φ(w) = p = UUHDDUD.
In general, we can count the number of permutations corresponding to a given Motzkin path by reconstructing the possible permutation diagrams as in Example 4.1; first we count the number of ways of matching up the outgoing and incoming half-arcs above the line of numbers, then we count the number of ways of matching up the half-arcs below, and finally we count the number of ways of dealing with the positions corresponding to H steps.
To do this, we will need to define the height of each step in a Motzkin path. We draw our paths starting at (0, 0) and define the height h i of a step p i to be the maximum height achieved on that part of the path. That is, . For example, the steps of p = UUHDDUD have heights (h 1 , . . . , h 7 ) = (1, 2, 2, 2, 1, 1, 1).
Note that the height h i of a step in p is also the number of strings of right-pointing arrows which appear above position i in the diagram of any permutation w with φ(w) = p. Indeed, if we scan the diagram from left to right, the height increases by one for each U step, which marks the beginning of a string of right-pointing arrows, stays constant for each H step, and decreases by one for each D step, which marks the end of a string of right-pointing arrows. This observation lets us count the number of ways of matching up the half-arcs above the line of numbers; if we build the permutation diagram from left to right, then we have no choices to make for U and H steps, but for a step p i = D, we can freely choose any of the h i strings of right-pointing arrows above position i to terminate there. In other words, the number of ways to match up the half-arcs above the line of numbers is We can apply the same argument to the half-arcs below the line of numbers. If we construct a matching from right to left, the number of possibilities is simply Hence, we define the weight of step p i to be and the weight of a path p = p 1 · · · p n to be the product ω(p) = ω 1 · · · ω n .
We can recover this result as a corollary of Proposition 4.2 by noting that this is the weight of the Motzkin path with maximal area, namely p = U k D k if n = 2k is even, and p = U k HD k if n = 2k + 1 is odd.
Statements equivalent to [4,Proposition 3.2] and [4,Proposition 3.3] can be found in the paper of Diaconis and Graham, although without proof (see Table 1 and Remark 2 of [1]). They are also mentioned in the remarks (and links therein) for entry A062870 of [5].

Counting weighted Motzkin paths by area
Taking Propositions 3.2 and 4.2 into account, we can express the generating function for permutations with respect to depth as where |p| is the number of steps in the path p. Furthermore, if we decompose each Motzkin path into vertical strips (instead of horizontal strips as in Section 3) to compute its area, we can rewrite the whole term ω(p)t area(p) z |p| as a product over the steps of p. For example, if p = UUHDDUD, we would have the modified weights Following [2, Section 5.2], we can count Motzkin paths with these modified weights using continued fractions. For brevity, let a i , b i , and c i represent the weight of a U, H, or D step at height i, respectively. Any Motzkin path can be uniquely decomposed as a list of subpaths by cutting it at every (integer) point where it reaches height 0. The pieces in this decomposition will be of the form H or Up 1 D, where p 1 is a Motzkin path with base height 1 instead of 0. Hence, the generating function for weighted Motzkin paths is where F 1 is the generating function for weighted Motzkin paths with base height 1. We can apply the same reasoning to F 1 to express it in terms of the generating function F 2 for weighted Motzkin paths with base height 2: Continuing in this manner, we obtain the continued fraction If we substitute our actual modified weights for a i , b i and c i , we get Remark 5.1. If we set t = 1 in F (t, z), so that we are ignoring depth, we recover a continued fraction due to Euler (see [2, Section 5.2.11]) for the series In [2] this is obtained algebraically, whereas here we get a refined version F (t, z) of this series combinatorially (although see [2, Section 5.2.16] for a closely related continued fraction with a combinatoral interpretation).
Note that in the continued fraction (5.1), the denominators contain both a linear term and a quadratic term in z, so in the language of [2] it would be called a J-fraction. However, because of the special form of these terms in our case, we can rewrite it as a simpler S-fraction, which contains only linear terms in z in its denominators (see [2, Proposition 5.2.2]): Here, for k ≥ 0, the (2k)th term is (k + 1)t k z, and the (2k + 1)st term is (k + 1)t k+1 z.   Table 3. The formulas are polynomials of degree k that hold for n ≥ k. Each of these polynomials gives the coefficients of t k z n in F (t, z) for a fixed value of k, so they can be extracted from Equation 5.2 by computing modulo t k+1 , in which case the continued fraction eventually terminates. The result is a rational function of t and z, and setting t = 0 in a suitable derivative of this bivariate rational function gives a univariate rational function of z, say F k (z), whose power series expansion gives the desired coefficients.
It is somewhat surprising that the coefficient of z n in the power series expansion of F k (z) is polynomial in n; one would usually expect the coefficients to grow exponentially. This implies that the denominator of F k (z) is (1 − z) k , which is not immediately obvious to us.
However, this polynomiality can be seen directly from the combinatorics of Motzkin paths with fixed area. Indeed, for any fixed area k, there are finitely many Motzkin paths with area k that do not contain an H step at height 0, and we can get an arbitrary Motzkin path by inserting H steps at height 0. The number of ways of doing this is a binomial coefficient, which is polynomial in n.