~' ~ n c rio n P ERM~UT o A l g o r i t h m 176o qPERMUT[D]V ~ ENUMERATE [01] A G e n e r a t o r fbr P e r r a u % a ~ [ o n s w i t h F i x e d Repetitions J. Berry [02j [03] [043 [053 [063 [07] [083 [09] [iO] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [2~] [25] [26] [27] [28] [29] [30] [31] [32] [33] [34] ALL A L L O C A T I O N S n PARTITIONED OF +/R SUBJECTS, INTO PR N O N - E M P T Y SUBSETS ARGUMENT: R--A N U M E R I C The function P E R M U T generates all possible allocations of +/R objects into PR externally ordered, internally non-ordered, non-empty subsets. If 2*->PR, the allocations corresponding to all R [ i ] ~+ / R combinations are generated; if I + + A / R = I all permutations of PR natural numbers are generated. Argument. P E R M U T takes one argument, vector of the sizes of .,g subsets; e.g., two objects in the first subset, three and two objects in the third, R+2 3 Result. PERMUT returns an exhaustiveset of allocations. Each o u t p u t row is an ordered set of subset numbers to which the ordered items "t+/R are allocated. The total number of permutations is the multinomial coefficient, ( ! + / R ) à / ! R , and may be recovered from PERMUT as global variable COUNT VECTOR CONTAINING THE SIZES OF T H E SUBSETS on RESULT: P--A N U M E R I C VECTOR C O N T A I N I N G THE A S U C C E S S I V E ALLOCATIONS R R, which is a numeric for three subsets with objects in the second, 2. COLS++/R ROWS -I+PR L ~+\~R M+(ROWS,COLS)PO I EXIT+COUNT I+K 0 1 L00PI:+((I I+I)>ROWS)/BEGIN M[I;~L[I]] (R[I]PI),(L-R)[I]PI+I +LOOPI BEGIN:I J I +(COUNT=O)/REPLACE SWITCH: POS i+(A~K¢Ko.=M[I;%L[I]]-I)/zL[I] +(POS=L[I])/REVERSE M[I;POS,POS+I] (I+I),I M[I;IPOS-1] M[I;AM[I;%POS-1]] REPLACE:COUNT COUNT+I P M[1;] L00P2:+((J+J+1)>ROWS)/PRINT P[(P>J-I)/%COLS] M[J;IL[J]] +L00P2 PRINT:D+P +BEGIN REVERSE:M[I;IL[I]]+M[I;AM[I;%L[I]]] Discussion. PERMUT utilizes exchanges of adjacent marks to generate successive allocations. The generation of each allocation depends only on knowledge of the current one, and the first allocation can be generated from the last by applying the same rules for generating any other. Thus, PERMUT is order-preserved, historyless, and cyclic. The K subsets within each permutation are represented by the natural integers IK. Because, in many applications, subscripts are required, the marks may be recoded to the natural integers IN by substituting B+$P for D P in line [31]. In most applications, it is expected that line [31] of PERMUT will be replaced with specific code for some task to be performed, e.g., computation of a permutation statistic. PERMUT may also be modified to store a matrix of all permutations to be passed to another function. To accomplish this, line [31] should be replaced with a lamination routine such as: [31] [31.1] [31.2] [31.3] PRINT:+(COUNT=I)/INITIAL +((IgROWS),(I I+I)>ROWS)/SWITCH,EXIT v R A EXAMPLE R PERMUT I 2 I OUTP OUTP,[1]P +BEGIN INITIAL:OUTP (1,COLS)PP Dept. of Sociology Colorado State University Fort Collins, CO 80523 USA A P L Q u o t e Q u a d 17 3 M a r c h 1987
/lp/association-for-computing-machinery/a-generator-for-permutations-with-fixed-repetitions-Y4EKScG4iu