6
Information Processing Letters 109 (2009) 799–804 Contents lists available at ScienceDirect Information Processing Letters www.elsevier.com/locate/ipl More restrictive Gray codes for some classes of pattern avoiding permutations Jean-Luc Baril LE2I UMR-CNRS 5158, Université de Bourgogne, B.P. 47 870, 21078 Dijon Cedex, France article info abstract Article history: Received 8 July 2008 Received in revised form 26 January 2009 Accepted 22 March 2009 Available online 27 March 2009 Communicated by D. Pointcheval Keywords: Algorithms Gray code Pattern avoiding permutation Generating algorithm Catalan numbers Schröder numbers Central binomial coefficients In a recent article [W.M.B. Dukes, M.F. Flanagan, T. Mansour, V. Vajnovszki, Combinatorial Gray codes for classes of pattern avoiding permutations, Theoret. Comput. Sci. 396 (2008) 35–49], Dukes, Flanagan, Mansour and Vajnovszki present Gray codes for several families of pattern avoiding permutations. In their Gray codes two consecutive objects differ in at most four or five positions, which is not optimal. In this paper, we present a unified construction in order to refine their results (or to find other Gray codes). In particular, we obtain more restrictive Gray codes for the two Wilf classes of Catalan permutations of length n; two consecutive objects differ in at most two or three positions which is optimal for n odd. Other refinements have been found for permutation sets enumerated by the numbers of Schröder, Pell, even index Fibonacci numbers and the central binomial coefficients. A general efficient generating algorithm is also given. © 2009 Elsevier B.V. All rights reserved. 1. Introduction Let S n be the set of all permutations of length n (n 1). We represent permutations in one-line notation, i.e. if i 1 , i 2 ,..., i n are n distinct values in [n]={1, 2,..., n}, we denote the permutation σ S n by the sequence i 1 i 2 ... i n if σ (k) = i k for 1 k n. For instance, the identity per- mutation of length n, id n , will be written 12 ...(n 1)n. Moreover, if γ = γ (1)γ (2)... γ (n) is an n-length per- mutation then the composition (or product) γ · σ is the permutation γ (σ (1))γ (σ (2))... γ (σ (n)). In S n ,a k-cycle σ =i 1 , i 2 ,..., i k is an n-length permutation satisfy- ing σ (i 1 ) = i 2 , σ (i 2 ) = i 3 ,..., σ (i k1 ) = i k , σ (i k ) = i 1 and σ ( j ) = j for j ∈[n]\{i 1 ,..., i k }; in particular, a transpo- sition is a 2-cycle. A permutation σ = σ 1 σ 2 ... σ n S n contains the pattern γ S k (k 2) if and only if a se- quence of indices 1 i 1 < i 2 < ··· < i k n exists such that σ i 1 σ i 2 ... σ i k is ordered as γ . We denote by S n (γ ) the set of permutations in S n avoiding the pattern γ . For example, 25314 / S 5 (123) but 43152 S 5 (123). E-mail address: [email protected]. Several algorithms for generating permutation classes have been published (generated in lexicographic order or in constant time per permutation, in the amortized sense). For example, efficient algorithms are known for derangements [1], up-down permutations [7,19], permu- tations with a fixed number of inversions [12] and for permutations avoiding a pattern [9]. On the other hand, al- gorithms for generating permutation classes in Gray code order are given for permutations [14,22] and their restric- tions [17,19,20], derangements [3,16], with a fixed number of cycles [2], involutions and fixed-point free involutions [25] or their generalizations (multiset permutations [24]). Juarna and Vajnovszki [15] presented Gray codes and gen- erating algorithms for two classes of pattern avoiding per- mutations: S n (123, 132), S n (123, 132, p( p 1)... 1( p + 1)). In a recent paper, Dukes et al. [11] give Gray codes and generating algorithms for classes of pattern avoiding per- mutations enumerated by Catalan, Schröder, Pell, even in- dex Fibonacci numbers and the central binomial coeffi- cients: two consecutive elements in their codes differ in at most four or five positions. Inspired by their article, we give here Gray codes for Catalan permutations and for 0020-0190/$ – see front matter © 2009 Elsevier B.V. All rights reserved. doi:10.1016/j.ipl.2009.03.025

More restrictive Gray codes for some classes of pattern avoiding permutations

Embed Size (px)

Citation preview

Information Processing Letters 109 (2009) 799–804

Contents lists available at ScienceDirect

Information Processing Letters

www.elsevier.com/locate/ipl

More restrictive Gray codes for some classes of pattern avoidingpermutations

Jean-Luc Baril

LE2I UMR-CNRS 5158, Université de Bourgogne, B.P. 47 870, 21078 Dijon Cedex, France

a r t i c l e i n f o a b s t r a c t

Article history:Received 8 July 2008Received in revised form 26 January 2009Accepted 22 March 2009Available online 27 March 2009Communicated by D. Pointcheval

Keywords:AlgorithmsGray codePattern avoiding permutationGenerating algorithmCatalan numbersSchröder numbersCentral binomial coefficients

In a recent article [W.M.B. Dukes, M.F. Flanagan, T. Mansour, V. Vajnovszki, CombinatorialGray codes for classes of pattern avoiding permutations, Theoret. Comput. Sci. 396 (2008)35–49], Dukes, Flanagan, Mansour and Vajnovszki present Gray codes for several familiesof pattern avoiding permutations. In their Gray codes two consecutive objects differ inat most four or five positions, which is not optimal. In this paper, we present a unifiedconstruction in order to refine their results (or to find other Gray codes). In particular,we obtain more restrictive Gray codes for the two Wilf classes of Catalan permutationsof length n; two consecutive objects differ in at most two or three positions which isoptimal for n odd. Other refinements have been found for permutation sets enumeratedby the numbers of Schröder, Pell, even index Fibonacci numbers and the central binomialcoefficients. A general efficient generating algorithm is also given.

© 2009 Elsevier B.V. All rights reserved.

1. Introduction

Let Sn be the set of all permutations of length n (n �1). We represent permutations in one-line notation, i.e. ifi1, i2, . . . , in are n distinct values in [n] = {1,2, . . . ,n}, wedenote the permutation σ ∈ Sn by the sequence i1i2 . . . in

if σ(k) = ik for 1 � k � n. For instance, the identity per-mutation of length n, idn , will be written 12 . . . (n − 1)n.Moreover, if γ = γ (1)γ (2) . . . γ (n) is an n-length per-mutation then the composition (or product) γ · σ is thepermutation γ (σ (1))γ (σ (2)) . . . γ (σ (n)). In Sn , a k-cycleσ = 〈i1, i2, . . . , ik〉 is an n-length permutation satisfy-ing σ(i1) = i2, σ (i2) = i3, . . . , σ (ik−1) = ik , σ(ik) = i1 andσ( j) = j for j ∈ [n]\{i1, . . . , ik}; in particular, a transpo-sition is a 2-cycle. A permutation σ = σ1σ2 . . . σn ∈ Sn

contains the pattern γ ∈ Sk (k � 2) if and only if a se-quence of indices 1 � i1 < i2 < · · · < ik � n exists such thatσi1σi2 . . . σik is ordered as γ . We denote by Sn(γ ) the setof permutations in Sn avoiding the pattern γ . For example,25314 /∈ S5(123) but 43152 ∈ S5(123).

E-mail address: [email protected].

0020-0190/$ – see front matter © 2009 Elsevier B.V. All rights reserved.doi:10.1016/j.ipl.2009.03.025

Several algorithms for generating permutation classeshave been published (generated in lexicographic orderor in constant time per permutation, in the amortizedsense). For example, efficient algorithms are known forderangements [1], up-down permutations [7,19], permu-tations with a fixed number of inversions [12] and forpermutations avoiding a pattern [9]. On the other hand, al-gorithms for generating permutation classes in Gray codeorder are given for permutations [14,22] and their restric-tions [17,19,20], derangements [3,16], with a fixed numberof cycles [2], involutions and fixed-point free involutions[25] or their generalizations (multiset permutations [24]).Juarna and Vajnovszki [15] presented Gray codes and gen-erating algorithms for two classes of pattern avoiding per-mutations: Sn(123,132), Sn(123,132, p(p −1) . . . 1(p +1)).In a recent paper, Dukes et al. [11] give Gray codes andgenerating algorithms for classes of pattern avoiding per-mutations enumerated by Catalan, Schröder, Pell, even in-dex Fibonacci numbers and the central binomial coeffi-cients: two consecutive elements in their codes differ inat most four or five positions. Inspired by their article,we give here Gray codes for Catalan permutations and for

800 J.-L. Baril / Information Processing Letters 109 (2009) 799–804

some other classes: two consecutive elements differ in atmost three (or two) positions. This induces optimal Graycodes for some of these classes: Sn(321) and Sn(312) forn odd. Notice that in [8] or [23], Gray codes are givenfor some classes previously mentioned, but they do notprovide them in term of classes of pattern avoiding permu-tations. Despite the fact that some well-known bijectionsexist between these classes they do not conserve the Graycode property.

This paper is organized as follow. In Section 2, we givethe construction for our Gray codes based on the ECOmethod [4]. We obtain sufficient conditions showing thatour construction produces Gray codes. Thus we investigateseveral classes of avoiding pattern permutation sets in Ta-ble 2. In the third and last sections, we shortly explain ourgenerating algorithm to list our Gray codes. We present animplementation of our algorithm, and we discuss its com-plexity.

2. Construction for our Gray codes

In this part, we develop a general construction thatprovides Gray codes for several classes of pattern avoid-ing permutations. This construction is a refinement of thatgiven in [11] and, in some cases, yields codes that are op-timal. The construction is based on the method ECO [4].It consists in determining a recursive construction for theconsidered class based on local expansion performed onthe objects. Each object is produced exactly once by an ob-ject of lower size.

Let σ ∈ Sn; the sites of σ are the positions between twoconsecutives entries, before the first and after the last en-try. We suppose that the sites are numbered, from rightto left, from 1 to n + 1. For example, the 3rd site of thepermutation σ = 463512 is between the entries 5 and 1.Moreover, let σ ∈ Sn(T ), where T is a set of forbidden pat-terns, the ith site of σ is said active if the permutation γobtained from σ by inserting (n + 1) into this site belongsto Sn+1(T ). We will denote this permutation γ by φ(i, σ )

and we will say that γ is a successor (or son) of σ . The ac-tive sites of a permutation σ ∈ Sn(T ) are right-justified (see[11]) if all sites to the right of the leftmost active site arealso active. We denote by χT (i, σ ) the number of activesites of φ(i, σ ). A set of patterns T is called regular [11] iffor any n � 1 and γ ∈ Sn(T ),

– γ has at least two active sites, and the active sites ofγ are right-justified,

– there exist σ ∈ Sn−1(T ) and an integer i, 1 � i � n,such that γ = φ(i, σ ),

– χT (i, σ ) does not depend on σ but only on the num-ber k of active sites of σ ; in this case, we will denoteχT (i, σ ) by χT (i,k) if σ has k active sites.

For the remainder of this paper, we only consider regularset T of patterns. Now, for each n � 1 and for any permu-tation σ ∈ Sn(T ), we associate:

– a direction: up or down; a permutation σ with the di-rection up (down respectively) will be denoted by σ 1

(σ 0 respectively). Such a permutation will be called adirected permutation,

– a list of its sons considered with their directions, i.e.a list consisting of some φ(i, σ ) j where j ∈ {0,1} andi are the active sites of σ in an order that we willdescribe below.

The list of successors of σ 0 will be obtained by reversingthe list of successors of σ 1 and by reversing the directionof each element of the list.

Let σ 1 be a directed permutation in Sn(T ) with k activesites and let L be a unimodal sequence of integers whereL(i) denotes the ith (1 � i � k) integer of the followingsequence L:

L =

⎧⎪⎪⎪⎨⎪⎪⎪⎩

3,5, . . . , (k − 3), (k − 1),k, (k − 2),

(k − 4), . . . ,4,2,1 if k is even,

3,5, . . . , (k − 4), (k − 2),k, (k − 1),

(k − 3), . . . ,4,2,1 if k is odd.

We also consider the sequence L′ defined as follows:

L′ =

⎧⎪⎪⎪⎨⎪⎪⎪⎩

2,4, . . . , (k − 4), (k − 2),k, (k − 1),

(k − 3), . . . ,5,3,1 if k is even,

2,4, . . . , (k − 3), (k − 1),k, (k − 2),

(k − 4), . . . ,5,3,1 if k is odd.

Let Π(σ 1) be the list of successors of σ 1 consisting of kdirected permutations in Sn+1(T ) such that: the jth el-ement is φ(L( j),σ )(i−1) mod 2 for 1 � j � k − 1 and thekth element is φ(L(k),σ )1. Moreover, if we replace the se-quence L by L′ in this definition, we obtain a list that wedenote Π ′(σ 1). Figs. 1 and 2 illustrate the different casesfor the lists Π(σ) and Π ′(σ ) for σ having k = 7 or k = 8active sites.

Now, we set dn = card(Sn(T )) and we recursively defineour Gray code Sn(T ) for Sn(T ) as follows:

Sn(T ) ={

10 if n = 1,⊕dn−1i=1 Π̃(σ [i]) otherwise,

where

– σ [i] is the ith directed permutation of the list Sn−1(T ),– Π̃(σ [i]) = Π ′(σ [i]) if: the direction of σ [i] is 0 and

the direction of σ [i + 1] is 1; σ [i] and σ [i + 1] differin three positions and have respectively at least threesons and exactly two sons,

– Π̃(σ [i]) = Π ′(σ [i]) if: the direction of σ [i] is 1 andthe direction of σ [i − 1] is 0; σ [i] and σ [i − 1] differin three positions and have respectively at least threesons and exactly two sons,

– Π̃(σ [i]) = Π(σ [i]) otherwise.

Table 1 illustrates our construction for S5(321) and S5(312).Notice that the lists Π and Π ′ appear in the constructionof the Gray code S5(321); but only the lists of the form Π

are used for S5(312).For the remainder of this paper, we study several prop-

erties that allow us to prove this definition gives almost allGray codes such that two consecutive elements in the listdiffer in at most three positions. If L is a list, we denoteby first(L) and last(L) the first and last element of L.

J.-L. Baril / Information Processing Letters 109 (2009) 799–804 801

Fig. 1. If k = 7 then Π(σ 1) is the list φ(3, σ )0, φ(5, σ )1, φ(7, σ )0, φ(6, σ )1, φ(4, σ )0, φ(2, σ )1, φ(1, σ )1; if k = 8, Π(σ 1) is φ(3, σ )0, φ(5, σ )1, φ(7, σ )0,

φ(8, σ )1, φ(6, σ )0, φ(4, σ )1, φ(2, σ )0, φ(1, σ )1. Each point (resp. encircled point) numbered by � ∈ [1..k] represents φ(�,σ )1 (resp. φ(�,σ )0).

Fig. 2. If k = 7 then Π ′(σ 1) is the list φ(2, σ )0, φ(4, σ )1, φ(6, σ )0, φ(7, σ )1, φ(5, σ )0, φ(3, σ )1, φ(1, σ )1; if k = 8, Π ′(σ 1) is φ(2, σ )0, φ(4, σ )1, φ(6, σ )0,

φ(8, σ )1, φ(7, σ )0, φ(5, σ )1, φ(3, σ )0, φ(1, σ )1. Each point (resp. encircled point) numbered by � ∈ [1..k] represents φ(�,σ )1 (resp. φ(�,σ )0).

Table 1The optimal cyclic Gray codes for S5(312) and S5(321). Two consecutiveelements differ in at most three positions.

S5(312) S5(321)

12345 32145 24315 12345 13245 2314512354 32154 24351 12354 13254 2315412543 32541 25431 15234 13524 2351412453 32451 24531 51234 13452 2345112435 32415 23541 12534 13425 2341514325 43215 23451 12453 34125 2413514352 43251 23415 12435 34152 2415315432 45321 23154 41235 34512 2451314532 54321 23145 41253 31452 2145313542 43521 21435 45123 31425 2143513452 34521 21453 41523 31524 2153413425 35421 21543 14523 35124 2513413254 34251 21354 14253 31254 2135413245 34215 21345 14235 31245 21345

Proposition 1. Let T be a regular set of patterns. The list Sn(T )

contains exactly once all permutations in Sn(T ). The first el-ement of the list is 123 . . . (n − 1)n and the last element is2134 . . . (n − 1)n.

Proof. This holds for n = 1 and n = 2 since S1(T ) = {1}and S2(T ) = {12,21} where T is a regular pattern setthat does not contain the pattern 21. By induction, letus suppose that this is true for all k, 2 � k < n. Then,first(Sn(T )) = φ(1,first(Sn−1(T ))) = 1234 . . . (n − 1)n, andlast(Sn(T )) = φ(1, last(Sn−1(T ))) = 2134 . . . (n − 1)n. Thefact that the list Sn(T ) contains each of the permutationsexactly once is deduced directly from the above recursivedefinition of Sn(T ). �Lemma 2. Let T be a regular set of patterns. Let σ be a per-mutation in Sn(T ), and let r ∈ {0,1} be its direction, then twosuccessive permutations in Π(σ r) differ in at most three posi-tions.

Proof. Let α and β the two successive permutations inΠ(σ r). Thus if k is the number of active sites of σ , thereexists i and j, i �= j, 1 � i, j � k, such that α = φ(i, σ ) andβ = φ( j, σ ). Considering the definitions of the sequencesL or L′ , we distinguish two cases: (a) if i = j − 1 thenα = β · 〈i, j〉 which induces the result; (b) if i = j − 2 then

α = β · 〈i, j, i + 1〉 and the two permutations α and β dif-fer in three positions. The other cases (i.e. j = i − 1 andj = i − 2), are similar. �Lemma 3. Let T be a regular set of patterns. Let σ 1 and γ 0 betwo consecutive directed permutations in the list Sn(T ). Thend(σ ,γ ) = d(last(Π(σ 1)),first(Π(γ 0))).

Proof. Indeed, let us assume that σ has k sons and γ has� sons. Then we have:

d(last

(σ 1)),first

(γ 0)))

= d(φ(L(k),σ

), φ

(L(�), γ

))= d

(φ(1, σ ),φ(1, γ )

) = d(σ ,γ ). �Lemma 4. Let T be a regular set of patterns. Let σ 0 and γ 1 betwo consecutive directed permutations in the list Sn(T ). If bothpermutations σ and γ have at least three successors then wehave: d(σ ,γ ) = d(last(Π(σ 0)),first(Π(γ 1))).

Proof. Since each permutation σ and γ has at least threesons, their corresponding sequences L satisfy L(1) = 3.Thus, we have:

d(last

(σ 0)),first

(γ 1)))

= d(φ(L(1),σ

), φ

(L(1), γ

))= d

(φ(3, σ ),φ(3, γ )

) = d(σ ,γ ). �Lemma 5. Let T be a regular set of patterns. Let σ 0 and γ 1

be two consecutive directed permutations in the list Sn(T ). Ifboth permutations σ and γ have two successors then we have:d(σ ,γ ) = d(last(Π(σ 0)),first(Π(γ 1))).

Proof. Since each permutation σ and γ has two sons,their corresponding sequences L verify L(1) = 2. Thus, wehave:

d(last

(σ 0)),first

(γ 1)))

= d(φ(L(1),σ

), φ

(L(1), γ

))= d

(φ(2, σ ),φ(2, γ )

) = d(σ ,γ ). �

802 J.-L. Baril / Information Processing Letters 109 (2009) 799–804

Lemma 6. Let T be a regular set of patterns. Let σ 0 and γ 1

be two consecutive directed permutations in the list Sn(T ) suchthat σ = γ · 〈n − 1,n〉. If σ has at least three sons and γ hastwo sons then we have: d(last(Π(σ 0)),first(Π(γ 1))) = 3.

Proof. Since the permutation σ has at least three sons, ithas a corresponding sequence L such that L(1) = 3; as γhas two sons its corresponding sequence L is such thatL(1) = 2. Thus, we have:

d(last

(σ 0)),first

(γ 1)))

= d(φ(L(1),σ

), φ

(L(1), γ

))= d

(φ(3, σ ),φ(2, γ )

)= d

(φ(3, γ · 〈n − 1,n〉), φ(2, γ )

)= d

(φ(2, γ · 〈n − 1,n〉) · 〈n − 1,n〉, φ(2, γ )

)= d

(φ(2, γ ) · 〈n − 1,n,n + 1〉, φ(2, γ )

) = 3. �Lemma 7. Let T be a regular set of patterns. Let σ 0 and γ 1

be two consecutive directed permutations in the list Sn(T ) suchthat σ and γ differ in three positions. If σ has two sons (resp.at least three sons) and γ has at least three sons (resp. exactlytwo sons), then we have: d(last(Π(σ 0)),first(Π(γ 1))) = 3.

Proof. Since the permutation σ has two sons, it has a cor-responding sequence L such that L(1) = 2; as γ has atleast three sons its corresponding sequence L′ is such thatL′(1) = 2. Thus, we have:

d(last

(σ 0)),first

(γ 1)))

= d(φ(L(1),σ

), φ

(L′(1), γ

))= d

(φ(2, σ ),φ(2, γ )

) = 3. �Lemma 8. Let T be a regular set of patterns. Let σ 1 and γ 1

be two consecutive directed permutations in the list Sn(T ) suchthat σ = γ · 〈n−1,n〉. If γ has at least three sons, then we have:d(last(Π(σ 1)),first(Π(γ 1))) = 2. On the other hand if γ hastwo sons then we have: d(last(Π(σ 1)),first(Π(γ 1))) = 3.

Proof. Let us assume that the permutation σ has k sons.Thus its corresponding sequence L is such that L(k) = 1.Let us assume that γ has at least three sons. It correspond-ing sequence L is such that L(1) = 3. Thus, we have:

d(last

(σ 1)),first

(γ 1)))

= d(φ(L(k),σ

), φ

(L(1), γ

))= d

(φ(1, σ ),φ(3, γ )

)= d

(φ(1, γ · 〈n − 1,n〉), φ(3, γ )

)= d

(φ(1, γ ) · 〈n − 1,n〉, φ(3, γ )

)= d

(φ(1, γ ) · 〈n − 1,n〉, φ(1, γ ) · 〈n − 1,n〉〈n,n + 1〉)

= d(φ(1, γ ),φ(1, γ ) · 〈n − 1,n + 1〉) = 2.

The proof for γ with two sons is similar. �Remark that:

– if χT (i,k) = 2 for all i and k, the list Sn(T ) definedabove is a cyclic Gray code for Sn(T ) such that twoconsecutive elements in the list differ in two positions.Indeed, it suffices to apply Lemmas 2, 3, and 5.

– If χT (i,k) � 3 for all i and k, then the list Sn(T ) de-fined above is a cyclic Gray code for Sn(T ). Two con-secutive elements in the list differ in at most threepositions. It suffices to apply Lemmas 2, 3, 4 and 8.

In Table 2, we give several avoiding pattern sets T forwhich we can apply our construction in order to obtainGray codes for Sn(T ). Notice that this construction al-lows us to obtain optimal Gray code for S2n+1(321) andS2n+1(312), n � 0. Indeed the difference between the num-ber of even and odd permutations in S2n+1(321) (resp.S2n+1(312)) is at least two (see [21]). Thus we cannot havea Gray code such that two consecutive permutations dif-fer by a transposition. Since the difference between thenumber of even and odd permutations in S2n(321) (resp.S2n(312)) is zero, we cannot deduce the optimality ofour Gray code for S2n(321) (resp. S2n(312)) and for otherclasses. Besides the Gray code for Sn(321,312) is optimaland it corresponds to the one in [15] which is obtained byanother method.

3. Algorithmic considerations

In this part, we provide the procedure gen_up(size,k)

for generating our Gray codes for Sn(T ) characterized bythe succession functions χT (k, i) given in Table 2. We ini-tialize σ := 123 . . . (n − 1)n and the call gen_down(1,2)

produces the Gray code Sn(T ) (the procedure gen_down(size,k) consists in the statements of gen_up(size,k)

in reverse order). This algorithm is inspired from the onein [11]. Only the order of the statements are modified ac-cording to the two sequences L and L′ . Notice that thesequence L′ is used if and only if we have the hypothesesof Lemma 7, i.e. when the current permutation σ pro-duces a list Π ′ obtained from L′ . This algorithm enablesus to ensure that we transform an object into its suc-cessor in constant amortized time. Indeed, the degree ofeach call is not 1; moreover, between two recursive callswe execute at most two transpositions when we updatethe current permutation σ . Moreover at least one per-mutation is generated in each recursive call. This meansthat the total amount of computation divided by thenumber of objects is bounded by a constant. Thus thecomplexity of this algorithm is O(|Sn(T )|). An applet forthe generation of Gray codes Sn(T ) for each set T citedin Table 2, is available on the web site http://www.u-bourgogne.fr/jl.baril/applet.html.

procedure gen_up(size,k)

dr := 0;if size = n then output σ ;else

for i := 1 to k doupdate σif we have hypotheses of Lemma 7 then

gen_down(size + 1,χT (k, L′(i)));dr := 1 − dr;

J.-L. Baril / Information Processing Letters 109 (2009) 799–804 803

Table 2Classes of permutations avoiding patterns in T with their corresponding succession functions χT (i,k). We give also the different lemmas that are used inorder to obtain a Gray code and the maximum numbers such that two consecutive permutations differ in [11] and in our Gray code.

Classes T χT (i,k) Lemmas [11], B

2n−1 [6] {321,312} 2 2, 3, 5 3, 2

Catalan [26] {312} i + 1 2, 3, 4, 6, 8 5, 3

{321} k + 1 if i = 1i otherwise

2, 3, 4, 6, 7, 8 5, 3

Even indexFibonacci [6]

{321,3412} k + 1 if i = 12 otherwise

2, 3, 5, 6, 8 5, 3

{321,4123} 3 if i = 1i otherwise

2, 3, 4, 5, 6, 8 5, 3

{321,4321} 3 if i = 3 and k = 3i + 1 otherwise

2, 3, 4, 6, 8 ·, 3

Pell [6] {321,3412,4123} 3 if i = 12 otherwise

2, 3, 5, 6, 8 5, 3

{312,4321,3421} 3 if i = 22 otherwise

2, 3, 5, 6, 8 ·,3

Schröder [13] {4321,4312} k + 1 if i = 1 or i = 2i otherwise

2, 3, 4, 6, 8 5, 3

{4231,4132} k + 1 if i = 1 or i = ki + 1 otherwise

2, 3, 4, 6, 8 5, 3

{4123,4213} k + 1 if k − 1 � i � ki + 2 otherwise

2, 3, 4, 6, 8 5, 3

Central binomialcoefficient [13]

{4321,4231,4312,4132} k + 1 if i = 13 if i = 2i otherwise

2, 3, 4, 6, 8 5, 3

{4231,4132,4213,4123} 3 if i = 1i + 1 otherwise

2, 3, 4, 6, 8 5, 3

Generalizedpattern [6,10]

{321, (p + 1)12 . . . p} k + 1 if i = 1 and k < pp if i = 1 and k = pi otherwise

2, 3, 4, 6, 7, 8 5, 3

{321,3412, (p + 1)12 . . . p} k + 1 if i = 1 and k < pp if i = 1 and k = p2 otherwise

2, 3, 5, 6, 8 5, 3

GeneralizedMotzkin [5]

for p � 2,

{321, (p + 2)1̄(p + 3)2 . . . (p + 1)}k + 1 if i = 1i if 2 � i � pi − 1 otherwise

2, 3, 4, 6, 7, 8 ·, 3

GeneralizedSchröder [18]

⋃τ∈Sn−1

{(p + 1)τ p} k + 1 if k � p orp + i � k + 1

i + p − 1 otherwise

2, 3, 4, 6, 8 5, 3

A dot in the last column means that there is no Gray code for these classes in the literature.

elseupdate σif dr = 1 then gen_up(size + 1,χT (k, L(i)));else gen_down(size + 1,χT (k, L(i)));end ifdr := 1 − dr;

end ifend ifend procedure;

Acknowledgements

I thank the anonymous referees for their constructiveremarks which have greatly improved this paper.

References

[1] S.G. Akl, A new algorithm for generating derangements, BIT 20 (1980)2–7.

[2] J.-L. Baril, Gray code for permutations with a fixed number of cycles,Discrete Math. 30 (13) (2007) 1559–1571.

[3] J.-L. Baril, V. Vajnovszki, Gray code for derangements, Discrete Appl.Math. 140 (2004) 207–221.

[4] E. Barcucci, A. Del Lungo, E. Pergola, R. Pinzani, A methodologyfor the enumeration of combinatorial objects, Discrete J. DifferenceEquations Appl. 5 (1999) 435–490.

[5] E. Barcucci, A. Del Lungo, E. Pergola, R. Pinzani, From Motzkin toCatalan permutations, Discrete Math. 217 (1–3) (2000) 33–49.

[6] E. Barcucci, A. Bernini, M. Poneti, From Fibonacci to Catalan permu-tations, PuMA 17 (1–2) (2006) 1–17.

[7] B. Bauslaugh, F. Ruskey, Generating alternating permutations lexico-graphically, BIT 30 (1990) 17–26.

[8] A. Bernini, E. Grazzini, E. Pergola, R. Pinzani, A general exhaustivegeneration algorithm for Gray structures, Acta Inform. 44 (5) (2007)361–376.

[9] M. Bona, Combinatorics of Permutations, Chapman & Hall, 2004.[10] T. Chow, J. West, Forbidden sequences and Chebyshev polynomials,

Discrete Math. 204 (1999) 119–128.[11] W.M.B. Dukes, M.F. Flanagan, T. Mansour, V. Vajnovszki, Combinato-

rial Gray codes for classes of pattern avoiding permutations, Theoret.Comput. Sci. 396 (2008) 35–49.

804 J.-L. Baril / Information Processing Letters 109 (2009) 799–804

[12] S. Effler, F. Ruskey, A CAT algorithm for generating permutations witha fixed number of inversions, Inform. Process. Lett. 86 (2003) 107–112.

[13] O. Guibert, Combinatoire des permutations à motifs exclus en liaisonavec mots, cartes planaires et tableaux de Young, PhD thesis, Univer-sit’e Bordeaux 1, 1995.

[14] S.M. Johnson, Generation of permutations by adjacent transpositions,Math. Comp. 17 (1963) 282–285.

[15] A. Juarna, V. Vajnovszki, Some generalizations of a Simion–Schmidtbijection, Comput. J. 50 (2007) 574–580.

[16] J.F. Korsh, Constant time generation of derangements, Inform. Pro-cess. Lett. 90 (4) (2004) 181–186.

[17] J.F. Korsh, Loopless generation of up-down permutations, DiscreteMath. 240 (1–3) (2001) 97–122.

[18] D. Kremer, Permutations with forbidden subsequences and a gener-alized Schröder number, Discrete Math. 218 (1–3) (2000) 121–130.

[19] D. Roelants van Baronaigien, F. Ruskey, Generating permutations withgiven ups and downs, Discrete Appl. Math. 36 (1) (1992) 57–65.

[20] F. Ruskey, U. Taylor, Fast generation of restricted classes of permuta-tions, Manuscript, 1995.

[21] R. Simion, F.W. Schmidt, Restricted permutations, European J. Com-bin. 6 (1985) 383–406.

[22] H.F. Trotter, PERM (Algorithm 115), Comm. ACM 5 (8) (1962) 434–435.

[23] V. Vajnovszki, Gray visiting Motzkins, Acta Inform. 38 (2002) 793–811.

[24] V. Vajnovszki, A loopless algorithm for generating the permutationsof a multiset, Theoret. Comput. Sci. 307 (2003) 415–431.

[25] T. Walsh, Gray codes for involutions, J. Combin. Math. Combin. Com-put. 36 (2001) 95–118.

[26] J. West, Generating trees and the Catalan and Schröder numbers, Dis-crete Math. 146 (1994) 247–262.