Combinatorial Algorithms
CBMS-NSF REGIONAL CONFERENCE SERIES IN APPLIED MATHEMATICS A series of lectures on topics of...

Author:
Herbert S. Wilf

This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!

Combinatorial Algorithms

CBMS-NSF REGIONAL CONFERENCE SERIES IN APPLIED MATHEMATICS A series of lectures on topics of current research interest in applied mathematics under the direction of the Conference Board of the Mathematical Sciences, supported by the National Science Foundation and published by SIAM. GARRETT BIRKHOFF, The Numerical Solution of Elliptic Equations D. V. LINDLEY, Bayesian Statistics, A Review R. S. VARGA, Functional Analysis and Approximation Theory in Numerical Analysis R. R. BAHADUR, Some Limit Theorems in Statistics PATRICK BILLINGSLEY, Weak Convergence of Measures: Applications in Probability J. L. LIONS, Some Aspects of the Optimal Control of Distributed Parameter Systems ROGER PENROSE, Techniques of Differential Topology in Relativity HERMAN CHERNOFF, Sequential Analysis and Optimal Design J. DURBIN, Distribution Theory for Tests Based on the Sample Distribution Function SOL I. RUBINOW, Mathematical Problems in the Biological Sciences P. D. LAX, Hyperbolic Systems of Conservation Laws and the Mathematical Theory of Shock Waves I. J. SCHOENBERG, Cardinal Spline Interpolation IVAN SINGER, The Theory of Best Approximation and Functional Analysis WERNER C. RHEINBOLDT, Methods of Solving Systems of Nonlinear Equations HANS F. WEINBERGER, Variational Methods for Eigenvalue Approximation R. TYRRELL ROCKAFELLAR, Conjugate Duality and Optimization SIR JAMES LIGHTHILL, Mathematical Biofluiddynamics GERARD S ALTON, Theory of Indexing CATHLEEN S. MORAWETZ, Notes on Time Decay and Scattering for Some Hyperbolic Problems F. HOPPENSTEADT, Mathematical Theories of Populations: Demographics, Genetics and Epidemics RICHARD ASKEY, Orthogonal Polynomials and Special Functions L. E. PAYNE, Improperly Posed Problems in Partial Differential Equations S. ROSEN, Lectures on the Measurement and Evaluation of the Performance of Computing Systems HERBERT B. KELLER, Numerical Solution of Two Point Boundary Value Problems J. P. LASALLE, The Stability of Dynamical Systems - Z. ARTSTEIN, Appendix A: Limiting Equations and Stability ofNonautonomous Ordinary Differential Equations D. GOTTLIEB AND S. A. ORSZAG, Numerical Analysis of Spectral Methods: Theory and Applications PETER J. HUBER, Robust Statistical Procedures HERBERT SOLOMON, Geometric Probability FRED S. ROBERTS, Graph Theory and Its Applications to Problems of Society JURIS HARTMANIS, Feasible Computations and Provable Complexity Properties ZOHAR MANNA, Lectures on the Logic of Computer Programming ELLIS L. JOHNSON, Integer Programming: Facets, Subadditivity, and Duality for Group and Semi-Group Problems SHMUEL WINOGRAD, Arithmetic Complexity of Computations J. F. C. KINGMAN, Mathematics of Genetic Diversity MORTON E. GURTIN, Topics in Finite Elasticity THOMAS G. KURTZ, Approximation of Population Processes

JERROLD E. MARSDEN, Lectures on Geometric Methods in Mathematical Physics BRADLEY EFRON, The Jackknife, the Bootstrap, and Other Resampling Plans M. WOODROOFE, Nonlinear Renewal Theory in Sequential Analysis D. H. SATTINGER, Branching in the Presence of Symmetry R. TEMAM, Navier-Stokes Equations and Nonlinear Functional Analysis MIKLOS CSORGO, Quantile Processes with Statistical Applications J. D. BUCKMASTER AND G. S. S. LuoFORD, Lectures on Mathematical Combustion R. E. TARJAN, Data Structures and Network Algorithms PAUL WALTMAN, Competition Models in Population Biology S. R. S. VARADHAN, Large Deviations and Applications KIYOSI ITO, Foundations of Stochastic Differential Equations in Infinite Dimensional Spaces ALAN C. NEWELL, Solitons in Mathematics and Physics PRANAB KUMAR SEN, Theory and Applications of Sequential Nonparametrics LASZLO LovAsz, An Algorithmic Theory of Numbers, Graphs and Convexity E. W. CHENEY, Multivariate Approximation Theory: Selected Topics JOEL SPENCER, Ten Lectures on the Probabilistic Method PAUL C. FIFE, Dynamics of Internal Layers and Diffusive Interfaces CHARLES K. CHUI, Multivariate Splines HERBERT S. WILF, Combinatorial Algorithms: An Update HENRY C. TUCKWELL, Stochastic Processes in the Neurosciences FRANK H. CLARKE, Methods of Dynamic andNonsmooth Optimization ROBERT B. GARDNER, The Method of Equivalence and Its Applications GRACE WAHBA, Spline Models for Observational Data RICHARD S. VARGA, Scientific Computation on Mathematical Problems and Conjectures INGRID DAUBECHIES, Ten Lectures on Wavelets STEPHEN F. MCCORMICK, Multilevel Projection Methods for Partial Differential Equations HARALD NIEDERREITER, Random Number Generation and Quasi-Monte Carlo Methods JOEL SPENCER, Ten Lectures on the Probabilistic Method, Second Edition CHARLES A. MICCHELLI, Mathematical Aspects of Geometric Modeling ROGER TEMAM, Navier-Stokes Equations and Nonlinear Functional Analysis, Second Edition GLENN SHAFER, Probabilistic Expert Systems PETER J. HUBER, Robust Statistical Procedures, Second Edition }. MICHAEL STEELE, Probability Theory and Combinatorial Optimization WERNER C. RHEINBOLDT, Methods for Solving Systems of Nonlinear Equations, Second Edition ]. M. GUSHING, An Introduction to Structured Population Dynamics TAI-PING Liu, Hyperbolic and Viscous Conservation Laws MICHAEL RENARDY, Mathematical Analysis ofViscoelastic Flows GERARD CORNUEJOLS, Combinatorial Optimization: Packing and Covering

This page intentionally left blank

HERBERTS. WILF University of Pennsylvania

Combinatorial Algorithms: An Update

SOCIETY FOR INDUSTRIAL AND APPLIED MATHEMATICS PHILADELPHIA

Copyright © 1989 by the Society for Industrial and Applied Mathematics. 1098765432 All rights reserved. Printed in the United States of America. No part of this book may be reproduced, stored, or transmitted in any manner without the written permission of the publisher. For information, write to the Society for Industrial and Applied Mathematics, 3600 University City Science Center, Philadelphia, PA 19104-2688. Library of Congress Cataloging-in-Publication Data Wilf, Herbert S., 1931Combinatorial algorithms—an update. (CBMS-NSF regional conference series in applied mathematics ; 55) Update ed. of: Combinatorial algorithms for computers and calculators / Albert Nijenhuis and Herbert S. Wilf, 2nd ed. 1978. Based on a series of 10 lectures given at the CBMS-NSF Conference on Selection Algorithms for Combinatorial Objects, held in the summer of 1987 at the Baca Grande campus of Colorado College. Bibliography: p. Includes index. 1. Combinatorial analysis. 2. Algorithms. I. Nijenhuis, Albert. Combinatorial algorithms for computers and calculators. II. Title. III. Series. QA164.W54 1989 511'.6 89-6070 ISBN 0-89871-231-9

is a registered trademark.

Contents

ix

PREFACE

1

CHAPTER 1. The Original Gray Code

7

CHAPTER 2. Other Gray Codes

17

CHAPTER 3. Variations on the Theme

21

CHAPTER 4. Choosing 2-Samples

23

CHAPTER 5. Listing Rooted Trees

27

CHAPTER 6. Random Selection of Free Trees

31

CHAPTER 7. Listing Free Trees

37

CHAPTER 8. Generating Random Graphs

43

BIBLIOGRAPHY

47

INDEX

vii

This page intentionally left blank

Preface

This monograph is based on a series of ten lectures that I gave at the CBMSNSF Conference on Selection Algorithms for Combinatorial Objects, held in the summer of 1987 at the beautiful place that is the Baca Grande campus of Colorado College. It is intended to be an updating of a book [NW] that Albert Nijenhuis and I first published in 1975 and revised in 1978. That book was a collection of algorithms for listing and choosing at random combinatorial objects from given families, and of certain sizes within those families. For instance, how can we make a list of all of the partitions of the integer n? of all of the partitions of the set [«]? How can we choose uniformly at random a rooted tree from the set of all rooted trees of a given number n of vertices? Much has been done even in the ten years since the appearance of the second edition of our book. In these pages I will try to survey some of this new work. Among the topics to be discussed are progress in Gray codes, in listing subsets of given size of a given universe, in listing rooted and free trees (a subject that had not existed in 1978), and in selecting free trees and unlabeled graphs uniformly at random. We also discuss ranking and unranking problems on unlabeled trees. It will, I hope, be apparent that the subject has continued to develop vigorously. I wish to thank the United States Office of Naval Research for their support of the research that led to this monograph.

ix

This page intentionally left blank

CHAPTER 1 The Original Gray Code

United States patent number 2,632,058, granted March 17, 1953 after having been applied for on November 13, 1947, to "Frank Gray, East Orange, N.J., assignor to Bell Telephone Laboratories, Incorporated, New York, N.Y., a corporation of New York" was for "pulse code communication" and it provided

... in a pulse code communication system, means for translating signal samples into reflected binary code groups of on-or-off pulses, means for transmitting pulse groups to a receiver station, means at said receiver station for converting each received group of said pulses into a conventional binary code pulse group, and means for translating said conventional binary code pulse groups into message signal samples. The inventor, Frank Gray, is described in [Gr] as: "B.S., Purdue, 1911; Ph.D., University of Wisconsin, 1916. Western Electric Company, Engineering Department, 1919-1925. Bell Telephone Laboratories, 1925-. Dr. Gray has been engaged in work on electro-optical systems." His idea was basically the following. Suppose you want to transmit a finite string of bits using an analogue transmission device. You might take the bit string, compute the integer whose bits they are, and transmit a signal of that strength (frequency, or whatever). The problem is that if a small error occurs in the signal strength, a small error will occur in the integer that is received, but a small change in an integer can cause a huge change in its bit string (e.g., between the strings of 31 and 32). The question then arises as to how to associate integers with bit strings in such a way that small changes in the integers produce only small changes in the strings. One way to do this is to list all finite strings of bits in a sequence with the property that each string differs from its successor in only a single bit position; that was the idea of the Gray code. In Fig. 1.1 there is shown a list of all bit strings of length < 4 in Gray code order. For each string we show its rank in the list, as an integer in the range [0,15], the binary digits of its rank, and the bit string itself. i

2

CHAPTER 1

0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111

0000 0001 0011 0010 0110 0111 0101 0100 1100 1101

1111

1110 1010 1011 1001 1000

FIG. 1.1

We will now study some of the properties of the Gray code shown above, which is called the standard reflected Gray code. First the list of strings has a simple recursive description. Let £„ denote the list of all «-bit strings, arranged in (standard reflected) Gray code order. Then we can generate all of these lists as follows. 1. £o is the empty list. 2. For each n = 1,2,..., having formed £ n -i, then (a) write out list Ln-\ and prefix each entry with an additional bit "0"; (b) write out list Ln-\ in reverse order, and prefix each entry with an additional bit "1"; 3. The list £„ is the result of concatenating the two lists that were formed in step 2. The reader should take a moment to check that the list £4 that is shown in Fig. 1.1 can be formed in exactly the way that we have just described. If we denote the reversal of a list £„ by Zn, then the recipe given in steps 1-3 above can be summarized as This method of copying a list and its reversal, with some extra insertions going on, will be a common theme in some of the later constructions also, and it seems to be a good thing to think of when trying to construct some new kind of Gray code. It is now a simple matter to prove by induction that if we start with the empty list and do (1.1) for n > 1 then we will indeed form lists that have the Gray code property: successive entries will differ in only a single bit position. Sometimes we don't want to think of the individual lists £„, but of their "limit" as n —> oo. Notice that the entry of rank m on some list £„ will

THE ORIGINAL GRAY CODE

3

be identical with the entry of that rank on £ n+1 except for an additional leading "0". Since additional leading "0" bits don't change the integer that is represented by the bit string, we can, if it is convenient, think of the entries of a Gray code list as integers encoded by their bit strings. Then the Gray code can be regarded as a permutation of the set of all positive integers that is the union of a family of disjoint bijections of the intervals [ 2 J , 2 J + l - l ] (; = 0,1,...). From that point of view, the Gray code is the single infinite sequence 0,1,3,2,6,7,5,4,12,13,15,14,10,11, 9,8,.... Yet another manifestation of the code results from thinking of the entries as representing neither bit strings nor integers, but sets. The set that a bit string represents is the one whose members are the indices of the positions where the "1" bits appear in the string. Thus "00101011" is the set {1,2,4,6}. In this state of mind, the list £„ is the list of all 2" subsets of [n], arranged so that each one can be obtained from its immediate predecessor by either a single adjunction of a singleton or a deletion of a single element. As if that weren't enough, here's another pretty way to contemplate the world of Gray codes. Fix a positive integer n and imagine a graph G that has 2" vertices, one for each possible string of n O's and 1's. Now connect two of those vertices by an edge of G if and only if the two corresponding strings differ in only a single position. You are now looking at an n-dimensional cube.1 In Fig. 1.2 we show the cube Qy. As we follow down the Gray code list, we take a walk from one vertex of the cube to another, walking always on edges of the cube. The Gray code list £„ encodes a Hamilton walk (actually a circuit) on this graph. The standard binary reflected Gray code that we have been talking about so far is certainly not the only Hamilton path on Qn. There are a lot of them, although their exact number has not been determined (see [Gi]). In fact, the number of them has not even been determined asymptotically. This particular Hamilton path, however, does have a host of elegant properties which justify singling it out for discussion. Hence a Gray code is a list of bit strings, or of integers, or of sets, or it is a Hamilton path on the cube. Next we are going to study the relationships between the bit string of an integer m and the bit string that has rank m in the Gray code. Take m = 9, for instance. The string of rank 9 is 1101 (see Fig. 1.1), and the string of the integer 9 is 1001. Is there some simple rule from which we can construct one of these strings given the other? For instance, what is the string of rank 3982226135 in the Gray code? We have the following remarkable theorem. THEOREM 1.1. Let m = £)e,2' be the binary representation of the integer m, and let... e^e^o be the string of rank m in the Gray code. Then

1

Graph theorists call this graph Qn ("Q" as in Qbe, after all).

4

CHAPTER 1

FIG. 1.2. The Gray code £3 as a walk on Q$.

Proof. By induction on n we will show that the result holds for all m that occur on the list £„. This is extremely clear when n = 0. Suppose (1.2) holds for all strings on the list £,n-i, and now consider the string of rank m on the £„. If m < 2""1 - 1 then there is nothing more to prove because the string of rank m is unchanged. Hence suppose m > 2"~', and write m' = 2" - 1 - m. Then (1.2) holds for the integer m' and the string that has rank m' because m1 < 2"-1. But the bits in the strings of ranks m and m' are related by

and the bits in the binary representations of the integers m and m' are related by It is now immediate to check that (1.2) continues to hold for the integer m, completing the proof. D Exercise. Now, what is the string of rank 3982226135 in the Gray code? It is easy to invert (1.2) in order to express the e's in terms of the e's. We obtain

We summarize with the following theorem. THEOREM 1.2. The bit string ...e^e^ has rank m in the Gray code list, where the binary digits of m are given by (1.3). Conversely, ifm is a given integer, then the bits in the string of rank m in the Gray code are given by (1.2).

THE ORIGINAL GRAY CODE

5

In general, for any list of combinatorial objects, the ranking problem is the question of determining the position in the list of some given object. The unranking problem gives the rank and asks for the object of that rank. Hence we may say that for the Gray code list, equation (1.3) solves the ranking problem and (1.2) solves the unranking problem. Next, at the outset we stated that one of the motivations for the Gray code ordering was that if the ranks of two objects on the list are close then the objects themselves differ in rather few bit positions. By the Hamming distance between two bit strings we mean the number of bit positions in which they differ. Thus consecutive elements of the Gray code list have Hamming distance 1. The question is this: Given a positive integer m, how close can two objects be to each other on the Gray code list if their Hamming distance is ml Symbolically, we are asking for

where p is the rank function. For the given m, among all pairs s, t of strings that achieve the minimum in (1.4), choose a pair of minimum length, say of length L. Then the leftmost bit of s is different from the leftmost bit of t, for otherwise we could delete those bits and obtain shorter strings s', t' that are still at a distance m, while which would be a contradiction. Now let q > 2 be the first bit position, reading from the left, in which s and t have the same bit present. Suppose we construct 5', t' by deleting that #th bit from each of .s and t. Then the Hamming distance remains unchanged and the strings are shorter. To obtain a contradiction we must show that (1.5) still holds. What happens to the rank of s when we delete the qih bit? Suppose that bit is 0. Then from (1.3), the binary digits of the rank of s are unaffected except that one of those digits has been deleted. The same happens for the rank of t, so the difference of their ranks does not change, and we have the contradiction. If the common bit in position q is a 1 then, according to (1.3) again, if we delete it then we don't affect any of the bits of the rank that are more significant than the #th, and every one of the bits that is less significant is reversed. Hence again both ranks change by the same amount under the deletion, and the difference is unaffected. It follows that s and t differ in every bit position. Hence in seeking the minimum (1.4) we may confine attention to complementary pairs s, t, that is, to pairs in which each of the L bits of the string t is the complement of the corresponding bit of 5. We will write t = s. Evidently we now have L = m (recall that m is the Hamming distance between the two strings), so s is a string of length m. We standardize by supposing that s is that one of the pair s, s that has a "1" in the leftmost bit position, i.e., s is the one of higher rank. Next we

6

CHAPTER 1

look at (1.3) to see the relationship between the ranks of s and of.?. We see that the bits of their ranks are alternately the same and opposite! Precisely, we have (we let p be the rank function)

and

and therefore the difference between their ranks is

where the terms continue as long as the exponents are nonnegative. It is a simple matter to sum the above series and obtain the following theorem. THEOREM 1.3. Two strings on the Gray code list whose Hamming distance is > m have ranks that differ by at least [2m/3]. The bound is best possible. D The above result is essentially due to Yuen [Yu].

CHAPTER 2 Other Gray Codes

The idea of the Gray code has found applications in combinatorial families other than the family of subsets of a set. In any combinatorial family where we want to make a list of all of the objects in such a way that only the smallest possible change takes place as we go from each object to its successor, we can speak of a (generalized) Gray code. It should be remarked that the concepts are meaningful only if we apply them to encodings of families of objects rather than to the underlying objects. More precisely, it may be possible (and usually is) to encode one and the same family of objects in several different ways. We might expect that a Gray code scheme will work on one of these encodings, but it might not provide minimal changes in any other one of the encodings. A small example will illustrate the point. Suppose n is fixed and we consider the permutations of n letters. If we generate the list of all permutations of n letters by applying, at each stage, a single transposition to get from one to the next, then it is reasonable to expect that we will have a Gray code for permutations. Suppose, however, that instead of encoding permutations by their values, we choose to encode them by their cycles. Then as we apply a single transposition, large changes may take place that aren't at all in the spirit of the minimal changes that we expect from Gray coding. By the way, I can't resist pointing out that there might be an interesting Gray code for permutations in cycle form, but I don't know what it is. In contrast to that example, let's think about subsets of a set again. The original Gray code was designed for the bit-string encoding of a subset. Suppose, instead, we use the list-of-members encoding. Do we still have the minimal change property? Indeed yes, because each set will be obtained from its predecessor by either deleting or adjoining a single member, and that is about the most minimal change we could expect. The point is that sometimes Gray codes have pleasant properties with respect to more than one encoding of a combinatorial family, but don't count on it.. For a first example of a Gray code applied to a family other than 0-1 strings, suppose we are given two positive integers n and k (k < n), and we want to make a list of all of the /c-subsets of [n]. Let's adopt the encoding in which we simply display the list of members of each ^-subset. Now it's quite out of the question to have each item on the list differ from its predecessor by a single adjunction or deletion, because the cardinalities of the subsets are supposed to be kept fixed at k. The "Gray-est" thing we 7

8

CHAPTER 2

can hope for, then, is to have each subset formed from its predecessor by two operations, a deletion and an insertion. In [NW] such an algorithm was given, and many others are known. We called ours a "revolving door" (RD) algorithm, for presumably obvious reasons, and it goes like this. x Let A(n, k) denote the list of all fc-subsets of [n], arranged in RD order, where the first set in the list is {1,2,..., k}, and the last one is {1,2,..., k l,n}. Then we form the lists A(n,k) recursively, as follows:

In words, the list A(n,k) is formed by writing down the list A(n -l,k) and then following it with the list that is obtained from A(n - \, k - 1) by first writing it down in reverse order and then adjoining the singleton {«} to each set on that list. The recursion has the same music as the Pascal triangle, i.e., the same three grid points (n, k), (n -1, k), and (n—I, k -1) are involved at each step. Hence the boundary conditions that are naturally needed for (2.1) to boot itself up will be the same kind that are needed by the binomial coefficients. Therefore we add to (2.1) the conditions that the list A(n, k) is empty if k < 0 or if k > n. The reader should then work out a few of these lists, for instance, enough to verify that the list A(4,6) of 4-subsets of [6] is as follows.

1 1 2 1 1 1 2 1 3 2 1 1 2 1 1

2 2 3 3 2 2 3 3 4 4 4 2 3 3 2

3 4 4 4 3 5 5 5 5 5 5 4 4 4 3

4 5 5 5 5 6 6 6 6 6 6 6 6 6 6

Here are some of the general principles of Gray code construction that already appear in this example. First, the general step is recursive, and is carried out by stitching together some earlier lists to make the next one. Second, there is the list reversal in the second part of the recurrence. The reason it is there is to smooth out the transition between the two portions of the recurrence. To put it a little more graphically, in the construction (2.1) we see a list, a comma, and another list. The hard part is at the comma. Indeed, inductively we can suppose that the first list is already in some Gray code sequence, and the second sublist is also. The difficulty comes in

OTHER GRAY CODES

(1,2,3) (1,2,4) (1,2,5) (1,2,6) (1,5,6) (1,4,6) (1.4,5) (1,3,5) (1,3,6) (1,3,4)

9

(2,3,4) (2,3,5) (2,3,6) (2,5,6) (2,4,6) (2,4,5) (3,4,5) (3,4,6) (3,5,6) (4,5,6)

FIG. 2.1. Strong revolving-door order.

gluing them together so the transition from the first to the second also has just a minimal change; in this case one element enters and one leaves. In order to prove that (2.1) does what it is supposed to do the reader will find that it is necessary to do more than just make the inductive assumption that the earlier lists A(n - \,k) and A(n - \,k - 1) are in minimal change order. In order to prove that the new list A(n, k) is also in Gray code order we have to know what the first subset and the last subset on the earlier lists are, so as to be able to prove that there is no bumpiness across the comma (again, what that means is that as we go from the last fc-subset that appears on the first of the two sublists that appear on the right of (2.1) to the first of the fc-subsets that appears in the second one of those two sublists, only one element gets deleted from the set and one element gets added). Exercises. (1) Find a simple and elegant nonrecursive implementation of the RD algorithm by describing a successor algorithm. Precisely, given a particular ^-subset S of [n], and given only that, find the next set in the list A(n, k). (2) Prove that (2.1) does indeed provide a recursive construction of the revolving door lists, by building into the inductive hypothesis not only that the earlier sets are Gray codes, but also what they begin and end with. To "peek" at the answers, see page 28 of [NW]. Although the lists A(n,k) satisfy the revolving door condition, there are minimal-change algorithms for fc-subsets that are even more severely restricted. Suppose we specify a subset by giving its members in ascending order: Then the subset S is represented by a ^-vector whose components strictly increase. Let's define the minimal-change condition by requiring that two ^-subsets that are consecutive on our list may differ only in a single component of their membership vectors. This is stronger than the revolving-door condition, but it can be satisfied. In Fig. 2.1 are the 20 3-subsets of [6] arranged in that way.

10

CHAPTER 2

If B(n, k) denotes this list of all of the fc-subsets of [n] in strong revolving door order, then the recursive construction of these lists, due to Eades and McKay [EM], is given by Lest we should think that Gray coding questions that sound reasonable always have affirmative answers, here is yet a third way to think about making a Gray code for ^-subsets of a set, where the answer turns out to be complicated. Suppose we are permitted to alter the status only of two adjacent elements. That is, at each stage we can delete / and adjoin i + 1, or we can delete i + 1 and adjoin /, and furthermore we regard elements 1 and n as being adjacent. Then is it possible to construct the lists? This problem has been studied by Joichi and White [JW], Buck and Wiedemann [BW], Eades, Hickey, and Read [EHR], and Ruskey and Miller [RM], and the answer is given by the following theorem. THEOREM 1.4. There is an adjacent interchange Gray code for k-subsets of [n] if and only if either k has one of the four values 0, 1, n - 1, n or else n is even and k is odd. Next we leave the subject of Ar-subsets of a set, and turn to that of the compositions of an integer. If, once more, n and k are given positive integers, then by a composition of n into k parts we mean an ordered representation of n as a sum of exactly k nonnegative integers. For our present purposes it is convenient to adopt the language of balls and boxes. Hence suppose we have k labeled boxes and n unlabeled balls. Then there is obviously a bijection between compositions of n into k parts and arrangements of the balls in the boxes. It is well known, and easy to see, that there are exactly (n+kn~l) such arrangements, and it isn't very hard to figure out ways of making lists of all of them. But now let's add the Gray code restriction to make things a little more interesting. By a Gray code for compositions of n into k parts we mean a list of all of those compositions, sequenced so that each arrangement of n balls in k boxes arises from its immediate predecessor by moving exactly one ball from one box to another. In the first edition of [NW] we asked if such an algorithm could be constructed, and Donald Knuth [Kn2] found the elegant solution that we will now describe. Let £(n, k) denote the desired lists. We will construct them recursively, for n fixed, and k = 1,2,.... First, the list L(n, 1) has just one entry, namely, the composition n = n of n into one part. If, inductively, the lists L(n,j) (j = \,2,...k) have been constructed, then

OTHER GRAY CODES

11

where the last term will be the one that contains £(0, k). The general approach that we described earlier for building Gray codes is shown here in full flower. The flip-flopping of the lists ensures smooth transitions across the commas, and the rest is taken care of inductively. Here is the list of the 21 compositions of 5 into three parts as they appear on the list £(5,3). Can you follow the bouncing ball? Just to the right of each composition on the list we show the jump that the ball made in the form of the number of its origin and destination boxes, starting from the composition 5+0+0. 4

3 2 1 0 0 1 2 3 4 3 2 1

0 0 1 2 1 0 0

1

2 3 4 5 4 3 2 1 0 0 1 2 3 2 1 0 0 1 0

0 0 0 0 0

1 1 1 1 1

2 2 2 2 3 3 3 4 4 5

1^2 1 ->2 1 -+2 1 -*2 1 ->2 2 ->3 2 -» 1 2 -» 1 2 -» 1 2 -f 1 1^3 1 ^2 1 -+2 1 ^2 2 -»3 2 -» 1 2 -f 1 1 ^3 1 -+2 2 ^3

Here is another example of the genre, whose origins are similar. That is to say, Nijenhuis and I asked if, for n fixed, all of the partitions of a set of n elements could be arranged in Gray code sequence, in the sense that as we pass down the list from a set partition to its successor, exactly one element moves from one equivalence class to another (the rule permits an element to leave a certain class and start a singleton class of its own). The answer was again found by Knuth, in a recursive form that will now be described. A set partition is described by giving a list of the members of each of its classes. We will suppose that the classes are given in ascending order of their smallest elements. For a fixed partition P of [n] we construct the list of its children. These are the partitions of [« + 1] that are obtained by successively placing the letter n + 1 into each of the classes of P in turn, and then placing n + 1 into its own singleton class. Thus, the children of the partition (1,3)(2,4,5) of [5] are the three partitions (1,3,6)(2,4,5), (1,3)(2,4,5,6), and(l,3)(2,4,5)(6)of [6].

12

CHAPTER 2

Inductively, suppose we have before us the list of all partitions of the set [n] arranged in a Gray code order so each partition is obtainable from its predecessor by a single move of one letter from one class to another. We make the corresponding list of partitions of [n + 1] as follows. Write down the children of the first partition on the list. Then write down in reverse order the children of the next partition. Then create the children of the next partition, then the reverse ... etc., and that's it. For example the partitions of [2] are (1,2) and (1)(2). Hence the list of partitions of [3] that we would create is (1,2,3), (1,2)(3), (1)(2)(3), (1)(2,3), (1,3)(2). It should be noted that when the complete lists are needed, the recursive form of these algorithms is sufficient. If, however, we want to produce the objects one at a time without storing them all at once, then nonrecursive versions may be helpful. Nonrecursive implementations of Knuth's algorithms have been given by Klingsberg [Kl] and Kaye [Ka]. Gray code constructions don't always work so smoothly. Consider, for a fixed positive integer n, the problem of arranging all of the partitions of the integer n in Gray code order. What we mean is that each partition should be obtainable from its predecessor by diminishing one part by 1 and adding 1 to another part (the latter to include creating a new part =1). This problem was treated by Yoshimura [Yo], who gave such lists for n = 1,2,.... 11. It has been completely solved by Carla D. Savage [7. Algorithms, to appear]. Here, for instance, is a list for n = 10: 10, 9+1, 8+2, 7+3, 6+4, 5+5, 5+4+1, 4+4+1+1, 4+4+2, 4+3+3, 3+3+3+1, 3+3+2+2, 3+3+2+1+1, 4+3+2+1, 5+3+2, 5+3+1 + 1, 6+3+1, 6+2+2, 5+2+2+1, 4+2+2+2, 3+2+2+2+1, 2+2+2+2+2, 2+2+2+2+1 + 1, 2+2+2+1 + 1 + 1 + 1, 3+2+2+1 + 1 + 1, 4+2+2+1 + 1, 4+3+1 + 1 + 1, 3+3+1+1+1 + 1, 3+2+1+1 + 1 + 1 + 1, 4+2+1 + 1 + 1 + 1, 5+2+1 + 1 + 1, 6+2+1+1, 7+2+1, 8+1 + 1, 7+1 + 1 + 1, 6+1 + 1+1 + 1, 5+1+1+1 + 1 + 1, 4+1 + 1 + 1+1+1+1, 3+1+1 + 1+1 + 1 + 1 + 1, 2+2+1+1+1+1 + 1 + 1, 2+1 + 1 + 1+1 + 1 + 1+1 + 1, 1+1+1 + 1+1 + 1+1 + 1+1+1. Here is one last Gray coding problem that has received attention recently. Consider, for n fixed, the triangulations of a labeled n-gon. This is a wellknown Catalan-countable family of objects. In that family we will now define an elementary operation, called a flip, which maps a triangulation onto another one. To do a flip, select a quadrilateral, delete the diagonal that it has, and draw the other one. In Fig. 2.2 we show a flip in a triangulation of a hexagon. The question, of course, is whether it is possible to arrange the set of all C/,-1 of these triangulations in a sequence with the property that each member of the sequence is a flip of its predecessor. Such an arrangement of the 5 triangulations of a pentagon is shown in Fig. 2.3, where, in each case, the edge that will get flipped is marked.

OTHER GRAY CODES

FIG. 2.2. A flip.

FIG. 2.3. Flipping through the triangulations.

13

14

CHAPTER 2

This question is not only pretty, but it relates, via the familiar family of bijections that map Catalan-countable problems onto each other, to other questions about other families. In the case of binary trees, it turns out that the motion of flipping triangulations maps into an operation called rotation of a binary tree, and so the question concerns the possibility of arranging all binary trees of « internal nodes in sequence so that each can be obtained from its predecessor by a single rotation. Here is a precise description of a rotation of a binary tree with respect to an edge (caution: it does not mean interchange of the right and left subtrees at some vertex). In Fig. 2.4 we show a binary tree before and after a flip with respect to the edge x - y. The symbols T\, TI, T^ denote the left and right subtrees at y and the right subtree at x, respectively. It may (or may not) help to visualize the operation by thinking of the tree as a mobile that hangs from a point of attachment at x. Then pick it up at y, lifting y above x. Then let the subtree TI slide down the string to become the left subtree of x, which wouldn't otherwise have one.

FIG. 2.4. A rotation in a binary tree.

Anyway, the operation of tree rotation is used in computer science to balance trees that are being used as data structures. The connection between the flips of triangulations and rotations of binary trees appears in the thesis of Lucas [Luc], who also gave the first proof of the existence of these Gray codes. Her successful solution of this problem involved overcoming a number of subtle difficulties. Weight sequences for binary trees were introduced by Pallo. They associate with a binary tree of n internal nodes, a sequence (w\,...,w n) that is constructed as follows: visit the internal nodes of the tree in inorder, which is to say, visit the left subtree, the root, and the right subtree, in that order. For each i = 1,«, as the /th internal node is visited, let tt>, denote the number of leaves in the left subtree at that node. A binary tree and its weight sequence are shown in Fig. 2.5. Observe that if a binary tree is obtained from another

OTHER GRAY CODES

15

FIG. 2.5. A binary tree of weight sequence (123116).

one by a single rotation, then its weight sequence differs from the original in only a single component.

This page intentionally left blank

CHAPTER 3 Variations on the Theme

Here is a variant of the idea of a Gray code. For fixed n, we want to list the permutations of Sn in such a way that each permutation will be totally different from its immediate predecessor. What that means is that if a and T are consecutive on the list, then for all i: a(i) / r(i). We can restate the condition by saying that whenever a and T are consecutive, then 1, where "(•)" denotes the "nearest-integer" function. There is a theorem of Jackson2 [Jal] which asserts that a 2-connected graph of N vertices in which each vertex has degree > A7 3 is Hamiltonian. It is easy to check (see below) that the Cayley graph of the symmetric group with respect to the set of fixed-point free permutations is connected for all n ^ 1,3 and is 2-connected3 [Im] for all n > 4. Further since

the hypotheses of Jackson's theorem are fulfilled for n > 4, and a Hamiltonian path exists. We do not know of any simple algorithm for an algorithmic description of such a path. 2 3

My thanks to Dr. Linda Lesniak for this reference. I thank Dr. Yahya Hamidoune for this reference. 17

18

CHAPTER 3

In the case n = 4, here is a list of the 24 permutations in which each one has no common values with its predecessor: 1234, 2143, 3214, 4321, 3412, 2341, 3124, 1432, 2314, 3421, 4132, 1423, 2134, 3241, 4312, 1243, 2431, 3142, 4213, 1324, 2413, 4231, 1342, 4123. Most of the interesting questions about Cayley graphs and their Hamiltonicity involve extremely sparse graphs, and for those we have no general theorems. The redeeming quality that these graphs do have, however, is their symmetry. Hence, what the world needs are some theorems to the effect that if a graph is somehow symmetrical enough, then it has a Hamilton path. A question that was raised in 1969 by Lovasz [Lov] addresses this need directly. Say that a graph G is vertex-transitive if for every pair v, w of vertices of G there is an automorphism a of G that carries v into w (the vertices of G are "all alike"). The question asks if it is true that every connected, vertex-transitive undirected graph has a Hamilton path. The graph (?„ in our little example above is certainly vertex-transitive, so if we can show that it is connected (we can), then an affirmative answer to Lovasz's question would imply that it is Hamiltonian also. In fact, it would imply that every Cayley graph with respect to a set of generators that is undirected, in that the inverse of each generator is also a generator, is Hamiltonian. The question that Lovasz asked has not been answered, but progress has been made, nonetheless. A survey article of Alspach [Al] reviews some of these partial results. Also, Babai [Ba] proved that every connected vertextransitive graph on n > 4 vertices has a cycle longer than V3n. Jackson [Ja2] showed that every 3-connected cubic graph has a cycle of length \V(G)\*, where t = .69.... Watkins [Wat] proved that if p(G) is the minimum vertex degree of G, and K(G) is its vertex connectivity, then l.u.b. p(G)/K(G) is exactly 3/2, where the l.u.b. is taken over all connected, vertex-transitive graphs G. In [NW], by the way, we described a directed Cayley graph that is not Hamiltonian. Consider the symmetric group as being generated by just two generators: a cyclic right-shift of one unit, and an interchange of the first and second values. We found that the Cayley graph here is Hamiltonian if n = 1,2,3,4, but not if n = 5. The status for n > 6 is unknown. THEOREM 3.1. The symmetric group Sn is generated by the fixed-point free permutations if and only ifn^l, 3. Proof. Since Sn is generated by the transpositions, it will be enough to decide when the transpositions are generated by the fixed-point free permutations. But if any single transposition is so generated, then they all are. Hence Sn is generated by the fixed-point free permutations if and only if the single transposition

is a product of fixed-point free permutations.

VARIATIONS ON THE THEME

19

First, let n be odd, n > 5, and let

Then r"~2 = 6. With T as in the previous case, define

Then cor""4 = a, which completes this case. If n = 4 let o)\ in cycle form be (1432) and let (02 in cycle form be (1324). Then o>2wf = a. The case n = 2 is trivial. By drawing G$, we see that it has two connected components, so all assertions have now been proved. D The following result is stronger and is also easier to prove. THEOREM 3.2. Fix n > 5. Let C be a fixed conjugacy class of the symmetric group Sn. Then C generates Sn if and only ifC contains an odd permutation. Proof.4 Consider the subgroup H that C generates. Then clearly H is normal in Sn. Suppose H is a proper subgroup. If n > 5 the only proper normal subgroup that Sn has is the alternating group An. If C contains an odd permutation then H cannot be An so it must be all of Sn. Conversely, if C consists of even permutations only, then so does H, and so it cannot be the full symmetric group. D

4

This pretty proof was found independently by two of my colleagues, Drs. Gerstenhaber and Nijenhuis.

This page intentionally left blank

CHAPTER 4

Choosing 2-Samples

This chapter contains a small technical procedure that will be of use in Chapter 6, where we will be selecting random free trees. The problem is quite simple to state. Given a device that will produce, on demand, one of N given objects, uniformly at random (u.a.r.), it is required to produce, u.a.r., one of the (N^1} unordered pairs (= "2-samples") of the objects. The objects are to be thought of as complicated, so it may be expensive to ask if two of them are different or the same. We will give two methods, of which the first is really here only to define the problem a little better, and the second is the one that works. Method 1. Choose two objects independently u.a.r. and output the pair. If we use this method then while for A ^ B, so pairs of distinct elements are chosen twice as often as the others. If we attempt to rectify this by rejecting such pairs occasionally, then we run into the problem of recognizing these pairs at all, which we have hypothesized as being expensive to carry out. Method 2. For a fixed real number p, (Al) with probability p do choose an object a> and output {co, a>} or (A2) with probability 1 - p do choose 2 objects (a/, a/') independently u.a.r. and (without examining them further!) output the pair {CD', to"}. D Now let A be some fixed object. What is the probability that Method 2 produces the output {A,A} (note that either (Al) or (A2) can give this result)? It is Next suppose that A, B are two distinct objects. Then what is the probability that the unordered pair {A, B} will be output? It is These two probabilities are equal if we use the p that satisfies 21

22

CHAPTER 4

i.e., if p = \I(N +1), and we have the following algorithm.

ALGORITHM 2-SAMPLE.

With probability l/(N + 1), choose an object &> u.a.r. and output the pair {co, G)}, else, With probability N/(N + 1), choose two objects w!, to" independently u.a.r. and output the pair {&>',«"}. D

CHAPTER 5 Listing Rooted Trees

In the years since [NW] appeared there has been a great deal of work on algorithms for selection of trees. In particular, there are now efficient algorithms for listing all rooted trees of given size [BH], listing all free trees of given size ([Re], [DZ], [Ko], [WROM]), choosing uniformly at random a free tree of a given number of vertices [Wi], and ranking and unranking rooted and free trees ([WY], [Yo]). In this chapter we discuss the problem of listing rooted trees in constant average time. The results are due to Beyer and Hedetniemi [BH]. If n is fixed, their algorithm will produce a list of all rooted trees of n vertices where, in a certain encoding, the average labor per tree is uniformly bounded, as a function of «. In their scheme, rooted trees are encoded by means of their level sequences. To define the level sequence of a tree we must first define it for an ordered tree. In that case we begin by visiting its vertices in preorder, which means (a) visit the root, and then (b) visit the subtrees in order. While the visitation is going on we must note the level on which each vertex lives at the time we visit it. The root is defined to live on level 1. In general, the level of a vertex is equal to the number of vertices in the path that joins it to the root. The result of visiting the vertices in preorder and writing down the levels on which they live is the level sequence of the given ordered tree. In Fig. 5.1 we show an ordered tree and its level sequence. The vertices of the tree are unlabeled but we show next to each vertex the epoch at which it gets visited. The appearances of the 2's in a level sequence of a tree T are important. If a 2 appears in a certain position, then we have arrived at the level just below the root, i.e., we are at the root of one of the subtrees at the root of T. Hence all entries in the level sequence, beginning with that 2, and ending just before the next 2, comprise the level sequence of a single root subtree of T. An ordered tree of course comes equipped with an ordering of its subtrees. The level sequence provides another, natural, ordering of all subtrees. Precisely, let U and V be subtrees of an ordered tree T. The level subsequence L(U) of U is the (consecutive) subsequence of L(T) that belongs to vertices of U. The level subsequences of all subtrees of T can be ordered lexicograph23

24

CHAPTER 5

FIG. 5.1. An ordered, rooted tree, with its visitation and level sequences.

ically. Then we can say that U < V if L(U) < L(V) in that ordering. In general, if T is some given rooted tree, then there will be many ordered trees T' such that T' is an ordering of T. We will now specify a single ordered tree T* to be the canonical ordered tree that corresponds to T. It is the one ordered tree, among all of those that correspond to T, whose level sequence is the largest, in the lexicographic ordering of level sequences. In Fig. 5.2 we show two ordered trees that have the same underlying rooted tree T, and in fact, they are the only two ordered trees that T has. Their level sequences are, respectively, (1,2,3,3,2) and (1,2,2,3,3), of which the first is the larger. Hence T\ is the canonical ordered tree that corresponds to the rooted tree T, and we write TI = T*.

FIG. 5.2. T corresponds to these two ordered trees.

We say that two subtrees of an ordered tree are adjacent if their roots are two consecutive children of the same parent vertex. In a level sequence L(T), the subsequences that belong to two adjacent subtrees are two consecutive blocks.

LISTING ROOTED TREES

25

We say that a level sequence of a rooted ordered tree is regular if whenever U and V are two consecutive (in that order) adjacent subtrees then L(U) > L(V). It is then easy to prove the following proposition. PROPOSITION. An ordered tree is the canonical ordering of a given rooted tree if and only if its level sequence is regular. In that case, if two consecutive elements of the level sequence are equal to 2, then all remaining elements are equal to 2. D Now we're just about ready to write down the algorithm for generating rooted trees of given size. If n is given, then what we are going to do is to write down the level sequences of all canonical ordered trees that correspond to rooted trees of n vertices. In other words, the encoding of trees that is used is that a rooted tree is encoded by the level sequence of its associated canonical ordering. What we would like to avoid is writing down every possible level sequence and discarding the ones that aren't regular. Instead, Beyer and Hedetniemi [BH] found a way to hop directly from one of those level sequences to its successor. It hinges on the positions of the 2's in the original sequence. Let a level sequence L = (l\,li,...,ln) be given. If L contains no entry > 2 then the algorithm halts and all trees of n vertices have been generated. Else, let lp be the rightmost entry of L that is > 2. Let lq be the rightmost position preceding p such that lq = lp-\ (vertex q is the parent of vertex p). The successor s(L) of the level sequence L is defined by

What this amounts to is that we take a certain subtree and make enough copies of it to fill in the n entries of the level vector, including possibly a partial copy at the end. We show in Fig. 5.3 some examples of canonical level sequences and their successors. L(T0

s(L(T))

(1,2,3,2,2,2) (1,2,2,2,2,2) (1,2,3,4,2,2) (1,2,3,3,3,3) (1,2,3,4,5,5,2,2,2,2) (1,2,3,4,5,4,5,4,5,4) (1,2,3,3,2,3,3) (1,2,3,3,3,3,3) FIG. 5.3. Examples of successors.

This page intentionally left blank

CHAPTER 6

Random Selection of Free Trees

In [NW] we discussed how to choose, uniformly at random, a rooted tree of a given number n of vertices. It isn't hard to extend this to an algorithm for choosing, u.a.r., a free (i.e., unlabeled and unrooted) tree of n vertices, so we will discuss that extension here, following [Wi]. Hence we assume that Algorithm Ranrut of [NW] is available, and we will use it here as a subroutine. The main point is that free trees have hidden "roots," called centroids. It turns out that every free tree has exactly one or two of these distinguished vertices, and that they can serve as roots for Algorithm Ranrut. We remark here that Algorithm Ranrut has unknown (to me, anyway) average complexity. It seems that it runs in linear or just slightly superlinear time, but the analysis appears to be difficult. I am willing to make a formal conjecture that, for every e > 0, Algorithm Ranrut will select a rooted tree of n vertices in average time O(nl+c), and perhaps even e = 0. To get back to free trees, we will first define a centroid. For each vertex v of a free tree T we let w(v), the weight of v, denote the size of the largest subtree that is rooted at v. A centroid v* is a vertex of minimum weight. The main properties of centroids are (see [Knl] for proofs) the following: (1) Every tree T has exactly one or exactly two centroids; (2) T has two centroids if and only if these are joined by an edge of T, and removal of that edge would leave two trees of equal sizes; (3) A vertex X is the unique centroid of a tree T of n vertices if and only if w(X) is Euler's function. The appearance of the "l/«" in front of this formula means that the sum by itself does overcounting that needs to be compensated for. Consequently, it will be hard to use the derivation of this formula as a model for an algorithm that would produce a list of all (n, k) necklaces. Indeed, such an algorithm would involve examining each necklace as it is produced and checking somehow to see if it had previously been produced. Hence there are considerable difficulties involved in listing necklaces of given type. As far as I know there are no known loop-free algorithms for doing so. That is, they all engage in some following of blind alleys with backtracking, rejection, and ultimate selection. The random selection process is a lot easier because the factor of 1/n that caused the troubles in listing is not a bit troublesome for random selection since the same factor is common to all necklaces, and it doesn't affect the uniformity of the distribution. Thus there are various ways of sampling necklaces with equal probabilities. We would like now to describe one of these, but in a setting that is a little more general than the necklace problem. Just as in the problem of counting the necklaces, where one can use very special methods or one can invoke Burnside's lemma, we choose here to give an algorithm that randomly selects an orbit of the action of a group of permutations on a set. That is, the algorithm will apply to precisely the situations that Burnside's lemma covers. The discussion follows [DW]. When a finite group G acts as a group of permutations on a set Q it induces an equivalence relation on Q, in which the classes are called the orbits of the action of G. For every g e G we let Fix(g) be the set of objects (elements of £1) that are fixed by g. Then classical Burnside theory gives us the following facts. (a) The number of orbits is

(b) For each orbit co, define

GENERATING RANDOM GRAPHS

39

Then |rw| = \G\ for all orbits co. (c) For each orbit co and for each conjugacy class C of G, every element of C has the same number of fixed points in co. In particular, each element of G has the same number of fixed points. Property (b) above is the basis for the algorithm that selects an orbit of the action uniformly at random. Indeed, it implies that the multiset U?ecF/x(g) contains exactly \G\ representatives of each orbit, and consequently if we choose an element from that multiset u.a.r., then the orbit in which the element lives is distributed u.a.r. The algorithm that follows uses that idea, but economizes it by working with conjugacy classes rather than with the elements of G themselves, as permitted by property (c) above. Let C\,...,Cr be the conjugacy classes of G. Let hj — |C7| and let gj be a representative of C/, for each j = 1,..., r. Further, let the weight of a conjugacy class C/ be Wj = hj\Fix(gj)\, and note that from property (a) above, we have £/ Wj = m\G\. In terms of these parameters we can now state the algorithm. We will then give two examples of its operation. The first will be to the necklace problem, where the group, the conjugacy classes, etc., are all very transparent, and the second will be to the selection of unlabeled graphs, where life is more complicated. ALGORITHM RANDOM ORBIT. [Input is a group G that acts on a set Q. Output is an orbit co chosen u.a.r. from the orbits of the group action.] RO1 Choose a conjugacy class of G so that the probability of choosing C/ is Prob(Cj) = Wj/(m\G\)(j = 1,..., r), where m is the number of orbits, given by (8.1). RO2 Let Cj be the conjugacy class that was chosen in step RO1, and let gj be its representative. Then choose an element a u.a.r. from Fix(gj). RO3 Return the orbit co that contains a. D The following computation shows that each orbit co has the same a priori probability (= 1/m) of being chosen by this algorithm:

40

CHAPTER 8

Example 1. Choosing necklaces at random. In this example we fix two integers 1 < k < n, and take the set Q of objects to be the set of k" necklaces of n labeled beads, where each bead is colored in one of the k colors 1,2,...,k. The beads are labeled with 1,2,...,n, so each bead has a label that gives its position on the necklace, and a color. The group G is the cyclic group of order n, which we take in the form

where c is a cyclic right shift of one unit. For each j, 1 < j < n, the mapping cj has gcd(n, j) cycles, each of length n/gcd(n, j). Since the cycle length set is the only conjugacy invariant, there is a conjugacy class Q for each divisor d of n. There are r — d(ri) conjugacy classes altogether, and the class Q contains exactly hd = \Cd\ = (j>(n/d) elements of G, for each d\n. A representative of Cj is gj = cd and

It is now trivial to check that Buraside's lemma gives the number of orbits m as in (8.1) above. The weight wj of the class Q is 4>(n/d)kd, and Algorithm Random Orbit boils down to the following: 1. Choose a class Q with probability

2. Choose d colors from the available set {1,2,...,A:} independently u.a.r. Then color beads 1, d+1, Id +1,... in the first chosen color, and beads 2, d+2, 2d + 2,... in the second color chosen, etc. until all d chosen colors have been used (and all beads have been colored!). We have now chosen an element a u.a.r. from Fix(cd). 3. Erase the labels of the beads, leaving only their colors, and output the resulting necklace. Example 2. Choosing unlabeled graphs at random. The setting is now the following: A positive integer n is fixed. The set Q of objects is the set of all vertex-labeled graphs of n vertices. The group that acts on Q is the symmetric group Sn and it acts by permuting vertex labels. Now consider, for a fixed permutation g e Sn, the set Fix(g) of graphs that are fixed by g. We associate with g another permutation g* which acts not on the n letters 1,2,..., n but on the (£) unordered pairs of those letters. Alternatively, we can say that g* acts on the edges of the complete graph Kn. Its action is just this: g*{i,j} = {gi.gj}. Now Fix(g) can be described easily. It is the set of all graphs whose edge sets are mapped into themselves by g*. Again, a graph H is fixed by g if and only if for each cycle q of the induced permutation g* either all pairs in q are edges of H or none of them are.

GENERATING RANDOM GRAPHS

41

It is easy to count the graphs that g fixes. For each cycle of g* we must either take all of the edges in the cycle or none of them, so there are exactly 2c(g) graphs in Fix(g), where c(g) is the number of cycles of the pair permutation g*. Next we want to express these counts in a more quantitative nuts-and-bolts fashion. Hence we let (k\, k^,..., kn) denote the partition of the integer n that has exactly kt parts of size /, for all / > 1. We let [ki, k-i,..., kn] denote the corresponding conjugacy class of Sn, which consists of all permutations that have fc, cycles of size / for each / > 1. A well-known and elementary counting argument shows that the number of permutations in the conjugacy class [k\, ki,..., kn] is exactly

The second fact that we need is less well known. It states the following. THEOREM 8.1. The number of cycles in the induced pair permutation g* can be expressed in terms of the cycle structure of g itself as

where is Euler's function and /(/) = £)/|; kj. The following proof is from Robinson [Ro]. Consider separately the edges of the complete graph Kn that join distinct cycles of g and those that join points of the same cycle of g. There are ij edges in Kn that join distinct cycles of length i and length j of g, and g* acts on these to produce gcd(ij) cycles of lcm(i,j) points each. On the other hand, the edges of Kn that join points of a single cycle of g whose length is 2i + 1 are permuted into i cycles of length 2i + 1, while the edges in a single cycle of length 2i are permuted into / — 1 cycles of length 2i and one of length i, and the formula results immediately. The orbits of the action of Sn on fl are the unlabeled graphs of n vertices. Hence, Algorithm Random Orbit applies to this situation and will produce unlabeled graphs of n vertices with equal a priori probabilities. The weight of the conjugacy class [k\,..., kn] is

where c(g) is computed from the theorem above.

42

CHAPTER 8

ALGORITHM RANDOM GRAPH. RG1 Choose a partition of the integer n such that the probability of choosing n = (ki kn) is

RG2 Take a representative g from the class that corresponds to the partition n, and choose u.a.r. a (labeled) graph H from Fix(g). RG3 Erase the labels and output the unlabeled graph H. D Concerning the actual implementation of this algorithm, consider step RG2 first. To choose the representative of the conjugacy class, all we need is any permutation whose cycle partition is the given one. That's easy to do. A permutation for which [fc,, k2, k3] = [2,2,1], for example, is (1)(2)(34)(56)(789). Next we have to choose a graph that is fixed by the induced permutation g*. To do that, just follow around all of the cycles of g*, and then decide, independently u.a.r. for each cycle, whether to admit all of its edges into the graph H or to admit none of them. The time required to trace the cycles of g* and make these decisions is clearly O(n2}. In step RG1 we must select a partition of the integer n with certain given probabilities attached to each partition. There are a great many partitions of n, around eK^" of them, so we do not want to have to look at very many of them, on average, before finding the winner. Hence it is important to know which of the partitions are most likely to occur, and to arrange the calculation so we examine the partitions in roughly decreasing order of their likelihoods of being chosen. In that way we'll be more likely to get the game over quickly. The fact is that the partitions of n that have many parts = 1 are the ones that are most likely to be chosen. Indeed the one partition whose parts are all ones has by far the largest probability. Its weight is 2")/n!, and that one term already gives the correct asymptotic behavior of the number of unlabeled «-graphs. It was shown by Oberschelp [Ob] that the total probability of all partitions of n that have no more than r 1's is O(nr2~nrl2), so the probabilities decrease geometrically as the number of parts = 1 decreases. A careful analysis shows that the average number of partitions that need to be looked at before selecting one is no more than 3 if they are examined in a sequence that respects the number of 1's in the partition. For the details, see [DW]. The result is that with a careful implementation we can choose unlabeled n-graphs u.a.r. in average time O(n2} per graph, which is surely the best that could be expected.

Bibliography

[Al] BRIAN ALSPACH, The search for long paths and cycles in vertex-transitive graphs and digraphs; in Combinatorial Mathematics VIII, Proc. Eighth Australian Conference on Combinatorial Math., Lecture Notes in Mathematics 884, Springer-Verlag, Berlin, 1981, pp. 14-22. [Ar] BENJAMIN ARAZI, An approach for generating different types of Gray codes, Inform, and Control, 63 (1984), pp. 1-10. [Ba] L. BABAI, Long cycles in vertex-transitive graphs, J. Graph Theory, 3 (1979), pp. 301-304. [BER] JAMES R. BITNER, GIDEON EHRLICH, AND EDWARD M. REINGOLD, Efficient generation of the binary reflected Gray code and its applications, ACM Comm., 19 (1976), pp. 517-521. [BH] TERRY BEYER AND SANDRA MITCHELL HEDETNIEMI, Constant time generation of rooted trees, SIAM J. Comput., 9 (1980), pp. 706-712. [BW] M. BUCK AND D. WIEDEMANN, Gray codes with restricted density, Discrete Math., 48 (1984), pp. 19-29. [CLD] C. C. CHANG, R. C. T. LEE, AND M. W. Du, Symbolic Gray code as a perfect multiattribute hashing scheme for partial match queries, IEEE Trans. Software Engrg., 8 (1982), pp. 235249. [DW] JOHN D. DIXON AND HERBERT S. WILF, The random selection of unlabeled graphs, J. Algorithms, 4 (1983), pp. 205-212. [DZ] E. A. DINITS AND M. A. ZAITSEV, Algorithm for the generation of nonisomorphic trees, Avtomat. i Telemekh., 4(1977), pp. 121-126; Automat. Remote Control, 38 (1977), pp. 554-558. [EHR] P. EADES, M. HICKEY, AND R. C. READ, Some Hamilton paths and a minimal change algorithm, J. Assoc. Comput. Mach., 31 (1984), pp. 19-29. [EM] PETER EADES AND BRENDAN MCKAY, An algorithm for generating subsets of a fixed size with a string minimal change property, preprint ,1982. [Er] M. C. ER, Two recursive algorithms for generating the binary reflected Gray code, J. Inform. Optim. Sci., 6(1985), pp. 213-216. [Fr] MICHAEL FREDMAN, Observations on the complexity of generating quasi-Gray codes, SIAM J. Comput., 7 (1978), pp. 134-146. [FR] P. FLAIOLET AND LYLE RAMSHAW, A note on Gray code and odd-even merge, SIAM J. Comput., 9(1980), pp. 142-158. [Gi] E. N. GILBERT, Gray codes and paths on the n-cube, Bell System Tech. J., 37 (1958), pp. 815-826. [Gr] Bell System Tech. J., 18 (1939), p. 252. [Ha] RICHARD W. HAMMING, Coding and Information Theory, Prentice-Hall, Englewood Cliffs, N.J., 1980. [Im] W. IMRICH, On the connectivity of Cayley graphs, J. Combin. Theory Ser. B, 26 (1979), pp. 323-326. [Jal] BILL JACKSON, Hamilton cycles in regular 2-connected graphs, J. Combin. Theory Ser. B, 29(1980), pp. 27-46. [Ja2] , Longest cycles in 3-connected cubic graphs, J. Combin. Theory Ser. B, 41 (1986), pp. 17-26. 43

44

BIBLIOGRAPHY

[JW] J. T. JOICHI AND DENNIS E. WHITE, Gray codes in graphs of subsets, Discrete Math., 31 (1980), pp. 29-41. [JWW] J. T. JOICHI, DENNIS E. WHITE, AND S. G. WILLIAMSON, Combinatorial Gray codes, SIAM J. Comput., 9 (1980), pp. 130-141. [Ka] RICHARD KAYE, A Gray code for set partitions, Inform. Process. Lett., 5 (1976), pp. 171-173. [Kl] PAUL KLINGSBERG, A Gray code for compositions, J. Algorithms, 3 (1982), pp. 41-44. [Knl] DONALD E. KNUTH, The Art of Computer Programming, Vol. 1: Fundamental Algorithms, Addison-Wesley, Reading, MA, 1968. [Kn2] , personal communication, 1988. [Ko] A. V. KOZINA, Coding and generation of nonisomorphic trees, Cybernetics, 15 (1975), pp. 645-651. Kibernetika, 5 (1979), pp. 38-43. (In Russian.) [KP] P. KIRSCHENHOFER AND H. PRODiNOER, Subblock occunences in positional number systems and Gray code representation, J. Inform. Optim. Sci., 5 (1984), pp. 29-42. [Lov] L. LOVASZ, Problem 11, Combinatorial structures and their applications, Gordon and Breach, New York, 1970. [Lu] HEINZ LUNEBERG, Gray codes, Abb. Math. Sem. Univ. Hamburg, 52(1982), pp. 208-227. [Luc] J. LUCAS, The rotation graph of binary trees is Hamiltonian, Tech. Report TR-021, Dept. of Computer Science, Princeton University, Princeton, N.J., 1986. [NW] A. NUENHUIS AND H. S. WiLF, Combinatorial algorithms, second edition, Academic Press, New York, 1978. [Ob] W. OBERSCHELP, Kombinatorische anzahlbestimmungen in Relationen, Math. Annalen, 174(1967), pp. 53-58. [Pr] HELMUT PRODINCER, Nonrepetitive sequences and Gray code, Discrete Math., 43 (1983), pp. 113-116. [PR] ANDRZEJ PROSKUROWSKI AND FRANK RUSKEY, Binary tree Gray codes, J. Algorithms, 6 (1985), pp. 225-238. [Re] R. C. READ, How to grow trees, in Combinatorial Structures and Their Applications, Gordon and Breach, New York, 1970. [RH] F. RUSKEY AND T. C. Hu, Generating binary trees lexicographically, SIAM J. Comput, 6 (1977), pp. 745-758. [Ri] DANA RICHARDS, Data compression and Gray-code sorting, Inform. Process. Lett., 22 (1986), pp. 201-205. [RM] F. RUSKEY AND D. J. MILLER, Adjacent interchange generation of combinations and trees, University of Victoria, British Colombia, Canada, DCS-44-IR, 1984. [Ro] ROBERT W. ROBINSON, Enumeration of Euler graphs, in Proof Techniques in Graph Theory, F. Harary, ed., Academic Press, New York, 1969, pp. 147-153. [RP] FRANK RUSKEY AND ANDRZEJ PROSKUROWSKI, Generating binary trees by transpositions, preprint, 1987. [Ru] FRANK RUSKEY, Generating t-ary trees lexicographically, SIAM J. Comput., 7 (1978), pp. 424-439. [Ru2] , Adjacent interchange generation of combinations, J. Algorithms, to appear. [SKI] B. D. SHARMA AND R. K. KHANNA, J. Combin. Inform. System Sci., 4 (1979), pp. 227-236. [SK2] , On level weight studies of binary and m-ary Gray codes, Inform. Sci., 21 (1980), pp. 179-186. [SK3] , On m-ary Gray codes, Inform. Sci., 15 (1978), pp. 31-43. [Tr] A. TROJANOWSKI, Ranking and listing algorithms for k-ary trees, SIAM J. Comput., 7 (1978), pp. 492-509. [Wat] MARK E. WATKINS, Connectivity of vertex transitive graphs, J. Combin. Theory, 8 (1970), pp. 23-29. [Wi] H. S. WILF, The uniform selection of free trees, 3. Algorithms, 2 (1981), pp. 204-207. [Wo] NICOLAS C. WORMALD, Generating random regular graphs, J. Algorithms, 5 (1984), pp. 247-280. [WROM] ROBERT ALAN WRIGHT, BRUCE RICHMOND, ANDREW ODLYZKO, AND BRENDAN D. McKAY, Constant time generation of free trees, SIAM J. Comput., 15 (1986), pp. 540-548.

BIBLIOGRAPHY

45

[WY] HERBERT S. WILF AND NANCY A. YOSHIMURA, Ranking rooted trees, and a graceful application, in Perspectives in Computing, Proc. Japan-U.S. Joint Seminar in Discrete Algorithms and Complexity, June 4-6, 1986, Kyoto, Japan, Academic Press, 1987, pp. 341-350. [Yo] NANCY ALLISON YOSHIMURA, Ranking and unranking algorithms for trees and other combinatorial objects, Ph.D. thesis, Computer and Information Science, University of Pennsylvania, Philadelphia, 1987. [Yu] C. K. YUEN, The separability of Gray code, IEEE Trans. Inform. Theory (1974), p. 668. [Zal] S. ZAKS, Lexicographic generation of ordered trees, Theoret. Comput. Sci., 10 (1980), pp. 63-82. [Za2] , Generating k-ary trees lexicographically, Tech. Report UICSCS-R77-901, University of Illinois, Urbana, IL, 1977.

This page intentionally left blank

Index

minimal-change algorithms, 9

adjacent interchange Gray code, 10 Algorithm Forest, 28 Algorithm Free, 29 Algorithm Random Orbit, 39-41 Algorithm Random Graph, 42 Algorithm Ranrut, 27, 28 Algorithm 2-Sample, 22, 27, 28 algorithms, minimal-change, 9 alternating group, 19 automorphism, 18

"nearest-integer" function, 17 necklaces, 37, 38-40 orbits, 38, 39 ordered tree, 23-25

bicentral, 31, 32, 34 bicentroidal, 28 binary tree, 14, 15 bipartite graphs, 37 Burnside's lemma, 38, 40 canonical level sequences, 25, 32, 34, 35; primary, 32, 34, 35 canonical ordered tree, 24, 25 Catalan-countable problems, 14 Cayley graph, 17, 18 center, 31, 32, 36 centroids 27, 31 compositions of an integer, 10 conjugacy class 19, 39, 41, 42; weight of, 39 Euler's function, 38, 41 fixed-point free permutations, 17, 18 free trees, 23, 28, 29, 31-34, 36, 37

partitions of a set, 11, 12 Pascal triangle, 8 planar graphs, 37 prefab, 28 preorder, 23 primary rooted version, 32 primary canonical level sequence, 32, 34,35 principal subsequences, 35 random orbit, 39, 40 ranking problem, 5 regular graphs, 37 revolving door algorithm, 8, 9 rooted forest, 28 rotation of a binary tree, 14 set, partitions of, 11, 12; set, subsets of, 7 standard reflected Gray code, 2 strong revolving door order, 10 subsets of a set, 7 symmetric group, 17, 19

Gray code, 1-12, 14, 17; adjacent interchange, 10; standard reflected, 2

transpositions, 7, 18 triangulations, 12-14 2-sample, 22, 27, 28

Gray, Frank, 1

unranking problem, 5

Hamilton path, 3, 17-18 Hamilton walk, 3, 17 Hamiltpnian graphs, 37 Hamming distance, 5, 6

vertex-transitive, 18 weight of a conjugacy class, 39 weight sequences, 14, 15

integer, compositions of, 10 level sequences, 23-25, 31, 32, 34, 35; canonical, 25, 32, 34, 35 listing rooted trees, 23 47

CBMS-NSF REGIONAL CONFERENCE SERIES IN APPLIED MATHEMATICS A series of lectures on topics of current research interest in applied mathematics under the direction of the Conference Board of the Mathematical Sciences, supported by the National Science Foundation and published by SIAM. GARRETT BIRKHOFF, The Numerical Solution of Elliptic Equations D. V. LINDLEY, Bayesian Statistics, A Review R. S. VARGA, Functional Analysis and Approximation Theory in Numerical Analysis R. R. BAHADUR, Some Limit Theorems in Statistics PATRICK BILLINGSLEY, Weak Convergence of Measures: Applications in Probability J. L. LIONS, Some Aspects of the Optimal Control of Distributed Parameter Systems ROGER PENROSE, Techniques of Differential Topology in Relativity HERMAN CHERNOFF, Sequential Analysis and Optimal Design J. DURBIN, Distribution Theory for Tests Based on the Sample Distribution Function SOL I. RUBINOW, Mathematical Problems in the Biological Sciences P. D. LAX, Hyperbolic Systems of Conservation Laws and the Mathematical Theory of Shock Waves I. J. SCHOENBERG, Cardinal Spline Interpolation IVAN SINGER, The Theory of Best Approximation and Functional Analysis WERNER C. RHEINBOLDT, Methods of Solving Systems of Nonlinear Equations HANS F. WEINBERGER, Variational Methods for Eigenvalue Approximation R. TYRRELL ROCKAFELLAR, Conjugate Duality and Optimization SIR JAMES LIGHTHILL, Mathematical Biofluiddynamics GERARD S ALTON, Theory of Indexing CATHLEEN S. MORAWETZ, Notes on Time Decay and Scattering for Some Hyperbolic Problems F. HOPPENSTEADT, Mathematical Theories of Populations: Demographics, Genetics and Epidemics RICHARD ASKEY, Orthogonal Polynomials and Special Functions L. E. PAYNE, Improperly Posed Problems in Partial Differential Equations S. ROSEN, Lectures on the Measurement and Evaluation of the Performance of Computing Systems HERBERT B. KELLER, Numerical Solution of Two Point Boundary Value Problems J. P. LASALLE, The Stability of Dynamical Systems - Z. ARTSTEIN, Appendix A: Limiting Equations and Stability ofNonautonomous Ordinary Differential Equations D. GOTTLIEB AND S. A. ORSZAG, Numerical Analysis of Spectral Methods: Theory and Applications PETER J. HUBER, Robust Statistical Procedures HERBERT SOLOMON, Geometric Probability FRED S. ROBERTS, Graph Theory and Its Applications to Problems of Society JURIS HARTMANIS, Feasible Computations and Provable Complexity Properties ZOHAR MANNA, Lectures on the Logic of Computer Programming ELLIS L. JOHNSON, Integer Programming: Facets, Subadditivity, and Duality for Group and Semi-Group Problems SHMUEL WINOGRAD, Arithmetic Complexity of Computations J. F. C. KINGMAN, Mathematics of Genetic Diversity MORTON E. GURTIN, Topics in Finite Elasticity THOMAS G. KURTZ, Approximation of Population Processes

JERROLD E. MARSDEN, Lectures on Geometric Methods in Mathematical Physics BRADLEY EFRON, The Jackknife, the Bootstrap, and Other Resampling Plans M. WOODROOFE, Nonlinear Renewal Theory in Sequential Analysis D. H. SATTINGER, Branching in the Presence of Symmetry R. TEMAM, Navier-Stokes Equations and Nonlinear Functional Analysis MIKLOS CSORGO, Quantile Processes with Statistical Applications J. D. BUCKMASTER AND G. S. S. LuoFORD, Lectures on Mathematical Combustion R. E. TARJAN, Data Structures and Network Algorithms PAUL WALTMAN, Competition Models in Population Biology S. R. S. VARADHAN, Large Deviations and Applications KIYOSI ITO, Foundations of Stochastic Differential Equations in Infinite Dimensional Spaces ALAN C. NEWELL, Solitons in Mathematics and Physics PRANAB KUMAR SEN, Theory and Applications of Sequential Nonparametrics LASZLO LovAsz, An Algorithmic Theory of Numbers, Graphs and Convexity E. W. CHENEY, Multivariate Approximation Theory: Selected Topics JOEL SPENCER, Ten Lectures on the Probabilistic Method PAUL C. FIFE, Dynamics of Internal Layers and Diffusive Interfaces CHARLES K. CHUI, Multivariate Splines HERBERT S. WILF, Combinatorial Algorithms: An Update HENRY C. TUCKWELL, Stochastic Processes in the Neurosciences FRANK H. CLARKE, Methods of Dynamic andNonsmooth Optimization ROBERT B. GARDNER, The Method of Equivalence and Its Applications GRACE WAHBA, Spline Models for Observational Data RICHARD S. VARGA, Scientific Computation on Mathematical Problems and Conjectures INGRID DAUBECHIES, Ten Lectures on Wavelets STEPHEN F. MCCORMICK, Multilevel Projection Methods for Partial Differential Equations HARALD NIEDERREITER, Random Number Generation and Quasi-Monte Carlo Methods JOEL SPENCER, Ten Lectures on the Probabilistic Method, Second Edition CHARLES A. MICCHELLI, Mathematical Aspects of Geometric Modeling ROGER TEMAM, Navier-Stokes Equations and Nonlinear Functional Analysis, Second Edition GLENN SHAFER, Probabilistic Expert Systems PETER J. HUBER, Robust Statistical Procedures, Second Edition }. MICHAEL STEELE, Probability Theory and Combinatorial Optimization WERNER C. RHEINBOLDT, Methods for Solving Systems of Nonlinear Equations, Second Edition ]. M. GUSHING, An Introduction to Structured Population Dynamics TAI-PING Liu, Hyperbolic and Viscous Conservation Laws MICHAEL RENARDY, Mathematical Analysis ofViscoelastic Flows GERARD CORNUEJOLS, Combinatorial Optimization: Packing and Covering

This page intentionally left blank

HERBERTS. WILF University of Pennsylvania

Combinatorial Algorithms: An Update

SOCIETY FOR INDUSTRIAL AND APPLIED MATHEMATICS PHILADELPHIA

Copyright © 1989 by the Society for Industrial and Applied Mathematics. 1098765432 All rights reserved. Printed in the United States of America. No part of this book may be reproduced, stored, or transmitted in any manner without the written permission of the publisher. For information, write to the Society for Industrial and Applied Mathematics, 3600 University City Science Center, Philadelphia, PA 19104-2688. Library of Congress Cataloging-in-Publication Data Wilf, Herbert S., 1931Combinatorial algorithms—an update. (CBMS-NSF regional conference series in applied mathematics ; 55) Update ed. of: Combinatorial algorithms for computers and calculators / Albert Nijenhuis and Herbert S. Wilf, 2nd ed. 1978. Based on a series of 10 lectures given at the CBMS-NSF Conference on Selection Algorithms for Combinatorial Objects, held in the summer of 1987 at the Baca Grande campus of Colorado College. Bibliography: p. Includes index. 1. Combinatorial analysis. 2. Algorithms. I. Nijenhuis, Albert. Combinatorial algorithms for computers and calculators. II. Title. III. Series. QA164.W54 1989 511'.6 89-6070 ISBN 0-89871-231-9

is a registered trademark.

Contents

ix

PREFACE

1

CHAPTER 1. The Original Gray Code

7

CHAPTER 2. Other Gray Codes

17

CHAPTER 3. Variations on the Theme

21

CHAPTER 4. Choosing 2-Samples

23

CHAPTER 5. Listing Rooted Trees

27

CHAPTER 6. Random Selection of Free Trees

31

CHAPTER 7. Listing Free Trees

37

CHAPTER 8. Generating Random Graphs

43

BIBLIOGRAPHY

47

INDEX

vii

This page intentionally left blank

Preface

This monograph is based on a series of ten lectures that I gave at the CBMSNSF Conference on Selection Algorithms for Combinatorial Objects, held in the summer of 1987 at the beautiful place that is the Baca Grande campus of Colorado College. It is intended to be an updating of a book [NW] that Albert Nijenhuis and I first published in 1975 and revised in 1978. That book was a collection of algorithms for listing and choosing at random combinatorial objects from given families, and of certain sizes within those families. For instance, how can we make a list of all of the partitions of the integer n? of all of the partitions of the set [«]? How can we choose uniformly at random a rooted tree from the set of all rooted trees of a given number n of vertices? Much has been done even in the ten years since the appearance of the second edition of our book. In these pages I will try to survey some of this new work. Among the topics to be discussed are progress in Gray codes, in listing subsets of given size of a given universe, in listing rooted and free trees (a subject that had not existed in 1978), and in selecting free trees and unlabeled graphs uniformly at random. We also discuss ranking and unranking problems on unlabeled trees. It will, I hope, be apparent that the subject has continued to develop vigorously. I wish to thank the United States Office of Naval Research for their support of the research that led to this monograph.

ix

This page intentionally left blank

CHAPTER 1 The Original Gray Code

United States patent number 2,632,058, granted March 17, 1953 after having been applied for on November 13, 1947, to "Frank Gray, East Orange, N.J., assignor to Bell Telephone Laboratories, Incorporated, New York, N.Y., a corporation of New York" was for "pulse code communication" and it provided

... in a pulse code communication system, means for translating signal samples into reflected binary code groups of on-or-off pulses, means for transmitting pulse groups to a receiver station, means at said receiver station for converting each received group of said pulses into a conventional binary code pulse group, and means for translating said conventional binary code pulse groups into message signal samples. The inventor, Frank Gray, is described in [Gr] as: "B.S., Purdue, 1911; Ph.D., University of Wisconsin, 1916. Western Electric Company, Engineering Department, 1919-1925. Bell Telephone Laboratories, 1925-. Dr. Gray has been engaged in work on electro-optical systems." His idea was basically the following. Suppose you want to transmit a finite string of bits using an analogue transmission device. You might take the bit string, compute the integer whose bits they are, and transmit a signal of that strength (frequency, or whatever). The problem is that if a small error occurs in the signal strength, a small error will occur in the integer that is received, but a small change in an integer can cause a huge change in its bit string (e.g., between the strings of 31 and 32). The question then arises as to how to associate integers with bit strings in such a way that small changes in the integers produce only small changes in the strings. One way to do this is to list all finite strings of bits in a sequence with the property that each string differs from its successor in only a single bit position; that was the idea of the Gray code. In Fig. 1.1 there is shown a list of all bit strings of length < 4 in Gray code order. For each string we show its rank in the list, as an integer in the range [0,15], the binary digits of its rank, and the bit string itself. i

2

CHAPTER 1

0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111

0000 0001 0011 0010 0110 0111 0101 0100 1100 1101

1111

1110 1010 1011 1001 1000

FIG. 1.1

We will now study some of the properties of the Gray code shown above, which is called the standard reflected Gray code. First the list of strings has a simple recursive description. Let £„ denote the list of all «-bit strings, arranged in (standard reflected) Gray code order. Then we can generate all of these lists as follows. 1. £o is the empty list. 2. For each n = 1,2,..., having formed £ n -i, then (a) write out list Ln-\ and prefix each entry with an additional bit "0"; (b) write out list Ln-\ in reverse order, and prefix each entry with an additional bit "1"; 3. The list £„ is the result of concatenating the two lists that were formed in step 2. The reader should take a moment to check that the list £4 that is shown in Fig. 1.1 can be formed in exactly the way that we have just described. If we denote the reversal of a list £„ by Zn, then the recipe given in steps 1-3 above can be summarized as This method of copying a list and its reversal, with some extra insertions going on, will be a common theme in some of the later constructions also, and it seems to be a good thing to think of when trying to construct some new kind of Gray code. It is now a simple matter to prove by induction that if we start with the empty list and do (1.1) for n > 1 then we will indeed form lists that have the Gray code property: successive entries will differ in only a single bit position. Sometimes we don't want to think of the individual lists £„, but of their "limit" as n —> oo. Notice that the entry of rank m on some list £„ will

THE ORIGINAL GRAY CODE

3

be identical with the entry of that rank on £ n+1 except for an additional leading "0". Since additional leading "0" bits don't change the integer that is represented by the bit string, we can, if it is convenient, think of the entries of a Gray code list as integers encoded by their bit strings. Then the Gray code can be regarded as a permutation of the set of all positive integers that is the union of a family of disjoint bijections of the intervals [ 2 J , 2 J + l - l ] (; = 0,1,...). From that point of view, the Gray code is the single infinite sequence 0,1,3,2,6,7,5,4,12,13,15,14,10,11, 9,8,.... Yet another manifestation of the code results from thinking of the entries as representing neither bit strings nor integers, but sets. The set that a bit string represents is the one whose members are the indices of the positions where the "1" bits appear in the string. Thus "00101011" is the set {1,2,4,6}. In this state of mind, the list £„ is the list of all 2" subsets of [n], arranged so that each one can be obtained from its immediate predecessor by either a single adjunction of a singleton or a deletion of a single element. As if that weren't enough, here's another pretty way to contemplate the world of Gray codes. Fix a positive integer n and imagine a graph G that has 2" vertices, one for each possible string of n O's and 1's. Now connect two of those vertices by an edge of G if and only if the two corresponding strings differ in only a single position. You are now looking at an n-dimensional cube.1 In Fig. 1.2 we show the cube Qy. As we follow down the Gray code list, we take a walk from one vertex of the cube to another, walking always on edges of the cube. The Gray code list £„ encodes a Hamilton walk (actually a circuit) on this graph. The standard binary reflected Gray code that we have been talking about so far is certainly not the only Hamilton path on Qn. There are a lot of them, although their exact number has not been determined (see [Gi]). In fact, the number of them has not even been determined asymptotically. This particular Hamilton path, however, does have a host of elegant properties which justify singling it out for discussion. Hence a Gray code is a list of bit strings, or of integers, or of sets, or it is a Hamilton path on the cube. Next we are going to study the relationships between the bit string of an integer m and the bit string that has rank m in the Gray code. Take m = 9, for instance. The string of rank 9 is 1101 (see Fig. 1.1), and the string of the integer 9 is 1001. Is there some simple rule from which we can construct one of these strings given the other? For instance, what is the string of rank 3982226135 in the Gray code? We have the following remarkable theorem. THEOREM 1.1. Let m = £)e,2' be the binary representation of the integer m, and let... e^e^o be the string of rank m in the Gray code. Then

1

Graph theorists call this graph Qn ("Q" as in Qbe, after all).

4

CHAPTER 1

FIG. 1.2. The Gray code £3 as a walk on Q$.

Proof. By induction on n we will show that the result holds for all m that occur on the list £„. This is extremely clear when n = 0. Suppose (1.2) holds for all strings on the list £,n-i, and now consider the string of rank m on the £„. If m < 2""1 - 1 then there is nothing more to prove because the string of rank m is unchanged. Hence suppose m > 2"~', and write m' = 2" - 1 - m. Then (1.2) holds for the integer m' and the string that has rank m' because m1 < 2"-1. But the bits in the strings of ranks m and m' are related by

and the bits in the binary representations of the integers m and m' are related by It is now immediate to check that (1.2) continues to hold for the integer m, completing the proof. D Exercise. Now, what is the string of rank 3982226135 in the Gray code? It is easy to invert (1.2) in order to express the e's in terms of the e's. We obtain

We summarize with the following theorem. THEOREM 1.2. The bit string ...e^e^ has rank m in the Gray code list, where the binary digits of m are given by (1.3). Conversely, ifm is a given integer, then the bits in the string of rank m in the Gray code are given by (1.2).

THE ORIGINAL GRAY CODE

5

In general, for any list of combinatorial objects, the ranking problem is the question of determining the position in the list of some given object. The unranking problem gives the rank and asks for the object of that rank. Hence we may say that for the Gray code list, equation (1.3) solves the ranking problem and (1.2) solves the unranking problem. Next, at the outset we stated that one of the motivations for the Gray code ordering was that if the ranks of two objects on the list are close then the objects themselves differ in rather few bit positions. By the Hamming distance between two bit strings we mean the number of bit positions in which they differ. Thus consecutive elements of the Gray code list have Hamming distance 1. The question is this: Given a positive integer m, how close can two objects be to each other on the Gray code list if their Hamming distance is ml Symbolically, we are asking for

where p is the rank function. For the given m, among all pairs s, t of strings that achieve the minimum in (1.4), choose a pair of minimum length, say of length L. Then the leftmost bit of s is different from the leftmost bit of t, for otherwise we could delete those bits and obtain shorter strings s', t' that are still at a distance m, while which would be a contradiction. Now let q > 2 be the first bit position, reading from the left, in which s and t have the same bit present. Suppose we construct 5', t' by deleting that #th bit from each of .s and t. Then the Hamming distance remains unchanged and the strings are shorter. To obtain a contradiction we must show that (1.5) still holds. What happens to the rank of s when we delete the qih bit? Suppose that bit is 0. Then from (1.3), the binary digits of the rank of s are unaffected except that one of those digits has been deleted. The same happens for the rank of t, so the difference of their ranks does not change, and we have the contradiction. If the common bit in position q is a 1 then, according to (1.3) again, if we delete it then we don't affect any of the bits of the rank that are more significant than the #th, and every one of the bits that is less significant is reversed. Hence again both ranks change by the same amount under the deletion, and the difference is unaffected. It follows that s and t differ in every bit position. Hence in seeking the minimum (1.4) we may confine attention to complementary pairs s, t, that is, to pairs in which each of the L bits of the string t is the complement of the corresponding bit of 5. We will write t = s. Evidently we now have L = m (recall that m is the Hamming distance between the two strings), so s is a string of length m. We standardize by supposing that s is that one of the pair s, s that has a "1" in the leftmost bit position, i.e., s is the one of higher rank. Next we

6

CHAPTER 1

look at (1.3) to see the relationship between the ranks of s and of.?. We see that the bits of their ranks are alternately the same and opposite! Precisely, we have (we let p be the rank function)

and

and therefore the difference between their ranks is

where the terms continue as long as the exponents are nonnegative. It is a simple matter to sum the above series and obtain the following theorem. THEOREM 1.3. Two strings on the Gray code list whose Hamming distance is > m have ranks that differ by at least [2m/3]. The bound is best possible. D The above result is essentially due to Yuen [Yu].

CHAPTER 2 Other Gray Codes

The idea of the Gray code has found applications in combinatorial families other than the family of subsets of a set. In any combinatorial family where we want to make a list of all of the objects in such a way that only the smallest possible change takes place as we go from each object to its successor, we can speak of a (generalized) Gray code. It should be remarked that the concepts are meaningful only if we apply them to encodings of families of objects rather than to the underlying objects. More precisely, it may be possible (and usually is) to encode one and the same family of objects in several different ways. We might expect that a Gray code scheme will work on one of these encodings, but it might not provide minimal changes in any other one of the encodings. A small example will illustrate the point. Suppose n is fixed and we consider the permutations of n letters. If we generate the list of all permutations of n letters by applying, at each stage, a single transposition to get from one to the next, then it is reasonable to expect that we will have a Gray code for permutations. Suppose, however, that instead of encoding permutations by their values, we choose to encode them by their cycles. Then as we apply a single transposition, large changes may take place that aren't at all in the spirit of the minimal changes that we expect from Gray coding. By the way, I can't resist pointing out that there might be an interesting Gray code for permutations in cycle form, but I don't know what it is. In contrast to that example, let's think about subsets of a set again. The original Gray code was designed for the bit-string encoding of a subset. Suppose, instead, we use the list-of-members encoding. Do we still have the minimal change property? Indeed yes, because each set will be obtained from its predecessor by either deleting or adjoining a single member, and that is about the most minimal change we could expect. The point is that sometimes Gray codes have pleasant properties with respect to more than one encoding of a combinatorial family, but don't count on it.. For a first example of a Gray code applied to a family other than 0-1 strings, suppose we are given two positive integers n and k (k < n), and we want to make a list of all of the /c-subsets of [n]. Let's adopt the encoding in which we simply display the list of members of each ^-subset. Now it's quite out of the question to have each item on the list differ from its predecessor by a single adjunction or deletion, because the cardinalities of the subsets are supposed to be kept fixed at k. The "Gray-est" thing we 7

8

CHAPTER 2

can hope for, then, is to have each subset formed from its predecessor by two operations, a deletion and an insertion. In [NW] such an algorithm was given, and many others are known. We called ours a "revolving door" (RD) algorithm, for presumably obvious reasons, and it goes like this. x Let A(n, k) denote the list of all fc-subsets of [n], arranged in RD order, where the first set in the list is {1,2,..., k}, and the last one is {1,2,..., k l,n}. Then we form the lists A(n,k) recursively, as follows:

In words, the list A(n,k) is formed by writing down the list A(n -l,k) and then following it with the list that is obtained from A(n - \, k - 1) by first writing it down in reverse order and then adjoining the singleton {«} to each set on that list. The recursion has the same music as the Pascal triangle, i.e., the same three grid points (n, k), (n -1, k), and (n—I, k -1) are involved at each step. Hence the boundary conditions that are naturally needed for (2.1) to boot itself up will be the same kind that are needed by the binomial coefficients. Therefore we add to (2.1) the conditions that the list A(n, k) is empty if k < 0 or if k > n. The reader should then work out a few of these lists, for instance, enough to verify that the list A(4,6) of 4-subsets of [6] is as follows.

1 1 2 1 1 1 2 1 3 2 1 1 2 1 1

2 2 3 3 2 2 3 3 4 4 4 2 3 3 2

3 4 4 4 3 5 5 5 5 5 5 4 4 4 3

4 5 5 5 5 6 6 6 6 6 6 6 6 6 6

Here are some of the general principles of Gray code construction that already appear in this example. First, the general step is recursive, and is carried out by stitching together some earlier lists to make the next one. Second, there is the list reversal in the second part of the recurrence. The reason it is there is to smooth out the transition between the two portions of the recurrence. To put it a little more graphically, in the construction (2.1) we see a list, a comma, and another list. The hard part is at the comma. Indeed, inductively we can suppose that the first list is already in some Gray code sequence, and the second sublist is also. The difficulty comes in

OTHER GRAY CODES

(1,2,3) (1,2,4) (1,2,5) (1,2,6) (1,5,6) (1,4,6) (1.4,5) (1,3,5) (1,3,6) (1,3,4)

9

(2,3,4) (2,3,5) (2,3,6) (2,5,6) (2,4,6) (2,4,5) (3,4,5) (3,4,6) (3,5,6) (4,5,6)

FIG. 2.1. Strong revolving-door order.

gluing them together so the transition from the first to the second also has just a minimal change; in this case one element enters and one leaves. In order to prove that (2.1) does what it is supposed to do the reader will find that it is necessary to do more than just make the inductive assumption that the earlier lists A(n - \,k) and A(n - \,k - 1) are in minimal change order. In order to prove that the new list A(n, k) is also in Gray code order we have to know what the first subset and the last subset on the earlier lists are, so as to be able to prove that there is no bumpiness across the comma (again, what that means is that as we go from the last fc-subset that appears on the first of the two sublists that appear on the right of (2.1) to the first of the fc-subsets that appears in the second one of those two sublists, only one element gets deleted from the set and one element gets added). Exercises. (1) Find a simple and elegant nonrecursive implementation of the RD algorithm by describing a successor algorithm. Precisely, given a particular ^-subset S of [n], and given only that, find the next set in the list A(n, k). (2) Prove that (2.1) does indeed provide a recursive construction of the revolving door lists, by building into the inductive hypothesis not only that the earlier sets are Gray codes, but also what they begin and end with. To "peek" at the answers, see page 28 of [NW]. Although the lists A(n,k) satisfy the revolving door condition, there are minimal-change algorithms for fc-subsets that are even more severely restricted. Suppose we specify a subset by giving its members in ascending order: Then the subset S is represented by a ^-vector whose components strictly increase. Let's define the minimal-change condition by requiring that two ^-subsets that are consecutive on our list may differ only in a single component of their membership vectors. This is stronger than the revolving-door condition, but it can be satisfied. In Fig. 2.1 are the 20 3-subsets of [6] arranged in that way.

10

CHAPTER 2

If B(n, k) denotes this list of all of the fc-subsets of [n] in strong revolving door order, then the recursive construction of these lists, due to Eades and McKay [EM], is given by Lest we should think that Gray coding questions that sound reasonable always have affirmative answers, here is yet a third way to think about making a Gray code for ^-subsets of a set, where the answer turns out to be complicated. Suppose we are permitted to alter the status only of two adjacent elements. That is, at each stage we can delete / and adjoin i + 1, or we can delete i + 1 and adjoin /, and furthermore we regard elements 1 and n as being adjacent. Then is it possible to construct the lists? This problem has been studied by Joichi and White [JW], Buck and Wiedemann [BW], Eades, Hickey, and Read [EHR], and Ruskey and Miller [RM], and the answer is given by the following theorem. THEOREM 1.4. There is an adjacent interchange Gray code for k-subsets of [n] if and only if either k has one of the four values 0, 1, n - 1, n or else n is even and k is odd. Next we leave the subject of Ar-subsets of a set, and turn to that of the compositions of an integer. If, once more, n and k are given positive integers, then by a composition of n into k parts we mean an ordered representation of n as a sum of exactly k nonnegative integers. For our present purposes it is convenient to adopt the language of balls and boxes. Hence suppose we have k labeled boxes and n unlabeled balls. Then there is obviously a bijection between compositions of n into k parts and arrangements of the balls in the boxes. It is well known, and easy to see, that there are exactly (n+kn~l) such arrangements, and it isn't very hard to figure out ways of making lists of all of them. But now let's add the Gray code restriction to make things a little more interesting. By a Gray code for compositions of n into k parts we mean a list of all of those compositions, sequenced so that each arrangement of n balls in k boxes arises from its immediate predecessor by moving exactly one ball from one box to another. In the first edition of [NW] we asked if such an algorithm could be constructed, and Donald Knuth [Kn2] found the elegant solution that we will now describe. Let £(n, k) denote the desired lists. We will construct them recursively, for n fixed, and k = 1,2,.... First, the list L(n, 1) has just one entry, namely, the composition n = n of n into one part. If, inductively, the lists L(n,j) (j = \,2,...k) have been constructed, then

OTHER GRAY CODES

11

where the last term will be the one that contains £(0, k). The general approach that we described earlier for building Gray codes is shown here in full flower. The flip-flopping of the lists ensures smooth transitions across the commas, and the rest is taken care of inductively. Here is the list of the 21 compositions of 5 into three parts as they appear on the list £(5,3). Can you follow the bouncing ball? Just to the right of each composition on the list we show the jump that the ball made in the form of the number of its origin and destination boxes, starting from the composition 5+0+0. 4

3 2 1 0 0 1 2 3 4 3 2 1

0 0 1 2 1 0 0

1

2 3 4 5 4 3 2 1 0 0 1 2 3 2 1 0 0 1 0

0 0 0 0 0

1 1 1 1 1

2 2 2 2 3 3 3 4 4 5

1^2 1 ->2 1 -+2 1 -*2 1 ->2 2 ->3 2 -» 1 2 -» 1 2 -» 1 2 -f 1 1^3 1 ^2 1 -+2 1 ^2 2 -»3 2 -» 1 2 -f 1 1 ^3 1 -+2 2 ^3

Here is another example of the genre, whose origins are similar. That is to say, Nijenhuis and I asked if, for n fixed, all of the partitions of a set of n elements could be arranged in Gray code sequence, in the sense that as we pass down the list from a set partition to its successor, exactly one element moves from one equivalence class to another (the rule permits an element to leave a certain class and start a singleton class of its own). The answer was again found by Knuth, in a recursive form that will now be described. A set partition is described by giving a list of the members of each of its classes. We will suppose that the classes are given in ascending order of their smallest elements. For a fixed partition P of [n] we construct the list of its children. These are the partitions of [« + 1] that are obtained by successively placing the letter n + 1 into each of the classes of P in turn, and then placing n + 1 into its own singleton class. Thus, the children of the partition (1,3)(2,4,5) of [5] are the three partitions (1,3,6)(2,4,5), (1,3)(2,4,5,6), and(l,3)(2,4,5)(6)of [6].

12

CHAPTER 2

Inductively, suppose we have before us the list of all partitions of the set [n] arranged in a Gray code order so each partition is obtainable from its predecessor by a single move of one letter from one class to another. We make the corresponding list of partitions of [n + 1] as follows. Write down the children of the first partition on the list. Then write down in reverse order the children of the next partition. Then create the children of the next partition, then the reverse ... etc., and that's it. For example the partitions of [2] are (1,2) and (1)(2). Hence the list of partitions of [3] that we would create is (1,2,3), (1,2)(3), (1)(2)(3), (1)(2,3), (1,3)(2). It should be noted that when the complete lists are needed, the recursive form of these algorithms is sufficient. If, however, we want to produce the objects one at a time without storing them all at once, then nonrecursive versions may be helpful. Nonrecursive implementations of Knuth's algorithms have been given by Klingsberg [Kl] and Kaye [Ka]. Gray code constructions don't always work so smoothly. Consider, for a fixed positive integer n, the problem of arranging all of the partitions of the integer n in Gray code order. What we mean is that each partition should be obtainable from its predecessor by diminishing one part by 1 and adding 1 to another part (the latter to include creating a new part =1). This problem was treated by Yoshimura [Yo], who gave such lists for n = 1,2,.... 11. It has been completely solved by Carla D. Savage [7. Algorithms, to appear]. Here, for instance, is a list for n = 10: 10, 9+1, 8+2, 7+3, 6+4, 5+5, 5+4+1, 4+4+1+1, 4+4+2, 4+3+3, 3+3+3+1, 3+3+2+2, 3+3+2+1+1, 4+3+2+1, 5+3+2, 5+3+1 + 1, 6+3+1, 6+2+2, 5+2+2+1, 4+2+2+2, 3+2+2+2+1, 2+2+2+2+2, 2+2+2+2+1 + 1, 2+2+2+1 + 1 + 1 + 1, 3+2+2+1 + 1 + 1, 4+2+2+1 + 1, 4+3+1 + 1 + 1, 3+3+1+1+1 + 1, 3+2+1+1 + 1 + 1 + 1, 4+2+1 + 1 + 1 + 1, 5+2+1 + 1 + 1, 6+2+1+1, 7+2+1, 8+1 + 1, 7+1 + 1 + 1, 6+1 + 1+1 + 1, 5+1+1+1 + 1 + 1, 4+1 + 1 + 1+1+1+1, 3+1+1 + 1+1 + 1 + 1 + 1, 2+2+1+1+1+1 + 1 + 1, 2+1 + 1 + 1+1 + 1 + 1+1 + 1, 1+1+1 + 1+1 + 1+1 + 1+1+1. Here is one last Gray coding problem that has received attention recently. Consider, for n fixed, the triangulations of a labeled n-gon. This is a wellknown Catalan-countable family of objects. In that family we will now define an elementary operation, called a flip, which maps a triangulation onto another one. To do a flip, select a quadrilateral, delete the diagonal that it has, and draw the other one. In Fig. 2.2 we show a flip in a triangulation of a hexagon. The question, of course, is whether it is possible to arrange the set of all C/,-1 of these triangulations in a sequence with the property that each member of the sequence is a flip of its predecessor. Such an arrangement of the 5 triangulations of a pentagon is shown in Fig. 2.3, where, in each case, the edge that will get flipped is marked.

OTHER GRAY CODES

FIG. 2.2. A flip.

FIG. 2.3. Flipping through the triangulations.

13

14

CHAPTER 2

This question is not only pretty, but it relates, via the familiar family of bijections that map Catalan-countable problems onto each other, to other questions about other families. In the case of binary trees, it turns out that the motion of flipping triangulations maps into an operation called rotation of a binary tree, and so the question concerns the possibility of arranging all binary trees of « internal nodes in sequence so that each can be obtained from its predecessor by a single rotation. Here is a precise description of a rotation of a binary tree with respect to an edge (caution: it does not mean interchange of the right and left subtrees at some vertex). In Fig. 2.4 we show a binary tree before and after a flip with respect to the edge x - y. The symbols T\, TI, T^ denote the left and right subtrees at y and the right subtree at x, respectively. It may (or may not) help to visualize the operation by thinking of the tree as a mobile that hangs from a point of attachment at x. Then pick it up at y, lifting y above x. Then let the subtree TI slide down the string to become the left subtree of x, which wouldn't otherwise have one.

FIG. 2.4. A rotation in a binary tree.

Anyway, the operation of tree rotation is used in computer science to balance trees that are being used as data structures. The connection between the flips of triangulations and rotations of binary trees appears in the thesis of Lucas [Luc], who also gave the first proof of the existence of these Gray codes. Her successful solution of this problem involved overcoming a number of subtle difficulties. Weight sequences for binary trees were introduced by Pallo. They associate with a binary tree of n internal nodes, a sequence (w\,...,w n) that is constructed as follows: visit the internal nodes of the tree in inorder, which is to say, visit the left subtree, the root, and the right subtree, in that order. For each i = 1,«, as the /th internal node is visited, let tt>, denote the number of leaves in the left subtree at that node. A binary tree and its weight sequence are shown in Fig. 2.5. Observe that if a binary tree is obtained from another

OTHER GRAY CODES

15

FIG. 2.5. A binary tree of weight sequence (123116).

one by a single rotation, then its weight sequence differs from the original in only a single component.

This page intentionally left blank

CHAPTER 3 Variations on the Theme

Here is a variant of the idea of a Gray code. For fixed n, we want to list the permutations of Sn in such a way that each permutation will be totally different from its immediate predecessor. What that means is that if a and T are consecutive on the list, then for all i: a(i) / r(i). We can restate the condition by saying that whenever a and T are consecutive, then 1, where "(•)" denotes the "nearest-integer" function. There is a theorem of Jackson2 [Jal] which asserts that a 2-connected graph of N vertices in which each vertex has degree > A7 3 is Hamiltonian. It is easy to check (see below) that the Cayley graph of the symmetric group with respect to the set of fixed-point free permutations is connected for all n ^ 1,3 and is 2-connected3 [Im] for all n > 4. Further since

the hypotheses of Jackson's theorem are fulfilled for n > 4, and a Hamiltonian path exists. We do not know of any simple algorithm for an algorithmic description of such a path. 2 3

My thanks to Dr. Linda Lesniak for this reference. I thank Dr. Yahya Hamidoune for this reference. 17

18

CHAPTER 3

In the case n = 4, here is a list of the 24 permutations in which each one has no common values with its predecessor: 1234, 2143, 3214, 4321, 3412, 2341, 3124, 1432, 2314, 3421, 4132, 1423, 2134, 3241, 4312, 1243, 2431, 3142, 4213, 1324, 2413, 4231, 1342, 4123. Most of the interesting questions about Cayley graphs and their Hamiltonicity involve extremely sparse graphs, and for those we have no general theorems. The redeeming quality that these graphs do have, however, is their symmetry. Hence, what the world needs are some theorems to the effect that if a graph is somehow symmetrical enough, then it has a Hamilton path. A question that was raised in 1969 by Lovasz [Lov] addresses this need directly. Say that a graph G is vertex-transitive if for every pair v, w of vertices of G there is an automorphism a of G that carries v into w (the vertices of G are "all alike"). The question asks if it is true that every connected, vertex-transitive undirected graph has a Hamilton path. The graph (?„ in our little example above is certainly vertex-transitive, so if we can show that it is connected (we can), then an affirmative answer to Lovasz's question would imply that it is Hamiltonian also. In fact, it would imply that every Cayley graph with respect to a set of generators that is undirected, in that the inverse of each generator is also a generator, is Hamiltonian. The question that Lovasz asked has not been answered, but progress has been made, nonetheless. A survey article of Alspach [Al] reviews some of these partial results. Also, Babai [Ba] proved that every connected vertextransitive graph on n > 4 vertices has a cycle longer than V3n. Jackson [Ja2] showed that every 3-connected cubic graph has a cycle of length \V(G)\*, where t = .69.... Watkins [Wat] proved that if p(G) is the minimum vertex degree of G, and K(G) is its vertex connectivity, then l.u.b. p(G)/K(G) is exactly 3/2, where the l.u.b. is taken over all connected, vertex-transitive graphs G. In [NW], by the way, we described a directed Cayley graph that is not Hamiltonian. Consider the symmetric group as being generated by just two generators: a cyclic right-shift of one unit, and an interchange of the first and second values. We found that the Cayley graph here is Hamiltonian if n = 1,2,3,4, but not if n = 5. The status for n > 6 is unknown. THEOREM 3.1. The symmetric group Sn is generated by the fixed-point free permutations if and only ifn^l, 3. Proof. Since Sn is generated by the transpositions, it will be enough to decide when the transpositions are generated by the fixed-point free permutations. But if any single transposition is so generated, then they all are. Hence Sn is generated by the fixed-point free permutations if and only if the single transposition

is a product of fixed-point free permutations.

VARIATIONS ON THE THEME

19

First, let n be odd, n > 5, and let

Then r"~2 = 6. With T as in the previous case, define

Then cor""4 = a, which completes this case. If n = 4 let o)\ in cycle form be (1432) and let (02 in cycle form be (1324). Then o>2wf = a. The case n = 2 is trivial. By drawing G$, we see that it has two connected components, so all assertions have now been proved. D The following result is stronger and is also easier to prove. THEOREM 3.2. Fix n > 5. Let C be a fixed conjugacy class of the symmetric group Sn. Then C generates Sn if and only ifC contains an odd permutation. Proof.4 Consider the subgroup H that C generates. Then clearly H is normal in Sn. Suppose H is a proper subgroup. If n > 5 the only proper normal subgroup that Sn has is the alternating group An. If C contains an odd permutation then H cannot be An so it must be all of Sn. Conversely, if C consists of even permutations only, then so does H, and so it cannot be the full symmetric group. D

4

This pretty proof was found independently by two of my colleagues, Drs. Gerstenhaber and Nijenhuis.

This page intentionally left blank

CHAPTER 4

Choosing 2-Samples

This chapter contains a small technical procedure that will be of use in Chapter 6, where we will be selecting random free trees. The problem is quite simple to state. Given a device that will produce, on demand, one of N given objects, uniformly at random (u.a.r.), it is required to produce, u.a.r., one of the (N^1} unordered pairs (= "2-samples") of the objects. The objects are to be thought of as complicated, so it may be expensive to ask if two of them are different or the same. We will give two methods, of which the first is really here only to define the problem a little better, and the second is the one that works. Method 1. Choose two objects independently u.a.r. and output the pair. If we use this method then while for A ^ B, so pairs of distinct elements are chosen twice as often as the others. If we attempt to rectify this by rejecting such pairs occasionally, then we run into the problem of recognizing these pairs at all, which we have hypothesized as being expensive to carry out. Method 2. For a fixed real number p, (Al) with probability p do choose an object a> and output {co, a>} or (A2) with probability 1 - p do choose 2 objects (a/, a/') independently u.a.r. and (without examining them further!) output the pair {CD', to"}. D Now let A be some fixed object. What is the probability that Method 2 produces the output {A,A} (note that either (Al) or (A2) can give this result)? It is Next suppose that A, B are two distinct objects. Then what is the probability that the unordered pair {A, B} will be output? It is These two probabilities are equal if we use the p that satisfies 21

22

CHAPTER 4

i.e., if p = \I(N +1), and we have the following algorithm.

ALGORITHM 2-SAMPLE.

With probability l/(N + 1), choose an object &> u.a.r. and output the pair {co, G)}, else, With probability N/(N + 1), choose two objects w!, to" independently u.a.r. and output the pair {&>',«"}. D

CHAPTER 5 Listing Rooted Trees

In the years since [NW] appeared there has been a great deal of work on algorithms for selection of trees. In particular, there are now efficient algorithms for listing all rooted trees of given size [BH], listing all free trees of given size ([Re], [DZ], [Ko], [WROM]), choosing uniformly at random a free tree of a given number of vertices [Wi], and ranking and unranking rooted and free trees ([WY], [Yo]). In this chapter we discuss the problem of listing rooted trees in constant average time. The results are due to Beyer and Hedetniemi [BH]. If n is fixed, their algorithm will produce a list of all rooted trees of n vertices where, in a certain encoding, the average labor per tree is uniformly bounded, as a function of «. In their scheme, rooted trees are encoded by means of their level sequences. To define the level sequence of a tree we must first define it for an ordered tree. In that case we begin by visiting its vertices in preorder, which means (a) visit the root, and then (b) visit the subtrees in order. While the visitation is going on we must note the level on which each vertex lives at the time we visit it. The root is defined to live on level 1. In general, the level of a vertex is equal to the number of vertices in the path that joins it to the root. The result of visiting the vertices in preorder and writing down the levels on which they live is the level sequence of the given ordered tree. In Fig. 5.1 we show an ordered tree and its level sequence. The vertices of the tree are unlabeled but we show next to each vertex the epoch at which it gets visited. The appearances of the 2's in a level sequence of a tree T are important. If a 2 appears in a certain position, then we have arrived at the level just below the root, i.e., we are at the root of one of the subtrees at the root of T. Hence all entries in the level sequence, beginning with that 2, and ending just before the next 2, comprise the level sequence of a single root subtree of T. An ordered tree of course comes equipped with an ordering of its subtrees. The level sequence provides another, natural, ordering of all subtrees. Precisely, let U and V be subtrees of an ordered tree T. The level subsequence L(U) of U is the (consecutive) subsequence of L(T) that belongs to vertices of U. The level subsequences of all subtrees of T can be ordered lexicograph23

24

CHAPTER 5

FIG. 5.1. An ordered, rooted tree, with its visitation and level sequences.

ically. Then we can say that U < V if L(U) < L(V) in that ordering. In general, if T is some given rooted tree, then there will be many ordered trees T' such that T' is an ordering of T. We will now specify a single ordered tree T* to be the canonical ordered tree that corresponds to T. It is the one ordered tree, among all of those that correspond to T, whose level sequence is the largest, in the lexicographic ordering of level sequences. In Fig. 5.2 we show two ordered trees that have the same underlying rooted tree T, and in fact, they are the only two ordered trees that T has. Their level sequences are, respectively, (1,2,3,3,2) and (1,2,2,3,3), of which the first is the larger. Hence T\ is the canonical ordered tree that corresponds to the rooted tree T, and we write TI = T*.

FIG. 5.2. T corresponds to these two ordered trees.

We say that two subtrees of an ordered tree are adjacent if their roots are two consecutive children of the same parent vertex. In a level sequence L(T), the subsequences that belong to two adjacent subtrees are two consecutive blocks.

LISTING ROOTED TREES

25

We say that a level sequence of a rooted ordered tree is regular if whenever U and V are two consecutive (in that order) adjacent subtrees then L(U) > L(V). It is then easy to prove the following proposition. PROPOSITION. An ordered tree is the canonical ordering of a given rooted tree if and only if its level sequence is regular. In that case, if two consecutive elements of the level sequence are equal to 2, then all remaining elements are equal to 2. D Now we're just about ready to write down the algorithm for generating rooted trees of given size. If n is given, then what we are going to do is to write down the level sequences of all canonical ordered trees that correspond to rooted trees of n vertices. In other words, the encoding of trees that is used is that a rooted tree is encoded by the level sequence of its associated canonical ordering. What we would like to avoid is writing down every possible level sequence and discarding the ones that aren't regular. Instead, Beyer and Hedetniemi [BH] found a way to hop directly from one of those level sequences to its successor. It hinges on the positions of the 2's in the original sequence. Let a level sequence L = (l\,li,...,ln) be given. If L contains no entry > 2 then the algorithm halts and all trees of n vertices have been generated. Else, let lp be the rightmost entry of L that is > 2. Let lq be the rightmost position preceding p such that lq = lp-\ (vertex q is the parent of vertex p). The successor s(L) of the level sequence L is defined by

What this amounts to is that we take a certain subtree and make enough copies of it to fill in the n entries of the level vector, including possibly a partial copy at the end. We show in Fig. 5.3 some examples of canonical level sequences and their successors. L(T0

s(L(T))

(1,2,3,2,2,2) (1,2,2,2,2,2) (1,2,3,4,2,2) (1,2,3,3,3,3) (1,2,3,4,5,5,2,2,2,2) (1,2,3,4,5,4,5,4,5,4) (1,2,3,3,2,3,3) (1,2,3,3,3,3,3) FIG. 5.3. Examples of successors.

This page intentionally left blank

CHAPTER 6

Random Selection of Free Trees

In [NW] we discussed how to choose, uniformly at random, a rooted tree of a given number n of vertices. It isn't hard to extend this to an algorithm for choosing, u.a.r., a free (i.e., unlabeled and unrooted) tree of n vertices, so we will discuss that extension here, following [Wi]. Hence we assume that Algorithm Ranrut of [NW] is available, and we will use it here as a subroutine. The main point is that free trees have hidden "roots," called centroids. It turns out that every free tree has exactly one or two of these distinguished vertices, and that they can serve as roots for Algorithm Ranrut. We remark here that Algorithm Ranrut has unknown (to me, anyway) average complexity. It seems that it runs in linear or just slightly superlinear time, but the analysis appears to be difficult. I am willing to make a formal conjecture that, for every e > 0, Algorithm Ranrut will select a rooted tree of n vertices in average time O(nl+c), and perhaps even e = 0. To get back to free trees, we will first define a centroid. For each vertex v of a free tree T we let w(v), the weight of v, denote the size of the largest subtree that is rooted at v. A centroid v* is a vertex of minimum weight. The main properties of centroids are (see [Knl] for proofs) the following: (1) Every tree T has exactly one or exactly two centroids; (2) T has two centroids if and only if these are joined by an edge of T, and removal of that edge would leave two trees of equal sizes; (3) A vertex X is the unique centroid of a tree T of n vertices if and only if w(X) is Euler's function. The appearance of the "l/«" in front of this formula means that the sum by itself does overcounting that needs to be compensated for. Consequently, it will be hard to use the derivation of this formula as a model for an algorithm that would produce a list of all (n, k) necklaces. Indeed, such an algorithm would involve examining each necklace as it is produced and checking somehow to see if it had previously been produced. Hence there are considerable difficulties involved in listing necklaces of given type. As far as I know there are no known loop-free algorithms for doing so. That is, they all engage in some following of blind alleys with backtracking, rejection, and ultimate selection. The random selection process is a lot easier because the factor of 1/n that caused the troubles in listing is not a bit troublesome for random selection since the same factor is common to all necklaces, and it doesn't affect the uniformity of the distribution. Thus there are various ways of sampling necklaces with equal probabilities. We would like now to describe one of these, but in a setting that is a little more general than the necklace problem. Just as in the problem of counting the necklaces, where one can use very special methods or one can invoke Burnside's lemma, we choose here to give an algorithm that randomly selects an orbit of the action of a group of permutations on a set. That is, the algorithm will apply to precisely the situations that Burnside's lemma covers. The discussion follows [DW]. When a finite group G acts as a group of permutations on a set Q it induces an equivalence relation on Q, in which the classes are called the orbits of the action of G. For every g e G we let Fix(g) be the set of objects (elements of £1) that are fixed by g. Then classical Burnside theory gives us the following facts. (a) The number of orbits is

(b) For each orbit co, define

GENERATING RANDOM GRAPHS

39

Then |rw| = \G\ for all orbits co. (c) For each orbit co and for each conjugacy class C of G, every element of C has the same number of fixed points in co. In particular, each element of G has the same number of fixed points. Property (b) above is the basis for the algorithm that selects an orbit of the action uniformly at random. Indeed, it implies that the multiset U?ecF/x(g) contains exactly \G\ representatives of each orbit, and consequently if we choose an element from that multiset u.a.r., then the orbit in which the element lives is distributed u.a.r. The algorithm that follows uses that idea, but economizes it by working with conjugacy classes rather than with the elements of G themselves, as permitted by property (c) above. Let C\,...,Cr be the conjugacy classes of G. Let hj — |C7| and let gj be a representative of C/, for each j = 1,..., r. Further, let the weight of a conjugacy class C/ be Wj = hj\Fix(gj)\, and note that from property (a) above, we have £/ Wj = m\G\. In terms of these parameters we can now state the algorithm. We will then give two examples of its operation. The first will be to the necklace problem, where the group, the conjugacy classes, etc., are all very transparent, and the second will be to the selection of unlabeled graphs, where life is more complicated. ALGORITHM RANDOM ORBIT. [Input is a group G that acts on a set Q. Output is an orbit co chosen u.a.r. from the orbits of the group action.] RO1 Choose a conjugacy class of G so that the probability of choosing C/ is Prob(Cj) = Wj/(m\G\)(j = 1,..., r), where m is the number of orbits, given by (8.1). RO2 Let Cj be the conjugacy class that was chosen in step RO1, and let gj be its representative. Then choose an element a u.a.r. from Fix(gj). RO3 Return the orbit co that contains a. D The following computation shows that each orbit co has the same a priori probability (= 1/m) of being chosen by this algorithm:

40

CHAPTER 8

Example 1. Choosing necklaces at random. In this example we fix two integers 1 < k < n, and take the set Q of objects to be the set of k" necklaces of n labeled beads, where each bead is colored in one of the k colors 1,2,...,k. The beads are labeled with 1,2,...,n, so each bead has a label that gives its position on the necklace, and a color. The group G is the cyclic group of order n, which we take in the form

where c is a cyclic right shift of one unit. For each j, 1 < j < n, the mapping cj has gcd(n, j) cycles, each of length n/gcd(n, j). Since the cycle length set is the only conjugacy invariant, there is a conjugacy class Q for each divisor d of n. There are r — d(ri) conjugacy classes altogether, and the class Q contains exactly hd = \Cd\ = (j>(n/d) elements of G, for each d\n. A representative of Cj is gj = cd and

It is now trivial to check that Buraside's lemma gives the number of orbits m as in (8.1) above. The weight wj of the class Q is 4>(n/d)kd, and Algorithm Random Orbit boils down to the following: 1. Choose a class Q with probability

2. Choose d colors from the available set {1,2,...,A:} independently u.a.r. Then color beads 1, d+1, Id +1,... in the first chosen color, and beads 2, d+2, 2d + 2,... in the second color chosen, etc. until all d chosen colors have been used (and all beads have been colored!). We have now chosen an element a u.a.r. from Fix(cd). 3. Erase the labels of the beads, leaving only their colors, and output the resulting necklace. Example 2. Choosing unlabeled graphs at random. The setting is now the following: A positive integer n is fixed. The set Q of objects is the set of all vertex-labeled graphs of n vertices. The group that acts on Q is the symmetric group Sn and it acts by permuting vertex labels. Now consider, for a fixed permutation g e Sn, the set Fix(g) of graphs that are fixed by g. We associate with g another permutation g* which acts not on the n letters 1,2,..., n but on the (£) unordered pairs of those letters. Alternatively, we can say that g* acts on the edges of the complete graph Kn. Its action is just this: g*{i,j} = {gi.gj}. Now Fix(g) can be described easily. It is the set of all graphs whose edge sets are mapped into themselves by g*. Again, a graph H is fixed by g if and only if for each cycle q of the induced permutation g* either all pairs in q are edges of H or none of them are.

GENERATING RANDOM GRAPHS

41

It is easy to count the graphs that g fixes. For each cycle of g* we must either take all of the edges in the cycle or none of them, so there are exactly 2c(g) graphs in Fix(g), where c(g) is the number of cycles of the pair permutation g*. Next we want to express these counts in a more quantitative nuts-and-bolts fashion. Hence we let (k\, k^,..., kn) denote the partition of the integer n that has exactly kt parts of size /, for all / > 1. We let [ki, k-i,..., kn] denote the corresponding conjugacy class of Sn, which consists of all permutations that have fc, cycles of size / for each / > 1. A well-known and elementary counting argument shows that the number of permutations in the conjugacy class [k\, ki,..., kn] is exactly

The second fact that we need is less well known. It states the following. THEOREM 8.1. The number of cycles in the induced pair permutation g* can be expressed in terms of the cycle structure of g itself as

where is Euler's function and /(/) = £)/|; kj. The following proof is from Robinson [Ro]. Consider separately the edges of the complete graph Kn that join distinct cycles of g and those that join points of the same cycle of g. There are ij edges in Kn that join distinct cycles of length i and length j of g, and g* acts on these to produce gcd(ij) cycles of lcm(i,j) points each. On the other hand, the edges of Kn that join points of a single cycle of g whose length is 2i + 1 are permuted into i cycles of length 2i + 1, while the edges in a single cycle of length 2i are permuted into / — 1 cycles of length 2i and one of length i, and the formula results immediately. The orbits of the action of Sn on fl are the unlabeled graphs of n vertices. Hence, Algorithm Random Orbit applies to this situation and will produce unlabeled graphs of n vertices with equal a priori probabilities. The weight of the conjugacy class [k\,..., kn] is

where c(g) is computed from the theorem above.

42

CHAPTER 8

ALGORITHM RANDOM GRAPH. RG1 Choose a partition of the integer n such that the probability of choosing n = (ki kn) is

RG2 Take a representative g from the class that corresponds to the partition n, and choose u.a.r. a (labeled) graph H from Fix(g). RG3 Erase the labels and output the unlabeled graph H. D Concerning the actual implementation of this algorithm, consider step RG2 first. To choose the representative of the conjugacy class, all we need is any permutation whose cycle partition is the given one. That's easy to do. A permutation for which [fc,, k2, k3] = [2,2,1], for example, is (1)(2)(34)(56)(789). Next we have to choose a graph that is fixed by the induced permutation g*. To do that, just follow around all of the cycles of g*, and then decide, independently u.a.r. for each cycle, whether to admit all of its edges into the graph H or to admit none of them. The time required to trace the cycles of g* and make these decisions is clearly O(n2}. In step RG1 we must select a partition of the integer n with certain given probabilities attached to each partition. There are a great many partitions of n, around eK^" of them, so we do not want to have to look at very many of them, on average, before finding the winner. Hence it is important to know which of the partitions are most likely to occur, and to arrange the calculation so we examine the partitions in roughly decreasing order of their likelihoods of being chosen. In that way we'll be more likely to get the game over quickly. The fact is that the partitions of n that have many parts = 1 are the ones that are most likely to be chosen. Indeed the one partition whose parts are all ones has by far the largest probability. Its weight is 2")/n!, and that one term already gives the correct asymptotic behavior of the number of unlabeled «-graphs. It was shown by Oberschelp [Ob] that the total probability of all partitions of n that have no more than r 1's is O(nr2~nrl2), so the probabilities decrease geometrically as the number of parts = 1 decreases. A careful analysis shows that the average number of partitions that need to be looked at before selecting one is no more than 3 if they are examined in a sequence that respects the number of 1's in the partition. For the details, see [DW]. The result is that with a careful implementation we can choose unlabeled n-graphs u.a.r. in average time O(n2} per graph, which is surely the best that could be expected.

Bibliography

[Al] BRIAN ALSPACH, The search for long paths and cycles in vertex-transitive graphs and digraphs; in Combinatorial Mathematics VIII, Proc. Eighth Australian Conference on Combinatorial Math., Lecture Notes in Mathematics 884, Springer-Verlag, Berlin, 1981, pp. 14-22. [Ar] BENJAMIN ARAZI, An approach for generating different types of Gray codes, Inform, and Control, 63 (1984), pp. 1-10. [Ba] L. BABAI, Long cycles in vertex-transitive graphs, J. Graph Theory, 3 (1979), pp. 301-304. [BER] JAMES R. BITNER, GIDEON EHRLICH, AND EDWARD M. REINGOLD, Efficient generation of the binary reflected Gray code and its applications, ACM Comm., 19 (1976), pp. 517-521. [BH] TERRY BEYER AND SANDRA MITCHELL HEDETNIEMI, Constant time generation of rooted trees, SIAM J. Comput., 9 (1980), pp. 706-712. [BW] M. BUCK AND D. WIEDEMANN, Gray codes with restricted density, Discrete Math., 48 (1984), pp. 19-29. [CLD] C. C. CHANG, R. C. T. LEE, AND M. W. Du, Symbolic Gray code as a perfect multiattribute hashing scheme for partial match queries, IEEE Trans. Software Engrg., 8 (1982), pp. 235249. [DW] JOHN D. DIXON AND HERBERT S. WILF, The random selection of unlabeled graphs, J. Algorithms, 4 (1983), pp. 205-212. [DZ] E. A. DINITS AND M. A. ZAITSEV, Algorithm for the generation of nonisomorphic trees, Avtomat. i Telemekh., 4(1977), pp. 121-126; Automat. Remote Control, 38 (1977), pp. 554-558. [EHR] P. EADES, M. HICKEY, AND R. C. READ, Some Hamilton paths and a minimal change algorithm, J. Assoc. Comput. Mach., 31 (1984), pp. 19-29. [EM] PETER EADES AND BRENDAN MCKAY, An algorithm for generating subsets of a fixed size with a string minimal change property, preprint ,1982. [Er] M. C. ER, Two recursive algorithms for generating the binary reflected Gray code, J. Inform. Optim. Sci., 6(1985), pp. 213-216. [Fr] MICHAEL FREDMAN, Observations on the complexity of generating quasi-Gray codes, SIAM J. Comput., 7 (1978), pp. 134-146. [FR] P. FLAIOLET AND LYLE RAMSHAW, A note on Gray code and odd-even merge, SIAM J. Comput., 9(1980), pp. 142-158. [Gi] E. N. GILBERT, Gray codes and paths on the n-cube, Bell System Tech. J., 37 (1958), pp. 815-826. [Gr] Bell System Tech. J., 18 (1939), p. 252. [Ha] RICHARD W. HAMMING, Coding and Information Theory, Prentice-Hall, Englewood Cliffs, N.J., 1980. [Im] W. IMRICH, On the connectivity of Cayley graphs, J. Combin. Theory Ser. B, 26 (1979), pp. 323-326. [Jal] BILL JACKSON, Hamilton cycles in regular 2-connected graphs, J. Combin. Theory Ser. B, 29(1980), pp. 27-46. [Ja2] , Longest cycles in 3-connected cubic graphs, J. Combin. Theory Ser. B, 41 (1986), pp. 17-26. 43

44

BIBLIOGRAPHY

[JW] J. T. JOICHI AND DENNIS E. WHITE, Gray codes in graphs of subsets, Discrete Math., 31 (1980), pp. 29-41. [JWW] J. T. JOICHI, DENNIS E. WHITE, AND S. G. WILLIAMSON, Combinatorial Gray codes, SIAM J. Comput., 9 (1980), pp. 130-141. [Ka] RICHARD KAYE, A Gray code for set partitions, Inform. Process. Lett., 5 (1976), pp. 171-173. [Kl] PAUL KLINGSBERG, A Gray code for compositions, J. Algorithms, 3 (1982), pp. 41-44. [Knl] DONALD E. KNUTH, The Art of Computer Programming, Vol. 1: Fundamental Algorithms, Addison-Wesley, Reading, MA, 1968. [Kn2] , personal communication, 1988. [Ko] A. V. KOZINA, Coding and generation of nonisomorphic trees, Cybernetics, 15 (1975), pp. 645-651. Kibernetika, 5 (1979), pp. 38-43. (In Russian.) [KP] P. KIRSCHENHOFER AND H. PRODiNOER, Subblock occunences in positional number systems and Gray code representation, J. Inform. Optim. Sci., 5 (1984), pp. 29-42. [Lov] L. LOVASZ, Problem 11, Combinatorial structures and their applications, Gordon and Breach, New York, 1970. [Lu] HEINZ LUNEBERG, Gray codes, Abb. Math. Sem. Univ. Hamburg, 52(1982), pp. 208-227. [Luc] J. LUCAS, The rotation graph of binary trees is Hamiltonian, Tech. Report TR-021, Dept. of Computer Science, Princeton University, Princeton, N.J., 1986. [NW] A. NUENHUIS AND H. S. WiLF, Combinatorial algorithms, second edition, Academic Press, New York, 1978. [Ob] W. OBERSCHELP, Kombinatorische anzahlbestimmungen in Relationen, Math. Annalen, 174(1967), pp. 53-58. [Pr] HELMUT PRODINCER, Nonrepetitive sequences and Gray code, Discrete Math., 43 (1983), pp. 113-116. [PR] ANDRZEJ PROSKUROWSKI AND FRANK RUSKEY, Binary tree Gray codes, J. Algorithms, 6 (1985), pp. 225-238. [Re] R. C. READ, How to grow trees, in Combinatorial Structures and Their Applications, Gordon and Breach, New York, 1970. [RH] F. RUSKEY AND T. C. Hu, Generating binary trees lexicographically, SIAM J. Comput, 6 (1977), pp. 745-758. [Ri] DANA RICHARDS, Data compression and Gray-code sorting, Inform. Process. Lett., 22 (1986), pp. 201-205. [RM] F. RUSKEY AND D. J. MILLER, Adjacent interchange generation of combinations and trees, University of Victoria, British Colombia, Canada, DCS-44-IR, 1984. [Ro] ROBERT W. ROBINSON, Enumeration of Euler graphs, in Proof Techniques in Graph Theory, F. Harary, ed., Academic Press, New York, 1969, pp. 147-153. [RP] FRANK RUSKEY AND ANDRZEJ PROSKUROWSKI, Generating binary trees by transpositions, preprint, 1987. [Ru] FRANK RUSKEY, Generating t-ary trees lexicographically, SIAM J. Comput., 7 (1978), pp. 424-439. [Ru2] , Adjacent interchange generation of combinations, J. Algorithms, to appear. [SKI] B. D. SHARMA AND R. K. KHANNA, J. Combin. Inform. System Sci., 4 (1979), pp. 227-236. [SK2] , On level weight studies of binary and m-ary Gray codes, Inform. Sci., 21 (1980), pp. 179-186. [SK3] , On m-ary Gray codes, Inform. Sci., 15 (1978), pp. 31-43. [Tr] A. TROJANOWSKI, Ranking and listing algorithms for k-ary trees, SIAM J. Comput., 7 (1978), pp. 492-509. [Wat] MARK E. WATKINS, Connectivity of vertex transitive graphs, J. Combin. Theory, 8 (1970), pp. 23-29. [Wi] H. S. WILF, The uniform selection of free trees, 3. Algorithms, 2 (1981), pp. 204-207. [Wo] NICOLAS C. WORMALD, Generating random regular graphs, J. Algorithms, 5 (1984), pp. 247-280. [WROM] ROBERT ALAN WRIGHT, BRUCE RICHMOND, ANDREW ODLYZKO, AND BRENDAN D. McKAY, Constant time generation of free trees, SIAM J. Comput., 15 (1986), pp. 540-548.

BIBLIOGRAPHY

45

[WY] HERBERT S. WILF AND NANCY A. YOSHIMURA, Ranking rooted trees, and a graceful application, in Perspectives in Computing, Proc. Japan-U.S. Joint Seminar in Discrete Algorithms and Complexity, June 4-6, 1986, Kyoto, Japan, Academic Press, 1987, pp. 341-350. [Yo] NANCY ALLISON YOSHIMURA, Ranking and unranking algorithms for trees and other combinatorial objects, Ph.D. thesis, Computer and Information Science, University of Pennsylvania, Philadelphia, 1987. [Yu] C. K. YUEN, The separability of Gray code, IEEE Trans. Inform. Theory (1974), p. 668. [Zal] S. ZAKS, Lexicographic generation of ordered trees, Theoret. Comput. Sci., 10 (1980), pp. 63-82. [Za2] , Generating k-ary trees lexicographically, Tech. Report UICSCS-R77-901, University of Illinois, Urbana, IL, 1977.

This page intentionally left blank

Index

minimal-change algorithms, 9

adjacent interchange Gray code, 10 Algorithm Forest, 28 Algorithm Free, 29 Algorithm Random Orbit, 39-41 Algorithm Random Graph, 42 Algorithm Ranrut, 27, 28 Algorithm 2-Sample, 22, 27, 28 algorithms, minimal-change, 9 alternating group, 19 automorphism, 18

"nearest-integer" function, 17 necklaces, 37, 38-40 orbits, 38, 39 ordered tree, 23-25

bicentral, 31, 32, 34 bicentroidal, 28 binary tree, 14, 15 bipartite graphs, 37 Burnside's lemma, 38, 40 canonical level sequences, 25, 32, 34, 35; primary, 32, 34, 35 canonical ordered tree, 24, 25 Catalan-countable problems, 14 Cayley graph, 17, 18 center, 31, 32, 36 centroids 27, 31 compositions of an integer, 10 conjugacy class 19, 39, 41, 42; weight of, 39 Euler's function, 38, 41 fixed-point free permutations, 17, 18 free trees, 23, 28, 29, 31-34, 36, 37

partitions of a set, 11, 12 Pascal triangle, 8 planar graphs, 37 prefab, 28 preorder, 23 primary rooted version, 32 primary canonical level sequence, 32, 34,35 principal subsequences, 35 random orbit, 39, 40 ranking problem, 5 regular graphs, 37 revolving door algorithm, 8, 9 rooted forest, 28 rotation of a binary tree, 14 set, partitions of, 11, 12; set, subsets of, 7 standard reflected Gray code, 2 strong revolving door order, 10 subsets of a set, 7 symmetric group, 17, 19

Gray code, 1-12, 14, 17; adjacent interchange, 10; standard reflected, 2

transpositions, 7, 18 triangulations, 12-14 2-sample, 22, 27, 28

Gray, Frank, 1

unranking problem, 5

Hamilton path, 3, 17-18 Hamilton walk, 3, 17 Hamiltpnian graphs, 37 Hamming distance, 5, 6

vertex-transitive, 18 weight of a conjugacy class, 39 weight sequences, 14, 15

integer, compositions of, 10 level sequences, 23-25, 31, 32, 34, 35; canonical, 25, 32, 34, 35 listing rooted trees, 23 47

Our partners will collect data and use cookies for ad personalization and measurement. Learn how we and our ad partner Google, collect and use data. Agree & close