Vous êtes sur la page 1sur 1

Algorithms L D.

FOSDICK, Editor

'

ALGORITHM 382 if j > 1 then p[j-1} : == 0;


COMBINATIONS OF M OUT OF N OBJECTS [G6) L2:
j := p[jJ > 0 then go to L2;
j + 1; if
PmLLIP J. CHAsE (Reed. 18 Mar. 1969 and 31 Oct. 1969)
i := k ;= j -- 1;
Department of Defense, Fort Meade, MD 20755 L3:

KEY WORDS AND PHRASES: permutations and combina­


i i + 1; if p[ i) = 0 then
: ==

begin p[i) := -1; go to L3 end;


tions, permutations
if p (i] "" -1 then
CR CATEGORIES: 5.39
begin
p(ij := Z :� p[k}; X := i; y := k;
procedure TWIDDLE (x, y, z, done, p); i n tege r x, y, z;
p[k) := -1; go to L4
Boolean done; in t e ge r array p;
end;
comment TWIDDLE can be used (1) in generating all combi­
if i p[O] then begin done1:= true; go to L4 end;
nations of m out of n objects, or (2) in generating all n-length
""

Z := p[j) := p(i); p{i) := 0; X := j; y := i;


sequences containing m l s and (n-m) O's.
'
L4:
In the case (1), suppose then objects are given by an array
end of TWIDDLE
a[l:n), and let us successively store combinations in another
array, say, c[1:m]. For the first combination, c(1J through c[mJ
are equated, respectively, to a[n-m+1l through a[n].
ALGORITHM 383
TWIDDLE (x, y, z, dn o e, p) is called. If done true, then all
=

combinations have been processed and we therefore stop. If PERMUTATIONS OF A SET WITH
not, a new combination is made available by setting c[z] equal REPETITIONS [G6]
to a[x]. TWIDDLE .is called, and we continue on this loop until PBILLIP J. CHAsE (Recd. 4 Aug. 1969 and 13 Feb. 1970)
done = true. Department of Defense, Fort Meade, MD 20755
In the case (2), let the sequences of m l's and (n - m) O's be
stored successively in an integer array, say, b[1:n}. The first KEY WORDS AND PHRASES: permutations and combina­
sequence is obtained by setting b[1} through b[n-m] equal to 0, tions, permutations
and b[n-m+1J through b[nJ equal to 1. TWIDDLE (x, y, z, CR CATEGORIES: 5.39
done, p) is called. If do ne = true, then all required sequences
have been processed, and we therefore stop. If not, a new se­ procedure EXTENDED TWIDDLE (x, y, k, u, done, p);
quence is made available by setting b[xJ equal to 1, and b[yJ value k, u; integer x, y, k, u; Boolean done; integer array
equal to 0. TWIDDLE is again called, and we continue on this p;
loop until done = t.rue. comment EXTENDED TWIDDLE is a generalization both of
m and n are used only in the initialization of the auxiliary TWIDDLE [2], which is used in generating combinations by
integer array p[On : +I], which is done in the main program as transpositions, and of the Trotter-Johnson adjacent-transpo­
follows. (It is assumed that 0 < m ;:; n and 1 ;:; n.) p[O] is set sition permutation algorithms [5, 3}.
equal to n + 1, and pn { +ll is set equal to -2. p{l} through In the main program, to successively store all distinct permu­
p[n-mJ are set equal to 0. p[n-m+ll through pn ( ] are set tations of G[IJ numbers equal to N[Il (I=1 to J) in an array A,
equal, respectively, to 1 through m. If m = 0, then set p[1) take, as the :first permutation, that obtained by dividing
equal to 1. do ne is set equal to false. A.[l:C[1J+ .. +C[JJ) into J intervals and setting the C[IJ
The algorithm has several features which deserve mention. numbers of interval I equal to N[I) (I==1 to J). (We assume
When used in generating combinations: (a) at each stage, only that J > 2 and that each C[IJ � 1. For distinct permutations,
one combination number, namely c[z], .is changed, (b) we need N[/');6N[I"] whenever I' ;.6 I". For somewhat better
TWIDDLE is order preserving in the sense that at each stage efficiency, it is desirable, but not necessary, that the sequence
c[1) through c[m} will equal, respectively, some a[it) through C{I] be non-increasing.)
a[im} where it through i,. are strictly increasing. When used in EX'l'ENDED TWIDDLE (x, y, k, u, done, p) is called. If
generating fixed-density 0-1 sequences: (c) at each stage, it is done = true, then all permutations have been processed and
only necessary to change two numbers of the sequence, b[x} we therefore stop. If not, a new permutation is made available
and bfy}, and these are changed in a specific manner. by transposing A[x} and A.(y), EXTENDED TWIDDLE is
The algorithm underlying this procedure was discovered by called, and we continue on this loop until done = true.
Leo W. Lathroum in 1965. Another algorithm which accom­ EXTENDED TWIDDLE is initialized in the main program.
plishes combinations by transpositions was discovered by k is equated to J, u is equated to C(1} + + C[J] + 1, done
· · ·

Donald E. Knuth in 1964. The author has knowledge of the work is equated to false, and p[O) and p[u] are equated to J + 1.
of Lathroum and Knuth from private communications. He will p[l:u-1} is initialized by setting the members of the Ith in­
include further detail in a mathematical paper, which will in­ terval, of length C[IJ, equal to J - I + 1 (I= 1 to J);
clude justification of this procedure, to be published elsewhere; That the procedure proceeds by transpositions (not neces­
begin integer i, j, k; j := 0; sarily adjacent, this being impossible in general) will introduce
Ll: a special economy in some cases. If this feature is of no value
j :"" j + 1; if p[j};:; 0 then go to L!; in a particular application, then the algorithm of Bratley [1)
if p[j-1] = 0 then or of Sagg [4] might be appropriate. For J = 2, TWIDDLE £2],
begin which also has the transposition feature, will be more efficient
fori := j - 1 step -1 until 2 do p[i] := -1; p[j} -
0; than EXTENDED TWIDDLE. If each C[Ij = 1, then Trotter's
p[l} : z := z := 1; y := j; go to L4
"" algorithm [5} for generating permutations by transpositions,
end; is appropriate.

368 Communications of the ACM Volume 13 I Number 6 I June, 1970

Vous aimerez peut-être aussi