Permutations generated by a depth 2 stack and an inﬁnite stack in series are algebraic

,


Introduction
Let p = p 1 p 2 . . .p n and q = q 1 q 2 . . .q k be permutations of length n k.We say p avoids q if there are no k indices i 1 < • • • < i k so that for all s, t, p is < p it if and only if q s < q t .
For example, 25413 avoids 123 since it has no increasing subsequence of length 3.
Interest in sets of permutations that avoid a small set of "patterns" arose naturally in the study of stack-sorting (or equivalently stack-generating) algorithms.Knuth showed that a permutation p can be generated by passing the ordered sequence 12 . . .|p| through an infinite stack if and only if p avoids 312, and that permutations of length n avoiding 312 are counted by the Catalan numbers [15].
If q is a list of permutations, let Av n (q) be the set of permutations of length n that avoid q for each q ∈ q.We call Av(q) = ∞ n=0 Av n (q) a pattern-avoidance class.A basis for a pattern avoidance class Av(q) is a set p of pairwise avoiding permutations so that Av(p) = Av(q).A class is finitely based if it is equal to Av(p) for p finite.
The list of pattern-avoidance classes for which a generating function for the sequence counting Av n (q) has been computed, or shown to be rational, algebraic or non-algebraic, is limited.Classes avoiding a single pattern of length 3 are enumerated by the Catalan numbers [15,17] and so have an algebraic generating function.For length four, Av({1342}) has an algebraic generating function [8], Av({1234}) has a generating function that is Dfinite but not algebraic [13], and a closed form generating function for Av({1324}) has not be found [2,9].It is known that for any pattern p of length four, Av({p}) is in bijection with one of these three classes.For single patterns of length greater than four, and classes avoiding two or more patterns, various isolated results are known [4,18].
In this article we consider the class of permutations generated by passing an ordered sequence through a stack of depth 2 and infinite stack is series, which was shown by the first author to have a finite basis consisting of 20 patterns [11].The more general case of two infinite stacks in series has not been enumerated.Pierrot and Rossin recently proved a polynomial time algorithm to decide if a permutation can be sorted by two stacks in series [16].A special case where both stacks are required to remain ordered was solved by Atkinson, Murphy and Ruškuc [7].The number of permutations sortable by 2 stacks in parallel was recently solved by Albert and Bousquet-Mélou [3].
Several authors have considered the language-theoretic complexity of pattern avoidance classes -see for example [1,5,6,10].Atkinson, Livesey, and Tulley [6] showed that the set of permutations generated by passing an ordered sequence through a finite token-passing network is in bijection with a regular language.Initially we applied this technique to the finite network consisting of a stack of depth 2 followed by a stack of depth k in series, constructing a sequence of languages and corresponding rational generating functions for small values of k.As k increased, the rational generating functions appeared to converge to the algebraic function given in Theorem 11 below.However, this method does not constitute a proof.To prove the result we instead follow another path -we establish a bijection between permutations generated and an unambiguous context-free language.The generating function is then guaranteed to be algebraic by a well known theorem of Chomsky and Schützenberger.
The main work in this article is to establish the bijection with the context-free language.It has been suggested that the method employed to transform the relatively simple pushdown-automaton description of the language to the quartic generating function should be much easier than the method we detail here.We would welcome any insights into this -in our approach we merely apply the standard theory, and give the details for an interested reader.

Establishing a bijection
Let P be the set of permutations that can be generated by a stack of depth 2 and infinite stack in series, and fix ρ, λ, µ as the stack moves indicated in Figure 1.
Moreover, a word of length 3n in L 2,∞ encodes a permutation of length n.
Proof.The first claim is clear from the definition.If w ∈ L 2,∞ has n ρ letters, then D ρ,λ (w) = 0 implies w has n λ letters, and D λ,µ (w) = 0 then implies w has n µ letters, so the length of w is 3n, and the number of tokens moves through the stacks is n.
the electronic journal of combinatorics 22(1) (2015), #P2.16 The language L 2,∞ consists of all possible ways to pass tokens through the system of stacks as in Figure 1.We wish to find a sublanguage that is in bijection with P. From the set of all words in L 2,∞ that generate the same permutation, we will try to choose the string that outputs tokens as soon as possible, that is, has more µ letters closer to the front.The next definition will help to formalise this.Definition 4 (µ-ordering).Define an ordering, ≺ µ , on words in {ρ, λ, µ} * as follows.Let θ : {ρ, λ, µ} * → {ν, µ} * be a monoid homomorphism defined by θ(µ) = µ and θ(ρ) = θ(λ) = ν.If u = v as strings then u ≺ µ v if |u| = |v| and θ(u) precedes θ(v) in lexographic ordering on {µ, ν} * where µ < ν.
For example, if u = ρλµρλµ and v = ρλρµλµ then u ≺ µ v.Note that both words generate the permutation 12, and u is obtained from v by replacing the subword ρµ by µρ, which has no affect on the permutation being produced.More generally we have the following.
Proof.In each case it is clear that w ≺ µ w.We must show that in each case the two strings generate the same permutation.For case (1) this is clear since ρ and µ do not interact.
For case (2), since D ρ,λ (w 0 ) = 1, there must be one token (say a) left in the first stack after reading w 0 , and since the next letter to be read is ρ, there must be one token (say b) ready to enter the first stack.See Figure 2.
After reading ρλ, b moves to the top of stack B and a stays in stack A. Reading w 1 leaves a and b in place and outputs some permutation of input tokens.Finally λµ outputs a, leaving b on the top of stack B and stack A empty.
Starting from the initial configuration in Figure 2, the prefix w 0 λρw 1 µλ of w moves a to the top of stack B and places b in stack A. The permutation generated by w 1 is then passed across as before, then a is output, and finally b is moved to stack B, leaving the stacks in the same configuration as the prefix w 0 ρλw 1 λµ of w.
A similar argument applies for Case (3) and is left to the reader.
Definition 6 (L).The language L is the set of words w ∈ L 2,∞ that do not 1.contain a subword ρµ,   Proof.Suppose for contradiction that w ∈ L, w = w 0 vw 1 λw 2 µw 3 with v ∈ {ρλ, λρ}, D ρ,λ (w 0 ) = 1, w 1 ∈ L 1,∞ , w 2 generates a permutation that avoids 312, and moreover that w 0 is the longest prefix of w with this property.That is, if w = u 0 vu 1 λu 2 µu 3 with v ∈ {ρλ, λρ}, D ρ,λ (u 0 ) = 1, u 1 ∈ L 1,∞ and u 2 generates a permutation that avoids 312, then |u 0 | |w 0 |.Since D ρ,λ (w 0 vw 1 ) = 1 and λ moves a token from stack A to stack B, after reading w 0 vw 1 λ we have no tokens in stack A, and some token, say a, in stack B. See Figure 3. Since w ∈ L, w 2 cannot be empty, and since w 2 is a subword of w ∈ L we have the electronic journal of combinatorics 22(1) (2015), #P2.16 w 2 ∈ L. So w 2 moves some sequence of tokens completely through the stacks, leaving a in place.The first letter of w 2 must be ρ, which moves some token, say b, onto stack A. Let ρ b , λ b , µ b be the letters in w 2 that correspond to moving b through the stacks.Then w 2 has prefix ρ b sλ b tµ b where s, t are subwords.
Since stack A contains b while s is read, if ρ occurs in s it must be immediately followed by λ, so D ρ,λ (u) ∈ [0, 1] for all prefixes u of s, and D ρ,λ (s) = 0. Further, if D λ,µ (u) < 0 for any prefix u of s, then a would be output.Either D λ,µ (s) = 0 (and s ∈ L) or D λ,µ (s) > 0.
If s ∈ L 1,∞ then t ∈ L 2,∞ and generates a permutation avoiding 312 since it is a subword of w 2 .In this case w has prefix w = w 0 vw 1 λρ b sλ b tµ b with D λ,µ (w 0 vw 1 ) = 1 and t generating a permutation avoiding 312, which contradicts the choice of w 0 as the longest such prefix.
Therefore we must have D λ,µ (s) > 0. In this case, after reading s at least one token, say c, remains on top of a in stack B when b is moved into it.After reading λ b , the stack configuration is as in the third diagram shown in Figure 3.
Note that a < b < c since they are input in this order.Let ε denote the empty word.If t = ε then it must contain at least one µ (it cannot leave a token covering b, and cannot just be ρ or ρρ) so it moves a token d > c to the output.This means w 2 generates the subpermutation dbc which is order equivalent to 312, contradicting our assumption.Thus t = ε and w 2 has prefix ρ b sλ b µ b , with s ∈ {ρλ, µ} * .Either s ends with ρλ, or s = uρλs where D λ,µ (u) = D λ,µ (s) since D λ,µ starts at zero and increases to this value.Thus s ∈ L, and Theorem 8.There is a bijection between permutations in P of length n and words in L of length 3n.
Proof.Consider the map that sends a word of length 3n in L ⊆ L 2,∞ to the permutation of length n it generates.If σ ∈ P then there is some word w ∈ L 2,∞ that generates it by Lemma 3. If w / ∈ L, then w must either contain ρµ, or have prefix w 0 ρλw 1 λµ or w 0 λρw 1 λµ with D ρ,λ (w 0 ) = 1 and w 1 ∈ L 1,∞ .We rewrite w as follows.
While w contains ρµ or has prefix w 0 ρλw 1 λµ or w 0 λρw 1 λµ: 1. Replace ρµ with µρ 2. Replace w 0 ρλw 1 λµ with w 0 λρw 1 µλ 3. Replace w 0 λρw 1 λµ with w 0 ρλw 1 µλ Each iteration replaces the current word by a word which generates the same permutation and is shorter in the µ-ordering by Lemma 5, so the procedure must terminate (there are finitely many words less than w in the µ-ordering).It follows that the map is surjective.We complete the proof by showing it is injective.Suppose we have two words u, v ∈ L that generate the same permutation, and that u = v as strings.Write First consider the case that one of u k , v k is µ.Without loss of generality assume u = zµu k+1 . . .u n .Then z must leave some token, say a, at the top of stack B, and u k = µ outputs this token.
If v k = λ, then a will be covered and v will not be able to generate the same permutation.So we must have then a is covered, and v k+2 = ρ since stack A contains two tokens.So we have a contradiction, and it follows that neither u k , v k can be µ.
Without loss of generality assume u k = ρ and v k = λ.Then z must leave at least one token in stack A to be followed by λ, and at most one token in stack A to be followed by ρ.Let a be the token in A, and b the token moved from the input by u k = ρ.See Figure 4.Note that we have D ρ,λ (z) = 1.
In u, zρ must be followed by λ since stack A is full after the ρ and ρ cannot be followed by a µ.So u has prefix zρλ and we have the configuration shown in the second diagram in Figure 4.
In v, zλ can be followed by either µ or ρ but not λ since stack A is empty after v k = λ.Suppose v k+1 = µ.Then after reading zλµ we have the configuration shown in the third diagram in Figure 4. Since u and v are assumed to produce the same permutation, the next µ letter appearing in u after the prefix zρλ must move a to the output.Let λ a , µ a be the letters in u that move the token a. Then u has prefix zρλκ 1 λ a κ 2 µ a where κ 1 , κ 2 ∈ {ρ, λ} * .The subword κ 2 cannot move tokens to cover a in stack B, so cannot contain any λ letters, and cannot contain any ρ letters since it is followed by µ, so it must be empty.The subword κ 1 must be of the form (ρλ) i for i 0, since it cannot move a.So u has prefix z(ρλ) i ρλλ a µ a with D ρ,λ (z(ρλ) i ) = 1, so u ∈ L.
It follows that v k+1 = ρ, so we have The two configurations of the stacks after reading the length k + 1 prefixes of u and v respectively are shown in Figure 5.
We now consider two possibilities: either a precedes b in the permutation generated by u and v, or b precedes a.

Case 1: a precedes b
Mark the letters λ, µ in u and v that correspond to moving the token a by appending the subscript a.So u has prefix zρλw 1 λ a w 2 µ a and v has prefix zλ a ρwµ a where w, w 1 , w 2 ∈ {ρ, λ, µ} * .
First consider the word v. Since w cannot end with ρ, and b must remain in stack A until a is output, and w cannot leave any tokens covering a in stack B, and we have the electronic journal of combinatorics 22(1) (2015), #P2.16 After z: w ∈ L 1,∞ .If w is empty then v contains ρµ a which means v ∈ L. Thus w is nonempty, so moves some tokens, say t 1 , . . ., t s , from the input to the output.
Since u generates the same permutation as v, it must also move the tokens t 1 , . . ., t s through the stacks and output them before a is output.The subword w 1 cannot leave any tokens covering a in stack A, so w 1 ∈ {ρλ, µ} * .If w 1 leaves some token t i in stack B, then λ a will cover it by a, which means t i will appear after a in the output, so u does not generate the same permutation as v. Thus after w 1 the stacks are the same as before w 1 , so w 1 ∈ L 1,∞ .Let t 1 , . . .t r with r s be the tokens moved to the output by w situtation after reading w 1 is shown in Figure 6.If w 2 is empty then u has prefix zρλw 1 λ a µ a with w 1 ∈ L 1,∞ which is forbidden, so w 2 must move some tokens.Also w 2 cannot leave any tokens in stack B. Either w 2 leaves some tokens in stack A, or not.
If w 2 leaves a token in stack A, this token cannot be one of t r+1 , . . ., t s or else u would generate a different permutation to v. Therefore this token is moved into stack A after t r by a letter ρ.This letter cannot be followed by µ, and since it remains in stack A it is not followed by λ.So this letter is either the last letter of w 2 , or is followed by another ρ, which must also remain in stack A. Thus w 2 ends with ρ, but this is a contradiction since w 2 is followed by µ a .
Thus w 2 does not leave any tokens in stacks A or B, so moves t r+1 , . . ., t s from the input to the output, so w 2 ∈ L 2,∞ .Note that w 1 w 2 produces the same permutation of t 1 , . . ., t s as w does, and w ∈ L 1,∞ so generates a 312-avoiding permutation of t 1 , . . ., t s .The subword w 1 permutes the first r tokens, and so w 2 must produce a permutation of t r+1 , . . ., t s that avoids 312.In this case u has prefix zρλw 1 λ a w 2 µ a where D ρ,λ (zρλ) = 1, the electronic journal of combinatorics 22(1) (2015), #P2.16 w 1 ∈ L 1,∞ and w 2 generates a 312-avoider, so by Lemma 7 u must also contain a prefix that is not allowed if u ∈ L. This is a contradiction, so this case does not apply.Thus w is nonempty, so moves some tokens, say t 1 , . . ., t s , from the input to the output.Since v generates the same permutation as u, it must also move the tokens t 1 , . . ., t s through the stacks and output them before b is output.The subword w 1 cannot leave any tokens covering b in stack A, so w 1 ∈ {ρλ, µ} * .
If w 1 leaves some token t i in stack B, then λ b will cover t i with b so t i will appear after b in the permutation generated by v, which is different to the permutation generated by u.Thus after w 1 the stacks are the same as before w 1 , so w 1 ∈ L 1,∞ .Let t 1 , . . .t r with r s be the tokens moved to the output by w 1 .The situation is shown in Figure 7.If w 2 is empty then v has prefix zλρw 1 λ b µ b with w 1 ∈ L 1,∞ which is forbidden, so w 2 must move some tokens.The subword w 2 cannot leave any tokens in stack B. Either w 2 leaves some tokens in stack A, or not.
If w 2 leaves a token in stack A, this token cannot be one of t r+1 , . . ., t s or else v would generate a different permutation to u. Therefore this token is moved into stack A after t r by a letter ρ.This letter cannot be followed by µ, and since it remains in stack A it is not followed by λ.So this letter is either the last letter of w 2 , or is followed by another ρ, which must also remain in stack A. Thus w 2 ends with ρ, but this is a contradiction since w 2 is followed by µ b .
Thus w 2 does not leave any tokens in stacks A or B, so moves t r+1 , . . ., t s from the input to the output, and w 2 ∈ L 2,∞ .Note that w 1 w 2 produces the same permutation of t 1 , . . ., t s as w does, and w ∈ L 1,∞ so generates a 312-avoiding permutation of t 1 , . . ., t s .The subword w 1 permutes the first r tokens, and so w 2 must produce a permutation of t r+1 , . . ., t s that avoids 312.In this case v has prefix zλρ b w 1 λ b w 2 µ b where D ρ,λ (zλρ b ) = 1, w 1 ∈ L 1,∞ and w 2 generates a 312-avoider, so by Lemma 7 v must also contain a prefix that is not allowed if v ∈ L. This is a contradiction, so we cannot have two such words u and v.

A related class of permutations
A natural question to ask is whether switching the order of the stacks makes any difference to the problem.Let Q be the set of permutations that can be generated by passing an ordered sequence through an infinite stack followed by a depth 2 stack in series.Each word w ∈ L 2,∞ encodes a permutation in Q as follows: reading w from right to left, for each µ move a token from the input to the infinite stack, for each λ move a token from the infinite stack to the depth 2 stack, and for each ρ move a token from the depth 2 stack to the output.It follows that P and Q are in bijection.

Constructing a pushdown automaton
In this section we construct a deterministic pushdown automaton accepting on empty stack, which accepts the language A pushdown automaton accepting on empty stack M is the following: 1. Q a finite set of states, 2. Σ a finite input alphabet, 3. Γ a finite stack alphabet, 4. q 0 ∈ Q the start state, which runs as follows.Before reading input, the stack contains a single 0. Input strings are accepted as soon as the stack becomes empty.A configuration of M is a pair (q, ω) where q is the current state and ω ∈ Γ * is a string of stack symbols representing the contents of the stack (the first letter of ω is the top of the stack).The notation δ(q i , a, k) = {(q j 1 , γ 1 ), . . ., (q js , γ s )} means that if M has the configuration (q i , kω) and a ∈ Σ ∪ {ε} is the next input letter to be read, then M can move to the configuration (q j l , γ l ω) for some 1 l s, removing the token k from the top of the stack and replacing it by γ l .See [14] for more details.
A pushdown automaton is deterministic if for each state q and stack symbol i for each a ∈ Σ ∪ {ε} the set δ(q, a, i) has size at most one.
Note that a determistic pushdown automaton accepting on empty stack cannot accept the empty string (unless this is the only string it accepts) since there would have to be a transition δ(q 0 , ε, 0) as well as a transition δ(q 0 , a, 0) for some letter a.
Let M be the pushdown automaton shown in Figure 8, which accepts on empty stack.M uses its stack to keep track of D λ,µ as it reads its input, and its states to keep track of D ρ,λ .It uses the stack symbol 2 as a device to flag when the input has the potential to have a prefix of the form w 0 ρλ or w 0 λρ with D ρ,λ (w 0 ) = 1.Paths ρµ are forbidden.We will prove that the language of this automaton is precisely the language L$.
Here is the formal description of M .Note that states q 3 , q 6 , q 7 are reached only when 1 is on top of the stack, and q 5 , q 8 are reached when either 1 or 2 are on top of the stack, so we have omitted transitions from configurations that are not possible.
To prove that M accepts precisely the language L$, we first show that M is deterministic.This allows us to identify input words with unique paths in M and simplify our arguments slightly.Proof.Since M is deterministic, we identify input words with their corresponding unique path in M .
2. if w fails to be in L 2,∞ , then w$ is rejected, 3. if w has a bad prefix (conditions ( 2) and (3) in Definition 6), then w$ rejected.
4. if w$ is rejected, then w ∈ L.
The only states that can be reached by a path uρ for u ∈ {ρ, λ, µ} * from the start configuration are q 1 , q 2 , q 6 and q 8 and since none are the source of a µ transition, any word containing ρµ will be rejected.
Next, we show that if w is not in L 2,∞ , then w$ is rejected by M .Each state represents the endpoint of a path labeling a prefix of an input string accepted by the automaton.One can verify the values of D ρ,λ (u) for each path labeled u ending at state q i given by Table 1.state q 0 q 1 q 2 q 3 q 4 q 5 q 6 q 7 q 8 D ρ,λ 0 1 2 0 1 1 1 0 2 Table 1: Value of D ρ,λ for any prefix ending at each state Let h(u) be the height of the second (infinite) stack after reading u ∈ {ρ, λ, µ} * starting from the start configuration (q 0 , 0).Then h(ε) = 1, h(uρ) = h(u), h(uλ) = h(u) + 1 and h(uµ) = h(u) − 1 since λ pushes a token to the stack, µ pops a token and ρ keeps the stack unchanged.It follows that h(u) = D λ,µ (u) + 1, and since 0 stays on the stack until $ is read, h(u) 1 for all prefixes u ∈ {ρ, λ, µ} * , so D λ,µ (u) 0. If w$ is accepted then the stack must contain only 0 after reading w, so D λ,µ (w) = 0.
Next, suppose w ∈ L 2,∞ has no ρµ substring and a prefix of the form w 0 vw 1 λµ where D ρ,λ (w 0 ) = 1, v ∈ {ρλ, λρ} and w 1 ∈ L 1,∞ .The string w 0 labels a path in the automaton starting at q 0 and ending at state q 1 , q 4 , q 5 or q 6 by Table 1.From each of these states, reading v = ρλ ends in state q 5 , and reading v = λρ ends in state q 6 .From q 5 , the word w 1 labels a path that visits only states q 5 and q 8 , since D λ,µ (z) 0 for all prefixes z of w 1 , so the 1 on top of the stack before reading w 1 remains (and is covered by 2s, which are removed by the µ loop at q 5 ), and ends at q 5 since D λ,µ (w 1 ) = 0. From here reading λµ is rejected.
From q 6 , if w 1 = ε then uλρλµ is rejected.Otherwise w 1 labels a path from q 6 to q 8 and then moves between q 5 and q 8 , and ends at q 5 .From here reading λµ is rejected.
We have now established that if w ∈ L then w$ is rejected by M .To complete the proof we must show that if w$ is rejected, then w ∈ L. To show this, assume w ∈ L 2,∞ with no ρµ substring, but w$ is rejected by M .We will prove that w must have a bad prefix.
Let p be the longest prefix of w$ labeling a path that is not rejected by M .Since w ∈ L 2,∞ we have D λ,µ (w) = 0, so if w = p, after reading w the stack contains just 0 so w$ will be accepted, a contradiction.Thus p is strictly shorter than w.Let w = pxw where x ∈ {ρλ, µ} is the next letter input after reading p.
We now consider the possible states where p can end.
Otherwise M cannot reject on reading λ.
5. Suppose p ends at q 4 , so D ρ,λ (p) = 1.The only way M could reject is if the top of stack is 0 and x = µ, which is not possible since w ∈ L 2,∞ .
6. Suppose p ends at q 5 , so D ρ,λ (p) = 1 and 1 is on top of the stack.Then no letter will cause M to reject.
7. Suppose p ends at q 6 , so D ρ,λ (p) = 1 and p ends with λρ.Then x cannot be µ, and otherwise px is not rejected.
8. Suppose p ends at q 8 , so its last letter is ρ, and D ρ,λ (p) = 2. Then x = µ, ρ and M cannot reject if x = λ.
These cases show that if p ends at any state except q 7 , then M does not reject w on reading the next input letter.We finish the proof by showing that if p ends at q 7 , then px is a bad prefix.
Let p = p 1 λ.If p 1 ends at q 6 , then p 1 = p 2 λρ, and px = p 2 λρλµ where D ρ,λ (p 2 ) = 1 and so px is a bad prefix.The machine correctly rejects the string on reading x = µ.
Otherwise p 1 ends at q 5 .Either p 1 ends with ρλ, or µ.If p 1 = p 2 ρλ then D ρ,λ (p 2 ) = 1 and px = p 2 ρλλµ is a bad prefix.Otherwise p 1 ends in µ, and must pop a token 2 from the stack.Let λ * be the last λ letter in p 1 that pushed a 1 on top of the stack (which must exist, since all paths to q 5 must cross such an edge).Write p 1 = p 2 λ * p 3 µ.
The letter λ * labels one of the following edges: 1. from q 2 to q 5 , 2. from q 1 to q 3 , 3. from q 4 to q 3 , 4. from q 5 to q 7 , 5. from q 6 to q 7 .In the first case, p 2 ends at q 2 so must have the form p 2 = uρ with D ρ,λ (u) = 1.Then p 3 µ labels a path that moves between states q 5 and q 8 , reading ρλ and pushing a 2, or reading µ and popping a 2, so p 3 µ ∈ L 1,∞ .It follows that w has the bad prefix uρλ * (p 3 µ)λµ, and so M correctly rejects it.
In the other four cases we have that D ρ,λ (p 2 ) = 1 since p 2 ends at state q 1 , q 4 , q 5 or q 6 , λ * must be immediately followed by a letter ρ, and p 2 λ * ρ ends at state q 6 .Let p 3 = ρp 4 .Then p 4 µ labels a path that starts at q 6 , goes to q 8 , then moves between states q 5 and q 8 , reading ρλ and pushing a 2, or reading µ and popping a 2. So p 4 µ ∈ L 1,∞ .It follows that w has the bad prefix p 2 λ * ρ(p 4 µ)λµ, and so M correctly rejects it.
4 Obtaining the generating function Theorem 11.The sequence counting the number of permutations of each length in P has an algebraic generating function: where c n is the number of permutations in P of length n, and q ≡ q(z) = 1−2z− √ 1−4z 2z .Proof.We convert the pushdown automaton given in the previous section to an unambiguous context-free language, following the standard procedure as described in Hopcroft and Ullman [14].Theorem 10.12 of Hopcroft and Ullman guarantees that the grammar obtained from a deterministic pushdown automaton accepting on empty stack is LR(0) and hence unambiguous.
We then apply the Chomsky and Schützenberger theorem, as outlined for example in [12] I.5.4, to obtain an algebraic generating function.Since each step in this procedure is constructive, we can find the generating function explicitly.
We start by converting the pushdown automaton to a grammar.See Theorem 5.4 [14] for full details.
Define a grammar with nonterminals S and [q i , j, q k ] = N i,j,k for each pair of states q i , q k and stack symbol j.The nonterminal [q i , j, q k ] represents a path in the configuration space of the pushdown automaton starting at q i with j on top of the stack and ending at some state q k .The productions "fill out" these paths with subpaths according to the transitions that are possible.
The production rules are then defined as follows: N  Using Maple (version 14) we can solve to obtain an expression for the algebraic generating function f 000 (z), which counts the number of words in L$ of each length.Since words in L$ of length 3n + 1 are in bijection with permutations in P of length n, the generating function n 0 c n t n where c n is the number of permutations of length n in P is obtained by dividing f 000 by z and substituting z 3 = t.

Figure 3 :
Figure 3: Stack configurations in the proof of Lemma 7.

Case 2 :
b precedes a We return to the situation shown in Figure 5 with u = zρλu k+2 . . .u n and v = zλρv k+2 . . .v n .Mark the letters ρ, λ, µ in u and v that correspond to moving the token b by appending a subscript b.Then u has prefix zρ b λ b wµ b and v has prefix zλρ b w 1 λ b w 2 µ b where w, w 1 , w 2 ∈ {ρ, λ, µ} * .First consider the word u.Since w is followed immediately by µ a , w cannot end with a ρ, and since a must remain in stack A until b is output and w cannot leave any tokens covering b in stack B, we have w ∈ L 1,∞ .If w is empty then u has prefix zρ b λ b µ b .For v to produce the same permutation the next token it must output from the configuration shown in Figure 5 is b, so w 1 ∈ {ρλ} * and w 2 ∈ {ρ} * .Since w 2 is followed by µ b then w 2 must be empty.Then v has prefix zλρ b (ρλ) i λ b µ b for some i 0. If i = 0 then v has prefix zλρ b λ b µ b with D ρ,λ (z) = 1 which violates the third condition of Definition 6, and if i > 0 then v has prefix (zλρ b (ρλ) i−1 )ρλλ b µ b with D ρ,λ (zλρ b (ρλ) i−1 ) = 1 which violates the second condition of Definition 6, so v ∈ L.

Figure 7 :
Figure 7: Stack configuration after zλρ b w 1 λ b in Case 2 in Theorem 8.