
Permutation Generation Methods* ROBERT SEDGEWlCK Program ~n Computer Science and Dwlsmn of Applled Mathematics Brown Unwersity, Prowdence, Rhode Island 02912 This paper surveys the numerous methods that have been proposed for permutatmn enumeration by computer. The various algorithms which have been developed over the years are described in detail, and zmplemented in a modern ALc,oL-hke language. All of the algorithms are derived from one rumple control structure. The problems involved with implementing the best of the algorithms on real com- puters are treated m detail. Assembly-language programs are derived and analyzed fully. The paper is intended not only as a survey of permutation generation methods, but also as a tutomal on how to compare a number of different algorithms for the same task Key Words and Phrases: permutations, combmatomal algorithms, code optimlzatmn, analysis of algorithms, lexicographlc ordering, random permutatmns, recursion, cyclic rotatzon. CR Categories: 3.15, 4.6, 5 25, 5.30. INTRODUCTION tation generation was being used to solve problems. Most of the problems that he Over thirty algorithms have been pub- described are now handled with more so- lished during the past twenty years for phisticated techniques, but the paper stim- generating by computer all N! permuta- ulated interest in permutation generation tions of N elements. This problem is a by computer per se. The problem is simply nontrivial example of the use of computers stated, but not easily solved, and is often in combinatorial mathematics, and it is used as an example in programming and interesting to study because a number of correctness. (See, for example, [6]). different approaches can be compared. The study of the various methods that Surveys of the field have been published have been proposed for permutation gener- previously in 1960 by D. H. Lehmer [26] ation is still very instructive today because and in 1970-71 by R. J. Ord-Smith [29, 30]. together they illustrate nicely the rela- A new look at the problem is appropriate tionship between counting, recursion, and at this time because several new algo- iteration. These are fundamental concepts rithms have been proposed in the inter- in computer science, and it is useful to vening years. have a rather simple example which illus- Permutation generation has a long and trates so well the relationships between distinguished history. It was actually one them. We shall see that algorithms which of the first nontrivial nonnumeric prob- seem to differ markedly have essentially lems to be attacked by computer. In 1956, the same structure when expressed in a C. Tompkins wrote a paper [44] describing modern language and subjected to simple a number of practical areas 'where permu- program transformations. Many readers * Thin work was supported by the Natmnal Science may find it surprising to discover that Foundatmn Grant No. MCS75-23738 ~'top-down" (recursive) and '~bettom-up" Copyright © 1977, Associahon for Computing Machinery, Inc. General permismon to repubhsh, but not for profit, all or part of this materml is granted provided that ACM's copymght notice is given and that reference is made to the publication, to its date of issue, and to the fact that repnntmg privileges were granted by permission of the Association for Computing Machinery Computing Surveys, Vol 9, No 2, June 1977 138 • R. Sedgewick CONTENTS help, but not as much as one might think. Table 1 shows the values of N! for N -< 17 along with the time that would be taken by a permutation generation program that produces a new permutation each micro- second. For N > 25, the time required is far greater than the age of the earth! INTRODUCTION For many practical applications, the 1 METHODS BASED ON EXCHANGES sheer magnitude of N! has led to the devel- Recur~lve methods Adjacent exchanges opment of "combinatorial search" proce- Factorial counting dures which are far more efficient than "Loopless" algorithms permutation enumeration. Techniques Another lterahve method 2 OTHER TYPES OF ALGORITHMS such as mathematical programming and Nested cycling backtracking are used regularly to solve Lexlcograpluc algorlthras Random permutataons optimization problems in industrial situa- 3 IMPLEMENTATION AND ANALYSIS tions, and have led to the resolution of A recurslve method (Heap) several hard problems in combinatorial An lteratlve method (Ives) A cychc method (Langdon) mathematics (notably the four-color prob- CONCLUSION lem). Full treatment of these methods ACKNOWLEDGMENTS would be beyond the scope of this paper- REFERENCES they are mentioned here to emphasize that, in practice, there are usually alter- natives to the "brute-force" method of gen- erating permutations. We will see one ex- ample of how permutation generation can sometimes be greatly improved with a backtracking technique. v In the few applications that remain where permutation generation is really re- (iterative) design approaches can lead to quired, it usually doesn't matter much the same program. which generation method is used, since Permutation generation methods not the cost of processing the permutations far only illustrate programming issues in high-level (procedural) languages; they TABLE 1. APPROXIMATE TIME NEEDED TO GENERATE also illustrate implementation issues in ALL PERMUTATIONS OF N (1 /zsec per permutation) low-level (assembly) languages. In this pa- N NI Time per, we shall try to find the fastest possible way to generate permutations by com- 1 1 puter. To do so, we will need to consider 2 2 some program "optimization" methods (to 3 6 get good implementations) and some 4 24 mathematical analyses (to determine 5 120 which implementation is best). It turns out 6 720 that on most computers we can generate 7 5040 each permutation at only slightly more 8 40320 9 362880 than the cost of two store instructions. 10 3628800 3 seconds In dealing with such a problem, we must 11 39916800 40 seconds be aware of the inherent limitations. 12 479001600 8 minutes Without computers, few individuals had 13 6227020800 2 hours the patience to record all 5040 permuta- 14 87178291200 1 day tions of 7 elements, let alone all 40320 15 1307674368000 2 weeks permutations of 8 elements, or all 362880 16 20922789888000 8 months permutations of 9 elements. Computers 17 355689428096000 10 years Computing Surveys, Vol 9, No 2, June 1977 Permutation Generation Methods • 139 exceeds the cost of generating them. For P[1]:=:P[2] example, to evaluate the performance of to mean "exchange the contents of array an operating system, we might want to try elements P[1] and P[2]". This instruction all different permutations of a fLxed set of gives both arrangements of the elements tasks for processing, but most of our time P[1], P[2] (i.e., the arrangement before the would be spent simulating the processing, exchange and the one after). For N = 3, not generating the permutations. The several different sequences of five ex- same is usually true in the study of combi- changes can be used to generate all six natorial properties of permutations, or in permutations, for example the analysis of sorting methods. In such applications, it can sometimes be worth- P[1] =:P[2] P[2]:=:P[3] while to generate "random" permutations P[1] =:P[2] to get results for a typical case. We shall P[2]-=:P[3] examine a few methods for doing so in this P[1]:=-P[2]. paper. In short, the fastest possible permuta- If the initial contents of P[1] P[2] P[3] are tion method is of limited importance in A B C, then these five exchanges will pro- practice. There is nearly always a better duce the permutations B A C, B C A, way to proceed, and if there is not, the C B A,C A B, andA C B. problem becomes really hopeless when N It will be convenient to work with a is increased only a little. more compact representation describing Nevertheless, permutation generation these exchange sequences. We can think of the elements as passing through "permu- provides a very instructive exercise in the implementation and analysis of algo- tation networks" which produce all the rithms. The problem has received a great permutations. The networks are com- deal of attention in the literature, and the prised of "exchange modules" such as that techniques that we learn in the process of shown in Diagram 1 which is itself the carefully comparing these interesting al- gorithms can later be applied to the per- haps more mundane problems that we face from day to day. DIAGRAM 1 We shall begin with simple algorithms that generate permutations of an array by permutation network for N = 2. The net- successively exchanging elements; these work of Diagram 2 implements the ex- algorithms all have a common control change sequence given above for N = 3. structure described in Section 1. We then The elements pass from right to left, and a will study a few older algorithms, includ- new permutation is available after each ing some based on elementary operations exchange. Of course, we must be sure that other than exchanges, in the framework of the internal permutations generated are this same control structure (Section 2). Fi- distinct. For N = 3 there are 35 = 243 nally, we shall treat the issues involved in possible networks with five exchange mod- the implementation, analysis, and "opti- ules, but only the twelve shown in Fig. 1 mization" of the best of the algorithms are "legal" (produce sequences of distinct (Section 3). permutations). We shall most often repre- sent networks as in Fig. 1, namely drawn 1. METHODS BASED ON EXCHANGES vertically, with elements passing from top A natural way to permute an array of to bottom, and with the permutation se- elements on a computer is to exchange two of its elements.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages28 Page
-
File Size-