Jump to content

Tompkins–Paige algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by The suffocated (talk | contribs) at 08:16, 5 June 2012. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The Tompkins-Paige algorithm[1] is a computer algorithm for generating all permuatations of the of a finite set of objects.

The method

Let P and c be arrays of length n with 1-based indexing (i.e. the first entry of an array has index 1). The algorithm for generating all n! permutations of the set {1,2,...,n} is given by the following pseudocode:

P ← [1, 2, ..., n];
yield P;
c ← [*, 1, ..., 1]; (the first entry of c is not used)
i ← 2;
while in do
    left-rotate the first i entries of P;
    (e.g. left-rotating the first 4 entries of
    [4,2,5,3,1] would give [2,5,3,4,1])
    if c[i]<i then
        c[i] ← c[i]+1;
        i ← 2;
        yield P;
    else
        c[i] ← 1;
        ii+1;

In the above pseudocode, the statement "yield P" means to output or record the permutation P. If the algorithm is implemented correctly, this statement will be executed exactly n! times, each with a different permutation P.

This algorithm is not the the most efficient one among all existing permutation generation methods[2]. Not only does it have to keep track of an auxilliary counting array (c), redundant permutations are also produced and discarded in the course of generation.

References

  1. ^ Tompkins, C. (1956). "Machine attacks on problems whose variables are permutations". Proc. Symposium in Appl. Math., Numerical Analysis. Vol. 6. McGraw-Hill, Inc., N.Y. pp. 195–211. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help)
  2. ^ Sedgewick, Robert (1977). "Permutation Generation Methods". Computing Surveys. 9 (2): 137–164.