15
Ž . JOURNAL OF ALGORITHMS 25, 321]335 1997 ARTICLE NO. AL970889 Generating Multiset Permutations in Constant Time James Korsh and Seymour Lipschutz Department of Computer and Information Science, Temple Uni ¤ ersity, Philadelphia, Pennsyl ¤ ania 19122 Received September 1, 1994; revised August 31, 1995 An algorithm is presented that generates multiset permutations taking constant time between each permutation. Q 1997 Academic Press 1. INTRODUCTION Many papers have been written on algorithms generating permutations Ž . and multiset permutations permutations with repetitions . Ruskey and wx Roelants van Baronaigien 8 gave the first algorithm to generate multiset Ž . permutations in constant average time. They noted that Hu and Tien HT wx 3 , also gave an algorithm for their generation, but it was ‘‘unanalyzed’’ as there was no implementation or time analysis. This paper shows that the HT algorithm can be implemented to gener- ate multiset permutations using constant time between them. We note that wx HT used an algorithm by Johnson 4 which lists, using only adjacent Ž . transpositions, all permutations in S permutations of n distinct objects , n wx and an algorithm by Lehmer 5 which lists all n-bit vectors with a given weight k lexicographically. We first give constant time or loopless imple- mentations of Johnson’s and Lehmer’s algorithms and then combine the wx two, as in 3 , to obtain the loopless multiset permutation generation algorithm. For completeness, we include descriptions of the Johnson and Lehmer algorithms. Generating all n-bits vectors with weight k, that is, all permutations with k copies of 1 and n y k copies of 0, is equivalent to generating all combinations of n things taken k at a time. Bitner, Ehrlich, and Reingold wx Ž . 1 gave a loopless algorithm BER for their generation. However, Lehmer’s algorithm generates them lexicographically and seems more intuitive. They applied BER to obtain loopless generation algorithms for the compositions and t-part compositions of an integer. Our implementa- 321 0196-6774r97 $25.00 Copyright Q 1997 by Academic Press All rights of reproduction in any form reserved.

Generating Multiset Permutations in Constant Time

Embed Size (px)

Citation preview

Ž .JOURNAL OF ALGORITHMS 25, 321]335 1997ARTICLE NO. AL970889

Generating Multiset Permutations in Constant Time

James Korsh and Seymour Lipschutz

Department of Computer and Information Science, Temple Uni ersity,Philadelphia, Pennsyl ania 19122

Received September 1, 1994; revised August 31, 1995

An algorithm is presented that generates multiset permutations taking constanttime between each permutation. Q 1997 Academic Press

1. INTRODUCTION

Many papers have been written on algorithms generating permutationsŽ .and multiset permutations permutations with repetitions . Ruskey and

w xRoelants van Baronaigien 8 gave the first algorithm to generate multisetŽ .permutations in constant average time. They noted that Hu and Tien HT

w x3 , also gave an algorithm for their generation, but it was ‘‘unanalyzed’’ asthere was no implementation or time analysis.

This paper shows that the HT algorithm can be implemented to gener-ate multiset permutations using constant time between them. We note that

w xHT used an algorithm by Johnson 4 which lists, using only adjacentŽ .transpositions, all permutations in S permutations of n distinct objects ,n

w xand an algorithm by Lehmer 5 which lists all n-bit vectors with a givenweight k lexicographically. We first give constant time or loopless imple-mentations of Johnson’s and Lehmer’s algorithms and then combine the

w xtwo, as in 3 , to obtain the loopless multiset permutation generationalgorithm. For completeness, we include descriptions of the Johnson andLehmer algorithms.

Generating all n-bits vectors with weight k, that is, all permutations withk copies of 1 and n y k copies of 0, is equivalent to generating allcombinations of n things taken k at a time. Bitner, Ehrlich, and Reingoldw x Ž .1 gave a loopless algorithm BER for their generation. However,Lehmer’s algorithm generates them lexicographically and seems moreintuitive. They applied BER to obtain loopless generation algorithms forthe compositions and t-part compositions of an integer. Our implementa-

321

0196-6774r97 $25.00Copyright Q 1997 by Academic Press

All rights of reproduction in any form reserved.

KORSH AND LIPSCHUTZ322

tion of Lehmer’s algorithm can be similarly applied, yielding looplesslexicographic generation algorithms. They also described a recursive algo-rithm for the generation of multiset permutations, but it is not loopless.

2. JOHNSON’S ALGORITHM

Johnson’s algorithm generating the permutations of S using adjacentntransposition is illustrated in Fig. 1 for n s 4. Each of the 24 permutationsis numbered. The first is 1234, and they are arranged in six columns, each

Ž .column containing four permutations. In column i , the largest item 4Ž .sweeps from right to left; in column ii , it sweeps from left to right; and so

on. Note that items 1, 2, and 3 do not change their respective positionswithin a column. That is, the subpermutations involving 1, 2, and 3,obtained by deleting 4 from each permutation, are the same in eachcolumn. This relationship is indicated by labeling the columns by their

Žcorresponding subpermutations, 123, 132, 312, 321, 231, 213 viewed as.elements in S . Also note that items 1, 2, and 3 change their respective3

positions only when 4 is at the right or left end of the permutation. Henceitem 4 will never interfere with a transposition involving 1, 2, or 3.

Our implementation of Johnson’s algorithm in C is given in Section 5.Observe that there is a direction field for each item. In Fig. 1, an underlinemeans an item is moving right; otherwise, it is moving left. Initially eachitem is moving left. Whenever it reaches an end position in its respective

Ž .subpermutation, it changes direction. For example, in permutation 15items 3 and 4 are going right and 1 and 2 are going left.

ŽFurthermore there are ‘‘mobile’’ and ‘‘finished’’ lists similar to the‘‘unfinished’’ and ‘‘finished’’ lists used by Lucas, Roelants van Baronaigien,

w x .and Ruskey 6 to generate binary trees . Initially, all items are mobileexcept the smallest, 1, which is finished. The largest item on the mobile

� 4FIG. 1. Permutations of 1, 2, 3, 4 .

MULTISET PERMUTATION IN CONSTANT TIME 323

lists is the item that moves, and it becomes finished when it reaches an endposition. At that time it is deleted from the mobile list and inserted at thefront of the finished list.

When an item moves, all larger items on the finished list becomemobile. They are then deleted from the finished list and inserted at thefront of the mobile list, retaining their relative order. For example, item 4

Ž .becomes finished in permutation 4 ; however, when 3 moves to yieldŽ .permutation 5 , 4 becomes mobile. Similarly, items 4 and 3 are finished inŽ .permutation 12 . Accordingly, 2 is the next item to move and when it

Ž .moves to yield permutation 13 , 4 and 3 become mobile with 4 the nextitem to move.

All items will be on exactly one of the two lists and both lists are alwaysordered with their largest items first. The algorithm ends when all items

Ž .are finished and the mobile list is empty .

3. LEHMER’S ALGORITHM

Ž .Let V n, k denote the set of all n-bit vectors with weight k or allpermutations with k repetitions of 1 and n y k repetitions of 0. Lehmer’s

Ž .algorithm lists lexicographically all the elements of V n,k . The list forŽ .V 6, 3 is given in Fig. 2. It begins with all the 0s to the left of all the 1s.

Then the 1s ‘‘sweep’’ through the vector until finally all the 1s haveŽ .reached the left end in permutation 20 .

w xAdopting terminology from 3 , we say that an item 1 in a vector isŽ .‘‘mobile’’ if its ‘‘leftward neighbor’’ adjacent item on the left is 0. The last

Ž .rightmost mobile item 1 will be called the mo¨ing item since it is the nextitem 1 to move leftward. Also, if this moving item 1 is not the last item in

Ž . Ž .the vector, it will always be followed by either i a block of 0s or ii aŽ .block of 1s followed by a block of 0s. Whenever i occurs, the moving item

1 merely interchanges positions with its 0 left neighbor to yield the nextŽ .vector. On the other hand, suppose ii occurs, as in Fig. 3, which shows a

� 4FIG. 2. Permutations of 0, 0, 0, 1, 1, 1 .

KORSH AND LIPSCHUTZ324

FIGURE 3.

Ž .vector, with three mobile items, in V 22, 12 and the next vector in the list.Again the moving item 1 interchanges positions with its predecessor 0 andthe items before the 0 are not affected, but now the block of 1s and theblock of 0s following the moving item 1 interchange positions. We empha-size that throughout the algorithm the relative positions of the 1s do notchange and neither do the relative positions of the 0s.

Our implementation of Lehmer’s algorithm in C is given in Section 5.There is no need here for a direction field or a finished list. We are able toupdate the mobile list and interchange the blocks of 1s and 0s in constanttime by keeping track of the rightmost 1 and the item at the end of thevector.

4. HU AND TIEN ALGORITHM

Ž . Ž .Let M s M N s M a , a , . . . , a denote the multiset with a copies0 1 N 0of 0, a copies of 1, a copies of 2, and so on. We assume a ) 0. For1 2 N

Ž . Ž .k F N, let SM k denote the submultiset M a , a , . . . , a , where a ) 0.0 1 k kThe HT algorithm generates the permutations of M by weaving togetherthe Johnson and Lehmer algorithms. We are dealing with permutations ofn distinct integers when n of the a are 1 and the rest are 0, and withicombinations of n things taken k at a time when N is 1, a is n y k, and0a is k.1

We illustrate the algorithm using the multiset

� 4M s M 4 s M 0, 2, 2, 1, 3 s 1, 1, 2, 2, 3, 4, 4, 4 .Ž . Ž .

Figure 4 sketches the list of permutations of M generated by the HTŽ .algorithm. The initial permutation 1 consists of the 1s, 2s, 3s, and 4s in

order. Then Lehmer’s algorithm is used to sweep the three 4s to the left ofŽ . wthe permutation to finally arrive at permutation 56 . We add 55 s

Ž . x8!r 3!5! y 1 to 1 to arrive at 56. In effect, the 4s are treated as the 1s ofLehmer’s algorithm and all the smaller items are treated as the 0s. Now

MULTISET PERMUTATION IN CONSTANT TIME 325

� 4FIG. 4. Permutations of 1, 1, 2, 2, 3, 4, 4, 4 .

that the 4s are all on the left side, we can apply one step of Lehmer’sŽ .algorithm to the initial permutation of the submultiset SM 3 without

interference from any 4. Specifically, 3 moves one position to the leftŽ .giving permutation 57 . Then Lehmer’s algorithm is used to sweep the

Ž .three 4s to the right side, finally yielding permutation 112 . Again, the 3Ž .moves one place to the left giving permutation 113 and the three 4s

Ž .sweep to the right giving permutation 168 . Finally, the 3 arrives at the leftŽ .side in permutation 225 , and three 4s sweep to the left side producing

Ž .permutation 280 .At this point, all the 4s are ‘‘finished’’ and the 3 is ‘‘finished,’’ so we can

apply one step of Lehmer’s algorithm to the initial permutation of theŽ .submultiset SM 2 without interference from any 4 or the 3. Specially, one

Ž .2 moves to the left, giving permutation 281 . Then the three 4s sweep toŽ .the right resulting in permutation 336 . Now 3 begins to move to the right

Ž . Žgiving permutation 337 , and so on until the last permutation 1680 sŽ ..8!r 0!2!2!1!3! is generated.

Let us now introduce some more terminology. The term item is used todenote a particular integer in the multiset, and the integer itself will becalled the ¨alue of the item. Items with the same value are said to besimilar. As in Johnson’s algorithm, every item, except the smallest, has adirection. Observe that no similar items, such as the 2s or the 4s above,move in different directions, and that the relative positions of similar items

Ž .do not change throughout the algorithm. If an item is moving left rightthen the leading similar item and the trailing similar item refer, respectively,

Ž . Ž .to the leftmost rightmost and rightmost leftmost similar item. Finally,since we permit gaps in the values of our items, the predecessor of a ¨alue kis the largest value less than k. For example, if our multiset consists ofonly 2s, 3s, 5s, and 8s, then the predecessors of 3, 5, and 8 are, respectively,2, 3, and 5.

KORSH AND LIPSCHUTZ326

Three arrays are initialized and used that do not change during theexecution of the algorithm: pointer arrays lt most and rt most and integer] ]array pred. We use lt most and rt most to keep track of the leftmost and] ]

w xrightmost item of similar items, and pred k to hold the predecessor of k.Ž .Also, if an item is moving left right , then an adjacent item on the left

Ž .right is called its downward neighbor. Its other neighbor, if there is one, isits upward neighbor. An item is mobile if its downward neighbor is smaller

Ž .than itself. If a mobile item with largest value is moving left right , thenthe mo¨ing item is the mobile item similar to it that is farthest to the

Ž .right left .As the algorithm executes, similar items repeatedly sweep to the left and

then to the right. The leading similar item finishes its sweep when itbecomes the first or last item of the permutation or encounters an itemwhose value is greater than its own. Then each item similar to it finishes itssweep when it encounters the item similar to it that previously finished.Thus all similar items accumulate after the sweep. As each similar itemfinishes, its direction is changed, but only the trailing similar item is putonto the finished list. When these similar items sweep back in the opposite

Ž . Ž .direction, the leading trailing similar item becomes the trailing leadingsimilar item. It will be necessary to know which of these is actually on thefinished list, since, when a predecessor of the trailing similar item moves,all larger items on the finished list become mobile and must be transferredto the mobile list. For example, when item 2 moves to yield permutationŽ .281 in Fig. 4, the rightmost 4 and the 3 are transferred from the finishedlist to the mobile list.

Ž . Ž .Consider permutations 10 and 11 , which were not shown in Fig. 4:

10 11244423Ž .11 11422344Ž .

Since the 4s are sweeping leftward, we can think of the 4s as the 1s and theothers as the 0s:

10 00011100Ž .11 00100011Ž .

Ž .Thus permutation 11 is generated by exchanging the block of two 1s withthe block of two 0s to the right of the mobile 1 and then interchanging themoving 1 with its downward neighbor. Of course it is the block of two 4sthat is actually interchanged with the block 23. Remember that when the4s started out at the right end and the leading 4 was the moving item, its

Ž . Ž .downward neighbor, 3 here, was the rightmost end item in SM 3 and itremains so during the sweep. In order to accomplish the block exchange in

MULTISET PERMUTATION IN CONSTANT TIME 327

Ž .constant time, it is necessary to keep track of this ‘‘end’’ of SM 3 duringŽ .the sweep. That is, if similar items of value k are moving left right , then

Ž .the end item for ¨alue k is the rightmost leftmost item of the subpermuta-Ž w x.tion corresponding to SM pred k . It will have been the downward neigh-

bor of the leading item at the time the leading item started its sweep leftŽ .right .

Three additional arrays are used that need updating during execution:w w xxpointer arrays succ and end and integer array start. Succ pred k points to

w xthe item with value k whenever the item is on the finished list and end kpoints to the end item for value k. If last contains the value of the largest

w x w xitem, then succ last is NULL. Start k indicates whether or not a leadingŽ .item is just starting its sweep just starting to move in its direction . It is at

this time that its end item is updated.A pointer p points to the first item on the mobile list. This is the moving

item, the item which now moves. Item p is blocked if it does not remainmobile after it moves. Item p has an adjacent multiple if it has an upwardneighbor similar to it. Item p is finished if, after it moves, it will be the firstor last item of the permutation or its value does not exceed that of itsdownward neighbor. Item p is direction done if it is finished and it is the

w xtrailing similar item. Initially start k is set to TRUE for each a ) 0. Thekfinished list f contains only the rightmost smallest item and the mobile listm contains all the leading similar items in order with the largest first. Thepermutation is itself a list, perm. The algorithm for generating the nextpermutation follows:

Set p to m.If p is blocked then

remove it from m.If p has an adjacent multiple then

add its upward neighbor to m.w xIf succ p is not NULL then

transfer items whose value islarger than p’s from f to m.

If p is finished thenif p is direction done then

w xadd p to f, set succ p’s predecessorw xto p and set start p’s value to TRUE.

change p’s direction.Shift p.

The last step of the algorithm, shift p, involves updating, when neces-w x w xsary, end p’s value and start p’s value , perm, exchanging blocks, and

transposing p and its downward neighbor. We emphasize that, at any time,some items are on neither the finished nor the mobile list, and those on

KORSH AND LIPSCHUTZ328

FIG. 5. The loopless multiset generation program.

MULTISET PERMUTATION IN CONSTANT TIME 329

Ž .FIG. 5. Continued .

KORSH AND LIPSCHUTZ330

Ž .FIG. 5. Continued .

MULTISET PERMUTATION IN CONSTANT TIME 331

Ž .FIG. 5. Continued .

the finished list are always trailing items and those on the mobile list arealways leading items. The similar smallest items never move. After allpermutations are generated, all trailing items are in order, largest tosmallest, on the finished list and the mobile list is empty.

ŽGenerating the permutations of n distinct integers Johnson’s algo-. Žrithm and the combinations of n things taken k at a time Lehmer’s

.algorithm are special cases. Our constant time implementation of the HTalgorithm, as well as the much simpler implementations of these specialcases, are presented in the next section.

5. IMPLEMENTATION

The permutation is represented as a two way list, perm. Each list itemhas five fields: lt, rt, next, num, and dir. The fields lt and rt, respectively,

KORSH AND LIPSCHUTZ332

FIG. 6. The loopless permutation generation program.

MULTISET PERMUTATION IN CONSTANT TIME 333

Ž .FIG. 6. Continued .

KORSH AND LIPSCHUTZ334

Ž .FIG. 6. Continued .

hold pointers to the adjacent left and right items. In case the item is on thefinished or mobile list, then the field next contains a pointer to the nextitem on that list. The num and dir fields hold the items value anddirection. Each of the three implementations, Figs. 5, 6, and 7, uses thesame function main, which allows data to be entered to specify themultiset and initialize and print the first permutation and then repeatedly

MULTISET PERMUTATION IN CONSTANT TIME 335

FIG. 7. The loopless k of n generation program.

calls the function next perm to generate the next one until m is empty. It]also prints the last permutation and then the number of permutationsgenerated. Next perm implements the HT algorithm of Section 4. The]shift p step is carried out by using the appropriate function, shift left or]shift right.]

REFERENCES

1. J. R. Bitner, G. Ehrlich, and E. M. Reingold, Efficient generation of the binary reflectedŽ .code and its applications, Comm. ACM 19 1976 , 517]521.

Ž .2. J. Chase, Permutations of a set with repetitions, Comm. ACM 6 1970 , 368]369.3. T. C. Hu and B. N. Tien, Generating permutations with nondistinct items, Amer. Math.

Ž .Monthly 83 1976 , 193]196.4. S. M. Johnson, Generation of permutations by adjacent transpositions, Math. Comput. 17

Ž .1963 , 282]285.5. D. H. Lehmer, The machine tools of combinatorics, in ‘‘Applied Combinatorial Mathe-

Ž .matics’’ E. F. Beckenbach, Ed. , pp. 5]31, Wiley, New York, 1964.6. J. M. Lucas, D. Roelants van Barinaigien, and F. Ruskey, On rotations and the

Ž .generation of binary trees, J. Algorithms 15 1993 , 1]24.7. A. Nijenhuis and H. S. Wilf, ‘‘Combinatorial Algorithms,’’ 2nd ed., Academic Press, New

York, 1978.8. F. Ruskey and D. Roelants van Baronaigien, Fast recursive algorithms for generating

Ž .combinatorial objects, Congr. Numer. 41 1984 , 53]62.