aa r X i v : . [ m a t h . C O ] M a r Generating permutations with a given major index
Vincent
Vajnovszki
LE2I, Universit´e de BourgogneBP 47870, 21078 Dijon Cedex, France [email protected]
October 17, 2018
Abstract
In [S. Effler, F. Ruskey, A CAT algorithm for listing permutations with a given number of inver-sions,
I.P.L. , 86/2 (2003)] the authors give an algorithm, which appears to be CAT, for generatingpermutations with a given major index. In the present paper we give a new algorithm for generatinga Gray code for subexcedant sequences. We show that this algorithm is CAT and derive it into aCAT generating algorithm for a Gray code for permutations with a given major index.
We present the first guaranteed constant average time generating algorithm for permuta-tions with a fixed index. First we give a co-lex order generating algorithm for boundedcompositions. Changing its generating order and specializing it for particular classes ofcompositions we derive a generating algorithms for a Gray code for fixed weight subex-cedant sequences; and after some improvements we obtain an efficient version of this lastalgorithm. The generated Gray code has the remarkable property that two consecutivesequences differ in at most three adjacent positions and by a bounded amount in thesepositions. Finally applying a bijection introduced in [7] between subexcedant sequencesand permutations with a given index we derive the desired algorithm, where consecutivegenerated permutations differ by at most three transpositions.Often, Gray code generating algorithms can be re-expressed simpler as algorithms withthe same time complexity and generating the same class of objects, but in different ( e.g. lexicographical) order. This is not the case in our construction: the
Grayness of thegenerated subexcedant sequences is critical in the construction of the efficient algorithmgenerating permutations with a fixed index.A statistic on the set S n of length n permutations is an association of an element of N to each permutation in S n . For π ∈ S n the major index , MAJ , is a statistic defined by(see, for example, [3, Section 10.6])
MAJ π = X ≤ i
For two integers n and k , an n -composition of k is an n -sequence c = c c . . . c n of non-negative integers with P ni =1 c i = k . For an n -sequence b = b b . . . b n , c is said b -bounded if ≤ c i ≤ b i , for all i , 1 ≤ i ≤ n . n this context b b . . . b n is called bounding sequence and we will consider only boundingsequences with either b i > b i = b i − = . . . = b = 0 for all i , 1 ≤ i ≤ n . Clearly, b i = 0 is equivalent to fix c i = 0. We denote by C ( k, n ) the set of all n -compositions of k , and by C b ( k, n ) the set of b -bounded n -compositions of k ; and if b i ≥ k for all i , then C b ( k, n ) = C ( k, n ). Definition 2. A subexcedant sequence c = c c . . . c n is an n -sequence with 0 ≤ c i ≤ i − i ; and P ni =1 c i is called the weight of c .We denote by S ( k, n ) the set of length n and weight k subexcedant sequences, andclearly S ( k, n ) = C b ( k, n ) with b = 0 1 2 . . . ( n − We give three generating algorithms, and the third one generates efficiently combinatorialobjects in bijection with permutations having fixed index : • Gen Colex generates the set C b ( k, n ) of bounded compositions in co-lex order (de-fined later). • Gen1 Gray which is obtained from
Gen Colex by: – changing its generating order, and – restricting it to the bounding sequence b = 01 . . . ( n − S ( k, n ), and it can be seen as the definition ofthis Gray code. • Gen2 Gray is a an efficient version of
Gen1 Gray .Finally, in Section 4, regarding the subexcedant sequences in S ( k, n ) as McMahonpermutation codes (defined in Section 3), a constant average time generating algorithmfor a Gray code for the set of permutations of length n with the major index equals k isobtained. Gen Colex
This algorithm generates C b ( k, n ) in co-lex order , which is defined as: c c . . . c n precedes d d . . . d n in co-lex order if c n c n − . . . c precedes d n d n − . . . d in lexicographical order.Its worst case time complexity is O ( k ) per composition.For a set of bounded compositions C b ( k, n ), an increasable position (with respect to C b ( k, n )) in a sequence c c . . . c n / ∈ C b ( k, n ) is an index i such that: • c = c = . . . c i − = 0, and • there is a composition d d . . . d n ∈ C b ( k, n ) with c i < d i and c i +1 = d i +1 , c i +2 = d i +2 ,. . . , c n = d n . · · · · · · ∈ C (9 , Gen Colex( , ) where redundant nodes are avoided. For example, for C (3 ,
5) the increasable positions are underlined in the followingsequences: 0 0 0 1 0 and 0 0 2 0 0. Indeed, the first two positions in 0 0 0 1 0 are not in-creasable since there is no composition in C (3 ,
5) with the suffix 0 1 0; and the thirdposition in 0 0 2 0 0 is not increasable because 2 is the maximal value in this position.Clearly, if ℓ < r are two increasable positions in c , then each i , ℓ < i < r , is still anincreasable position in c (unless b i = 0).Here is the sketch of the co-lex order generating procedure for C b ( k, n ): • initialize c by the length n sequence 0 0 . . . • for each increasable position i in c , increase c i by one and call recursively the gen-erating procedure if the obtained sequence c is not a composition in C b ( k, n ), andoutput it elsewhere.The complexity of the obtained algorithm is O ( k ) per generated composition and soinefficient. Indeed, too many nodes in the generating tree induced by this algorithm havedegree one. Algorithm Gen Colex in Figure 2 avoids some of these nodes. We will identifya node in a generating tree by the corresponding value of the sequence c ; and a redundantnode in a generating tree induced by the previous sketched algorithm is a node with aunique successor and which differs in the same position from its ancestor and its successor.For example, in Figure 1 (a) redundant nodes are: 0 0 0 1, 0 0 0 2, 0 0 1 3, 0 0 2 3 and 0 1 3 3.These nodes occur when, for a given suffix, the smallest value allowed in an increasableposition in the current sequence c is not 1, and this position is necessarily ℓ , the leftmostincreasable one. Algorithm Gen Colex avoids redundant nodes by setting c ℓ to its minimalvalue e = k − P ℓ − j =1 b j (and P ij =1 b j can be computed for each i , 1 ≤ i ≤ n , in a pre-processing step). For example, in Figure 1 (b) there are no redundant nodes. However,in the generating tree induced by Gen Colex there still remain arbitrary length sequencesof successive nodes with a unique successor; they are avoided in procedure
Gen2 Gray .Algorithm
Gen Colex is given in Figure 2 where ℓ is the leftmost increasable position inthe current sequence c , and r the leftmost non-zero position in c , and thus the rightmostincreasable position in c is r if c r < b r and r − b b . . . b n being the boundingsequence). The main call is Gen Colex( k , n ) and initially c is 0 0 . . .
0. (As previously,in this algorithm the function k min { s | P sj =1 b j ≥ k } can be computed and stored inan array, in a pre-processing step.)The induced generating tree for the call Gen Colex( , ) is given in Figure 3 (a). rocedure Gen Colex( k , r )global n, c, b ;if k = 0 then print c ;else if c [ r ] = b [ r ] then r := r − ;end if ℓ := min { s | P sj =1 b [ j ] ≥ k } ;for i := ℓ to r doif i = ℓ then e := k − P ℓ − j =1 b [ j ] ;else e := 1 ;end if c [ i ] := c [ i ] + e ;Gen Colex( k − e , i ); c [ i ] := c [ i ] − e ;end doend ifend procedure. Figure 2: Algorithm
Gen Colex . Gen1 Gray
This algorithm is defined in Figure 4 and is derived from
Gen Colex : the order of recursivecalls is changed according to a direction (parameter dir ), and it is specialized for boundingsequences b = 0 1 2 . . . ( n − Gen Colex and we will show that it produces a Gray code.The call of
Gen1 Gray with dir = 0 produces, in order, a recursive call with dir = 0,then r − ℓ calls in the for statement with dir equals successively: • , , . . . ,
1, if r − ℓ is even, and • , , . . . , ,
1, if r − ℓ is odd.In any case, the value of dir corresponding to the last call is 1.The call of Gen1 Gray with dir = 1 produces the same operations as previously but inreverse order, and in each recursive call the value of dir is replaced by 1 − dir . Thus, thecall of Gen1 Gray with dir = 1 produces, in order, r − ℓ calls in the for statement with dir equals alternatively 0 , , , . . . , then a last call with dir = 1. See Figure 3 (b) for anexample of generating tree induced by this procedure.Let S ( k, n ) be the ordered list for S ( k, n ) generated by the call Gen1 Gray( k , n , ) ,and it is easy to see that S ( k, n ) is suffix partitioned, that is, sequences with the samesuffix are contiguous; and Theorem 4 shows that S ( k, n ) is a Gray code.For a sequence c , a k ≥ dir ∈ { , } we denote by first( k ; dir ; c ) and last( k ; dir ; c ),the first and last subexcedant sequence produced by the call of Gen1 Gray ( k, r, dir ) if thecurrent sequence is c , and r the position of the leftmost non-zero value in c . In particular,if c = 0 0 . . .
0, then first( k ; 0; c ) is the first sequence in S ( k, n ), and last( k ; 0; c ) the lastone. (a) (b)Figure 3: (a): The tree induced by the call of Gen Colex( , ) with b = 0 1 2 3 4, and (b): that inducedby Gen1 Gray( , ) . Terminal nodes are in bold-face5 emark 1.
1. For a sequence c , the list produced by the call Gen1 Gray ( k, r,
0) is the reverse of thelist produced by the call
Gen1 Gray ( k, r, k ; dir ; c ) = first( k ; 1 − dir ; c ) , for dir ∈ { , } .2. Since the bounding sequence is b = 0 1 . . . ( n −
1) it follows that, for c = 0 0 . . . c i c i +1 . . . c n , c i = 0, first( k ; 0; c ) is • a a . . . a i − c i c i +1 . . . c n if k ≤ P i − j =1 ( j −
1) = ( i − · ( i − , where a a . . . a i − is thesmallest sequence, in co-lex order, in S ( k, i − • a a . . . a i c i +1 . . . c n if k > ( i − · ( i − , where a a . . . a i is the smallest sequence, inco-lex order, in S ( k + c i , i ). procedure Gen1 Gray( k , r , dir )global n, c, b ;if k = 0 then output c ;else if c [ r ] = r − then r := r − ;end if ℓ := min { s | s ( s − ≥ k } ; e := k − ( ℓ − ℓ − ;if dir = 0 then c [ ℓ ] := c [ ℓ ] + e ; Gen1 Gray( k − e , ℓ , ); c [ ℓ ] := c [ ℓ ] − e ; dir := ( r − ℓ ) mod 2 ;for i := ℓ + 1 to r do c [ i ] := c [ i ] + 1 ; Gen1 Gray( k − , i , dir ); dir := ( dir + 1) mod 2 ; c [ i ] := c [ i ] − ;end doelse dir := 0 ;for i := r downto ℓ + 1 do c [ i ] := c [ i ] + 1 ; Gen1 Gray( k − , i , dir ); dir := ( dir + 1) mod 2 ; c [ i ] := c [ i ] − ;end do c [ ℓ ] := c [ ℓ ] + e ; Gen1 Gray( k − e , ℓ , ); c [ ℓ ] := c [ ℓ ] − e ;end ifend ifend procedure. Figure 4: Algorithm
Gen1 Gray , the Gray code counterpart of
Gen Colex specialized to subexcedantsequences.
Now we introduce the notion of close sequences. Roughly speaking, two sequences areclose if they differ in at most three adjacent positions and by a bounded amount in thesepositions. Definition 3 below defines formally this notion, and Theorem 4 shows thatconsecutive subexcedant sequences generated by
Gen1 Gray are close.Let s = s s . . . s n and t = t t . . . t n be two subexcedant sequences of same weightwhich differ in at most three adjacent positions, and let p be the rightmost of them (notice hat necessarily p ≥ difference between s and t is the 3-tuple( a , a , a ) = ( s p − − t p − , s p − − t p − , s p − t p ) . Since s and t have same weight it follows that a + a + a = 0; and we denote by − ( a , a , a ) the tuple ( − a , − a , − a ). Definition 3.
Two sequences s and t in S ( k, n ) are close if: • s and t differ in at most three adjacent positions, and • if ( a , a , a ) is the difference between s and t , then( a , a , a ) ∈ {± (0 , , − , ± (0 , , − , ± (1 , − , , ± (1 , − , , ± (1 , , − , ± (1 , , − } . Even if the second point of this definition sound somewhat arbitrary, it turns out thatconsecutive sequences generated by algorithm
Gen1 Gray are close under this definition,and our generating algorithm for permutations with a given index in Section 4 is basedon it.
Example 1.
The following sequences are close: 01201 and 00301; 01003 and 01021; 00201and 01011; 01132 and 01204; the positions where the sequences differ are underlined.Whereas the following sequences are not close: 00211 and 01030 (they differ in more thanthree positions); 01201 and 01030 (the difference 3-tuple is not a specified one).
Remark 2. If s and t are two close subexcedant sequences in S ( k, n ), then there are atmost two ‘intermediate’ subexcedant sequences s ′ , s ′′ in S ( k, n ) such that the differencesbetween s and s ′ , between s ′ and s ′′ , and s ′′ and t are ± (1 , − , Example 2.
Let s = 0 1 0 1 1 1 and t = 0 0 2 0 1 1 be two sequences in S (4 , s and t are close since they difference is (1 , − , s ′ = 0 0 1 1 1 1 in S (4 ,
6) with • the difference between s and s ′ is (1 , − , • the difference between s ′ and t is ( − , , Remark 3. If s and t are close subexcedant sequences and m is an integer such thatboth u = first( m ; 0; s ) and v = first( m ; 0; t ) exist, then u and v are also close. Theorem 4.
Two consecutive sequences in S ( k, n ) generated by the algorithm Gen1 Gray are close.Proof.
Let s and t be two consecutive sequences generated by the call of Gen1 Gray( k , n , ) .Then there is a sequence c = c c . . . c n and a recursive call of Gen1 Gray acting on c (re-ferred later as the root call for s and t ) which produces, in the for statement, two callsso that s is the last sequence produced by the first of them and t the first produced bythe second of them.By Remark 1.1 it is enough to prove that s and t are close when their root call hasdirection 0. et ℓ and r , ℓ = r , be the leftmost and the rightmost increasable positions in c (andso c = c = . . . = c r − = 0, and possibly c r = 0); and i and i + 1 be the positions where c is modified by the root call in order to produce eventually s and t . Also we denote m = k − P nj =1 c j and e = m − ℓ · ( ℓ − .We will give the shape of s and t according to the following four cases.1. i = ℓ and r − ℓ is even,2. i = ℓ and r − ℓ is odd,3. i = ℓ and the call corresponding to i in the for statement of the root call has direction0 (and so that corresponding to i + 1 has direction 1),4. i = ℓ and the call corresponding to i in the for statement of the root call has direction1 (and so that corresponding to i + 1 has direction 0).Case 1. s = last( m − e ; 0; 00 . . . ec ℓ +1 . . . c n )= first( m − e ; 1; 00 . . . ec ℓ +1 . . . c n )= (cid:26) first( m − e − ( ℓ − . . . ( ℓ − ec ℓ +1 . . . c n ) if e = ℓ − m − e − ( ℓ − . . . ( ℓ − e + 1) c ℓ +1 . . . c n ) if e < ℓ − , and t = first( m −
1; 0; 00 . . . ( c ℓ +1 + 1) . . . c n )= first( m − e ; 0; 00 . . . ( e − c ℓ +1 + 1) . . . c n )= first( m − e − ( ℓ − . . . ( ℓ − e − c ℓ +1 + 1) . . . c n ) . Case 2. In this case s is the same as in the previous case and t = first( m −
1; 1; 00 . . . c ℓ +1 + 1) . . . c n )= (cid:26) first( m −
2; 0; 00 . . . c ℓ +1 + 2) . . . c n ) if c ℓ +1 + 2 ≤ ℓ first( m − e ; 0; 00 . . . e − c ℓ +1 + 1) . . . c n ) if c ℓ +1 + 2 > ℓ = (cid:26) first( m − e − ( ℓ − . . . ℓ − e − c ℓ +1 + 2) . . . c n ) if c ℓ +1 + 2 ≤ ℓ first( m − e − ( ℓ − . . . ( ℓ − e − c ℓ +1 + 1) . . . c n ) if c ℓ +1 + 2 > ℓ. Case 3. In this case c i = 0 and s = last( m −
1; 0; 00 . . . c i +1 . . . c n )= last( m −
2; 1; 00 . . . c i +1 . . . c n )= first( m −
2; 0; 00 . . . c i +1 . . . c n ) , and t = first( m −
1; 1; 00 . . . c i +1 + 1) . . . c n )= (cid:26) first( m −
2; 0; 00 . . . c i +1 + 2) . . . c n ) if c i +1 + 2 ≤ i first( m −
2; 0; 00 . . . c i +1 + 1) . . . c n ) if c i +1 + 2 > i. · · Gen1 Gray (11,7,0)which generates the list S (11 , Case 4. As previously, c i = 0 and s = last( m −
1; 1; 00 . . . c i +1 . . . c n )= first( m −
1; 0; 00 . . . c i +1 . . . c n ) , and t = first( m −
1; 0; 00 . . . c i +1 + 1) . . . c n ) . Finally, by Remark 3 it follows that in each of the four cases s and t are close, and thestatement holds.As a byproduct of the previous theorem and Remark 1.2 we have Remark 4. If s = s s . . . s n and t = t t . . . t n are two consecutive sequences generatedby Gen1 Gray and p is the rightmost position where they differ, then s s . . . s p − and t t . . . t p − are the smallest, in co-lex order, sequences in S ( x, p −
2) and S ( y, p − x = s + s + . . . + s p − and y = t + t + . . . + t p − . Remark that s s . . . s p − = t t . . . t p − , and so x = y , if s and t differ in two (adjacent) positions. Gen2 Gray
Since the generating tree induced by the call of
Gen1 Gray contains still arbitrary lengthbranches of nodes of degree one it has a poor time complexity. Here we show howsome of these nodes can be avoided in order to obtain the efficient generating algorithm
Gen2 Colex presented in Figure 6.A quasi-terminal node ( q-terminal node for short) in the tree induced by a generatingalgorithm is defined recursively as: a q-terminal node is either a terminal node (node withno successor) or a node with only one successor which in turn is a q-terminal node. Theq-terminal nodes occur for the calls of Gen1 Gray( k, r, dir ) when k = r ( r − . See Figure 5for an example.The key improvement made by Gen2 Gray consists in its last parameter p , which givesthe rightmost position where the current sequence differ from its previous one in the list S ( k, n ), and Gen2 Gray stops the recursive calls of more than three successive q-terminalcalls. Thus,
Gen2 Gray generates only suffixes of the form c p − c p − c p . . . c n ; see Table 1 foran example. Since two consecutive sequences in the Gray code S ( k, n ) differ in at mostthree adjacent positions, these suffixes are enough to generate efficiently S ( k, n ), and togenerate (in Section 4) a Gray code for the set of length n permutations having the majorindex equal to k .Now we explain how the parameter p propagates through recursive calls. A non ter-minal call of Gen2 Gray produces one or several calls. The first of them (corresponding rocedure Gen2 Gray( k , r , dir , p , u )global n, c, b ;if k = 0 or ( p − r ) ≥ and k = r ( r − then output( p, u );else if c [ r ] = r − then r := r − ;end if ℓ := min { s | s ( s − ≥ k } ; e := k − ( ℓ − ℓ − ;if dir = 0 then c [ ℓ ] := c [ ℓ ] + e ; Gen2 Gray( k − e , ℓ , , p , u ); c [ ℓ ] := c [ ℓ ] − e ; dir := ( r − ℓ ) mod 2 ;for i := ℓ + 1 to r do c [ i ] := c [ i ] + 1 ; Gen2 Gray( k − , i , dir , i , k − c [ i ] ); dir := ( dir + 1) mod 2 ; c [ i ] := c [ i ] − ;end doelse dir := 0 ;for i := r downto ℓ + 1 doif i = r then q := p ; v := u ; else q =: i + 1 ; v := c [ i + 1] + k ; end if c [ i ] := c [ i ] + 1 ; Gen2 Gray( k − , i , dir , q , v ); dir := ( dir + 1) mod 2 ; c [ i ] := c [ i ] − ;end doif ℓ = r then q := p ; v := u ; else q := ℓ + 1 ; v := c [ ℓ + 1] + k ; end if c [ ℓ ] := c [ ℓ ] + e ; Gen2 Gray( k − e , ℓ , , q , v ); c [ ℓ ] := c [ ℓ ] − e ;end ifend ifend procedure. Figure 6: Algorithm
Gen2 Gray . to a left child in the generating tree) inherits the value of the parameter p from its parentcall; in the other calls the value of this parameter is the rightmost position where thecurrent sequence differs from its previous generated one; this value is i if dir = 0 and i + 1 if dir = 1. So, each call keeps in the last parameter p the rightmost position wherethe current generated sequence differs from its previous one in the list S ( k, n ). Proce-dure Gen2 Gray prevents to produce more than three successive q-terminal calls. Forconvenience, initially p = 0.The last two parameters p and u of procedure Gen2 Gray and output by it are usedby procedure
Update Perm in Section 4 in order to generates permutations with a givenmajor index; u keeps the value of c + c + . . . + c p , and for convenience, initially u = 0.Even we will not make use later we sketch below an algorithm for efficiently generatingthe list S ( k, n ): • initialize d by the first sequence in S ( k, n ), i.e, the the smallest sequence in S ( k, n )in co-lex order, or equivalently, the largest one in lexicographical orders, and c by0 0 . . . • run Gen2 Gray( k, n, , ,
0) and for each p output by it update d as: d [ p −
2] := c [ p − d [ p −
1] := c [ p − d [ p ] := c [ p ]. Analyze of
Gen2 Gray
For a call of
Gen2 Gray( k , r , dir , p , u ) necessarily k ≤ r ( r − , and if k > p permutation sequence p permutation0 1 2 1 0 0 2 1 4 3 5 6 0 1 0 0 1 2 5 5 3 6 1 2 40 1 0 3 0 0 4 3 2 4 1 5 6 0 0 1 0 1 2 3 6 3 5 1 2 40 0 1 3 0 0 3 4 2 3 1 5 6 0 0 0 1 1 2 4 1 3 5 6 2 40 0 2 2 0 0 4 4 1 3 2 5 6 0 0 0 0 2 2 5 2 3 5 6 1 40 1 1 2 0 0 3 3 1 4 2 5 6 0 0 0 0 0 4 6 3 4 5 6 1 20 1 2 0 1 0 5 2 1 5 3 4 6 0 0 0 0 1 3 6 2 4 5 6 1 30 1 1 1 1 0 4 3 1 5 2 4 6 0 0 0 1 0 3 5 1 4 5 6 2 30 0 2 1 1 0 3 5 1 3 2 4 6 0 0 1 0 0 3 4 6 4 5 1 2 30 0 0 3 1 0 4 1 2 3 5 4 6 0 1 0 0 0 3 3 5 4 6 1 2 30 0 1 2 1 0 4 5 2 3 1 4 6 0 1 0 0 2 1 6 4 3 6 1 2 50 1 0 2 1 0 3 3 2 5 1 4 6 0 0 1 0 2 1 3 6 3 4 1 2 50 1 0 0 3 0 5 4 3 5 1 2 6 0 0 0 1 2 1 4 1 3 4 6 2 50 0 1 0 3 0 3 5 3 4 1 2 6 0 0 0 0 3 1 5 2 3 4 6 1 50 0 0 1 3 0 4 1 3 4 5 2 6 0 0 0 2 1 1 5 1 2 4 6 3 50 0 0 0 4 0 5 2 3 4 5 1 6 0 0 1 1 1 1 4 6 2 4 1 3 50 0 0 2 2 0 5 1 2 4 5 3 6 0 1 0 1 1 1 3 4 2 6 1 3 50 0 1 1 2 0 4 5 2 4 1 3 6 0 0 2 0 1 1 4 6 1 4 2 3 50 1 0 1 2 0 3 4 2 5 1 3 6 0 1 1 0 1 1 3 4 1 6 2 3 50 0 2 0 2 0 4 5 1 4 2 3 6 0 1 1 1 0 1 5 3 1 6 2 4 50 1 1 0 2 0 3 4 1 5 2 3 6 0 0 2 1 0 1 3 6 1 3 2 4 50 1 1 0 0 2 6 5 1 6 2 3 4 0 0 0 3 0 1 4 1 2 3 6 4 50 0 2 0 0 2 3 6 1 5 2 3 4 0 0 1 2 0 1 4 6 2 3 1 4 50 0 0 2 0 2 4 1 2 5 6 3 4 0 1 0 2 0 1 3 3 2 6 1 4 50 0 1 1 0 2 4 6 2 5 1 3 4 0 1 2 0 0 1 4 2 1 6 3 4 50 1 0 1 0 2 3 5 2 6 1 3 4Table 1: The subexcedant sequences generated by the call of Gen1 Gray( , , ) and their correspondinglength 6 permutations with major index equals 4, permutations descent set is either { , } or { } . Thethree leftmost entries ( c p − , c p − , c p ) updated by the call of Gen2 Gray( , , , , ) are underlined, where p is the rightmost position where a subexcedant sequence differ from its predecessor.11 k ≤ ( r − r − , then this call produces at least two recursive calls, • ( r − r − < k < r ( r − , then this call produces a unique recursive call (of the form Gen2 Gray( k ′ , r , · , · , · ) , with k ′ = k − ( r − r − ), which in turn produce two calls, • k = r ( r − , then this call is q-terminal call.Sine the procedure Gen2 Gray stops after three successive q-terminal calls, with a slightmodification of Ruskey and van Baronaigien’s [4] ‘CAT’ principle (see also [5]) it followsthat
Gen2 Gray runs in constant amortized time.
Here we present the bijection ψ : S ( n ) → S n , introduced in [7], which have the followingproperties: • the image through ψ of S ( k, n ) is the set of permutations in S n with major index k , • ψ is a ‘Gray code preserving bijection’ (see Theorem 6), • τ is easily computed from σ and from the difference between s and t , the McMahoncode of σ and τ , if s and t are close.In the next section we apply ψ in order to construct a list for the permutations in S n with a major index equals k from the Gray code list S ( k, n ).Let permutations act on indices, i.e., for σ = σ σ . . . σ n and τ = τ τ . . . τ n twopermutations in S n , σ · τ = σ τ σ τ . . . σ τ n . For a fixed integer n , let k and u be twointegers, 0 ≤ k < u ≤ n , and define [[ u, k ]] ∈ S n as the permutation obtained after k rightcircular shifts of the length- u prefix of the identity in S n . In two line notation[[ u, k ]] = (cid:18) · · · k k + 1 · · · u u + 1 · · · nu − k + 1 u − k + 2 · · · u · · · u − k u + 1 · · · n (cid:19) . For example, in S we have: [[3 , , , ψ : S ( n ) → S n be the function defined by ψ ( t t . . . t n ) = [[ n, t n ]] · [[ n − , t n − ]] · . . . · [[ i, t i ]] · . . . · [[2 , t ]] · [[1 , t ]]= Y i = n [[ i, t i ]] . (1) Lemma 5 ([7]) .
1. The function ψ defined above is a bijection.2. For every t = t t . . . t n ∈ S ( n ) , we have MAJ Q i = n [[ i, t i ]] = P ni =1 t i . he first point of the previous lemma says that every permutation π ∈ S n can beuniquely written as Q i = n [[ i, t i ]] for some t i ’s, and the subexcedant sequence t t . . . t n iscalled the McMahon code of π . As a consequence of the second point of this lemma wehave: Remark 5.
The restriction of ψ maps bijectively permutations in S ( k, n ) into permuta-tions in S n with major index equals k . Example 3.
The permutation π = 5 2 1 6 4 3 ∈ S n can be obtained from the identity bythe following prefix rotations:1 2 3 4 5 6 [[6 , −→ [[5 , −→ [[4 , −→ [[3 , −→ [[2 , −→ [[1 , −→ , so π = [[6 , · [[5 , · [[4 , · [[3 , · [[2 , · [[1 , , and thus MAJ π = 3 + 4 + 2 + 2 + 1 + 0 = 12 . Theorem 6 below states that if two permutations have their McMahon code differing intwo adjacent positions, and by 1 and − h u, v i denote the permutation π (of convenientlength) with π ( i ) = i for all i , except π ( u ) = v and π ( v ) = u . Proposition 1.
Let n, u and v be three integers, n ≥ , ≤ u ≤ n − , ≤ v ≤ n − ,and σ, τ ∈ S n defined by: • σ = [[ n, u ]] · [[ n − , v ]] , and • τ = [[ n, u + 1]] · [[ n − , v − .Then τ = σ · h n, v i . Proof.
First, remark that: • [[ n, u + 1]], is a right circular shift of [[ n, u ]], and • [[ n − , v − n −
1) entries of [[ n − , v ]],and so σ ( i ) = τ ( i ) for all i , 1 ≤ i ≤ n , except for i = n and i = v . Example 4.
For n = 7, u = 4 and v = 3 we have • σ = [[ n, u ]] · [[ n − , v ]] = [[7 , · [[6 , • τ = [[ n, u + 1]] · [[ n − , v − , · [[6 , • h n, v i = h , i ,and τ = σ · h n, v i . roposition 2. If π ∈ S n and h u, v i is a transposition in S n , then π − · h u, v i · π = h π − ( u ) , π − ( v ) i . Proof.
Indeed, ( π − · h u, v i · π )( i ) = i , for all i , except for i = π − ( u ) and i = π − ( v ). Theorem 6.
Let σ and τ be two permutations in S n , n ≥ , and s = s s . . . s n and t = t t . . . t n their McMahon codes. If there is a f , ≤ f ≤ n − such that t i = s i forall i , except t f = s f − and t f +1 = s f +1 + 1 , then τ and σ differ by a transposition. Moreprecisely, τ = σ · h α − ( u ) , α − ( v ) i where α = Y i = f − [[ i, s i ]] = Y i = f − [[ i, t i ]] , and u = f + 1 , v = s f .Proof. • τ = Q i = n [[ i, t i ]], and so τ · α − = Q fi = n [[ i, t i ]], and • σ = Q i = n [[ i, s i ]], and σ · α − = Q fi = n [[ i, s i ]].But, by Proposition 1, f Y i = n [[ i, t i ]] = f Y i = n [[ i, s i ]] · h f + 1 , s f i or, equivalently τ · α − = σ · α − · h f + 1 , s f i , and by Proposition 2, the results holds.The previous theorem says that σ and τ ‘have a small difference’ provided that theirMcMahon code, s and t , do so. Actually, we need that s and t are consecutive sequencesin the list S ( k, n ) and they have a more particular shape (see Remark 4). In this context,permutations having minimal McMahon code play a particular role.It is routine to check the following proposition (see Figure 7 for an example). Proposition 3.
Let n and k be two integers, < k ≤ n ( n − ; a = a a . . . a n be thesmallest subexcedant sequence in co-lex order with P ni =1 a i = k , and α = α n,k = ψ ( a ) bethe permutation in S n having its McMahon code a . Let j = max { i : a i = 0 } , that is, a has the form . . . ( j − j − a j . . . . Then α ( i ) = j − a j − i if 1 ≤ i ≤ j − ( a j + 1) , j − a j − i if j − ( a j + 1) < i ≤ j,i if i > j. (2) ✲ r r r r r r r r iα ( i )Figure 7: The permutation α = 2 1 6 5 4 3 8 7 with the McMahon code a = 0 1 2 3 4 3 0 0, the the smallest,in co-lex order, subexcedant sequence in S (13 , Remark 6.
The permutation α defined in Proposition in 3 is an involution, that is α − = α .Combining Proposition 3 and Remark 6, Theorem 6 becomes in particular Proposition 4.
Let σ , τ , s and t be as in Theorem 6. In addition, let suppose that thereis a j , ≤ j ≤ f − , such that1. s i = t i = 0 for j < i ≤ f − , and2. if j > , then – s j = t j = 0 , and – s i = t i = i − for ≤ i < j .Then τ = σ · h φ j ( f + 1) , φ j ( s f ) i with φ j ( i ) = j − s j − i if 1 ≤ i ≤ j − ( s j + 1) , j − s j − i if j − ( s j + 1) < i ≤ j,i if i > j. (3)Notice that, the conditions 1 and 2 in the previous proposition require that s s . . . s f − = t t . . . t f − be the smallest subexcedant sequence, in co-lex order, in S ( f −
1) with fixedvalue for P f − i =1 s i = P f − i =1 t i . Also, for point 2, necessarily j ≥ Let σ and τ be two permutations with their McMahon code s = s s . . . s n and t = t t . . . t n belonging to S ( k, n ), and differing in positions f and f + 1 by 1 and − • v = s f − t f ∈ {− , } , and x = P f − i =1 s i = P f − i =1 t i .If s s . . . s f − is the smallest sequence in S ( x, f − transp( v, f, x ) defined in Figure 8transforms σ into τ and s into t . procedure transp( v, f, x ) j := min { i : i ( i − ≥ x } ;if v = 1 then σ := σ · h φ j ( f + 1) , φ j ( s [ f ]) i ;else σ := σ · h φ j ( f + 1) , φ j ( s [ f ] + 1) i ;endif s [ f ] := s [ f ] − v ; s [ f + 1] := s [ f + 1] + v ;end procedure. Figure 8: Algorithm transp , where φ j is defined in relation (3). Let now f be the leftmost position where two consecutive sequences s and t in thelist S ( k, n ) differ, and σ and τ be the permutations having they McMahon code s and t . By Remarks 2 and 4 we have that, repeated calls of transp transform s into t , and σ into τ . This is true for each possible 3-tuples given in Definition 3 and corresponding totwo consecutive subexcedant sequences in S ( k, n ), and algorithm Update Perm in Figure9 exhausts all these 3-tuples.For example, if s and t are the two sequences in Example 2 with they difference(1 , − , f = 2 and x = 0, then the calls transp( , f, x ) ; transp( − , f + 1 , x + s [ f ] ) ;transform s into t and σ into τ .Algorithm Gen2 Gray provides p , the rightmost position where the current sequence c differs from the previous generated one, and u = P pi =1 c i . Algorithm Update Perm uses f , the leftmost position where c differs from the previous generated sequence, and x = P f − i =1 c i .Now, we sketch the generating algorithm for the set of permutations in S n havingindex k . • initialize s by the smallest, in co-lex order, sequence in S ( k, n ) and σ by the permu-tation in S n having its McMahon code s , • run Gen2 Gray( k, n, , , ) where output( p, u ) is replaced by Update Perm( p, u ) .The obtained list of permutations is the image of the Gray code S ( k, n ) through thebijection ψ defined in relation (1); it consists of all permutations in S n with major indexequal to k , and two consecutive permutations differ by at most three transpositions. SeeTable 1 for the list of permutations in S and with major index 4. rocedure Update Perm( p, u ) x := u − c [ p ] − c [ p − ;if p − ≥ and s [ p −
2] = c [ p − then f := p − ;else f := p − ; x := x − c [ f ] ;endif ( a , a ) := ( s [ f ] − c [ f ] , s [ f + 1] − c [ f + 1]) ;if f + 2 > n then a := 0 ; else a := s [ f + 2] − c [ f + 2] ; endifif a > then v := 1 ; else v := − ; endifcase ( a , a , a ) of ± (1 , − , : transp( v, f, x ); ± (2 , − , : transp( v, f, x ); transp( v, f, x ) ± (1 , − , : transp( v, f, x ); transp( − v, f + 1 , x + s [ f ] ); ± (1 , − , : transp( v, f, x ); transp( − v, f + 1 , x + s [ f ] );transp( − v, f + 1 , x + s ( f ] ); ± (1 , , − : transp( v, f + 1 , x + s [ f ] ); transp( v, f, x ); transp( v, f + 1 , x + s [ f ] ); (1 , , − : transp( , f, x ); transp( , f + 1 , x + s [ f ] ); ( − , , : transp( − , f + 1 , x + s [ f ] ); transp( − , f, x );end caseend procedure. Figure 9: Algorithm
Update Perm . Numerical evidences show that if we change the generating order of algorithm
Gen Colex as for
Gen1 Gray , but without restricting it to subexcedant sequences, then the obtainedlist for bounded compositions is still a Gray code with the closeness definition slightlyrelaxed: two consecutive compositions differ in at most four adjacent positions. Also, T.Walsh gives in [8] an efficient generating algorithm for a Gray code for bounded compo-sitions of an integer, and in particular for subexcedant sequences. In this Gray code twoconsecutive sequences differ in two positions and by 1 and − ψ defined by relation (1) in Section 3 does not give a Gray code for permutations with afixed index. References [1]
M. B´ona , Combinatorics of Permutations , Chapman and Hall/CRC, Boca Raton,Florida, USA, 2004.[2]
S. Effler, F. Ruskey , A CAT Algorithm for listing permutations with a givennumber of inversions,
Information Processing Letters , /2 (2003) 107-112.[3] M. Lothaire , Combinatorics on Words , Encyclopedia of Math. and its Appl., vol.17, Addison-Wesley, London, 1983.[4]
D. Roelants van Baronaigien, F. Ruskey , Efficient generation of subsets witha given sum,
JCMCC , (1993), 87–96. F. Ruskey , Combinatorial generation . Book in preparation.[6]
V. Vajnovszki, R. Vernay , Restricted compositions and permutations: from oldto new Gray codes,
Information Processin Letters , (13) (2011), 650-655.[7] V. Vajnovszki , A new Euler-Mahonian constructive bijection,
Discrete AppliedMathematics , (2011), 1453-1459.[8] T. Walsh , Loop-free sequencing of bounded integer compositions,
Journal of Com-binatorial Mathematics and Combinatorial Computing , (2000), 323-345.(2000), 323-345.