V202: Permutations and Combinations

Author(s): F. Beck, T. Lindelöf Library: MATHLIB
Submitter: K.S. Kölbig Submitted: 15.09.1978
Language: Fortran Revised: 07.06.1992

Successive calls to subroutine subprogram PERMU will generate all permutations of a set of integers of total length N consisting of tex2html_wrap_inline132 repetitions of the integer 1, followed by tex2html_wrap_inline136 repetitions of the integer tex2html_wrap_inline138 etc, concluding with tex2html_wrap_inline140 repetitions of the integer m, where tex2html_wrap_inline144 .

Subroutine subprogram PERMUT generates directly a single member of the set of all lexicographically ordered permutations of the first integers tex2html_wrap_inline146 , as specified by its lexicographical ordinal.

Successive calls to subroutine subprogram COMBI will generate all the tex2html_wrap_inline148 possible combinations without repetition of tex2html_wrap_inline150 integers from the set tex2html_wrap_inline152 .

Structure:

SUBROUTINE subprogram
User Entry Names: PERMU, PERMUT, COMBI
Files Referenced: Unit 6

Usage:

Subroutine PERMU:

    CALL PERMU(IA,N)
IA
(INTEGER) One-dimensional array of length tex2html_wrap_inline154 . On entry, tex2html_wrap_inline156 , must contain the initial set of integers to be permuted (see Examples). A call with tex2html_wrap_inline158 will place the set tex2html_wrap_inline160 in IA. On exit, IA contains the "next" permutation. If all the permutations have been generated, the next call sets tex2html_wrap_inline162 .
N
(INTEGER) Length of the set to be permuted.
Subroutine PERMUT:
    CALL PERMUT(NLX,N,IP)
NLX
(INTEGER) Lexicographical ordinal of the permutation desired.
N
(INTEGER) Length of the set to be permuted.
IP
(INTEGER) One-dimensional array of length tex2html_wrap_inline164 . On exit, tex2html_wrap_inline166 , contains the NLX-th lexicographically ordered permutation of the integers tex2html_wrap_inline168 (see Examples).
Subroutine COMBI:
    CALL COMBI(IC,N,J)
IC
(INTEGER) One-dimensional array of length tex2html_wrap_inline170 . The first call must be made with tex2html_wrap_inline172 . This generates the first combination tex2html_wrap_inline174 . Each successive call generates a new combination and places it in the first J elements of IC. If all the combinations have been generated, the next call sets tex2html_wrap_inline176 .
N
(INTEGER) Length of the set from which the combinations are taken.
J
(INTEGER) Length of the combinations.

Examples:

  1. Consider the following set of N=12 objects, only 8 are different:

    displaymath180

    This set consists of m=8 sequences of length tex2html_wrap_inline184 , tex2html_wrap_inline186 , tex2html_wrap_inline188 . Thus, in order to get the possible permutations, set

    displaymath190

    before calling PERMU(IA,12) the first time.

  2. To generate all permutations of N indistinguishable objects, set tex2html_wrap_inline194 , which is equivalent to tex2html_wrap_inline196 , before calling PERMU(IA,N) the first time.
  3. To compute the, lexicographically second, third and last (4!=24) permutions of the set tex2html_wrap_inline200 :
    CALL PERMUT( 2,4,IP) sets tex2html_wrap_inline202
    CALL PERMUT( 3,4,IP) sets tex2html_wrap_inline204
    CALL PERMUT(24,4,IP) sets tex2html_wrap_inline206
  4. To generate and print all 20 combinations of 3 integers from the set tex2html_wrap_inline208 one could write:
        ...
        IA(1)=0
      1 CALL COMBI(IC,6,3)
        IF(IC(1) .NE. 0) THEN
         PRINT *, IC(1),IC(2),IC(3)
         GO TO 1
        ENDIF
        ...

Restrictions:

PERMUT: tex2html_wrap_inline210 .
COMBI: tex2html_wrap_inline212 .

Error handling:

If any of the above conditions is not satisfied, a message is written on Unit 6.

Notes:

  1. If tex2html_wrap_inline214 or tex2html_wrap_inline216 , the subprograms return control without action.
  2. The number of distinct permutations of a set of N numbers which can be decomposed into m groups of tex2html_wrap_inline222 indistinguishable elements is given by

    displaymath224

    where tex2html_wrap_inline226 . This number can become large even for seemingly simple cases, e.g. in Example 1 above,

    displaymath228

tex2html_wrap_inline230

Michel Goossens Wed Jun 5 09:02:37 METDST 1996