You are on page 1of 311

Combinatorial

Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatoria
Combinatori
Combinator
Combinato
Combinat
Combina
Combin
Combi
Comb
Com
Co
C
C
Co
Com
Comb
Combi
Combin
Combina
Combinat
Combinato
Combinator
Combinatori
Combinatoria
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial
Combinatorial

Generation
Generatio
Generati
Generat
Genera
Gener
Gene
Gen
Ge
G
October 1, 2003

Combinatorial
ombinatorial
mbinatorial
binatorial
inatorial
natorial
atorial
torial
orial
rial
ial
al
l

Combinatorial Generation
Working Version (1j-CSC 425/520)
no comments printed; pseudo-code version

Frank Ruskey
l
al
ial
rial
orial
torial
atorial
natorial
inatorial
binatorial
mbinatorial
ombinatorial
Combinatorial

Department of Computer Science


University of Victoria
Victoria, B.C. V8W 3P6
CANADA
fruskey@csr.csc.uvic.ca

G
Ge
Gen
Gene
Gener
Genera
Generat
Generati
Generatio
Generation

Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
eneration
neration
eration
ration
ation
tion
ion
on
n
n
on
ion
tion
ation
ration
eration
neration
eneration
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation
Generation

i
The covers of your book are too far apart. George Bernard Shaw.
No man but a blockhead ever wrote except for money. Dr. Samuel Johnson.
Everything has been said before by someone. Alfred North Whitehead
It is a wise father that knows his own child. William Shakespeare, The Merchant
of Venice, Act II, Scene II, 73. good quote for chapter 8.

There are various versions of this book. If there is a comment printed as a footnote to
this sentence then comments to myself are printed. These contain bibliographic remarks,
sources of info, rewriting suggestions, etc.

c
uvic01, Frank
Ruskey, 19952001

ii

c
uvic01, Frank
Ruskey, 19952001

Preface
Humanity has long enjoyed making lists. All children delight in their new-found ability
to count 1,2,3, etc., and it is a profound revelation that this process can be carried out
indefinitely. The fascination of finding the next unknown prime or of listing the digits
of appeals to the general population, not just mathematicians. The desire to produce
lists almost seems to be an innate part of our nature. Furthermore, the solution to many
problems begins by listing the possibilities that can arise.
There are lists of mathematical objects that have a historical interest, or maybe I should
say that there are lists of historical objects that have a mathematical interest. Examples
include the Fu Hsi sequencing of the hexagrams of the I Ching (e.g., as described in Martin
Gardner - Knotted Donuts [148]) and the Murasaki diagrams of the Tale of the Genji (by
Lady Shikibu Murasaki, 1000 A.D.) as explained in Gould [158]. Viewed properly, these are
lists of the first 64 binary numbers 0, 1, . . . , 63 and all 52 partitions of a 5-set, respectively.
They are illustrated in Figures 1 and 2 at the end of this preface.
However, it was not until the advent of the digital computer that the construction of
long lists of combinatorial objects became a non-tedious exercise. Indeed, some of the first
applications of computers were for producing such lists. With the computer as our able
assistant we can construct interesting lists to our hearts content. There is deeply satisfying
feeling that one obtains by watching a well ordered list of combinatorial objects marching
down the computer screen. The author hopes that the readers of this book will implement
some of the algorithms found herein and experience this feeling for themselves!
These lists are not only interesting but can be extremely useful. Nearly every computer
scientist and mathematician has encountered some instance where they solved a problem
simply by listing all the possibilities and checking for a desired property. One can find
applications of the ideas in this book to circuit design, statistical computing, chemistry,
and operations research, just to name a few.
Ever since the 1960s there has been a steady flow of new algorithms for constructing
lists of combinatorial objects of various types. There is no widely accepted name for the
field but I like to call it Combinatorial Generation. Many of the papers in the field have
a title that contains the word generating. Unfortunately, generate is an overworked
word in mathematics and computer science, so the name is not fully satisfying. A widely
used alternative is enumerate, which, unfortunately, also has another widely accepted
meaning, to count, as used in Goulden and Jacksons book Combinatorial Enumeration
[157]. Some people use generate to mean generate uniformly at random. In algebra,
generator is a term use for a subset of an algebraic structure that, via some operation,
can produce the entire structure, as in the generators of a group. A good alternative
title for the book would be Exhaustive Listing of Combinatorial Objects. Nevertheless,
Combinatorial Generation is the name chosen and used in this book.
iii

iv
Several books contain chapters about generating combinatorial objects, but there is no
book devoted solely to the subject (with pehaps the exception of Wilfs update [451] of
[289]). The books of Nijenhuis and Wilf [289], and of Reingold, Nievergelt and Deo [340]
contain such chapters and have provided an inspiration for me over the years.
Many
of the ideas of the present book can be traced back to these books. I would also like to
mention the influential books of Even [115], Page and Wilson [296], Rohl [347], Wells [440],
and Williamson [453], all of which contain interesting material on generating combinatorial
objects. The forthcoming Volume 4 of the Art of Computer Programming by Knuth will
contain material on combinatorial generation.
The concern of this book is not with existence questions By an existence question we
mean one of the form find a single instance of a combinatorial object with certain properties. These questions have quite different methods of attack and warrant a separate
treatment, although backtracking, which is covered is a widely used technique for solving
existence questions. Existence questions arise, for example, in the search for various types
of block designs. The recent book of Kreher and Stinson [233] covers such techniques, as
well as overlapping some of the exhaustive listing material discussed in this book.
Of course, the particular topics that are treated here reflect my own personal choices
about the elegance, importance and accessibility of particular topics not to mention that
they are those with which I am most familiar and find most interesting.
There is something of interest for many types of readers to be found in the chapters
to follow. The reader who simply wants a fast algorithm to generate a well-known class of
objects will find it here. Many algorithms are fully implemented as Pascal procedures, others
with a Pascal-like pseudo-code. The analysis of generation algorithms provides interesting
problems for those interested in combinatorial enumeration. The analysis of most of our
algorithms involves some sort of counting problem, and many techniques from enumerative
combinatorics have been brought to bear in solving these counting problems. The software
engineer will find interesting nontrivial, but short, procedures. These provide excellent
examples for students on which to try their skill at providing formal proofs of correctness
of programs.
Recently, there seems to be an explosion of results on combinatorial generation. This
renewed interest is fueled by many sources: the increasing speed of computers, mathematical
interest in Hamiltonicity of graphs, the emergence of parallel computers, the complexity
theory of counting problems, and so on. It is my belief that a book on combinatorial
generation is timely.
Many of the programs presented in this book can be downloaded from the authors
Combinatorial Object Server (COS) at http://www.theory.csc.uvic.ca/~cos. The
main purpose of this site is to provide an on-line resource for the generation of small
numbers of combinatorial objects. The user first selects a type of object, then supplies
input parameters, and output options, and COS returns the appropriate list of objects.
There are very few universities that offer courses on combinatorial generation so I dont
expect this to be often used as a textbook (of course, the appearance of a suitable text may
change that). The book is mainly intended as a reference and perhaps for supplementary
reading in a course. Each chapter contains some exercises, and some contain extensive sets
of exercises. It is quite feasible to use the book as a textbook, but it would be essential
for prospective students to have had a course in discrete mathematics or combinatorics and
enough programming experience to be totally comfortable with recursion. Other than that
the background necessary to read the book is minimal. Most of the material should be
c
uvic01, Frank
Ruskey, 19952001

v
accessible to the typical undergraduate Computer Science or Mathematics major.
The first chapter discusses some general issues in combinatorial generation. The second
chapter reviews some basic definitions and counting results that are useful in the rest of the
book; the reader is advised to skip over this chapter and just refer to it as needed. Chapter
3 is a short introduction to backtracking. The following chapters are broadly organized by
type of algorithm. Chapter 4 is Lexicographic Algorithms, Chapter 5 concerns algorithmic
aspects of Gray codes and Chapter 6 is a continuation, concentrating on graph theoretic
aspects of Gray codes. Chapter 7 considers problems that can be modeled as Eulerian cycles,
such as De Bruijn cycles. Chapters 4, 5, 6, and to a lesser extent, Chapter 7 are concerned
with elementary combinatorial objects. Orderly Algorithms are covered in Chapter 8 and
Chapter 9 looks at problems of generating various types of subgraphs of graphs, such as
spanning trees and cliques. Parallel algorithms for generating combinatorial objects are
discussed in Chapter ??. The uniform random selection of objects is the subject of Chapter
10. Some complexity issues are discussed in Chapter ??. And finally, Chapter ?? contains
solutions and hints to the exercises.
I have tried to provide a fairly comprehensive list of references to papers whose main
objective is the development of an algorithm for generating some class of combinatorial
objects. Inclusion in the references implies no judgement on my part about the merits of
the paper. It is evident from the large number of these references that there has been much
interest in generating combinatorial objects and that that interest is increasing. It is my
hope that this book will provide a guide to those interested in combinatorial generation,
both in regards to what has been done before, and in terms of what approaches have proved
most successful.
The exercises form an integral part of the book. Many of them expand upon the material
of the chapters and contain material about generating other types of combinatorial objects
beyond those covered in the main body of the chapter. Many useful generation problems
have been relegated to the exercises because of space limitations. The reader is urged to
at least read the exercises and understand what they are asking. Solutions or hints or
references to the relevant literature are provided for most of the exercises; these solutions
may be found in the final chapter at the end of the book. They are classified according to
difficulty using a modification of the rating scheme of Stanley [402] as follows.
[1] routine, straightforward.
[2] somewhat difficult or tricky.
[3] difficult.
[4] extraordinarily difficult.
[R] research problem.
Further gradations are indicated by + and . An [R] indicates that the problem is a
research problem, and that the solution is unknown to myself. An [R] or [R] rating does
not necessarily imply that the problem is extremely difficult; indeed, I suspect that some
of them can be solved quite easily. If you do solve any one of these problems, please let me
know! An [R] indicates that I dont know the solution or of anybody who has worked on
the problem before. Such problems may be quite easy to solve. An [R] indicates that at
least one person has unsuccessively worked on the problem, and a [R+] means that several
c
uvic01, Frank
Ruskey, 19952001

vi
well-known researchers have worked on the problem without success. Some of the [R+]
problems are notoriously difficult!
I would like to thank my favorite list, Susan, Jennifer, and Albert, for putting up with
the many lost hours spent away from them as I wrote this book.
Many people have contributed to this book, knowingly or not. In particular I would like
to mention Larry Cummings, Brendan McKay, Carla Savage, Ian Roberts, Colin Ramsay,
Peter Eades, Malcolm Smith, Gara Pruesse, Joe Sawada, and Donald Knuth.
Frank Ruskey

c
uvic01, Frank
Ruskey, 19952001

vii

Tai Chi
2 23 8 20 16 35 45 12 15 52 39 53 62 56 31 33 7 4 29 59 40 64 47 6 46 18 48 57 32 50 28 44

Kun

earth

Ken

mountain

Kan

water

Sun

wind

Lesser Yang

Great Yin
YIN

24 27 3 42 51 21 17 25 36 22 63 37 55 30 49 13 19 41 60 61 54 38 58 10 11 26 5 9 34 14 43 1

Chen

thunder

Li

fire

Tui

lake

Chien

Heaven

Great Yang

Lesser Yin
YANG

Figure 1: The 64 Hexagrams of the Fu Hsi ordering (from the I Ching). The I Ching (or Book
of Changes) is a book that has been used for centuries as an aid to fortune telling in China. The
I Ching is said to have originated around the eighth century B.C. and the Fu Hsi ordering is from
the 11th century. Each of the 26 hexagrams consists of six symbols, either a long dash or two short
dashes, placed one above the other. Originally the hexagrams were arranged in other, seemingly
random, orders; one such order is indicated by the numbers above each hexagram.

c
uvic01, Frank
Ruskey, 19952001

viii

Figure 2: The 52 Murasaki diagrams from The Tale of the Genji. The novel Tale of Genji was
written about 1000 A.D. by Lady Shikibu Murasaki, who is often referred to as the Shakespeare of
Japan. The book has 54 chapters and some early copies have one of the Murasaki diagrams at the
top of each chapter, except for the first and last. The diagrams themselves derive from a Japanese
game involving the identification, by smell alone, of a sequence of 5 incense sticks. Each diagram is
formed from 5 vertical bars, where bars representing the same type of incense are connected by a
horizontal bar.

c
uvic01, Frank
Ruskey, 19952001

Contents
1 What is Combinatorial Generation?
1.1 Some Examples . . . . . . . . . . . .
1.1.1 Fishers Exact Test . . . . . .
1.1.2 A Second Example . . . . . .
1.2 Elementary Objects . . . . . . . . .
1.3 Four Basic Questions . . . . . . . . .
1.4 A Word about the Algorithms . . . .
1.5 The Representation Issue . . . . . .
1.6 Complexity Measures . . . . . . . . .
1.7 Analyzing the Algorithms . . . . . .
1.8 Exercises . . . . . . . . . . . . . . .
1.9 Bibliographic Remarks . . . . . . . .
2 Basic Combinatorial Objects
2.1 Alphabets, Strings, Languages, and
2.2 Relations and Functions . . . . . .
2.3 Asymptotic Notation . . . . . . . .
2.4 Subsets and Combinations . . . . .
2.5 Permutations . . . . . . . . . . . .
2.5.1 Permutations of a Multiset
2.6 Partitions . . . . . . . . . . . . . .
2.6.1 Numerical Partitions . . . .
2.6.2 Set Partitions . . . . . . . .
2.7 The twelve-fold way . . . . . . . .
2.8 Tableau . . . . . . . . . . . . . . .
2.9 Partially Ordered Sets . . . . . . .
2.9.1 Antimatroids . . . . . . . .
2.10 Trees . . . . . . . . . . . . . . . . .
2.10.1 Ordered Trees . . . . . . . .
2.10.2 Rooted Trees . . . . . . . .
2.10.3 Free Trees . . . . . . . . . .
2.11 Graphs . . . . . . . . . . . . . . . .
2.11.1 The Matrix Tree Theorem .
2.11.2 Representing Graphs . . . .
2.11.3 Depth First Search . . . . .
2.12 Finite Groups . . . . . . . . . . . .
2.12.1 Permutation Groups . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

3
3
3
4
4
4
6
6
7
7
8
9

Lists .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

11
11
12
13
13
14
15
16
16
17
18
18
18
20
20
20
23
25
26
28
28
29
30
31

ix

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

CONTENTS
2.12.2 Burnsides Lemma . . . . . . . . .
2.12.3 Polya Theorem . . . . . . . . . . .
2.12.4 Cayley Graphs . . . . . . . . . . .
2.13 Miscellanea . . . . . . . . . . . . . . . . .
2.13.1 Summations . . . . . . . . . . . . .
2.13.2 Fibonacci Numbers . . . . . . . . .
2.13.3 Constant Time Array Initialization
2.14 Exercises . . . . . . . . . . . . . . . . . .
2.15 Bibliographic Remarks . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

32
32
33
33
33
33
34
34
38

3 Backtracking
3.1 Introduction . . . . . . . . . . . . . . . . . . . . .
3.2 Backtracking Algorithms . . . . . . . . . . . . . .
3.3 Solving Pentomino Problems with Backtracking.
3.3.1 Eliminating Isomorphic Solutions . . . . .
3.4 Estimating the Running Time of Backtracking. .
3.5 Exercises. . . . . . . . . . . . . . . . . . . . . . .
3.6 Bibliographic Remarks . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

41
41
43
44
47
48
50
53

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

55
55
55
57
66
68
71
73
74
78
86
89
93
98
98
98
98
98
98
98
99
101
103
111

4 Lexicographic Algorithms
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5 Permutations of a Multiset . . . . . . . . . . . . . . . . . . . . . . . .
4.5.1 Combinations of a Multiset . . . . . . . . . . . . . . . . . . . .
4.6 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.6.1 Binary Trees, Ordered Trees . . . . . . . . . . . . . . . . . . . .
4.6.2 Rooted Trees, Free Trees . . . . . . . . . . . . . . . . . . . . .
4.6.3 B-trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.7 Set Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.8 Numerical Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.9 Generalized Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.9.1 Wilfs Generalized Setting . . . . . . . . . . . . . . . . . . . . .
4.9.2 The Generalized Setting of Flajolet, Zimmerman, and Cutsem
4.10 Listing solutions to problems solved by dynamic programming . . . . .
4.11 Retrospective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.11.1 Why recusive and iterative algorithms have similar analyses . .
4.12 Ideals and Linear Extensions of Posets . . . . . . . . . . . . . . . . . .
4.12.1 Varol-Rotem Algorithm for Linear Extensions . . . . . . . . . .
4.12.2 Algorithms for Listing Ideals . . . . . . . . . . . . . . . . . . .
4.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.14 Bibliographic Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

c
uvic01, Frank
Ruskey, 19952001

CONTENTS
5 Combinatorial Gray Codes: Algorithmic Issues
5.1 Introductory Example . . . . . . . . . . . . . . .
5.1.1 Combinatorial Gray Codes . . . . . . . .
5.2 The Binary Reflected Gray Code . . . . . . . . .
5.2.1 Applications of the BRGC . . . . . . . . .
5.3 Gray Codes for Combinations . . . . . . . . . . .
5.4 Permutations . . . . . . . . . . . . . . . . . . . .
5.5 Gray Codes for Binary Trees . . . . . . . . . . .
5.5.1 Well-formed Parentheses . . . . . . . . . .
5.5.2 Binary Trees . . . . . . . . . . . . . . . .
5.6 Multisets . . . . . . . . . . . . . . . . . . . . . .
5.6.1 Permutations of a multiset . . . . . . . .
5.6.2 Combinations of a multiset . . . . . . . .
5.7 Compositions . . . . . . . . . . . . . . . . . . . .
5.8 Numerical Partitions . . . . . . . . . . . . . . . .
5.9 Set Partitions . . . . . . . . . . . . . . . . . . . .
5.10 Linear Extensions of Posets . . . . . . . . . . . .
5.10.1 Combinatorially Interesting Posets . . . .
5.10.2 Generating Linear Extensions Fast . . . .
5.11 Ideals of Posets . . . . . . . . . . . . . . . . . . .
5.12 Loopless Algorithms . . . . . . . . . . . . . . . .
5.12.1 Binary Trees . . . . . . . . . . . . . . . .
5.13 Finding a Hamiltonian cycle . . . . . . . . . . . .
5.13.1 Extension-Rotation Algorithms . . . . . .
5.13.2 Cubic graphs . . . . . . . . . . . . . . . .
5.14 Exercises . . . . . . . . . . . . . . . . . . . . . .
5.15 Bibliographic Remarks . . . . . . . . . . . . . . .

xi

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

6 Combinatorial Gray Codes: Graph Theoretic Issues


6.1 The Hypercube . . . . . . . . . . . . . . . . . . . . . .
6.1.1 Monotone Gray Codes . . . . . . . . . . . . . .
6.2 Hamiltonicity Results for Graphs . . . . . . . . . . . .
6.2.1 A useful lemma . . . . . . . . . . . . . . . . . .
6.3 Vertex-Transitive Graphs . . . . . . . . . . . . . . . .
6.4 Anti-Gray Codes . . . . . . . . . . . . . . . . . . . . .
6.4.1 Permutations . . . . . . . . . . . . . . . . . . .
6.4.2 Subsets . . . . . . . . . . . . . . . . . . . . . .
6.5 Hamilton Cycles in Cayley Graphs . . . . . . . . . . .
6.5.1 Directed Cayley Graphs . . . . . . . . . . . . .
6.5.2 Undirected Cayley Graphs . . . . . . . . . . . .
6.5.3 Cayley graphs over Sn . . . . . . . . . . . . . .
6.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .
6.7 Bibliographic Remarks . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

115
115
116
116
122
125
135
138
139
142
146
146
149
149
153
159
163
163
165
174
175
175
176
176
178
178
184

.
.
.
.
.
.
.
.
.
.
.
.
.
.

189
190
190
194
195
195
196
196
197
197
198
199
199
201
205

c
uvic01, Frank
Ruskey, 19952001

xii

CONTENTS

7 DeBruijn Cycles and Relatives


7.1 Eulerian Cycles . . . . . . . . . . . . . . . . . . .
7.1.1 Generating an Eulerian cycle . . . . . . .
7.1.2 An Eulerian Cycle in the Directed n-cube
7.2 Necklaces . . . . . . . . . . . . . . . . . . . . . .
7.3 De Bruijn Sequences . . . . . . . . . . . . . . . .
7.4 Computing the Necklace of a String . . . . . . .
7.5 Universal Cycles . . . . . . . . . . . . . . . . . .
7.6 Polynomials over finite fields . . . . . . . . . . .
7.6.1 Linear Feedback Shift Registers . . . . . .
7.6.2 Another look at the BRGC . . . . . . . .
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . .
7.8 Bibliographic Remarks . . . . . . . . . . . . . . .
8 Orderly Algorithms
8.1 Undirected Graphs . . . . . .
8.2 Tournaments . . . . . . . . .
8.3 Restricted Classes of Graphs
8.3.1 Bipartite Graphs . . .
8.3.2 Cubic Graphs . . . . .
8.4 Posets . . . . . . . . . . . . .
8.4.1 Dedekinds Problem .
8.5 Coset Enumeration . . . . . .
8.6 Exercises . . . . . . . . . . .
8.7 Bibliographic Remarks . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

207
208
208
210
213
220
222
224
226
228
229
229
232

.
.
.
.
.
.
.
.
.
.

235
236
239
240
240
240
240
240
241
241
241

9 Subgraph Generation
9.1 Spanning Trees . . . . . . . .
9.1.1 A naive algorithm . .
9.1.2 A CAT algorithm . . .
9.1.3 A Spanning Tree Gray
9.2 Reverse Search . . . . . . . .
9.3 Cycles . . . . . . . . . . . . .
9.4 Cliques . . . . . . . . . . . .
9.5 Maximal Independent Sets . .
9.6 Cutsets . . . . . . . . . . . .
9.7 Chromatic Polynomials . . .
9.8 Convex Polytopes . . . . . . .
9.9 Exercises . . . . . . . . . . .
9.10 Bibliographic Remarks . . . .

. . . .
. . . .
. . . .
Code
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

243
243
243
243
244
246
246
246
246
246
246
246
246
246

10 Random Selection
10.1 Permutations . . . . . . . .
10.2 Combinations . . . . . . . .
10.3 Permutations of a Multiset
10.4 Necklaces . . . . . . . . . .
10.5 Numerical Partitions . . . .
10.6 Set Partitions . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

249
249
249
250
250
251
252

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

c
uvic01, Frank
Ruskey, 19952001

CONTENTS
10.7 Trees . . . . . . . . . . . . . . .
10.7.1 Well-formed Parentheses
10.8 Graphs . . . . . . . . . . . . . .
10.9 Exercises . . . . . . . . . . . .
10.10Bibliographic Remarks . . . . .

xiii
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

252
252
252
252
252

Bibliography

253

11 Useful Tables

281

Term Index

281

c
uvic01, Frank
Ruskey, 19952001

xiv

CONTENTS

c
uvic01, Frank
Ruskey, 19952001

List of Figures
1
2

The 64 Hexagrams of the Fu Hsi ordering (from the I Ching). . . . . . . . .


The 52 Murasaki diagrams from The Tale of the Genji. . . . . . . . . . . .

vii
viii

2.1
2.2

Ferrers diagram and conjugate partition. . . . . . . . . . . . . . . . . . . .


(a) A standard Young tableau of shape 6,4,4,2,1,1. (b) The hook lengths of
that tableau. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
(a) Forbidden subposet for lattices. (b) Forbidden sublattices for distributive
lattices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The path corresponding to 11101011100000. . . . . . . . . . . . . . . . . . .
A triangulation of a rooted 10-gon and the corresponding extended binary
tree with 10 leaves. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Catalan correspondences for n = 3 . . . . . . . . . . . . . . . . . . . . . . .
The functional digraph of f and the bijection . . . . . . . . . . . . . . . . .
For this tree x is the centroid and y is the center. . . . . . . . . . . . . . . .
A graph and its adjacency list representation. . . . . . . . . . . . . . . . . .

16

The four queens backtracking tree. . . . . . . . . . . . . . . . . . . . . . . .


The 12 pentomino pieces. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A solution to the 6 by 10 pentomino problem. . . . . . . . . . . . . . . . . .
Numbering the board (and the placement of a piece). . . . . . . . . . . . . .
Lists for piece 11, with those anchored at location 7 shown explicitly. . . . .
Eliminating isomorphs in the 6 by 10 pentomino puzzle. . . . . . . . . . . .
(a) True backtracking tree with random root-to-leaf path shown. (b) Assumed tree from that random path. . . . . . . . . . . . . . . . . . . . . . . .
The Soma cube pieces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A 6 by 6 knights tour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42
45
45
45
46
48

Computation tree of B(5, 2). . . . . . . . . . . . . . . . . . . . . . . . . . .


The
9walk corresponding to the rank 100 = 56 + 35 + 4 + 3 + 2 combination
of 5 in colex order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Computation tree of GenMult on input h2, 1, 1i. . . . . . . . . . . . . . . . .
Computation tree of gen1 on C(5; 1, 2, 2, 1, 1). . . . . . . . . . . . . . . . . .
Extended binary tree represented by the sequence 1110001100. . . . . . . .
Lexicographic tree sequences for n = 4 (read down). . . . . . . . . . . . . .
The walk, 11101011100000, corresponding to the rank 333 = 132 + 132 + 42 +
14 + 9 + 4 tree in T(8). . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The rooted trees on at most 5 nodes. . . . . . . . . . . . . . . . . . . . . . .
Two isomorphic rooted trees. The one on the right is canonic. . . . . . . . .

60

2.3
2.4
2.5
2.6
2.7
2.8
2.9
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9

xv

18
19
21
21
22
25
26
29

49
51
52

65
70
72
74
74
78
79
80

xvi

LIST OF FIGURES
4.10 (a) The 20 canonic sequences of length n = 6 in relex order (read down).
(b) The 9 canonic sequences of length n = 5 with elements contributing to x
indicated in bold and those contributing to y underlined. . . . . . . . . . .
4.11 The rooted versions of the free trees on 6 vertices, listed in relex order.
Central vertices are darkened. . . . . . . . . . . . . . . . . . . . . . . . . . .
4.12 Condition (B2) fails: (The height of T2 is reduced too much). (a) original
tree T . (b) succ(e(T )). (c) Succ(e(T )). . . . . . . . . . . . . . . . . . . . . .
4.13 Condition (C) fails: (|T1 | has too many nodes). (a) original tree T . (b)
succ(e(T )). (c) Succ(e(T )). . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.14 Condition (D) fails: (The encoding of T1 is too large). (a) original tree T .
(b) succ(e(T )). (c) Succ(e(T )). . . . . . . . . . . . . . . . . . . . . . . . . .
4.15 B-tree of order 4 with encoding 32223242323. . . . . . . . . . . . . . . . . .
4.16 The eight trees in B(6, 2) (with m = 4). . . . . . . . . . . . . . . . . . . . .
4.17 Computation tree for n = k = 4. . . . . . . . . . . . . . . . . . . . . . . . .
4.18 A typical computation tree of a recursive algorithm generating strings in
lexicographic order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.19 Computation tree of Varol-Rotem algorithm on the example poset. . . . . .
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.16
5.17
5.18
5.19
5.20
5.21
5.22
5.23
5.24
5.25

Position detection on a rotating device with two different labellings. . . . .


The definition of Gn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A 3-cube. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Towers of Hanoi for n = 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
n = 3: Counting, Gray Code, and Towers of Hanoi. . . . . . . . . . . . . . .
A six ring Chinese rings puzzle. . . . . . . . . . . . . . . . . . . . . . . . . .
The Spin-out puzzle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiattribute file storage; Gray and lex order. . . . . . . . . . . . . . . . .
Bruns Brunnian link. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
[Trans],[H-Trans],[2-Trans](R(6, 3, 1)). . . . . . . . . . . . . . . . . . . . . .
Non-Hamiltonian Adjacent Transposition graph. . . . . . . . . . . . . . . .
Path in the prism of combs for n = 8 and k = 5. . . . . . . . . . . . . . . .
A left or right rotation at node x. . . . . . . . . . . . . . . . . . . . . . . . .
Example of induced tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
(a) Given T [1..n 1] there are 6 places where n could possibly be inserted
to get T . (b) The n has been inserted at the position of the double arrow.
List of trees for n = 2, 3, 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursion Tree (A) for n = h2, 1, 1i. . . . . . . . . . . . . . . . . . . . . . .
Recursion Tree (B) for n = h2, 1, 1i. . . . . . . . . . . . . . . . . . . . . . .
The list (a) Comp(5, 4) (first two columns, read down) and the corresponding
list (b) Comb(5, 4) of the elements of B(8, 5) (last two columns). . . . . . .
Peaks and valleys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The closeness graph G(6, 4) of L(6, 4). . . . . . . . . . . . . . . . . . . . . .
The L lists for the special cases in X. . . . . . . . . . . . . . . . . . . . . .
The M lists for the special cases in Y . . . . . . . . . . . . . . . . . . . . . .
Gray codes for RG functions n = 1, 2, 3, 4: (a) Knuth list, (b) differing in
only one position. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The lists S(5, 3, 0) and S(5, 3, 1). . . . . . . . . . . . . . . . . . . . . . . . .

80
83
84
84
85
86
87
91
99
100
116
117
117
118
118
123
123
124
126
133
133
135
142
143
144
145
148
148
151
154
154
156
157
160
162

c
uvic01, Frank
Ruskey, 19952001

LIST OF FIGURES

xvii

5.26 (a) Poset whose linear extensions correspond to binary trees. (b) Poset whose linear
extensions correspond to Young tableau of shape 6,4,4,1. . . . . . . . . . . . . .
5.27 Poset whose linear extensions correspond to set partitions of type (2,2,2,3,3). . . .
5.28 A poset and its transposition graph. . . . . . . . . . . . . . . . . . . . . . .
5.29 The graph G(P) K2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.30 Part of a Hamiltonian cycle through G0 (P) K2 . . . . . . . . . . . . . . .
5.31 A B-poset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.32 The trace of the calling sequence for the poset of Figure 1. . . . . . . . . . .
5.33 The extension and rotation operations. . . . . . . . . . . . . . . . . . . . . .
5.34 Implicated edges on Hamilton cycles in cubic graphs. . . . . . . . . . . . . .

164
164
165
166
166
167
169
177
178

6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8

A monotone Gray code for n = 5. . . . . . . . . . . . . . . . . . . . . . . .


Illustration of the 4-cycle construction. . . . . . . . . . . . . . . . . . . . .
Three of the four known non-Hamiltonian vertex-transitive graphs . . . .
~
The non-Hamiltonian directed Cayley graph Cay({(1
2)(3 4), (1 2 3)} : S4 ).
Hamilton cycles in Cayley graphs over Abelian groups. . . . . . . . . . . .
The Cayley graph Cay({(1 2), (2 3), (3 4)} : S4 ). . . . . . . . . . . . . . . .
The Cayley graph Cay({(1 2), (1 3), (1 4)} : S4 ). . . . . . . . . . . . . . . .
Base case listings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

191
195
196
198
199
200
201
202

7.1
7.2
7.3
7.4
7.5
7.6

Example (maximal) domino game. . . . . . . . . . . . . . .


Example of finding an Euler cycle in a directed multigraph
The six two-color necklaces with 4 beads. . . . . . . . . . .
Output of the FKM algorithm (read down columns). . . . .
The De Bruijn graph for k = 2 and n = 4. . . . . . . . . .
The LFSR corresponding to the polynomial x4 + x + 1. . .

.
.
.
.
.
.

207
209
213
216
220
228

8.1
8.2
8.3

An unlabelled graph, its canonic labelling, and its parent. . . . . . . . . .


The tree of unlabelled graphs on four vertices. . . . . . . . . . . . . . . . . .
Tree of tournaments for n 4. . . . . . . . . . . . . . . . . . . . . . . . . .

236
237
239

9.1
9.2
9.3

The connected graph K4 e with edges labelled. . . . . . . . . . . . . . . .


The spanning trees of K4 e. . . . . . . . . . . . . . . . . . . . . . . . . . .
The tree graph of K4 e. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

244
245
245

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

c
uvic01, Frank
Ruskey, 19952001

xviii

LIST OF FIGURES

c
uvic01, Frank
Ruskey, 19952001

List of Tables
4.1
4.2
11.1
11.2
11.3
11.4
11.5

The numbers b(s, d) when m = 4. . . . . . . . . . . . . . . .


Restricted tail numbers R(n, m) for 1 n + m 10. . . . .

Binomial coefficients nk for 0 k 11. . . . . . . . . . . .
2nk
k
The numbers T (n, k) = 2nk
for 1 k 9. . . . . . .
k

Stirling numbers of the second kind nk for 1 k 9. . . .
The number p(n, k) of numerical partitions of n into k parts
Eulerian numbers for 1 k 9. . . . . . . . . . . . . . . . .

xix

. . . . . . . . .
. . . . . . . . .
. .
. .
. .
for
. .

. . . . . . .
. . . . . . .
. . . . . . .
1 k 11.
. . . . . . .

87
93
282
282
282
283
283

xx

LIST OF TABLES

c
uvic01, Frank
Ruskey, 19952001

List of Algorithms
2.1
3.1
3.2
3.3
3.4
3.5
3.6
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12
4.13
4.14
4.15
4.16
4.17
4.18
4.19
4.20
4.21
4.22
4.23
4.24
4.25
4.26
4.27
4.28
4.29

A Pascal procedure for performing depth-first search of a graph. . . . . . .


30
Algorithm for the 8 queens problem. . . . . . . . . . . . . . . . . . . . . . .
42
Recursive backtracking algorithm. . . . . . . . . . . . . . . . . . . . . . . .
43
Non-recursive backtracking algorithm. . . . . . . . . . . . . . . . . . . . . .
44
Declarations for pentomino program. . . . . . . . . . . . . . . . . . . . . . .
46
Backtracking routine for pentomino problem. . . . . . . . . . . . . . . . . .
47
Estimation algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
49
Next for subsets by counting. . . . . . . . . . . . . . . . . . . . . . . . . . .
56
Recursive procedure to generate subsets in lexicographic order. . . . . . . .
57
Procedure Next for combinations in lex order(A(n, k)). . . . . . . . . . . . .
58
Improved version of Next for combinations in lex order(A(n, k)). . . . . . .
58
Recursive procedure for generating B(n, k) in lex order. . . . . . . . . . . .
59
A recursive procedure for generating B(n, k) in colex order. . . . . . . . . .
60
A CAT procedure for generating B(n, k) in colex order if k n/2. . . . . .
62
A CAT procedure for generating A(n, k) in colex order if k n/2. . . . . .
62
A CAT algorithm for the k-Partition problem. . . . . . . . . . . . . . .
63
Unranking combinations (A(n, k)) in colex order. . . . . . . . . . . . . . . .
65
Procedure Next for permutations in lexicographic order. . . . . . . . . . . .
66
Ranking algorithm for permutations in reverse colex order. . . . . . . . . .
68
Generate permutations of a multiset in colex order. . . . . . . . . . . . . . .
69
The procedure gen1 (to be used when k n/2). . . . . . . . . . . . . . . . .
72
The procedure gen2 (to be used when k > n/2). . . . . . . . . . . . . . . . .
73
Next for binary trees in lexicographic order. . . . . . . . . . . . . . . . . . .
75
Algorithm to generate the elements of T(n, k) in relex order. . . . . . . . .
77
Generate rooted trees in relex order. . . . . . . . . . . . . . . . . . . . . . .
81
Relex algorithm for generating free trees. Given L = e(hT i) it produces
Succ(e(hT i)). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
85
Generating B-trees in lexicographic order. . . . . . . . . . . . . . . . . . . .
88
Ranking B-trees in lexicographic order. . . . . . . . . . . . . . . . . . . . . .
89
Procedure to generate RG sequences in lex order. . . . . . . . . . . . . . . .
90
Generation of set partitions with exactly k blocks in a pseudo-colex order. .
91
Lexicographic generation of all partitions of n whose largest part is k. . . .
95
CAT generation of all partitions of n whose largest part is k. . . . . . . . .
95
Generation of all partitions of n into k parts with largest part s. . . . . . .
96
Next for lexicographic numerical partitions using multiplicity representation. 97
Recursive Varol-Rotem algorithm for generating linear extensions. . . . . .
99
Generic algorithm for generating ideals. . . . . . . . . . . . . . . . . . . . . 101
xxi

LIST OF ALGORITHMS
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.16
5.17
5.18
5.19
5.20
5.21
5.22
5.23
5.24
7.1
7.2
7.3
7.4
7.5
8.1
8.2
8.3
9.1
10.1

A C implementation of the BRGC on computer words. . . . . . . . . . . . .


Indirect algorithm for generating the BRGC. . . . . . . . . . . . . . . . . .
Direct algorithm for generating the BRGC. . . . . . . . . . . . . . . . . . .
Loop-free procedure Next for generating the BRGC. . . . . . . . . . . . . .
Direct transposition generation of B(n, k). . . . . . . . . . . . . . . . . . . .
A surprising indirect algorithm for generating A(n, k) by transpositions. . .
Loopfree algorithm for generating combinations by transpositions. . . . . .
Direct generation of combinations by homogenous transpositions. . . . . . .
Direct [2-Trans] generation of Combinations. . . . . . . . . . . . . . . . . .
Recursive permutation algorithm. . . . . . . . . . . . . . . . . . . . . . . . .
Recursive Pascal implementation of the SJT Algorithm. . . . . . . . . . . .
Iterative Next implementation of the SJT algorithm (assumes 0 = n+1 =
n + 1). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ranking algorithm for SJT Algorithm. . . . . . . . . . . . . . . . . . . . . .
Unranking algorithm for SJT algorithm. . . . . . . . . . . . . . . . . . . . .
Indirect Gray code algorithm for well-formed parentheses. . . . . . . . . . .
Direct Gray code algorithm for well-formed parentheses. . . . . . . . . . . .
A procedure to generate trees by rotations. . . . . . . . . . . . . . . . . . .
Procedures for generating multiset permutations. . . . . . . . . . . . . . . .
Indirect implementation of Knuths algorithm. . . . . . . . . . . . . . . . .
Direct implementation of Knuths algorithm for compositions. . . . . . . . .
Case k n < 2k 2 of the recursion for L. . . . . . . . . . . . . . . . . . .
Preprocessing a poset by stripping off minimal pairs. . . . . . . . . . . . . .
Pascal procedure GenLE to generate linear extensions. . . . . . . . . . . . .
Extension-rotation heuristic algorithm for finding a Hamilton cycle. . . . .
Algorithm to find an Eulerian cycle in a directed multigraph. . . . . . . . .
The original iterative FKM Algorithm (note: a[0] = 0.) . . . . . . . . . . .
Recursive FKM Algorithm (note: a[0] = 0.) . . . . . . . . . . . . . . . . . .
Duval algorithm for factoring a string. . . . . . . . . . . . . . . . . . . . . .
C code for iterating an LFSR. . . . . . . . . . . . . . . . . . . . . . . . . . .
Orderly algorithm (recursive version). . . . . . . . . . . . . . . . . . . . . .
Orderly algorithm with G global. . . . . . . . . . . . . . . . . . . . . . . . .
Orderly algorithm for tournaments on at most n vertices. . . . . . . . . . .
Contraction-Deletion algorithm for generating spanning trees. . . . . . . . .
Generate a random k-subset of [n]. . . . . . . . . . . . . . . . . . . . . . . .

120
120
121
122
128
129
130
131
132
136
137
137
138
138
140
141
146
150
152
152
159
170
173
177
209
217
219
223
228
237
238
240
244
250

c
uvic01, Frank
Ruskey, 19952001

LIST OF ALGORITHMS

c
uvic01, Frank
Ruskey, 19952001

Chapter 1

What is Combinatorial
Generation?
Lets look at all the possibilities. This phrase sums up the outlook of this book. In
computer science, mathematics, and in other fields it is often necessary to examine all of
a finite number of possibilities in order to solve a problem or gain insight into the solution
of a problem. These possibilities often have an underlying combinatorial structure which
can be exploited in order to obtain an efficient algorithm for generating some appropriate
representation of them.

1.1

Some Examples

1.1.1

Fishers Exact Test

Sir R.A. Fisher described an experiment to test a woman who claimed that she could
distinguish whether the milk or tea was poured first in a cup of tea and milk. Eight cups of
tea were prepared, 4 in which milk came before tea and 4 with tea before milk. The woman
knows that there will be 4 of each type. The results were as shown below in whats called
a 2 by 2 contingency table.
Guess Poured First
Poured First Milk
Tea
Milk
3
1
4
Tea
1
3
4
The probability that a particular contingency table T occurs follows a multinomial
distribution, where x1 and x2 are the entries in the first column, n1 and n2 are the row
totals, and N = n1 + n2 .
n1 n2
P rob(T ) = x1 N x2
x1 +x2

The typical questions that a statistician wishes to answer are: (1) How many tables
have a lower probability of occurring than the observed table? (2) What is the sum of
probabilities of tables having a value
Nof x1 at least as large as whats observed?
In our example, the value of x1 +x2 is 70, and the possible probabilities are given in
the table below.
3

CHAPTER 1. WHAT IS COMBINATORIAL GENERATION?

x1 x2 numerator
probability
44
0
4 0 4 = 1
1/70 = 0.0142857
1
3 4143 = 16 16/70 = 0.2285714
2
2 4242 = 36 36/70 = 0.5142857
4 4
3
1
3414= 16 16/70 = 0.2285714
4
0
1/70 = 0.0142857
4 0 =1
Answering each of these questions involves the generation of combinatorial objects. In
this case the generation is particularly simple. We wish to compute the value of P rob(T )
for all those values of x1 and x2 for which x1 + x2 = 4, with the additional constraints that
x1 n1 = 4 and x2 n2 = 4. The combinatorial objects being generated are the pairs
(x1 , x2 ).
In the more general setting of a k by 2 contingency table, we need to generate all
solutions to
x1 + x2 + + xk = r1 subject to 0 xi ni .
Algorithms for generating these objects, which we call combinations of a multiset, are
presented in Section 4.5.1.

1.1.2

A Second Example

This subsection is yet to be written.

1.2

Elementary Objects

There is no precise definition of an elementary combinatorial object. Our intuitive notion


is that if a class of combinatorial objects satisfies a simple recurrence relation then it is
elementary. We make no attempt to define simple recurrence relation. However, we
consider permutations, combinations, set partitions, numerical partitions, binary trees and
labeled graphs to all be elementary combinatorial objects; while unlabeled graphs, room
squares, and unlabeled partially ordered sets are not elementary.
Many recurrence relations many be stated in a form that involves no division or subtraction, only multiplication and addition, and further that involve only positive values, even
the base cases. Such recurrence relations are said to be positive. Given a simple recurrence
relation describing an elementary combinatorial object, it is typically straightforward to develop an algorithm for generating a natural representation of that object. If the recurrence
relation is positive, then the algorithm is often efficient in an amortized sense. This point
of view was perhaps first explored by Wilf in two papers [448], [449], and we have more to
say on this subject in Section 4.9.
Most of the book is devoted to elementary objects, namely Chapters 4, 5, and 6. Nonelementary objects are generated in Chapters 3 and 8.

1.3

Four Basic Questions

We consider in this book 4 basic questions: listing, ranking, unranking, and random selection, of which the listing question is of paramount importance.
[listing] Algorithms for generating combinatorial objects come in two varieties. Either
there is a recursive procedure, call it GenerateAll, that typically has the same recursive
c
uvic01, Frank
Ruskey, 19952001

1.3. FOUR BASIC QUESTIONS

structure as a recurrence relation counting the objects being generated, or there is a procedure, call it Next, that takes the current object, plus possibly some auxiliary information,
and produces the next object. Typically, but not always, Next is iterative. Throughout the
book we assume that Next is used by a section of code as shown below.
Initialize; {includes done := false }
repeat
PrintIt;
Next;
until done;
The boolean variable done is global and is eventually set true by Next. If Next contains
no loop then the generation algorithm is said to be loopless. Another term one sometimes
sees in connection with iterative generation algorithms is memoryless. This simply means
that Next contains no global variables whose value changes; the algorithm can be started
with any object.
Many older papers start with a nice recursive decomposition of the class of objects to be
generated and then jump immediately into the development of a iterative Next routine. This
unfortunate habit has turned many a beautiful decomposition into an ugly program. One
would sometimes see this uglification justified by a sentence stating that recursion was slow
because of the overhead involved in making procedure calls and passing parameters.1 This
justification can no longer be accepted! Its a throwback to a by-gone era. Most modern
machines include hardware to speed procedure calls. The Sun workstation sitting on the
authors desk has banks of window registers that make recursive programs written in C
faster than their iterative counterparts! For these reasons, the vast majority of generation
algorithms presented in this book are recursive.
However, there are at least two compelling reasons why iterative Next routines are
useful these reasons are due to more modern trends in computing. First is the cause of
modularization. Iteration and recursion generation procedures present two opposite views
to the user. Iteration says Give me and object and Ill give you the next one. Recursion
says Give me the procedure that uses the object and Ill apply it to every object. The
second reason is that iterative procedures are often more amenable to parallel computation.
Sometimes it is desirable to have a listing of objects in which successive objects are
close in some well-defined sense. Such listings are called Gray Codes, and are the subjects
of Chapter 5 and Chapter 6.
[ranking] Relative to an ordering of a set of combinatorial objects, such as the ordering
imposed by a generation algorithm, the rank of an object is the position that the object
occupies in the ordering. Our counting begins at 0 so another way of defining the rank is
as the number of objects that precede it in the list2 . One of the primary uses of ranking
algorithms is that they provide perfect hashing functions for a class of combinatorial
objects. The existence of a ranking algorithm allows you to set up an array indexed by
the objects. Ranking (and unranking) is generally only possible for some of the elementary
combinatorial objects.
[unranking] Unranking is the inverse process of ranking. To unrank an integer r is
to produce the object that has rank r. Unranking algorithms can be used to implement
1
2

Or perhaps that the language being used didnt support recursion.


In the literature many ranking algorithms begin counting at 1.

c
uvic01, Frank
Ruskey, 19952001

CHAPTER 1. WHAT IS COMBINATORIAL GENERATION?

parallel algorithms for generating combinatorial objects, when used in conjunction with a
Next procedure. For more on this see Chapter ?? For this reason we try ensure that our
unranking algorithms return enough information for Next to be restarted from any object.
[random selection] Here we want to produce a combinatorial object uniformly at
random. An unranking algorithm can be used, but often more direct and efficient methods
can be developed. This is an important topic, and is treated in Chapter 10, but is not a
main focus of this book.

1.4

A Word about the Algorithms

It is hoped that the contents of this book are not only interesting but also useful. The
algorithms in this book have been presented in a pseudo-code. In most cases it should be
a trivial exercise to translate these algorithms into languages such as C, C++, Java, or
Pascal. The most important difference in our pseudo-code from those languages is that
the scope of statements is indicated by indentation, rather than by the use of parentheses,
as in C, C++, and Java, or by the use of begin, endpairs, as in Pascal. In spirit our
algorithms are closest to Pascal. Procedures that do not return a value are indicated by
the reserved word procedure (these are like void procedures and methods in C and Java.
Those that return a value are indicated by the reserved word function . The return type
of the function is indicated after the parameter list, as in
function ( h parameter list i ) : h return type i;
Following Knuth, we use x :=: y to indicate the swap of the values of two variables; i.e., it
is shorthand for t := x; x := y; y := t. Also, we allow for multiple (parallel) assignment
statements as [x, y] := [e, f ] to indicate the assignments x := e and y := f , executed in
parallel. The notations ai and a[i] will be used interchangeably and a[i..j] indicates the
subarray of a indexed from i to j. Arrays can be declared over an arbitrary range; i.e., not
necessarily starting from 0, as in C and Java.
Linked structures are not often used in this book, but when they are we adopt simple
dot notation to denote fields within nodes.

1.5

The Representation Issue

There are typically many ways to represent a combinatorial object, and these different representations may lead to wildly differing algorithms for generating the objects. For example,
permutations may be represented in one-line notation, in cycle notation, by inversion tables,
or even by permutation matrices; binary trees may be represented as a linked data structure, as well-formed parentheses strings, as triangulations of convex polygons, or as ordered
trees, as well as many other sequence representations. Which representation is most useful
usually depends upon the application that requires the objects to be generated. This is a
matter over which the author of the book has no control.
On the other hand, each object usually has some small number of standard representations, and it is these representations that we try to generate, and that other developers of
generation algorithms should try to generate. These standard representations we use are
almost always sequences, usually of fixed length, but not always.
The user of an algorithm for generating combinatorial objects should specify the representation most useful to them. Assume that we wish to generate some combinatorial family
c
uvic01, Frank
Ruskey, 19952001

1.6. COMPLEXITY MEASURES

Sn indexed by numbers n, and where sn = |Sn | is known or easily computable. Left up to


the the lazy generator, you might get an algorithm like the following.
Compute sn ;
for i := 1 to sn do Output( i );
Typically the computation of sn is efficient as a function of sn , so that the above algorithm is very efficient in an amortized sense. But have we generated Sn ? No reasonable
person would think so, since all we are doing is counting, and to get a useful representation,
some unranking algorithm still has to be developed.
The point of this discussion is that representations are important and the generator
(person developing a generation algorithm) should be careful to use a representation that is
useful to others, and not just convenient because it makes the algorithm simple or efficient.

1.6

Complexity Measures

Not much attention has been paid to the model of computation in the area of combinatorial
generation. For the most part authors use the random access machine model. This is more
or less like counting operations in a Pascal program, with the understanding that integers
can be arbitrarily large and the assumption that standard arithmetic operations can be
done in constant time. For large integers this is an unrealistic assumption. It is generally
the case that integers are small in generation algorithms, but that they are large in ranking
and unranking algorithms. We will therefore measure ranking and unranking algorithms in
terms of the number of arithmetic operations used.
The complexity of generating combinatorial objects is not well addressed by the classical theory of computational complexity, with its emphasis on the polynomial versus nonpolynomial time question, and polynomial and log-space reductions. Most classes of combinatorial objects with which we are concerned have at least exponentially many elements,
and useful reductions of any kind are rare. Some complexity questions are addressed in
Chapter ??.
An enormous amount of research has gone into getting away from the brute-force
approach to solving discrete optimization problems, and very fruitful approaches to solving
a wide variety of these problems have been developed. Nevertheless, one is occasionally
forced into the approach of examining all the possibilities and the results of this book should
be useful in those instances. Because of the aversion to the brute-force approach, research
in combinatorial generation has never been popular; refining a generation algorithm to more
efficiently solve some discrete optimization problem is like an admission of defeat in this
view. Combinatorial generation is really a part of theoretical computer science, certainly
more so than the complexity of counting, which is now a well-established part of theoretical
computer science. Perhaps surprisingly, in the 500+ references in the fairly comprehensive
bibliography, there is an almost a total absence of references from the preeminent STOC
and FOCS conferences. Perhaps this will change; I hope so.

1.7

Analyzing the Algorithms

There are two terms that are used throughout the book. We strive to develop algorithms
that have these properties.
c
uvic01, Frank
Ruskey, 19952001

CHAPTER 1. WHAT IS COMBINATORIAL GENERATION?

CAT Algorithms The holy grail of generating combinatorial objects is to find an algorithm
that runs in Constant Amortized Time. This means that the amount of computation, after
a small amount of preprocessing, is proportional to the number of objects that are listed.
We do not count the time to actually output or process the objects; we are only concerned
with the amount of data structure change that occurs as the objects are being generated.
BEST Algorithms This means Backtracking Ensuring Success at Terminals. In other
words, the algorithm is of the backtracking type, but every leaf of the backtracking tree is
an object of the desired type; it is a success.
Suppose that the input to our generation algorithm is n, and that this will produce N
objects, each of size n. A CAT algorithm has running time O(N ). In the typical application of combinatorial generation, each object, as it is produced, is processed somehow.
If this processing takes time O(n), then having a CAT algorithm has no advantage over
having one with running time O(nN ), since the total running time is O(nN ) in either case.
There are are many papers about generating combinatorial objects that describe an
O(nN ) running time as optimal, because this is the amount of output produced by the
algorithm each object has size n and there are N objects in total. This is a misguided
notion that has its roots in the way lower bounds are discussed in the traditional introduction
to complexity theory course. There the trivial lower bound says that the amount of output
gives a lower bound on the amount of computation of course it says the same thing in
our setting, but the amount of output is the wrong thing to measure.
Dont Count the Output Principle: In combinatorial generation it is the
amount of data structure change that should be measured in determining the complexity of an algorithm; the time required to output each object should be ignored.
The trivial lower bound for combinatorial generation is (N ); it is independent of n.
This may seem suspicious, since it appears to take no account of the time that is frequently
necessary to initialize the various data structures that are used, but there are many algorithms that require only a constant amount of initialization we will encounter some, for
example, in Chapter 4 on lexicographic generation. Now back to our typical application,
where processing each object took time O(n) even though our generation algorithm was
CAT. Wouldnt it be nice if the O(nN ) could be brought down to O(N )? This is frequently
possible! If you go back and observe how successive objects are processed, it is often the
case that the amount of processing required is proportional to the amount of change that
successive objects undergo.

1.8

Exercises

1. [R] Explore the use of the accounting and/or potential function methods in analyzing algorithms for generating combinatorial objects.
2. [R] In the listing problem we ask that each object be produced exactly once. For
some applications, uniqueness is not required, only that each object is produced at
least once. Explore the idea of listing objects with repeats allowed, with the goal of
producing an algorithm that is simpler or more efficient than known algorithms for
listing the objects uniquely.
c
uvic01, Frank
Ruskey, 19952001

1.9. BIBLIOGRAPHIC REMARKS

3. [R] Can every CAT algorithm in which successive objects change by a constant
amount be converted into an equivalent (in the sense that the objects are produced
in the same order) loopfree algorithm?
4. [R] Investigate the number of steps required by various types of Turing machines
to generate well-known lists of combinatorial objects. For example, in Turing-CAT
time we can count in binary on a one-tape machine. Along the same lines, investigate
the bit complexity of generating some well-known combinatorial lists.

1.9

Bibliographic Remarks

The aggregate, accounting, and potential function methods for amortized algorithm analysis
are discussed in Cormen, Leiserson, and Rivest [70].
Perhaps Lehmer [244] was the first to propose that combinatorial objects be studied by
imposing an order on them and then developing ranking and unranking algorithms with
respect to this order.
The use of ranking functions for source encoding is discussed in Cover [71]. Ranking
functions naturally give rise to numeration systems. Some of these are discussed in Fraenkel
[137], [138].
The CAT and BEST acronyms are due to the author. What we call CAT has been
called linear (because O(N ) is linear in N ) by Reingold, Nievergelt, and Deo [340], and in
several papers.

c
uvic01, Frank
Ruskey, 19952001

10

CHAPTER 1. WHAT IS COMBINATORIAL GENERATION?

c
uvic01, Frank
Ruskey, 19952001

Chapter 2

Basic Combinatorial Objects


PLEASE! Dont read this section first. Youll get the wrong impression about the rest of
the book. This section is best ignored in an initial reading of the book and just referred to
as need be in later chapters.
The purpose of this chapter is to review the definitions and basic properties of the
combinatorial objects to be generated in succeeding chapters. For the most part proofs are
omitted or relegated to the exercises. Exceptions are made for a couple of results that the
author feels should be more well-known (e.g., Lemma 2.4 and the proof of Theorem 2.2) or
peculiarly relevant to the material of this book (e.g., inequality (2.17) and Lemma 2.5).

2.1

Alphabets, Strings, Languages, and Lists

Usually the objects being generated are finite sequences or are represented by finite sequences which are then generated. We use the language of strings to describe these sequences. The output of our programs are also sequences; sequences of strings. To avoid
confusion, these sequences are referred to as lists. Thus we always generate lists of strings.
An alphabet is set, often a finite set. A string of length k over an alphabet is a finite
sequence, a1 , a2 , . . . , ak , where each ai is in . Where no ambiguity arises the commas will
be omitted and the string written a1 a2 ak .
The empty string as length 0, and is denoted . If is an alphabet, then n denotes
the set of all strings of length n over , the notation means the set of all strings over ,
and + = \ {} denotes the set of all non-empty strings over .
By number we mean a natural number, a member of the set {0, 1, 2, . . .}. In most cases
the alphabet under consideration is either the set of the first n positive integers, which
is denoted [n], or is the set consisting of the first k numbers, which is denoted k . We
adopt very different notations for these two sets so no confusion should arise. To be more
specific, [n] = {1, 2, . . . , n} and k = {0, 1, . . . , k 1}. Strings over k are called k-ary
strings. Strings over 2 are called bitstrings. The Hamming distance, Ham(x, y), between
two bitstrings x and y of the same length is the number of positions in which they differ. We
also define the Hamming distance between two finite sets to be Ham(A, B) = |AB||AB|.
A list is a finite sequence, usually a sequence of strings. For a list L, let first(L) denote
the first element on the list and let last(L) denote the last element on the list L. If L is
a list l1 , l2 , . . . , ln , then L or L1 denotes the list obtained by listing the elements of L in
reverse order; i.e., L = L1 = ln , . . . , l2 , l1 . We have the obvious relations first(L) = last(L)
11

12

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

and last(L) = first(L). If L1 and L2 are lists then the interface between L1 and L2 is the
ordered pair (last(L1 ), first(L2 )). If L is a list of strings and x is a symbol, then Lx denotes
the list of strings obtained by appending an x to each string of L. By prepending an x
to each string of L we obtain x L. For example, if L = 01, 10 then L 1 = 011, 101 and
1L = 101, 110. If L and L0 are lists then L L0 denotes the concatenation of the two lists.
For example, if L = 01, 10 and L0 = 11, 00, then L L0 = 01, 10, 11, 00. If x is a string then
xn denotes the n-fold concatenation of x with itself, xn = xx x. In particular if x is a
symbol, then xn is the string consisting of n occurances of x.
!!! These next two paragraphs should be move later, since lex, relex, tree,
etc. not defined yet !!!
We say that a list of strings has the prefix property if those strings with a common prefix
appear contiguously on the list; the suffix property is defined analogously. Lex and relex
lists have the prefix property and colex lists have the suffix property. Given a closeness
relation for which the underlying graph has a Hamilton path, there may not exist a prefix
listing corresponding to a Hamilton path. Consider, for example, the strings 000, 100, 101,
111, 011, with strings close if they differ by a single bit.
With every finite language L is associated a prefix tree, T (L). Each node of the tree
is labelled by a symbol and every prefix of a string in L corresponds to a unique path in
T (L) starting at the root. The tree T (L) is a rooted labelled tree; it is not an ordered tree.
There is a corresponding suffix tree , T R (L).

2.2

Relations and Functions

If A and B are sets then A B denotes the set of all ordered pairs (a, b) where a A and
b B. A relation on a set A is a subset R of A A. The relation R is reflexive if (a, a) R
for all a A. The relation R is anti-reflexive if (a, a) 6 R for all a A. The relation
R is symmetric if (a, b) R implies that (b, a) R. The relation R is anti-symmetric if
(a, b) R implies that (b, a) 6 R. The relation R is transitive if (a, b) R and (b, c) R
implies that (a, c) R. The relation R is an equivalence relation if it is reflexive, symmetric,
and transitive. The relation R is a partial order if it is anti-reflexive, anti-symmetric, and
transitive (partial orders are discussed further in Section 2.9).
A function from a set A to a set B is a subset F of A B with the property that for
each a A there is exactly one element in {(a, b) F | b B}. In other words, with
each value of a A there is associated a unique value of b B. We write f : A B
to denote a function from A to B and f (a) = b if (a, b) F . By f (A) we denote the set
{b B | (a, b) F }. If f (A) = B then f is said to be surjective. If f (a) = f (b) implies that
a = b then f is said to be injective. A function f that is both surjective and injective is
said to be a bijection. Another name for a surjective function is an onto function. Another
name for an injective function is a one-to-one function.
Clearly, if A and B are finite sets and f : A B is a bijection then |A| = |B|.
Thus if the cardinality of A is known and a bijection between A and B is established,
then the cardinality of B is known. We shall use this style of proof several times. If the
bijection is made explicit, for example by means of an algorithm, it is called bijective proof
or combinatorial proof. Combinatorial proofs are particularly useful for translating between
various representations of combinatorial objects.
c
uvic01, Frank
Ruskey, 19952001

2.3. ASYMPTOTIC NOTATION

2.3

13

Asymptotic Notation

In analyzing algorithms it is generally accepted that running time matters most for big values of the input parameters and that constant factors are usually of secondary importance.
This has lead to notations useful for comparing algorithms; these notations arose out of the
mathematical literature for comparing the growth rate of functions.
Let f (n) and g(n) be functions from the natural numbers to the non-negative real
numbers. We say that f (n) is of order (at most) g(n) and write f (n) O(g(n)) if there
are positive constants c and n0 such that f (n) c g(n) for all n n0 . We say that f (n)
is of order at least g(n) and write f (n) (g(n)) if there are positive constants c and n0
such that f (n) c g(n) for all n n0 . We say that f (n) is of order exactly g(n) and
write f (n) (g(n)) if f (n) O(g(n)) and f (n) (g(n)). We say that f (n) and g(n)
are asymptotically equal and write f (n) g(n) if the limit of f (n)/g(n) is 1 as n tends to
infinity.

2.4

Subsets and Combinations

n
An n-set is a set with n elements. An n-set
n has 2 subsets. The number of k element
subsets (k-subsets) of an n-set is denoted k ; this notation is called a binomial coefficient.

n(n 1) (n k + 1)
n
n!
=
(2.1)
=
k!(n k)!
k(k 1) 1
k

If n and k do not satisfy 0 k n, then the value of nk is taken to be 0. A table of
binomial coefficients may be found in Appendix
n A.
Below is the Pascal Triangle recurrence
n
relation, valid for 0 < k < n. Note that 0 = n = 1.

n
n1
n1
=
+
(2.2)
k
k
k1

Two useful sums are obtained by iterating the first and second terms of (2.2).

n
X
k+j
j=0

k
X
n+j
j=0

n+1
=
k+1

n+k+1
k

(2.3)

(2.4)

A k-composition of n is a solution of x1 +x2 + +xk = n where xi 0 for i = 1, 2, . . . , n.


The number of such compositions is the same as the number of ways of selecting k objects
from n objects with repetitions allowed; i.e., it is

n+k1
.
k
The binomial coefficients are unimodal in k. Asymptotically, the value of the largest
coefficient is

2n
22n
.
n
n
c
uvic01, Frank
Ruskey, 19952001

14

2.5

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

Permutations

Permutations are a fundamental combinatorial object and they many variants, some of
which are introduced below. A permutation of a set S is an arrangement of the elements of
S in some order. Normally, our permutations will be of the set S = [n] = {1, 2, . . . , n}; and
we assume this unless stated otherwise. The set of all permutations of [n] is denoted Sn .
The mathematical definition of a permutation is that it is a bijection : S S. We usually
write a permutation as a sequence (1), (2), . . . , (n) or a string (1)(2) (n); this is
often called one-line notation. To simplify the notation we sometimes write i for (i).
As is well-known, the number of permutations of a set with n elements is n! = n(n 1) 1.
A useful asymptotic is given by Stirlings approximation

n n

1
1
n! = 2n
1+
+O
e
12n
n2
The inverse 1 of a permutation is the unique permutation 1 for which 1 ((i)) =
( 1 (i)) = i for i = 1, 2, . . . , n. For example, the inverse of 84235617 is 73425681.
Another notation for writing permutations is cycle notation. A cycle of length k, or a
k-cycle, in a permutation is a sequence of distinct elements a1 , a2 , . . . , ak such that ai =
(ai1 ) for i = 2, 3, . . . , k and a1 = (ak ). Such a cycle is written (a1 a2 ak ). Clearly
any permutation can be written as a collection of disjoint cycles. For example, a cycle
notation for 84235617 is (5)(8 7 1)(4 3 2)(6). It is customary to omit 1-cycles from the
notation, to write cycles with the smallest element listed first, and to order the cycles by
their smallest elements. Thus the previous example would be written as (1 8 7)(2 4 3).
A transposition is a permutation that is a 2-cycle. A permutation without 1-cycles is a
derangement. In one-line notation a derangement of [n] is characterized by the property
that i 6= i for all i [n].
The number of permutations of [n] with k cycles is the signless Stirling number of the
first kind, c(n, k). The numbers c(n, k) also count the number of permutations of n with
k left-to-right maxima (i.e., elements i such that i > j for all j < i). For example, the
4 left-to-right maxima in the permutation 43256871 are underlined. Exercise 3 asks for a
proof. Clearly, c(n, n) = 1 and c(n, 1) = (n 1)!. If 1 < k < n, then
c(n, k) = (n 1) c(n 1, k) + c(n 1, k 1).

(2.5)

A table may be found in Appendix A.


An inversion of a permutation is a pair (i , j ) where i < j and i > j . Let I()
denote the number of inversions of . For example, classifying the inversions by decreasing
values of i, the permutation 84235617 has 0 + 1 + 1 + 1 + 1 + 3 + 7 = 14 inversions. The
sign of is defined to be (1)I() . A permutation is said to be even or odd as I() is
even or odd. The parity of a permutation is the parity (either even or odd) of I(). The
parity difference of a set S of permutations is the number of permutations in S with even
parity minus the number with odd parity. The number of inversions of a permutation is
the same as the number of inversions of its inverse; i.e., I( 1 ) = I().
The index of a permutation is the sum of indices j (where 1 j n 1) such that
j > j+1 , and is denoted J(). For example, the index of 84235617 is 1 + 2 + 6 = 9.
A remarkable theorem (of MacMahon) states that the number of permutations with k
inversions is the same as the number of permutations with index k; i.e., that
|{ Sn : I() = k}| = |{ Sn : J() = k}|

(2.6)
c
uvic01, Frank
Ruskey, 19952001

2.5. PERMUTATIONS

15

for all k = 1, 2, . . . , n. Exercise 8 asks for a proof.


A permutation for which 1 = is said to be an involution ; i.e., it is a permutation
such that ((i)) = i for all i = 1, 2, . . . , n. Thus an involution is characterized by the
property that all of its cycles have length 1 or 2. Every transposition is an involution but
not vice-versa. More generally, a bijection : S S is an involution if ((x)) = x for all
x S. The number of standard Young tableau (Section 2.8) with n cells is the same as the
number of involutions of [n].
A run in a permutation is a maximal length sequence (i) < (i + 1) < < (j). For
example, the permutation
|3 5 7|1 6 8 9|4|2|
has four runs. The number
n of permutations of 1, 2, . . . , n with k runs isnthe
Eulerian

number, and is denoted k . A table may be found in Appendix A. Clearly, 1 = nn = 1.
If 1 < k < n, then

n
n1
n1
(2.7)
= k
+ (n k + 1)
.
k
k
k1
A permutation 1 2 n is up-down if 1 < 2 > 3 < 4 . Let En denote the set of
up-down permutations of {1, 2, . . . , n}. The cardinality of En is denoted En . The numbers
En are called the Euler numbers (not to be confused with the Eulerian numbers discussed
earlier) and the following recurrence relationmay be found in Comtet [68].
En+1

n
1X n
Ek Enk
=
k
2

(2.8)

k=0

The recurrence relation (2.8) may be proven by considering the 2En+1 permutations
that alternate in either direction. The permutations are then classified by the number of
integers, k, to the left of the n + 1 in the permutation.
A k-permutation of an n-set S is an arrangement of some k elements of S. The number
of such arrangements is
(n)k = n(n 1) (n k + 1).
The notation (n)k is called the falling factorial. There is an analogous rising factorial
notation (n)k = n(n + 1) (n + k 1), but we shall have no occasion to use it.

2.5.1

Permutations of a Multiset

A multiset is a like a set, except that repeated elements are allowed. In the literature,
sometimes bag is used instead of multiset. Suppose that the elements of a multiset S consist
of natural numbers between 0 and t, and that there are ni occurences of i. Then the number
of permutations of S is given by the multinomial coefficient

n
n!
,
=
n0 !n1 ! nt !
n0 , n1 , . . . , nt
where n = |S| = n0 + n1 + + nt .
c
uvic01, Frank
Ruskey, 19952001

16

CHAPTER 2. BASIC COMBINATORIAL OBJECTS


q
q
q
q
q
q

q q q q q q
q q q q q q
q q q
q
q

q
q
q
q
q
q
q

7+7+4+2+2+1

q
q
q
q
q
q
q

q q q q
q q q
q
q

6+5+3+3+3+2+2+2

Figure 2.1: Ferrers diagram and conjugate partition.

2.6

Partitions

Partitions come in two varieties, partitions of an integer and partitions of a set.

2.6.1

Numerical Partitions

A (numerical) partition of n into k parts is a sequence p1 p2 pk 1 such that


n = p1 + p2 + + pk . Let P (n) denote the number of partitions of the integer n. Heres a
table of P (n) for 1 n 17.
n
p(n)

1
1

2
2

3
3

4
5

5
7

6
11

7
15

8
22

9
30

10
42

11
56

12
77

13
101

14
135

15
176

16
231

17
297

Let p(n, k) denote the number of partitions of n into k parts. Then p(n, 1) = p(n, n) = 1
and for 1 < k < n,
p(n, k) = p(n 1, k 1) + p(n k, k).
A Ferrers diagram of a partition p1 p2 pk > 0 is an arrangement of n dots
(on a square lattice) where there are pi dots in the ith row and the leftmost dots on each
row are aligned in a column. See Figure 2.1 for an example. The conjugate of a partition is
obtained by flipping the Ferrers diagram about the diagonal running through the upper left
dot. Clearly, the conjugate of a partition with k parts results in a partition whose largest
part is k, and vice-versa. This observation leads us to the following lemma.
Lemma 2.1 The number of partitions of n whose largest part is k is p(n, k).
Number Theoretic Functions
The greatest common divisor, gcd(n, m), of two natural numbers n and m is the largest
natural number that divides both n and m. The least common multiple, lcm(n, m), of n
and m is the smallest natural number that is divisible by both n and m. Numbers n and m
are relatively prime if gcd(n, m) = 1. A proof of the following useful lemma may be found
in [159].
Lemma 2.2 The multiset of n numbers {jm mod n : j = 0, 1, . . . , n 1}, consists of d
copies of the m/d distinct numbers {kd : k = 0, 1, . . . , m/d 1}, where d = gcd(m, n).
c
uvic01, Frank
Ruskey, 19952001

2.6. PARTITIONS

17

Eulers totient function, (n), is the number of natural numbers less than n and relatively
prime to n. For example, (1) = (2) = 1, (3) = (4) = (6) = 2, (5) = (8) = (10) =
(12) = 4, and (7) = (9) = 6. An explicit formula is given below, where the product is
taken over all primes p that divide n.

Y
1
(n) = n
1
.
(2.9)
p
Observe that d = gcd(i, n) if and only if 1 = gcd(i, n/d). Thus
n
X
X
X
f (gcd(i, n)) =
(n/d)f (d) =
(d)f (n/d).
i=1

d\n

(2.10)

d\n

Define the M
obius function, (n) as follows.

(1)t if n is the product of t distinct primes


(n) =
0
otherwise
Thus (1) = (6) = 1, (4) = 0, and (p) = 1 for any prime p. The following useful
lemma is known as the Mobius inversion formula.
Lemma 2.3 If g and f are functions from positive integers to positive integers, then
X
X
(n/d)f (d).
g(d) if and only if g(n) =
f (n) =
(2.11)
d\n

d\n

2.6.2

Set Partitions

A (set) partition of S into k blocks is a collection of sets {S1 , S2 , . . . , Sk } where each Si S,


the sets are pairwise disjoint (Si Sj = for all i 6= j), and the union of all sets is S (for
each s S, there is an i for which s Si ). The number of set partitions of an n-set is
denoted Bn which is called a Bell number. They satisfy the following recurrence relation.
If n = 0, then B0 = 1, and for n 0,
n
X
n
Bk ,
Bn+1 =
(2.12)
k
k=0

from which we can construct the following table.


n
Bn

1
1

2
2

3
5

4
15

5
52

6
203

7
877

8
4140

9
21147

10
115975

11
???

12
???


There is a very cute tabular way to compute Bell numbers; see Exercise 17. By nk
we denote the number of partitions of an n-set into exactly
k disjoint subsets. The disjoint

subsets are traditionally called blocks. The numbers nk are known as the Stirling numbers
of the second kind. A table may be found in Appendix A. They satisfy the following
recurrence relation which may be proven by classifying partitions according to whether n
occurs in a block by itself or not.

n
n1
n1
=
+k
(2.13)
k
k1
k
The following explicit expression is known.


k
n
1 X
k n
=
(1)ki
i
k
k!
i
i=0

c
uvic01, Frank
Ruskey, 19952001

18

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

4
6

8 18

11 8

5 10 12

9 13 17

11 16

14

15

Figure 2.2: (a) A standard Young tableau of shape 6,4,4,2,1,1. (b) The hook lengths of that
tableau.

2.7

The twelve-fold way

The following table, taken from Stanleys book [402], gives a unified look at the most
fundamental numbers of combinatorics. Consider functions f : N X where |N | = n
and |X| = x. The different numbers arise depending upon whether the elements of N
and X are regarded as being distinguishable (dist.) or indistinguishable (indist.). Further
classification is done depending upon whether f is one-to-one or onto. If P is a proposition,
then [[P ]] is 1 if P is true and is 0 if P is false.
Elements of N
dist.
indist.
dist.
indist.

2.8

Elements of X
dist.
dist.
indist.
indist.

Any f
n
x
n+x1
Pxn n
i
Pi=1
x
p(n,
i)
i=1

Injective f
(x)
n
x
n

[[n x]]
[[n x]]

Surjective
f
n
x!
x
n1

nx

n
x

p(n, x)

Tableau

Let = 1 , 2 , . . . , k be a partition of the number n. A standard Young tableau (or simply


a tableau) of shape is an arrangement of 1, 2, . . . , n into k rows of numbers, where the ith
row contains i numbers, and the numbers along each row and each column are increasing.
The numbers are placed in the manner of a Ferrers diagram, the leftmost number in each
row aligned in a column.
There is an amazing formula that gives the number of standard tableau of given shape,
call is d . The hook length of a cell in a tableau is the number of cells to the right of
it or below it, including the cell itself. In the figure we show a tableau of shape 6,4,4,2,1,1
and to the right of it, the hook length of each cell. Heres the formula:
n!
d = Q
c hc

2.9

Partially Ordered Sets

Partially ordered sets are finding increased application in diverse areas of computer science
and discrete mathematics. There is even a journal, Order, that is devoted to results about
them.
c
uvic01, Frank
Ruskey, 19952001

2.9. PARTIALLY ORDERED SETS

19

Figure 2.3: (a) Forbidden subposet for lattices. (b) Forbidden sublattices for distributive
lattices.
A poset (partially ordered set) P is a pair P = (S(P), R(P)), where S(P) is a set and
R(P) is a reflexive, antisymmetric, transitive relation on S(P). If (x, y) R(P), then we
write x P y, or x y if P is understood from context.
If x y and x 6= y, then we write x y. If neither of the relations x y or y x hold,
then we say that x and y are incomparable and write x k y. A cover relation x y is one
for which there does not exist a z with x z y. A Hasse diagram is a representation of a
poset as an undirected graph embedded in the plane. Edges connect those elements related
by a cover relation. If x y, then x is drawn lower in the diagram than y. An element
x is minimal if it covers no other element. An element z is maximal if it is covered by no
other element.
An antichain is a set of incomparable elements. A chain is a set of elements, each pair
of which is comparable; thus a chain can be regarded as a sequence x1 x2 xn . If
P and Q are posets over the same set S of elements (i.e., S(P) = S(Q)) and R(P) R(Q),
then Q is said to be an extension of P. If Q is a chain, then Q is said to be a linear extension
of P. In other words, a linear extension is a permutation x1 x2 xn of the elements of S(P)
with the property that xi xj implies i < j. The number of linear extensions of a poset
P is denoted e(P). The probability, P (x < y), that x preceds y is the number of linear
extensions in which x appears before y, divided by e(P). Clearly, P (x < y) + P (y < x) = 1.
The height, h(x), of an element x S(P) is the average position that it occupies in a
linear extension. Thus, a minimum element has height 1, a maximum element has height
n = |S(P)|, and if P is an antichain then all elements have height (n + 1)/2.
The graph whose vertices are the linear extensions of P and with edges connecting those
vertices that differ by a transposition of two elements is called the permutohedron of P.
Lattices
Let P be a finite poset and A a subset of its elements. A lower bound on A is an element
x for which x y for all y A. An upper bound on A is an element z for which y z for
all y A. The greatest lower bound, glb(A), of A is the element z 0 for which z z 0 for all
lower bounds z on A; clearly a glb need not exist. Similarly, the least upper bound, lub(A),
of A is the element x0 for which x0 x for all upper bounds x on A. A poset for which
lub(x, y) and glb(x, y) exist for all elements x and y is a lattice. Lattices are characterized by
the property that they have no induced subposet isomorphic to a butterfly, as in Figure
2.3(a).
c
uvic01, Frank
Ruskey, 19952001

20

CHAPTER 2. BASIC COMBINATORIAL OBJECTS


A lattice is distributive if it satisfies the the following identities:
x (y z) = (x y) (x z),
x (y z) = (x y) (x z).

A set I where I S(P) is an ideal of P if y I and x y implies that x I. The set


of ideals of P ordered by set inclusion forms a distributive lattice, which we denote J(P).
In fact, all distributive lattices arise as the lattice of ideals of some poset and thus there is
a natural one-to-one correspondence between distributive lattices and posets as specified in
the following famous theorem of Birkhoff.
Theorem 2.1 A lattice L is distributive if and only if it is isomorphic to J(P) for some
poset P.
Distributive lattices also have a forbidden sublattice characterization where the forbidden sublattices are those shown in Figure 2.3(b).

2.9.1

Antimatroids

Antimatroids form an interesting and useful generalization of the ideals and linear extensions
of posets. An antimatroid A is a collection of subsets of some finite set S with the following
two properties: (a) For every non-empty A A, there is an element a A such that
A \ {a} A, and (b) if A, B A, then A B A. We will assume that the union of all sets
in A is S. It is easy to check that the ideals of any finite poset form an antimatroid. The basic
words of an antimatroid are all strings a1 a2 an such that n = |S| and {a1 , a2 , . . . , ak } A
for all k = 1, 2, . . . , n. In the poset case, basic words correspond to linear extensions.

2.10

Trees

There are several important types of trees. We begin with ordered trees, which are the type
most frequently encountered in computer science, and then move onto rooted trees and free
trees, which are more common in mathematics.

2.10.1

Ordered Trees

Definition 2.1 Ordered trees are defined recursively as a collection of distinct nodes constructed according to the following rules:
1. A single node is an ordered tree.
2. If T1 , T2 , . . . , Tk are ordered trees then so is the tree formed by taking a new root r and
making the roots of the Ti children of r. The subtrees are ordered from left-to-right.
When the root r of a rooted tree is removed what remains are called the principle
subtrees; these are the subtrees T1 , T2 , . . . , Tk in the definition above. The degree of r is k.
There are two ways of visiting all nodes of an ordered tree that are of great importance,
called preorder and postorder. In preorder we visit first the root r and then the principal
subtrees recursively in the order T1 , T2 , . . . , Tk . In postorder we visit first the principal
subtrees in order and then the root.
c
uvic01, Frank
Ruskey, 19952001

2.10. TREES

21

r7
6
?
5
 4
?

3
2

1
r
0


1

Figure 2.4: The path corresponding to 11101011100000.

root edge
left edge
right edge

Figure 2.5: A triangulation of a rooted 10-gon and the corresponding extended binary tree
with 10 leaves.
Let T(n) denote the set of binary trees with n nodes, Ord(n) the set of ordered trees
with n nodes and W(n) the set of well-formed parentheses with n left parentheses and n
right parentheses. There are natural bijections between each of the following sets: T(n),
Ord(n + 1), W(n).
Binary trees are counted by the Catalan numbers The nth Catalan number is denoted
Cn and has the following value.

1
2n
Cn =
.
(2.14)
n+1 n
Catalan numbers satisfy the following recurrence relation (with C0 = 1).
Cn+1 =

n
X

Ck Cnk .

(2.15)

k=0

c
uvic01, Frank
Ruskey, 19952001

22

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

111000

110100

110010

101100

101010

Figure 2.6: Catalan correspondences for n = 3: (a) triangulations of a 5-gon, (b) binary
trees with 3 internal nodes, (c) tree sequences, (d) lattice walks on 3 by 3 half-grid, (e)
ordered forests with 3 nodes.
The usual proof of (2.14) uses (2.15) to derive a generating function equation which is
then solved. A much nicer combinatorial proof is based on Lemma 2.4 below.
Definition 2.2 A valid sequence of 0s and 1s is one for which the number of 1s always
exceeds the number of 0s in any prefix.
For example, 11010 is valid but 110011 is not. The following lemma is known as the
Cycle Lemma.
Lemma 2.4 Among all the circular shifts of a bitstring with n 0s and m 1s with n < m
there are m n valid bitstrings.

To prove (2.14) take m = n + 1. There are 2n+1


bitstrings of length 2n + 1. They may
n
be divided into equivalence classes, each of which contains 2n+1 members (depending on the
position of the unique 1 that starts a valid bitstring). Each eqivalence class corresponds to an
element of T(n) by removing the initial 1 from the single valid bitstring of each equivalence
class. The number of equivalence classes (and hence the value of Cn ) is therefore


2n + 1
1
2n
1
=
.
Cn =
2n + 1
n
n+1 n
The cycle lemma may be proven by repeatedly deleting 10 pairs. After performing the
deletions there are m n 1s remaining, which are precisely the starting 1s of the valid
bitstrings. The cycle lemma is related to the so-called trip around the moon theorem,
which is based on the following scenario. Imagine that there are space stations in a fixed
orbit around the moon and a rocket ship that takes 100 units of fuel to complete one
revolution of the moon. Suppose that a total of 100 units of fuel have been distributed at
the stations. The theorem states that there is at least one station at which the rocket ship
may start its journey and be able to complete one revolution, no matter how the stations
are situated or how the fuel is distributed.
c
uvic01, Frank
Ruskey, 19952001

2.10. TREES

23

The number of ordered trees with ni nodes of degree i is


#

"
t
X
n
1
1+
(1 i)ni .
n0 n0 , n1 , . . . , nt

(2.16)

i=0

2.10.2

Rooted Trees

Rooted trees can be defined similarly to ordered trees with the difference that the collection
of subtrees of a node are regarded as being unordered. An in-tree is a rooted tree in which
all edges are directed towards the root, and an out-tree is a rooted tree in which all edges
are directed away from the root. Our definition of the degree of a node in a (rooted
or ordered) tree comes from viewing it as an out-tree and then taking degree to mean
out-degree.
Let T be a rooted tree with ni nodes with i children for i = 0, 1, . . . , t. Thus n =
n0 + n1 + + nt is the total number of nodes in the tree. Since the number of edges in a
tree is n 1, we have
n 1 = 0 n0 + 1 n1 + + t nt
It thus follows that
n0 1 = 1 n2 + + (t 1) nt n2 + + nt ,
with equality in the second relation only if t = 2. Thus
n 2n0 + n1 1.

(2.17)

The inequality (2.17) is important in analyzing certain recursive algorithms for generating combinatorial objects. In these algorithms n is a measure of the total amount of
computation and n0 is the number of objects produced. Then (2.17) allows us to conclude
that an algorithm is CAT if n1 = O(n0 ). In particular, if there are no nodes of degree one,
then the algorithm is CAT. Another condition that is useful in proving the CAT property
is to be found in the following lemma.
Lemma 2.5 If the maximum number of nodes of a chain of degree one nodes is bounded by
a constant c, then n1 = O(n0 ).
Proof:A node x of degree k > 1 has at most k chains of degree one nodes, the roots of
which chains are the children of x. Thus
n1 c(2n2 + 3n3 + + tnt )
= c(n 1 n1 )
2c(n0 1).
2
The number of rooted trees and free trees are asymptotic to
an C1

pn
n3/2

and

tn C2

pn
,
n5/2

respectively,

(2.18)

where C1 = 0.4399..., C2 = 0.5349..., and p = 2.956....


c
uvic01, Frank
Ruskey, 19952001

24

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

Labelled Free Trees


There is a simple expression for the total number of labelled free trees.
Theorem 2.2 The number of labelled free trees on n nodes is nn2 .
Proof:We present here a very nice proof due to Egecio
glu and Remmel [114] which has
a number of important combinatorial consequences. The idea is to establish a bijection
n+1 between the set of all labelled trees on n + 1 nodes that are rooted at n + 1 and the
set of functions f : {2, 3, . . . , n} {1, 2, . . . , n + 1}. The nodes are assumed to be labelled
{1, 2, . . . , n + 1} with edges directed towards n + 1. The bijection is best described by means
of an example. Consider the following function.
i
f (i)

2
3

3
4

4
5

5
3

6
21

7
7

8
12

9
1

10
4

11
4

12
20

13
19

14
19

15
6

16
1

17
16

18
6

19
7

20
12

Now draw the functional digraph of f as illustrated in Figure 2.7. The functional digraph
consists of two trees rooted at 1 and n + 1, together with a number of unicyclic digraphs; in
each case edges are either on the cycle or directed toward a cycle or a root. As illustrated
in the figure, imagine that the components of the digraph are drawn so that they satisfy
the following properties.
The components are ordered left-to-right by the smallest numbered node that is a
root or on a cycle; thus the trees with roots 1 and n + 1 are drawn on the extreme
left and right, respectively.
Cycles are drawn so their vertices form a directed path on the line between 1 and
n + 1, with one backedge above the line and other edges below the line and directed
upwards.
Each cycle is arranged so that its smallest element is at the right.
Suppose that there are k cycles. Let l1 , l2 , . . . , lk and r1 , r2 , . . . , rk be the leftmost
and rightmost vertices on each of those cycles, respectively. To obtain the corresponding
tree remove the edges [ri , li ] for each i = 1, 2, . . . , k and add the edges [ri1 , li ] for each
i = 1, 2, . . . , k + 1, where r0 = 1 and lk+1 = n + 1. Note that r0 < r1 < < rk .
We have now described how to obtain the tree from the function. To obtain the function
from the tree proceed as follows:
1. Locate the unique path from 1 to n+1; call the vertices on this path 1 = a1 , a2 , . . . , am =
n + 1.
2. From right to left determine li and ri by the following process. Let rk = am1 and
lk+1 = n + 1. Inductively assume that ri and li+1 have been determined. Then let
ri1 = ap where p = max{j : aj < ri1 },
and li = ap+1 . Do this for i = k 1, k 2, . . . , 1, 0.
3. Remove the edge [ri1 , li ] for i = 1, 2, . . . , k + 1 and add the edges [ri , li ] for i =
1, 2, . . . , k.
c
uvic01, Frank
Ruskey, 19952001

2.10. TREES

25
'

1s
 AKA

16 s
6

As
9

4 ?
s


 AKA
s
As
10
11

-5s

$
- s3
6
s
2

s
17

(f )

1s
 AKA

16 s
6

As
9

4 s
 AKA
s
As
10
11

-5s

s
17

7s

'$
?
s
20

19 s
 AKA
s
As
13
14

- s3
6

-7s
6

s
2

19 s
 AKA
s
As
13
14

- s12
6

21s
6
s6

s
8

 AKA

s
15

-s
20

- s12
6
s
8

As
18

-21s
6
s6
 AKA

s
15

As
18

Figure 2.7: The functional digraph of f and the bijection .


2
Enumeration of free trees
Lattice path correspondences

2.10.3

Free Trees

A free tree (or simply, a tree) is a connected graph without cycles.


A vertex v is a center of a tree T if its maximum distance to any vertex of T is minimum.
Three useful properties of centers are given below.
1. Every tree has either one or two centers. If it has two centers, then it is said to be
bicentral.
2. If a tree is bicentral, then its centers are adjacent.
3. A vertex is a center if and only if it is a center of every path of maximum length in
the tree.
By removing a vertex v from a tree, some subtrees T1 , T2 , . . . result. Call the weight of a
v, denoted w(v), the maximum of |T1 |, |T2 |, . . .. A vertex v is a centroid if it has minimum
weight. Three useful properties of centroids are given below.
1. Every tree has either one or two centroids. If it has two centroids, then it is said to
be bicentroidal.
2. A tree is bicentroidal if and only if they are adjacent and the removal of the edge
joining them results in two trees of equal sizes.
c
uvic01, Frank
Ruskey, 19952001

26

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

Figure 2.8: For this tree x is the centroid and y is the center.
3. A vertex v is a unique center if and only if w(v) (n 1)/2.
The centroid and center of a tree need not coincide; see for example Figure 2.8

2.11

Graphs

Graphs provide an important conceptual tool for us in dealing with Gray codes and, of
course, are of deep mathematical interest in their own right. We state here a few basic
definitions and results. For further information consult a basic text such as Bondy and
Murty [35]. There are a great many terms that are used in graph theory and a good deal
of care is required to define everything properly.
A undirected graph (or simply a graph) G is a pair (V (G), E(G)) where V (G) is a finite
set and E(G) is a set of 2-subsets of V (G). Where no confusion can arise a graph is
denoted (V, E). The elements of V are called the vertices of the graph and the elements
of E are called the edges of the graph. A graph H = (V (H), E(H)) is a subgraph of G if
V (H) V (G) and E(H) E(G). Graph H is an induced subgraph of G if V (H) V (G)
and E(H) = {[u, v] E(G) | u, v V (H)}. If [v, w] E then v and w are said to
be adjacent (or joined ). The degree of a vertex is the number of vertices to which it is
adjacent. A pendant vertex is a vertex of degree one. A graph in which every vertex has
degree k is said to be k-regular. Another name for a 3-regular graphic is cubic graph.
A path in a graph is a sequence of vertices v0 , v1 , . . . , vl such that [vi1 , vi ] E for i [l].
The length of the path is l. A cycle is a path for which v0 = vl . Again, the length of the
cycle is l. An l-cycle is a cycle of length l in which the vertices v1 , v2 , . . . , vl are all distinct.
A graph is connected if there is a path between every pair of vertices. It is acyclic if there
is no cycle of length three or more.
A function mapping V to V is an automorphism of G = (V, E) if [v, w] E if and only
if [(v), (w)] E. The set of all automorphisms of a graph form a group under function
composition. If, for every pair of vertices v and w, there is an automorphism that maps v
to w, then G is said to be vertex-transitive.
A directed graph G is a pair (V (G), E(G)) where V (G) is a finite set and E(G) is a
subset of V (G) V (G). An edge (v, w) E(G) is said to be directed from v to w, and is
q-q w
drawn in the figures as v
.
The out-degree, d (v), of a vertex v is the number of edges of the form (v, w) where
c
uvic01, Frank
Ruskey, 19952001

2.11. GRAPHS

27

w V . The in-degree, d+ (v), of a vertex v is the number of edges of the form (w, v) where
w V.
With each directed graph G = (V (G), E(G)) there is associated an undirected graph
0
G = (V (G), E(G0 )) where [v, w] E(G0 ) if (v, w) E(G) with v 6= w. A directed graph is
connected if the associated undirected graph is connected. Paths and cycles are defined as
they were for undirected graphs except that the directions of the edges must be respected.
I.e., a path is a sequence of vertices v0 , v1 , . . . , vl such that (vi1 , vi ) E for i [l]. A directed
graph is acyclic if it contains no cycles. Sometimes a directed acyclic graph is called a dag.
Every dag has a topological sorting which is an ordering of the vertices v1 v2 vn so that
(vi , vj ) E implies i < j; this concept is closely related to that of a linear extension of a
poset.
An acyclic orientation of an undirected graph G is a directed acyclic graph G0 obtained
from G by orienting each edge; i.e., each edge [u, v] E(G) becomes either (u, v) or (v, u)
in E(G0 ). A directed graph is strongly connected if there is a path between every pair of
vertices.
A graph is bipartite if the vertex set V can be partitioned into two sets A and B such
that every edge is incident with an element of A and an element of B. The sets A and B are
called partite sets . The basic characterization of bipartite graphs is given in the following
theorem.
Theorem 2.3 A graph is bipartite if and only if it contains no odd cycles.
The complete graph Kn has n vertices and an edge between every pair of vertices. The
complete bipartite graph Kn,m has n vertices in one partite set and m in the other partite
set, and an edge between every pair of vertices in different partite sets. A star graph is
K1,n for some n.
The cartesian product G H of two graphs G and H is the graph with vertex set
V (G) V (H) and with edge set
{{(u, v), (x, y)} | {u, x} E(G) and {v, y} E(H)}
The prism of G is the graph G e, where e is the graph consisting of a single edge.
An n-cube, denoted Qn is the graph K2 K2 K2 , where the product is taken n
times. Alternately, we may define Qn as the graph whose vertices are {0, 1}n and where two
edges are joined by an edge if they differ in exactly one position. A hypercube is a graph
that is an n-cube for some n. The graph Q4 is known as a tesseract.
If G is a graph then by Gk we denote the graph with the same vertex set as G but which
has an edge between every pair of vertices that are connected by a path of length at most
k in G. In other words, if M is the incidence matrix of G, then M k is the incidence matrix
of Gk , where arithmetic is done logically (1 means true, 0 means false, means , and +
means ). The cube of G is G3 and the square of G is G2 .
The concept of a graph is extended to allow for multiple copies of edges and self-loops,
which are edges of the form {v, v}. In a multi-graph G = (V (G), E(G)), the edge set
E(G) is a multiset of 1- or 2-subsets of V (G). In a multi-digraph E(G) is a multiset of
V (G) V (G). Many of the previous definitions make sense in the context of multi-graphs
and multi-digraphs but there are a few exceptions: the degree of a vertex counts a self-loop
twice; paths must be thought of as sequences of edges, rather than vertices. Let G = (V, E)
be a multi-graph with n vertices and e edges. An Eulerian cycle in a G is a sequence of
c
uvic01, Frank
Ruskey, 19952001

28

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

vertices of the graph v1 , v2 , . . . , ve (with repetitions allowed) such that the following multiset
equality holds:
E = {ve , v1 } {{vi , vi+1 } | i = 1, 2, . . . , e}.
Lemma 2.6 A connected undirected multigraph is Eulerian if and only if every vertex has
even degree. A connected directed multi-graph is Eulerian if and only if the out-degree of
each vertex is the same as its in-degree.
A Hamilton path H in G is a sequencing of vertices of the graph H = v1 , v2 , . . . , vn such
that each vertex occurs exactly once and {vi , vi+1 } E for i = 1, 2, . . . , n 1. If in addition
{vn , v1 } E then H is a Hamilton cycle. A graph that has a Hamilton cycle is said to
be Hamiltonian. Note that under this definition a graph consisting of a single edge has
(actually is) a Hamilton path, and is Hamiltonian. Many results about the Hamiltonicity
of particular graphs may be found in Chapter 6.
There are many important variants of Hamiltonicity: A graph is Hamilton-connected if
there is a Hamilton path between every pair of vertices in the graph. A bipartite graph is
Hamilton-laceable if there is a Hamilton path between every pair of vertices, where each of
the two vertices is in a different partite set. A graph G = (V, E) is pancyclic if it possesses
l-cycles for l = 3, 4, . . . , |V |. A graph is hypo-Hamiltonian if it is not Hamiltonian but every
vertex-deleted subgraph G {v} is Hamiltonian. All of the definitions of this paragraph
apply without modification to digraphs.
A set I V (G) is an independent set if there is no edge e = {v, w} E(G) such that
v I and w I. A set C V (G) is a clique if (u, v) E for every distinct pair u, v C.
A set U V (G) is a vertex cover if, for every edge (u, v) E, we have u U or v U (or
both). A k-coloring of a graph is a function f : V [k] such that f (u) 6= f (v) whenever
[u, v] E.
A graph H = (V (H), E(H)) is a spanning subgraph of G = (V (G), E(G)) if V (H) =
V (G) and E(H) E(G). A spanning tree of G is a spanning subgraph T which is a tree.
A directed graph with n vertices is a tournament if for every pair i, j with 1 i < j n
the graph either has the edge (i, j) or has the edge (j, i), but not both.

2.11.1

The Matrix Tree Theorem

The Matrix Tree Theorem provides a elegant and efficient way to count the number of
spanning trees in an undirected graph, or the number of spanning out-trees in a digraph.
We denote by (G) the number of spanning trees of G. Given a graph G on n vertices
labelled from [n], the Kirchoff matrix K(G) is the matrix whose ith diagonal entries is the
degree of vertex i and where the ij entry for i 6= j is 1 if [i, j] E(G) and 0 otherwise.
Theorem 2.4 (Matrix Tree) Let G be a labeled graph on [n]. The number of spanning
trees of G is equal to any cofactor of K(G).

2.11.2

Representing Graphs

There are two standard ways of representing graphs, one which uses a matrix and the other
which uses linked lists. The adjacency matrix representation is a binary matrix A = [aij ],
an n by n matrix for which aij is 1 if there is an edge from i to j and is 0 if not. This
c
uvic01, Frank
Ruskey, 19952001

2.11. GRAPHS

29

Figure 2.9: A graph and its adjacency list representation.


representation is useful for graphs with many edges. In the case of an undirected graph, A
is symmetric.
The adjacency list representation is most useful for graphs with smaller numbers of
edges, as often arise in practice. A graph together with its adjacency list representation is
shown in Figure 2.9. Assume that the vertices of the graph are labelled 1, 2, . . . , n. Define
an array Adj[1..n] of pointers to nodes with containing at least a field vert and another
field next. The next fields are used to form a linked list, pointed to by Adj[v] containing
those vertices pointed to by an edge starting from v. For each edge (v, w) E, there is a
node on the list Adj[v] whose vert field has the value w. If G is an undirected graph, then
each vertex [u, v] E is represented twice, as if there were directed edges (u, v) and (v, u).
In the case of multi-graphs both representations are easily extended. The adjacency list
representation is unchanged; multiple edges occur as multiple occurences on the relevant
list. The adjacency matrix representation is changed so that aij represents the number of
times that edge (i, j) occurs.

2.11.3

Depth First Search

Depth-first search is a simple recursive way of exploring, or traversing, a graph. It is often


used with graphs represented by adjacency lists. See Algorithm 2.1. Initially the array
num[1..n] is initialized to zero, as are the variables start time and finish time, and
then the loop
for v := 1 to n do if num[v] = 0 then dfs( v );
is executed. If G is an undirected multi-graph then dfs is called exactly once in this for loop
for every connected component of G. The final values of num give a depth-first numbering
of G.
Assuming that graph G has n vertices and m edges, depth-first search runs in time
O(n + m) since each vertex is visited exactly once, and its edges list is traversed exactly
once.
There are many graph algorithms based on depth-first search, including O(n + m) algorithms to find biconnected components, strongly connected components, and for testing
whether a graph is planar. We will not have occasion to use such algorithms, but topological
sorting is important to us and a depth-first-search algorithm for this is presented next.
Using depth-first-search for topological sorting
A topological sorting of a directed acyclic graph may quite easily be accomplished in time
O(n + m) by performing the following two steps.
c
uvic01, Frank
Ruskey, 19952001

30

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

procedure dfs ( v : vertex );


var w : vertex; p : ^Edge;
begin
start_time := start_time + 1;
num[v] := start_time;
p := adj[v];
while p <> nil do begin
w := p^.vert;
if num[w] = 0 then dfs( w );
p := p^.next;
end;
finish_time := finish_time + 1;
f[v] := finish_time;
end of dfs;

Algorithm 2.1: A Pascal procedure for performing depth-first search of a graph.


1. Do a depth-first-search of G computing a postorder numbering f .
2. Arrange the vertices in the order v1 , v2 , . . . , vn where f [v1 ] f [v2 ] f [vn ].
These two steps can be arranged into a single modified version of the dfs algorithm.

2.12

Finite Groups

A group, G, is a set together with a binary operation that satisfies the following laws.
1. There is an identity element 1 G such that g 1 = 1 g = g for all g G.
2. For each element g G there is is an inverse element, denoted g 1 such that g g 1 =
g 1 g = 1.
3. The operation is associative; that is (a b) c = a (b c) for all a, b, c G.
Where no confusion can arise the symbol for the operation is omitted. Thus, for example,
the associative law becomes (ab)c = a(bc).
The order of a group G, denoted |G|, is the number of elements in G. In this book all
groups are finite, and so their order is some natural number. Each element g of a finite
group has an order which is the smallest integer m such that g m = 1.
A subset H of G that is also a group under the operation of G is a subgroup of G.
Two groups are said to be isomorphic if there is a bijection between their elements that
preserves operations.
Some important groups
The cyclic group
The dihedral group
The symmetric group Sn is the group of permutations of [n] under the operation of
composition.
c
uvic01, Frank
Ruskey, 19952001

2.12. FINITE GROUPS

31

The hypo-octahedral group is the group of symmetries of a hypercube.


A group is Abelian if the group operation is commutative; that is, if ab = ba for all
group elements a and b.
New groups may be obtained from old by various operations. Only one product will be
used in this book. Let G and H be groups then their product, denoted G H is the group
whose elements are ordered pairs (g, h) with g G and h H with multiplication defined
componentwise. In other words, (a, x)(b, y) = (ab, xy).
Theorem 2.5 Every Abelian group is the product of cyclic groups.

2.12.1

Permutation Groups

Most groups considered in this book are groups whose elements are permutations. What is
the multiplication operation? Let 1 and 2 be permutations, viewed as bijective functions
from [n] to [n]. The multiplication operation is just the composition of the functions in
reverse order. That is, 1 2 (x) = 2 (1 (x)). The symmetric group, Sn , consists of all
n! permutations of [n] with multiplication defined as above. The reason for reversing the
order of function application is that we do computation with permutations most often in
cycle notation, and then multiplication can be done very simply with a single left-to-right
scan.
Theorem 2.6 Every finite group is isomorphic to a permutation group.
A permutation group is transitive if for each pair x, y [n] there is a permutation G
such that (x) = y. The automorphism group G of a graph G is the set of permutations of
its vertices which preserve adjacencies; i.e., G if for every edge [u, v] E(G) it is the
case that [(u), (v)] E(G). A graph is vertex-transitive if and only if its automorphism
group is transitive. In other words, a graph is vertex-transitive if, for every pair u, v V (G),
there is a (G) such that (u) = v.
Lemma 2.7 Every permutation is a product of transpositions.
Group Actions
We say that a group G acts on a set S if each group element g G is a function g : S S
with the properties that
1. g(h(s)) = (gh)(s) for all g, h G and s S.
2. 1(s) = s for all s S.
It follows from the existence of inverses that each group element in fact defines a bijection
on S.
We say that a group element g fixes an element of s if g(s) = s. Define
F ix(g) = {s S | g(s) = s}.
The orbit of s S is the following subset of S.
Orb(s) = {g(s) | g G}
c
uvic01, Frank
Ruskey, 19952001

32

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

The stabilizer of s S is the set of group elements which fix s. We denote


Gs = {g G | g(s) = s}.
Lemma 2.8 For all s S
|Gs ||Orb(s)| = |G|.

2.12.2

Burnsides Lemma

Let G act on S and define x y if and only if there is a g G such that g(x) = y. In
other words, x y if and only if x and y are in the same orbit. This defines an equivalence
relation on S. Burnsides lemma is used to count the number of equivalence classes of
(i.e., the number of orbits of S).
Lemma 2.9 (Burnside) The number of equivalence classes of is equal to
1 X
|F ix(g)|.
|G|
gG

Proof:Let S1 , S2 , . . . , Sm be the orbits of G acting on S. Clearly,


S = S1 S2 Sm
forms a partition of S. For each g G let Fi (g) = F ix(g)Si . Then F1 (g), F2 (g), . . . , Fm (g)
is a partition of F ix(g). Hence,
X

|F ix(g)| =

m X
X

|Gs |

i=1 sSi

gG

=
=

m X
X
i=1 sSi
m X
X
i=1 sSi

|G|
|Orb(s)|
|G|
|Si |

= n|G|.
2

2.12.3

Polya Theorem

Let G be a permutation group on [n] and define bk (g) to be the number of cycles of g of
length k. Then the cycle index polynomial of G is
1 X b1 (g) b2 (g)
P (x1 , x2 , . . . , xn ) =
x1 x2
xbnn (g)
|G|
gG

Let C be a finite set (of colors) and let be the set of all functions : [n] C (ways
of coloring X). Then G acts on in the following natural manner. For g G and
define
(g)(x) = (g 1 x).
Theorem 2.7 (P
olya) The number of orbits of G acting on is
P (|C|, |C|, . . . , |C|)
c
uvic01, Frank
Ruskey, 19952001

2.13. MISCELLANEA

2.12.4

33

Cayley Graphs

Let G be a group and S a subset of G. The subset S is said to be a generating set or set
of generators for G if every element of G can be written as g = g1 g2 gk for some k and
elements gi S. If G is a group and X is a set of generators1 of G, then the directed Cayley
~
graph, Cay(X
: G), is the graph whose vertex set is G and where the edges leaving g are all
of the form g gx for x X. The undirected Cayley graph, Cay(X : G), has vertex set
G and where the edges incident to g are either of the form [g, gx] or [g, gx1 ] for x X.
In the undirected case we can think of the edges as being colored by the corresponding
generator; Cay(X : G) is |X| edge-colorable.

2.13

Miscellanea

Suppose that we are generating sets of objects parameterized by numbers n, where there
are an objects with parameter n. The analysis of many generation algorithms involves
comparing the sum a1 + a2 + + an with an . The following lemma, easily proven by
induction, can be useful.
Lemma 2.10 If aj /aj+1 < 1 for all j 1, then
n
X

ai

i=1

1
an
1

P
P
P
As examples of its use, we observe that
Ci = O(Cn ),
Bi = O(Bn ), and
Ti = O(Tn ),
where Cn is the Catalan number, Bn is the Bell number, Tn is the number of rooted trees
with n nodes, and the summation is over i = 1, 2, . . . , n. In each case, an asymptotic
expression for the numbers is used to show that the condition of Lemma 2.10 is satisfied.

2.13.1

Summations

Two useful summation formula are


n X
i
X
i=0 j=0

n
X
i=0

2.13.2

f (i, j) =

f (i, j) =

i f (n i) =

f (i, j)

(2.19)

j=0 i=j

0jin
j
n1
XX

n X
n
X

f (i)

(2.20)

j=0 i=0

Fibonacci Numbers

The Fibonacci numbers, Fn , are the sequence defined by the recurrence relation Fn =
Fn1 + Fn2 with the initial conditions F0 = 0 and F1 = 1.
1

some authors allow X to be any subset of G, not necessarily a set of generators

c
uvic01, Frank
Ruskey, 19952001

34

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

2.13.3

Constant Time Array Initialization

Sometimes it is useful to use an array without initialization. This is possible however,


each array access and update becomes more expensive, but only by a constant factor.
Let us assume that we are maintaining an array a[1..n], whose values are initialized to 0.
Generalization to other array bounds and initial values is trivial.
We introduce a new variable t, and two arrays [1..n] and 1 [1..n]. Variable t, call
it time, is initialized to 0, and represents the number of array locations that have been
modified. Array entry [i] is the time when a[i] was last modified, if it has been modified;
otherwise it is garbage. Array entry 1 [i] is maintained so that 1 [ [i]] = i whenever [i]
is not garbage.
The initialization
for i:=1 to n do a[i]:=0;
becomes t:=0. The access a[i] becomes
if (1 [i] t) and ( 1 [ [i]] = i) then return( a[i] ) else return( 0 );
this code assumes a short-circuit and (i.e., if the first condition fails then the second is not
evaluated). The update a[i] := x becomes
a[i] := x;
if not ((1 [i] t) and ( 1 [ [i]] = i)) then
t := t + 1;
[i] := t;
1 [t] := i;
This technique should not be used indiscriminately since it will usually slow your program down, unless n is very large and large portions of the array are never modified.

2.14

Exercises
Questions about permutations

1. [1] Develop an O(n) algorithm for computing the inverse of a permutation of [n].
2. [1+] Develop an O(n) algorithm that takes as input a permutation of {1, 2, . . . , n}
in one line notation and computes the cycle representation of the permutation. Show
that the sign of a permutation can be determined in time O(n).
3. [2] (a) Prove recurrence relation (2.5). (b) By direct correspondence prove that c(n, k)
is the number of permutations of n with k left-to-right maxima. HINT: order the cycles
and elements of cycles in a certain way and then remove parentheses.
4. [2] Develop an O(n log n) algorithm for determining the number of inversions of a
permutation of {1, 2, . . . , n}. [R] Can the inversion vector of a permutation be
determined in time O(n)?
c
uvic01, Frank
Ruskey, 19952001

2.14. EXERCISES

35

5. [1] Prove that the number of inversions of a permutation is the same as the number
of inversions of its inverse.
6. [1] Prove that the number of permutations of [n] with an odd number of cycles is
equal to the number with an even number of cycles.
7. [1] Characterize the P -sequences of alternating permutations.
8. [2] Give an inductive proof that the number of permutations of [n] with (n) = p
and k inversions is the same as the number of permutations of [n] with (n) = p
and index k, thus proving (2.6).
9. [2] Let In denote the number of permutations of [n] that are involutions. Find a
simple recurrence relation for In . Find a explicit expression for In .
10. [1+] What is the number of even derangements of length n minus the number of odd
derangements of length n?
11. [3] Prove that the number of permutations with all cycles of even length is equal to the
number of permutations with all cycles of odd length. A bijective proof is preferred.

12. [2] Prove Rysers formula sumr (1)r nr (n r)r (n r 1)nr for the number of
derangements of [n].
Questions about trees
13. [2+] Prove (2.16), the formula for the number of ordered trees with given degrees.
14. [1] What is the average (out)degree of a node in a rooted tree?
15. [1] A sequence a1 , a2 , . . . , an is unimodal if there is some value k such that
a1 a2 ak ak+1 an .
Prove, for fixed n, that the binomial coefficients are unimodal.
16. [2] A sequence x0 , x1 , . . . , xn of positive real numbers is said to be log-concave if
x2i xi1 xi+1 for all 0 < i < n. A sequence x0 , x1 , . . . , xn of real numbers is said
to be concave if 2xi xi1 + xi+1 Prove that any concave sequence is unimodal and
prove that any log-concave sequence is unimodal.
17. [1+] If a = a1 , a2 , a3 , . . . is a sequence then a denotes the sequence a2 a1 , a3
a2 , a4 a3 , . . . and ahni denotes the nth element of a; i.e., ahni = an . Let B be the
sequence of Bell numbers. Prove that Bn = n Bh1i and use this relation to get a
tabular way of computing B. The computation of B1 , B2 , . . . , Bn should use at most
O(n) integers (of size at most Bn ) and O(n2 ) additions, with no other arithmetic
operations. Implement such an algorithm.
18. [3] Develop a fast algorithm for testing whether two bitstrings are rotationally equivalent; i.e., whether one can be obtained from the other by a rotation.
c
uvic01, Frank
Ruskey, 19952001

36

CHAPTER 2. BASIC COMBINATORIAL OBJECTS


Questions related to Catalan numbers

19. [1] Construct the binary and ordered trees corresponding to the bitstring 10111010011000.
20. [1] Imagine 2n persons seated around a circular table. How many ways are there for
all of them to shake hands simultaneously so that no pair of arms crosses?
21. [2] Prove that Cn is odd if and only if n = 2k 1 for some positive integer k.
22. [2] Show that, as n tends to infinity, the ratio (C1 + C2 + + Cn )/Cn tends to 4/3.

Questions about spanning trees


23. [1] What spanning trees (f ) arise from the functions (a) f (i) = i, (b) f (i) = i + 1,
and (c) f (i) = i 1?
24. [1+] Let labelled tree T with n vertices be specified by listing its edges in some order.
Given f : {2, 3, . . . , n} [n + 1], how fast can T = (f ) be computed? Given T how
fast can f = 1 (T ) be computed?
25. [1+] Give a bijective proof that the number of spanning trees of the complete bipartite
graph Kn,m is nm1 mn1 .
26. [2+] Another way to prove Cayleys theorem bijectively is using what is known as
Pr
ufers correspondence. The correspondence is a bijection
P : T (n) {a1 a2 an2 : 1 ai n}
as specified by the procedure below.
for i := 1 to n 2 do
v := lowest numbered leaf;
a[i] := vertex adjacent to v;
T := T {v};
(a) What is the array a corresponding to the tree of Figure 2.7? (b) What is the tree
corresponding to the function f used in Figure 2.7? (c) Write an efficient procedure
for computing P 1 . What is the running time of your procedure?
27. [2] Use Pr
ufers correspondence to prove that (a) the number of labelled trees with t
leaves and (b) the number of labelled trees where vertex 1 has degree k are given by
the following expressions, respectively.
(a)

n! n 2
n2
and (b)
(n 1)nk1 .
t! n t
k1
c
uvic01, Frank
Ruskey, 19952001

2.14. EXERCISES

37

Questions about numerical partitions


28. [1] Give an O(n) algorithm to compute the conjugate of a partition.
29. [3] Let P DO(n) and P DE(n) denote the set of partitions of n into distinct parts where
the number of parts is odd and even, respectively. Prove that |P DE(n)| |P DO(n)|
is (1)k if n = (3k 2 k)/2 for some number k and is 0 otherwise. This result is known
as the Euler pentagonal number theorem.
30. [2] Use the result of the previous exercise to prove that

(1)k p(n (3k 2 + k)/2) = 0

k=

and use this equality to develop a fast algorithm for determining p(n).
31. [1] Let N (n, k) denote the number of parts of size k taken from the set of all partitions
of n. Find a simple expression for N (n, k) in terms of p(n).
32. Add an exercise about the Squire result??
Questions about set partitions
33. [1+] Give a bijective proof showing that the number of ways of placing k nontaking
rooks on the n by
n half-chessboard, obtained by cutting the chessboard along a
n
diagonal, is nk
.
34. [1+] Let T (n, k) denote the number of partitions of [n] into k blocks
where no block
n
contains two consecutive numbers. Show that T (n + 1, k) = k1 .
35. [1+] Find a simple expression for the average number of blocks in a partition of an
n-set.
36. [1] Prove that the Bell numbers are convex: Bn 12 (Bn1 + Bn+1 ).
Miscellaneous questions
37. [2] Lemma 2.7 states that every permutation is a product of transpositions. Prove
that the least number of transpositions in the product is n r, where r is the number
of cycles in .
38. [2] Given a partition of n, develop a fast algorithm to comput d , the number of
standard tableau with shape . As a function of the number of parts and the largest
part, how many operations does your algorithm use?
39. [2] The fence poset, Fn on the set [n] has cover relations (i 1, i), (i + 1, i) for odd i.
How many linear extensions does Fn have? How many ideals does Fn have?
40. [1] Show that a bipartite Hamiltonian graph must have partite sets of equal size.
c
uvic01, Frank
Ruskey, 19952001

38

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

41. [2] Prove the following two sums for n > 1.


X

(d) = n and

d\n

(d) = 0

d\n

42. [2] Prove the following generalization of Mobius inversion: Let f and g be functions
from t-tuples of positive integers. Then
f (n1 , n2 , . . . , nt ) =

X
d| gcd(n1 ,n2 ,...,nt )

g(

n1 n2
nt
, ,..., )
d d
d

if and only if
g(n1 , n2 , . . . , nt ) =

X
d| gcd(n1 ,n2 ,...,nt )

(d)f (

n1 n2
nt
, , . . . , ).
d d
d

43. [2] Prove Lemma 2.2


44. [R+] Show that in any union closed collection of sets that there is some element that
occurs in at least half of the sets. This problem is due to Frankl.
45. [1+] Show that the antimatroid graph is connected. The antimatroid graph has
vertices that are the sets of the antimatroid, and two sets are adjacent if they differ
by only one element.
46. [1+] Modify the dfs Algorithm 2.1 so that it outputs a topological sorting of a graph
whose vertices are labelled 1, 2, . . . , n.
47. [2] Imagine an infinite deck of cards 1, 2, 3, . . .. At step n, pick up the top n cards and
interlace them with the next n cards. For example, after step 2 we have 3, 2, 4, 1, 5, 6, 7, . . ..
It is conjectured that eventually every number becomes the top card of the deck. It
can take a long time before a card gets to the top. Develop as efficient a method as
you can to determine the step at which card n gets to the top. At what step does
card 54 get to the top?
48. [2] Find a graph that is edge-transitive but is not vertex-transitive. Find a graph that
is vertex-transitive but is not edge-transitive.

2.15

Bibliographic Remarks

The table known as Pascals triangle actually predates Pascal, having appeared in a 14th
century Chinese text. A 12th century Hindu text contains an explanation of the binomial
coefficients. Edwards [98] has written a book devoted entirely to Pascals triangle.
Many further references and interesting commentary on the Bell and Catalan numbers
may be found in Gould [158]. An excellent discussion of Fibonacci numbers may be found
in Knuth [218].
For more on the cycle lemma see Dershowitz and Zaks [81], Ehrenfeucht, Haemer, and
Haussler [99], and Graham, Knuth, and Patashnik [159].
c
uvic01, Frank
Ruskey, 19952001

2.15. BIBLIOGRAPHIC REMARKS

39

Theorem 2.2 is often attributed to Cayley, but Cayley attributes it to Borchardt. See
Moon [285] for further bibliographic remarks, and a wealth of other information about
labelled trees.
Further material on antimatroids may be found in the book of Korte, Lov
asz, and
Schrader [231] and in the survey chapter of Bjorner and Ziegler [34].

c
uvic01, Frank
Ruskey, 19952001

40

CHAPTER 2. BASIC COMBINATORIAL OBJECTS

c
uvic01, Frank
Ruskey, 19952001

Chapter 3

Backtracking
3.1

Introduction

Backtracking is a very general technique that can be used to solve a wide variety of problems
in combinatorial enumeration. Many of the algorithms to be found in succeeding chapters
are backtracking in various guises. It also forms the basis for other useful techniques such
as branch-and-bound and alpha-beta pruning, which find wide application in operations
research (and, more generally, discrete optimization) and artificial intelligence, respectively.
As an introductory problem, consider the puzzle of trying to determine all ways to place
n non-taking queens on an n by n chessboard. Recall that, in the game of chess, the queen
attacks any piece that is in the same row, column or either diagonal. To gain insight, we
first consider the specific value of n = 4. We start with an empty chessboard and try to
build up our solution column by column, starting from the left. We can keep track of the
approaches that we explored so far by maintaining a backtracking tree whose root is the
empty board and where each level of the tree corresponds to a column on the board; i.e., to
the number of queens that have been places so far. Figure 3.1 shows the backtracking tree.
This tree is constructed as follows: Starting from an empty board, try to place a queen
in column one. There are four positions for the queen, which correspond to the four children
of the root. In column one, we first try the queen in row 1, then row 2, etc., from left to
right in the tree. After successfully placing a queen in some column we then proceed to the
next column and recursively try again. If we get stuck at some column k, then we backtrack
to the previous column k 1, where we again try to advance the queen in column k 1
from its current position. Observe that the tree is constructed in preorder. All nodes at
level n represent solutions; for n = 4 there are two solutions.
Let us consider now the case of a standard 8 by 8 chessboard. Here the problem is too
large to do the backtracking by hand, unless you are extremely patient. It is easier to write
a program.
The backtracking process, as described above, is recursive, so it is not surprising that
we can use a recursive procedure to solve the eight queens problem. A pseudo-Pascal
procedure for doing this is developed next. The solution x consists of a permutation of
1 through 8, representing the row locations of the queens in successive columns. For the
4-queens problem the permutations giving solutions were x = [2, 4, 1, 3] and x = [3, 1, 4, 2].
As the algorithm is proceeding we need some way to determine whether a queen is being
attacked by another queen. The easiest way to do this is to maintain three boolean arrays,
call them a, b, c. The array a indicates if a row does not contain a queen. The array
41

42

CHAPTER 3. BACKTRACKING

!
!!

!
!!

!!

@
@

a
aa
aa

aa
a

@
@

@
@

Figure 3.1: The four queens backtracking tree.

b indicates if a
diagonal does not contain a queen, and c indicates if a
diagonal
does not contain a queen. The sum of the row and column indices is constant along
diagonals, and the difference of the row and column indices is constant along
diagonals.
1
Thus a is indexed 1..8, array b is indexed 2..16, and c is indexed -7..7. These arrays
are initialized to be true, and then we call Queen( 1 ). The solution is given in the global
array x, which need not be initialized.
(Q1)
(Q2)
(Q3)
(Q4)
(Q5)
(Q6)
(Q7)
(Q8)

procedure Queen ( col : N )


local row : N;
for row := 1 to 8 do
if a[row ] and b[row +col ] and c[row col ] then
x[col ] := row ;
a[row ] := b[row +col ] := c[row col ] := false;
if col < 8 then Queen( col + 1 ) else PrintIt;
a[row ] := b[row +col ] := c[row col ] := true;
Algorithm 3.1: Algorithm for the 8 queens problem.

It turns out that there are 92 solutions to the 8 by 8 puzzle. Only 12 of the solutions
are non-isomorphic in the sense that all other solutions may be obtained from these 12 by
rotating and/or flipping the board.
1

In other languages, the indexing of c may have to be offset.

c
uvic01, Frank
Ruskey, 19952001

3.2. BACKTRACKING ALGORITHMS

3.2

43

Backtracking Algorithms

In the general backtracking scenario we wish to generate all strings S that satisfy some
predicate P .
S = {(x1 x2 x3 ) A1 A2 A3 |P (x1 x2 x3 )}
Each Ai is a finite set. Of course, the strings must be of finite length and there must be
finitely many of them. In most applications the strings will have a fixed length. In order
to apply the backtracking technique the predicate P must be extended to a predicate Q
defined on prefix strings such that the following properties hold:
P (x1 x2 x3 ) implies Q(x1 x2 x3 ), and
Q(x1 x2 xk1 ) implies Q(x1 x2 xk1 x) for all x Ak

(3.1)

In other words, if a string does not satisfy the predicate Q then it cannot be extended
to a string that does satisfy the predicate P . Given a partial solution x = x1 x2 xk1 we
let Sk (x) denote all the valid ways of extending the string by one element. If the string x
is understood we simply write Sk . In other words,
Sk (x) = {x Ak | Q(xx)}.
The vertices of the backtracking tree are all those strings that satisfy Q. The children of
a vertex are all those sequences obtained from the parent sequence by appending a single
element.
As an illustration of the preceeding definitions consider the 8 by 8 non-taking queens
problem. The sets Ai are {1,2,. . . ,8} for each i = 1, 2, . . . , 8. The predicate Q is simply
whether the queens placed so far are non-taking. An example in which the sets Ai differ
for different i will be given in the next section.
A recursive procedure for the general backtracking scenario is deceptively simple. It is
given in Algorithm 3.2. The initial call is Back(1, ); no initialization is necessary. The call
Back(k, x) generates all strings of the form xy for which xy S.
The parameter x, representing a partial solution, is often left as a global variable, as
it was in our procedure for the 8 queens problem. Another property that the 8 queens
solutions have is that they are all strings of the same length. This is usually the case and
results in a simplification of the algorithm. Suppose that all solutions have length n. Then
Back can be modified by making lines (B5) and (B6) into the else clause of the if statement
at line (B4). If, in addition, Q(x1 x2 xn ) implies P (x1 x2 xn ), then the test for P (x) at
line (B4) can be replace by the test k > n. This is done in many of the procedures to be
found in succeeding chapters.
(B1)
(B2)
(B3)
(B4)
(B5)
(B6)
(B7)

procedure Back ( k : N; x : string);


local x : Ak ; Sk : set of Ak
begin
if P (x) then PrintSolution( x );
compute Sk ;
for x Sk do Back( k + 1, xx );
end {of Back};
Algorithm 3.2: Recursive backtracking algorithm.

c
uvic01, Frank
Ruskey, 19952001

44

CHAPTER 3. BACKTRACKING

We also present a non-recursive version of the algorithm; see Algorithm 3.3. This version
is of interest when speed is of utmost concern for example, if the backtracking routine is
being written in assembly language.
k := 1;
compute S1 ;
while k > 0 do
while Sk 6= do
{Advance to next position}
xk := an element of Sk ;
Sk := Sk \ {xk };
if P (x1 x2 xk ) then PrintSolution;
k := k + 1;
compute Sk ;
{Backtrack to previous position}
k := k 1;
end;
Algorithm 3.3: Non-recursive backtracking algorithm.

3.3

Solving Pentomino Problems with Backtracking.

As a somewhat more complicated problem we consider a pentomino problem. A pentomino


is an arrangement of five unit squares joined along their edges. They were popularized
by Golomb [154] 2 . There are 12 non-isomorphic pentominoes as shown in Figure 3.2.
The pieces have been given and arbritrary numbering; traditionally letters have also been
associated with the pieces and we show those as well. We will develop an algorithm to find
all ways of placing the 12 pentomino pieces into a 6 by 10 rectangle. It turns out that
there are 2,339 non-isomorphic ways to do this. One of the solutions is shown in Figure
3.3. Each piece can be rotated or flipped, which can give different orientations of a piece.
Piece 1 has 1 orientation, piece 2 has two orientations, pieces 3,4,5,6,7 have 4 orientations,
and pieces 8,9,10,11,12 have 8 orientations. Each solution to the problem is a member of an
equivalence class of 4 other solutions, obtained by flipping and rotating the 6 by 10 board.
Figure 3.4 shows how the board is numbered. It also shows a particular placement of
the X piece. This placement is represented by the set [8,13,14,15,20]. Every placement
of a piece is represented by some 5-set. The anchor of a placement is the smallest number
in the 5-set.
The backtracking algorithm successively tries to place a piece in the lowest numbered
unoccupied square; call it k. Once k is determined we try to place an unused piece over it.
The pieces are tried in the order 1, 2, . . . , 12 as numbered in Figure 3.2. Thus we wish to
know, for each piece and number k, all placements that are anchored at k. These placements
are precomputed and stored in the array List from the type and variable declarations of
Figure 3.4. For example, the placement of X in Figure 3.4 is the only set in List[1,8].
Of course, the list usually contains more than one element; List[12,8] has eight members,
2

The term pentomino, for a 5-omino, is a registered trademark of Solomon W. Golomb (No. 1008964,
U.S. Patent Office, April 15, 1975).

c
uvic01, Frank
Ruskey, 19952001

3.3. SOLVING PENTOMINO PROBLEMS WITH BACKTRACKING.

1X

3V

4U

5W

45

6T

2I

7Z

11 F
8N

9L

12 P

10 Y

Figure 3.2: The 12 pentomino pieces.

Figure 3.3: A solution to the 6 by 10 pentomino problem.

12 18 24 30 36 42 48 54

13 19 25 31 37 43 49 55

14 20 26 32 38 44 50 56

15 21 27 33 39 45 51 57

10 16 22 28 34 40 46 52 58

11 17 23 29 35 41 47 53 59

Figure 3.4: Numbering the board (and the placement of a piece).

c
uvic01, Frank
Ruskey, 19952001

46

CHAPTER 3. BACKTRACKING

18

7 13

14 20
15

7 13 19
14

12
7 13
14 20

12
7 13 19
8

12 18
7 13
14

12

7 13 20

8 14 20

21

15

59

Figure 3.5: Lists for piece 11, with those anchored at location 7 shown explicitly.
the maximum possible number. The list for List[11,7] is shown in Figure 3.5. Piece 7
has 8 orientations but only 7 of them appear on the list because one of them would extend
outside of the board. The one not appearing is blank in Figure 3.5. The others show the 5
numbers that are in the set correspoding to piece 11 anchored at position 7.
type
PieceNumber = Z(1 . . . 12);
BoardNumber = Z(0 . . . 63);
Board = word(BoardNumber);
ListPtr = pointer to ListElement;
ListElement = node(Position : Board,Link : ListPtr);
global
TheBoard : Board;
List : array [PieceNumber,BoardNumber] of ListPtr;
PieceAvail : word(PieceNumber);
Solution : array[PieceNumber] of ListPtr;
Algorithm 3.4: Declarations for pentomino program.
With these global declarations the backtracking procedure is given in Figure 3.5. Intersection ( ) is used to determine whether a piece can be placed without overlap, union (
) is used to add a new piece to the board, and difference (\) is used to remove a piece from
the board. If the set operations are correctly implemented using AND, OR, and NOT on
machine words, then this program will be quite fast.
It is natural to wonder whether the backtracking can be sped up by checking for isolated
squares. That is to say, checking whether the unused portion of the board has a number of
squares divisible by five before trying to place the next piece. This strategy will certainly
cut down on the number of vertices in the backtracking tree, but now more work is done
at each vertex. In the authors experience it does not pay to check for isolated squares; the
extra work done at each vertex is greater than the savings obtained by having a smaller
tree. Backtracking is full of such tradeoffs between the size of the tree and the amount of
work done at each vertex.
The more general question that this raises is: Among two predicates Q and R both
satisfying (3.1), which is better? Of course the ultimate test is in the running time of the
c
uvic01, Frank
Ruskey, 19952001

3.3. SOLVING PENTOMINO PROBLEMS WITH BACKTRACKING.

47

procedure BackTrack ( k : BoardNumber );


local pc : PieceNumber;
while k TheBoard do k := k + 1;
for pc := 1 to 12 do
if pc in PieceAvail then
PieceAvail := PieceAvail \ [pc];
Solution[pc] := List[pc,k];
while Solution[pc] 6= null do
if TheBoard Solution[pc].Position = then
TheBoard := TheBoard Solution[pc].Position;
if PieceAvail =
then PrintSolution
else BackTrack( k + 1 );
TheBoard := TheBoard \ Solution[pc].Position;
Solution[pc] := Solution[pc].Link;
PieceAvail := PieceAvail [pc];
end {of BackTrack}
Algorithm 3.5: Backtracking routine for pentomino problem.

two algorithms that arise. Two extremes are worth noting. If Q is always true then (3.1) is
satisfied. No pruning of the backtracking is done; all of A1 A2 is generated. What
if Q is perfect? That is, what if Q(x) implies that there is some y such that P (xy)? Then
every leaf of the backtracking tree is in S. As mentioned in Chapter 1, this is the BEST
propery: Backtracking Ensuring Success at Terminals.

3.3.1

Eliminating Isomorphic Solutions

Reject isomorphs! The is no reason to generate isomorphic solutions when generating


all solutions to pentomino and other space-filling puzzles. In particular, for the 6 by
10 pentomino puzzle, each solution has four equivalent solutions (including itself) in the
equivalence class obtained by rotating and flipping. Thus, even if we wanted all solutions,
isomorphic or not, we could generate the non-isomorphic ones and then flip and rotate to
get the others. As we will see, there is absolutely no computational overhead in rejecting
isomorphic solutions in the 6 by 10 case, so that we save a factor of 4 in the running time.
In general, the basic idea is to fix the positions and orientations of some selected piece
(or pieces). Two properties are necessary: (a) Each equivalence class must have a member
with the selected piece in the one of the fixed positions and orientations, and (b) each
of the symmetries of the board must move the piece out of the set of fixed positions and
orientations.
For the 6 by 10 pentomino problem we will fix the center square of the X shaped piece
so that it lies in the upper quadrant (thinking of the board as being centered at its center).
See Figure 3.6.
c
uvic01, Frank
Ruskey, 19952001

48

CHAPTER 3. BACKTRACKING

Figure 3.6: Eliminating isomorphs in the 6 by 10 pentomino puzzle.

3.4

Estimating the Running Time of Backtracking.

In many instances it is useful to obtain an estimate of the number of vertices that will occur
in a backtracking tree, before the actual algorithm is run. The estimate presented in this
section only applies in those instances when we are searching for all possible solutions (i.e.
the entire tree is being examined).
The basic idea is to run an experiment in which we follow a random path in the tree
from the root to a leaf, and then assume that the entire tree has a similar parent number of children relationship as the vertices on this path. The random path is chosen
by successively picking a random child of the current vertex to be included next in the
path. Each child is regarded as being equally likely. For example, consider the tree shown
in Figure 3.7(a) where the thickened edges indicates the random path.
In the experiment the root had three children, the roots child on the thickened path
had two children, and so on. Thus we assume that the root has three children, all children
of the root have two children, etc. This gives rise to the tree of Figure 3.7(b). This tree has
1 + 3 + 3 2 + 3 2 1 + 3 2 1 2 + 3 2 1 2 1 = 40 vertices. In general, let nk denote the
degree of the (k 1)st vertex along the experimental path. There are n1 n2 nk vertices
at level k in the assumed tree. Thus the estimate is the (finite) sum
X = 1 + n1 + n1 n2 + n1 n2 n3 + .
A procedure to compute the estimate is given in Algorithm 3.6. A recursive version can
also be easily derived.
For any probabilistic estimate a desirable property is that the expected value of the
estimate is equal to the quantity being estimated. In probability theory such an estimator
c
uvic01, Frank
Ruskey, 19952001

3.4. ESTIMATING THE RUNNING TIME OF BACKTRACKING.

49

Figure 3.7: (a) True backtracking tree with random root-to-leaf path shown. (b) Assumed
tree from that random path.

estimate := product := 1;
k := 1;
compute S1 ;
while Sk 6= do
{Advance}
nk := |Sk |;
product := nk product;
estimate := estimate + product;
xk := an element of Sk , chosen at random;
k := k + 1;
compute Sk ;
Algorithm 3.6: Estimation algorithm.

c
uvic01, Frank
Ruskey, 19952001

50

CHAPTER 3. BACKTRACKING

is referred to as being unbiased. Let T be the tree whose size |T | is being estimated.
Intuitively, the reason that the estimate is unbiased is that the probability of reaching a
vertex x in the tree whose ancestors have degrees n1 , n2 , . . . , nt is equal to 1/(n1 n2 nt ),
which is the inverse of the weight assigned to that vertex by X. We will now give a more
formal argument. Define two functions on the vertices v of the backtracking tree:

1
if v is the root
(v) =
deg(
v ) (
v ) if v has parent v
and
I(v) = [[vertex v is visited in the experiment]].
Recall the [[]] notation, defined the previous chapter: [[P ]] is 1 if P is true and is 0 if P is
false. Note that depends only on the tree and not the experiment. The random variable
X may be rewritten as
X
X=
(v) I(v).
vT

Now take the expected value of the random variable X and use linearity of expectation to
obtain
X
X
1
E(X) =
(v) E(I(v)) =
(v)
(v)
vT

vT

1 = |T |.

vT

The estimator is therefore unbiased.


Some care should be exercised in applying this estimate. Backtracking trees tend to
vary wildly in their structure and subtrees with many vertices may be well hidden in the
sense that they are accessed only through paths from the root with vertices of low degree.
It is essential in applying the test that a large number of trials are used. Once this is done,
however, the test is remarkably effective.

3.5

Exercises.

1. [1+] Use backtracking by hand to determine the number of solutions to the 5 by 5


queens problem. How many of the solutions are non-isomorphic?
2. [1] Generate an estimate of the number of vertices in the backtracking tree for the 8
by 8 queens problem. In picking your random row positions, simply use the lowest
numbered valid row.
3. [1+] Show that the number of solutions to the n by n Queens problem is less than or
equal to n(n 4) (n 2)!. Can you derive a better bound?
4. The following 150 characters of C code outputs the number of solutions to the nQueens problem. Explain how the code works. Whats the largest value of n for
which it will produce a correct answer (after a potentially very long wait) on your
machine?
c
uvic01, Frank
Ruskey, 19952001

3.5. EXERCISES.

51

Figure 3.8: The Soma cube pieces.


t(a,b,c){int d=0,e=a&~b&~c,f=1;if(a)for(f=0;d=(e&=~d)&-e;f+=t(a&~d,(b|d)<<1,
(c|d)>>1));return f;}main(q){scanf("%d",&q);printf("%d\n",t(~(~0<<q),0,0));}

5. [2] Write a backtracking program to determine a configuration containing the smallest


number of queens on a n by n board so that every square of the board is under attack.
Is the problem easier if the queens are specified to be non-taking? Give explicit
solutions for 1 n 8.
6. [1+] How many hexominoes (6-ominoes) are there? [2] Prove that the set of all
hexominoes cannot be placed in a rectangular configuration without holes.
7. [3] Define the order of a polyomino to be the smallest number of copies of P that
will fit perfectly into a rectangle, where rotations and reflections of P are allowed.
has order at most 10. Show that
The figure below shows that the pentomino
its order is exactly 10. What are the orders of the other two polyomino shapes shown
below?

8. [2] A polyomino puzzle with the following pieces has been marketed.

The puzzle is to place the pieces on an 8 by 8 board. Write a backtracking program


to determine the number of different non-isomorphic solutions.
9. [2] The Soma Cube puzzle is to fit the seven pieces listed in Figure 3.8 into a 3 by 3
by 3 cube. Write a backtracking program to generate all 240 non-isomorphic solutions
to the Soma Cube puzzle.
10. [2] Snake-in-a-cube puzzle: A snake consists of 27 unit cubes arranged in order. They
are held together by a shock-cord, but can be rotated around the cord. Each unit cube
is either straight-through (the cord passed through one face of the cube and exits
c
uvic01, Frank
Ruskey, 19952001

52

CHAPTER 3. BACKTRACKING
v
v
v v
v
v
H
HH
A

A
HHAH
H

H HA
H HH
A H
A
HHv
HHv A Hv

v
v
v

HH
HH
A AH A
A A
AH
A A HA
A HH A A
v A Av H
A A v
v
AH
Av Hv

A A
A
A A
A
A
A
A
A

A
A
A Av A Av

v
v A v
Av

H
A A A
HAH A
A A

A
A
A HH
AHv
Av v

v A Av
v

A
HH
HH

A HH H

H
A

H
H
H
H

A HH

HHHHA
HHv
Hv
Av

HAv
v
HHv

Figure 3.9: A 6 by 6 knights tour.


throught the opposite face) or is an elbow (the cord passed through one face and
exists through another perpendicular face). The problem is to find all way of shaping
the snake into a 3x3x3 cube. A specific instance of the snake has been marketed and
has the following 27 cubes SSESESESEEEESESEEESEESEEESS, where S indicates
straight-through, and E indicates elbow. Determine the number of non-isomorphic
solutions of this puzzle, and of the puzzle where every unit cube is an elbow.
11. [2] A knights tour of an n by n chessboard is a sequence of moves of the knight on
the chessboard so that each of the n2 squares is visited exactly once. Shown in Figure
3.9 is a knights tour of a 6 by 6 chessboard. Write a backtracking program to find
a single knights tour on an 8 by 8 chessboard starting from the upper left corner. A
knights tour that returns to the square upon which it started is said to be re-entrant.
Modify your program from so that it finds a re-entrant knights tour. Estimate the
time that it would take to explore the entire backtracking tree assuming that it takes
a millisecond to process one vertex in the tree. Prove that no Knights tour can exist
if n is odd.
12. [1+] Draw a graph representing the possible moves of a knight on a 4 by 4 chessboard.
Use this graph to prove that there is no knights tour on a 4 by 4 board.
13. [2] In a noncrossing Knights tour the lines drawn for each move of the knight do
not cross. The tour shown in Figure 3.9 has many crossings, and in general, a noncrossing tour cannot visit every square of the board. Determine the longest possible
non-crossing knights tour on an 8 by 8 board.
14. [2] Write a backtracking program to determine the number of ways to color the vertices
of a graph with k colors for k = 1, 2, . . . , n where n is the number of vertices in the
graph. In any such coloring adjacent vertices must receive distinct colors. Deciding
whether a graph can be colored with k colors is a well-known NP-complete problem.
15. [2] Write a backtracking program to count the number of topological sortings (linear
extensions) of a directed acyclic graph (partially ordered set).
c
uvic01, Frank
Ruskey, 19952001

3.6. BIBLIOGRAPHIC REMARKS

53

16. [2] Write a backtracking program to list all Hamilton cycles (if any) in a graph. Use
your program to determine the number of Hamilton cycles in the tesseract.
17. [2] For given
P k, a subset K [k], is said to represent k if there is some unique I K
such that xI x = k. For given n, a pair of subsets P, Q [n] is said to dichotomize
[n] if, for each k [n], the number k is representable by P or by Q, but not by
both. For example {1, 2, 6}, {4, 5} dichotomizes [9], but [12] is not dichotomized by
{1, 2, 9}, {3, 4, 5} since 3 and 9 are represented twice and 6 is not represented. Write
a backtracking program that takes as input n and outputs all pairs of subsets of [n]
that dichotomize it. How many solutions are there for n = 17?
18. [3] Let = (1 2) and = (1 2 n). Write a backtracking program to find a
Hamilton cycle in the directed Cayley graph Cay(Sn : {, }) when n = 5. Can you
find a Hamilton path for n = 6? [R] Find a Hamilton cycle for n = 7. It is known
that there is no Hamilton cycle for n = 6.
19. There is a large family of combinatorial questions involving the packing of squares
into squares. [2+] For n 13 determine the least number of smaller squares that will
tile a n by n square. [2+] The sum 12 + 22 + + 242 = 702 suggests that it might
be possible to tile a 70 by 70 square with squares of sizes 1, 2, . . . , 24. Show that this
is impossible.
20. [1] Write Algorithm 3.6 as a recursive procedure.
21. [3] Extend the unbiased procedure for estimating the size of a backtracking tree to
that of estimating the size of a directed acyclic graph that is rooted at r (all vertices
are reachable from r).

3.6

Bibliographic Remarks

Perhaps the first description of the general backtracking technique is in Walker [435]. Early
papers about backtracking include Golomb and Baumert [156].
The eight queens problem is attributed to Nauck (1850) by Schuh [375]. It is said that
Gauss worked on the problem and obtained the wrong answer! Sosic and Gu [396] analyze
programs for the 8-queens problem.
A paper of Bitner and Reingold [33] discusses a number of interesting ways to speed up
backtracking programs and contains some applications to polyomino problems.
A (very ugly) program for generating all solutions to the Soma cube problem may be
found in Peter-Orth [306].
The book that really popularized polyominoes, and pentominoes in particular, is Polyominoes by Golomb [154]. The book by Martin [264] contains much interesting material
about polyominoes and contains further references on the topic. The two volume book
by Berlekamp, Conway and Guy [28] on mathematical games contains interesting chapters
about the Soma cube (including a map of all solutions!) as well as scattered references to
polyominoes.
The method for estimating the size of the backtracking tree comes from Hall and Knuth
[168] and further analysis is carried out in Knuth [220]. The efficiency of the method has
been improved by Purdom [324]. The estimation method can be extended from rooted trees
to directed acyclic graphs; see Pitt [311].
c
uvic01, Frank
Ruskey, 19952001

54

CHAPTER 3. BACKTRACKING
Other useful references for backtracking include Fillmore and Williamson [127].

c
uvic01, Frank
Ruskey, 19952001

Chapter 4

Lexicographic Algorithms
4.1

Introduction

A natural order for any list of strings or sequences is lexicographic order. It is easy to
visualize and work with, and is surprisingly useful in a variety of contexts. For many
combinatorial objects, the fastest known algorithms for listing, ranking and unranking are
with respect to lexicographic order. Many algorithms that are not overtly lexicographic
have some underlying lexicographic structure.
Lexicographic order is based on the familiar idea as the ordering of words in dictionaries.
The only requirement is that the letters that make up the alphabet of the language be
ordered. In the definitions below we use to denote the assumed underlying ordering of
the symbols of the alphabet and < to denote orderings of strings. We remark again that in
most instances the alphabet is the set of natural numbers under the usual numeric ordering
0 1 2 .
Definition 4.1 In lexicographic (or lex) order a1 a2 an <l b1 b2 bm if either
1. for some k, ak bk and ai = bi for i = 1, 2, . . . , k 1, or
2. n < m and ai = bi for i = 1, 2, . . . , n.
Definition 4.2 In reverse lexicographic (or relex) order a1 a2 an <r b1 b2 bm
if b1 b2 bm <l a1 a2 an in lex order.
Definition 4.3 In co-lexicographic (or colex) order a1 a2 an <c b1 b2 bm
if an a2 a1 <l bm b2 b1 in lex order.
For example, here is the same set of strings listed in each of the three orders:
lex:
relex:
colex:

4.2

12, 13, 134, 21, 224, 3


3, 224, 21, 134, 13, 12
21, 12, 3, 13, 224, 134

Subsets

There are two common ways to represent a subset of an n-set. One common representation
is a list of the elements in the subset. However, for the most part the representation that
55

56

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

we use is a bitstring b1 b2 bn where bi is one if and only if the ith element is included
in the subset; modifying the algorithms to handle the other representation presents no
difficulties. Recall that n2 = {0, 1}n is the set of all bitstrings of length n. With the
bitstring representation, subsets may be generated very simply by counting in binary. This
produces a lexicographic ordering of the bitstrings. In general, a bitstring of the form
b1 b2 bk1 011 1 becomes
b1 b2 bk1 100 0.
This process is easily translated into the procedure Next of Algorithm 4.1. The algorithm
is memoryless since the only input is the bitstring; the only variable k is local to Next. Is
it CAT?
(N1)
(N2)
(N3)
(N4)
(N5)
(N6)
(N7)
(N8)
(N9)
(N10)

procedure Next;
{Assumes b0 = 0}
local k : N;
begin
k := n;
while bk = 1 do
bk := 0; k := k 1;
bk := 1;
if k = 0 then done := true;
end{of Next};
Algorithm 4.1: Next for subsets by counting.

The algorithm may be analyzed by computing the number of times the test bk = 1 is
executed at line (N6). The test fails 2n times, once for each bitstring. How many times does
the test succeed? Refine the question by asking how many times it succeeds in position i.
Such a success has the form b1 b2 bi1 11 1 and there are 2i1 bitstrings of that form.
Thus, the total number of times the test is executed is
n

2 +

n
X
i=1

i1

n
X

2i = 2n+1 1.

i=0

Dividing by 2n , the amortized number comparison count is 2 2n and thus the amortized amount of work per subset is O(1); the algorithm is CAT.
A recursive algorithm is also simple. Observe that all bitstrings with b1 = 0 precede
all bitstrings with b1 = 1 and that, given a fixed value of b1 , the suffixes b2 bn must
also appear in lexicographic order. This observation leads us to the procedure Subset of
Algorithm 4.2. Variable n is global and the initial call is Subset(1).
Unlike algorithm Next, procedure Subset does no comparisons with the elements of a.
In this case the appropriate measure of the amount of computation done by the algorithm
is the total number of recursive calls, since a constant amount of computation is done for
every recursive call.
The recursion tree of the call Subset(1) is an extended binary tree with 2n leaves. Thus
the total number of nodes in the recursion tree is 2n+1 1 and consequently the algorithm
runs in constant amortized time. We could obtain this same result as the solution to the
recurrence relation t0 = 1, and tn = 1 + 2tn1 , which is obtained from examination of
Subset.
c
uvic01, Frank
Ruskey, 19952001

4.3. COMBINATIONS

57

procedure Subset ( k : N);


begin
if k > n then PrintIt
else
bk := 0; Subset( k + 1 );
bk := 1; Subset( k + 1 );
end{of Subset};
Algorithm 4.2: Recursive procedure to generate subsets in lexicographic order.
The rank function for subsets in lexicographic order is very simple; compute the value
of a number written in binary:
rank (b1 b2 bn ) =

n
X

bi 2ni

i=1

This clearly gives rise to an algorithm that uses O(n) arithmetic operations. It is not
difficult to construct a O(n) unranking algorithm as well by using the well-known algorithm
for converting a number into its binary representation. See Exercise 6.

4.3

Combinations

Combinations are among the most useful of combinatorial objects. We will use them to
introduce and illustrate some methods that are very useful for the efficient generation of
other types of combinatorial objects.
Suppose that we wish to generate all subsets of size k from a set with n elements. Such
subsets are commonly referred to as combinations or, more specifically, as k-combinations

of an n-set. The number of combinations of k objects chosen from n objects is denoted nk .
Recall that

n
n!
(n)k
=
=
.
k
k!(n k)!
k!
There are two representations of combinations that are in common use. One representation is the bitstring representation of subsets presented in the previous section with
the restriction that now each bitstring has exactly k 1s. Define B(n, k) to be the set of
bitstrings of length n that contain exactly k 1s.
B(n, k) = {b1 b2 bn n2 | b1 + b2 + + bn = k}

(4.1)

Another representation is obtained by listing in increasing order the k elements (from


{1, 2, . . . , n}) that are selected. Define
A(n, k) = {a1 a2 ak | 1 a1 < a2 < < ak n}.

(4.2)

It is quite simple to list these strings in lexicographic order. As an example, in lexicographic


order the strings of A(5, 3) are 123, 124, 125, 134, 135, 145, 234, 235, 245, 345. In general,
the lexicographically smallest string is 12 k, the lexicographically largest is (n k +
1) (n 1)n. Algorithm 4.3 produces the lexicographic successor of a string in A(n, k).
As with all lexicographic algorithms, we simply scan from the right for the rightmost index
c
uvic01, Frank
Ruskey, 19952001

58

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

(C1)
(C2)
(C3)
(C4)
(C5)
(C6)
(C7)
(C8)
(C9)
(C10)

procedure Next;
{Assumes 0 < k n and a0 < 0}
local i, j : N;
begin
j := k;
while aj = n k + j do j := j 1;
if j = 0 then done := true;
aj := aj + 1;
for i := j + 1 to k do ai := ai1 + 1;
end{of Next};
Algorithm 4.3: Procedure Next for combinations in lex order(A(n, k)).

j whose corresponding element can be increased. In this case it means that we scan for the
largest position j for which aj < n k + j.
The running time of this algorithm is determined by how many times the comparison
aj = n k + j at line (C6) is made. Note that the for loop at
line (C9) is iterated the
same number of times as the while loop. If we generate all nk combinations then the
comparison fails once for each combination and is true for the np
kp combinations for which
akp+1 = n p + 1 (where 1 p k); thus the number of times the comparison is made is

k
X
np
p=0

kp

k
X
nk+q
q=0

n+1
=
.
k

The first equality is obtained by the change


n of variable q = k p and the second
equality follows from (2.4). Dividing n+1
by
k
k , the amortized number of times that the
algorithm executes the comparison is (n + 1)/(n k + 1). Thus the generation algorithm
runs in constant amortized time if k n/2. If k > n/2 then it is better to generate the
complementary combinations with n k elements or use another algorithm.
The while loop of Algorithm 4.3 can be eliminated by making j a global variable (initialized to k) and observing that the rightmost changeable position is k if ak < n and is one less
than its previous value if ak = n. The result is Algorithm 4.4. Note that this new algorithm
is not memoryless, as was Algorithm 4.3. Furthermore, the for loop at (I6) is iterated the
same number of times as the for loop at (C9) so the running times of Algorithms 4.3 and
4.4 differ by only a constant factor. Nevertheless, simple observations like this can often be
used to make algorithms faster.
(I1)
(I2)
(I3)
(I4)
(I5)
(I6)
(I7)
(I8)
(I9)

procedure Next;
{Assumes 0 < k n and a0 < 0; variable j is global.}
local i : N;
begin
aj := aj + 1;
for i := j + 1 to k do ai := ai1 + 1;
if j = 0 then done := true;
if ak < n then j := k else j := j 1;
end{of Next};
Algorithm 4.4: Improved version of Next for combinations in lex order(A(n, k)).
c
uvic01, Frank
Ruskey, 19952001

4.3. COMBINATIONS

59

For the sake of contrast, we now switch to generating the elements of B(n, k), the
bitstring representation of combinations. A recursive algorithm is easy to construct and is
based on (2.2), the classic Pascals triangle recurrence relation for binomial coefficients.
Let us first rewrite the recurrence relation so that there are no possible terms with value
zero; i.e., transform it into a positive recurrence relation. Making the natural assumption
that 0 k n, a positive recurrence relation is as follows.

n1
n
0
n1
=

n1
n1
k

if
if
if
n1
+ k1 if

n=0
k=0
k=n
0<k<n

(4.3)

Based on (4.3) and maintaining lexicographic order, we are led to the procedure Combination
of Algorithm 4.5. There are two parameters. The first, j, is the position whose value is to
be set to 0 or 1. The second, m, is the number of 1s that have been placed so far. The
initial call is Combination(1,0); no other initialization is necessary.
procedure Combination ( j, m : N);
begin
if j > n then PrintIt
else
if k m < n j + 1 then
bj := 0; Combination( j + 1, m );
if m < k then
bj := 1; Combination( j + 1, m + 1 );
end{of Combination};
Algorithm 4.5: Recursive procedure for generating B(n, k) in lex order.
Algorithm 4.5 is somewhat unnatural since the subarray being modified moves to the
right the deeper the recursion becomes. This forces n and k to be global (or left unmodified
if they are passed as parameters), and the parameters j and m to not exactly match those
of the recurrence relation. These shortcomings are easily rectified by using colex order.
Then the parameters and structure of the procedure exactly match the recurrence relation
(4.3). See Algorithm 4.6. The initial call is Colex(n,k); no initialization is necessary.
Henceforth, we will try to use colex order instead of lex order whenever it results in more
natural algorithms. This observation is important enough to be highlighted.
Colex (or RL) Superiority Principle: Try to use colex order (right-to-left array
filling) whenever possible. It will make your programs shorter, faster, and more
elegant and natural.
In some sense the principle is misnamed as the colex superiority principle, since the
superiority arises from the right-to-left, instead of left-to-right, array filling, rather than
being a variant of lex order. However, in this chapter the name makes sense, and in later
chapters we will call it the RL superiority principle.
Figure 4.1 shows the computation tree of Algorithms 4.5 and 4.6 on input n = 5 and
k = 2. For lex order the levels of the tree correspond to the array indexing of b; for
c
uvic01, Frank
Ruskey, 19952001

60

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

procedure Colex ( n,k : N);


begin
if n = 0 then PrintIt
else
if k < n then
bn := 0; Colex( n 1, k );
if k > 0 then
bn := 1; Colex( n 1, k 1 );
end{of Colex};
Algorithm 4.6: A recursive procedure for generating B(n, k) in colex order.

P
PPP

PP
P

Q
Q

XXXX

Figure 4.1: Computation tree of B(5, 2).

c
uvic01, Frank
Ruskey, 19952001

4.3. COMBINATIONS

61

colex order tree level i corresponds to array index 5 i + 1. Let t(n, k) denote the number of recursive calls; i.e., the number of nodes in the computation tree of Colex(n,k)
(or Combination(1,0)). The following recurrence relation follows from consideration of
Algorithm 4.6.

1
if n = 0

1 + t(n 1, 0)
if k = 0
t(n, k) =
(4.4)
1 + t(n 1, n 1)
if k = n

1 + t(n 1, k) + t(n 1, k 1) if 0 < k < n


The following lemma may be proven by a simple induction (see Exercise 10).

Lemma 4.1 For all 0 k n, the solution to (4.4) is t(n, k) = n+2
k+1 1.
Unfortunately,
t(n, k) + 1
n
=
k

(n + 2)(n + 1)
,
(k + 1)(n k + 1)

so the algorithm will not be CAT if k is close to 0 or to n in those cases there are too
many nodes of degree one in the computation tree. The descendants of a node of degree
one all have degree one and, furthermore, the descendants of a node of degree one all have
the same labels, either all 0s or all 1s. Call these paths of degree one nodes with the same
label 0-paths and 1-paths (the paths terminate at a leaf). The number of degree one nodes
whose children have label 0 is

X n i
n
=
.
k
k+1
i1

If calls corresponding to nodes on a 0-path could be eliminated, then the number of calls
becomes one less than




n+2
n
n+1
n

=
+
,
k+1
k+1
k
k
which gives an amortized number of calls 1 + (n + 1)/(n k + 1), the same expression
we obtained in analyzing Algorithm 4.3. A similar calculation, but eliminating children
of degree one calls labeled 1, gives an amortized number of calls 1 + (n + 1)/(k + 1).
Fortunately, 0-paths (or 1-paths) can be eliminated by a trick that turns out to be quite
useful for generating other objects as well.
The idea for elmininating 0-paths is to stop the recursion at k = 0 instead of at n = 0.
We still have to ensure that the correct values are in the array b whenever PrintIt is
called. This is done by initializing b to be all 0s and by restoring b[n] := 0 after the call
Colex(n-1,k-1). See Algorithm 4.7. Strictly speaking the assignment b[n] := 0 at the
line marked {*} is not necessary, although it does obviate the need for initialization.
To illustrate the case of k n/2, we revert back to generating A(n, k). Array a is
initialized so that a[i] = i for i = 1, 2, . . . , k. See Algorithm 4.8. Here we stop the
recursion at k = n.
The technique of eliminating chains of nodes from the computation tree is one of two related tricks that we will use repeatedly. We call them PETs (Path Elimination Techniques).
Use PETs to get CAT behavior!
c
uvic01, Frank
Ruskey, 19952001

62

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


procedure Colex( n,k : N);
begin
if k = 0 then PrintIt
else
if k < n then
{*} bn := 0; Colex( n 1, k );
bn := 1; Colex( n 1, k 1 ); bn := 0;
end{of Colex};
Algorithm 4.7: A CAT procedure for generating B(n, k) in colex order if k n/2.
procedure Colex ( n,k : N);
begin
if k = n then PrintIt
else
Colex( n 1, k );
if k > 0 then
ak := n; Colex( n 1, k 1 ); ak := k;
end{of Colex};
Algorithm 4.8: A CAT procedure for generating A(n, k) in colex order if k n/2.
Path Elimination Techniques (PET):
#1 Chains in recursive computation trees for generating combinatorial objects are often caused by the values of the parameters reaching a boundary
condition. By initializing the array to the values of the string that occur at
that boundary condition, and restoring those values as the recursion backs
up, all chains caused by that boundary condition can be eliminated from the
computation tree.
#2 Chains can also be eliminated in many cases by stopping the recursion
one (or maybe more) steps earlier than is necessary to get BEST behavior.
These one-off boundary conditions often give rise to simple sub-instances of
the objects being generated; sub-instances which can be quickly generated by
using some other algorithm.

The second technique is implemented with our current example by stopping the recurrence relation when k = 1 or k = n 1, and treating these cases using simple for loops.
Assume that we are generating B(n, k). When k = 1 we generate the list
10n1 010n2 0n1 1
and when k = n 1 we generate the list
1n1 0 1n2 01 01n1 .
These lists are generated with simple for loops, taking time O(n). The resulting algorithm
is easily shown to be CAT because now every recursive call has two children or has a child
c
uvic01, Frank
Ruskey, 19952001

4.3. COMBINATIONS

63

that satisfies k = 1 or k = n
1.
Thus the number of recursive calls is less than the number
of leaves, which is less than nk . The amount of work done at the leaves is proportional to
the number of combinations. Exercise 11 asks you to supply the details.
An Application
At this point the reader may feel a little unease with the development as it has been carried
out so far. Is it really worth the effort to develop CAT algorithms? Are there really
applications where one does not have to look at all of the output and still have a use for a
generation algorithm? We present below an application of the ideas developed previously.
A well-known NP-complete problem is the Partition problem:
Instance: Positive integers x1 , x2 , . . . , xn .
Question: Is there a subset I {1, 2, . . . , n} such that
X
X
xi =
xi
iI

(4.5)

i6I

We modify this problem by making k part of the instance and in the question insist
that |I| = k. Call the resulting problem k-Partition. Clearly, it is also NP-complete
(it is important that k be part of the input) and thus there is no known polynomial time
solution. A nave solution would be to generate combinations and compute
n the sums (4.5)
for each combination generated. The resulting algorithm would be (n k ). However,
we

can determine the number of solutions to the k-Partition problem in time O( nk ) by using
our previously developed CAT algorithms for generating
combinations. See Algorithm 4.9.
Pj
We precompute an array init where init[j] = i=1 xi and set totD2 := init[n]
div
2. Both 0-paths and 1-paths have been eliminated and the procedure is thus O( nk ) (if a
call to PrintIt is given a unit cost) for all values of k. Procedure PrintIt needs both the
current value of n and k to actually output the subsets; they are not needed if only a count
of the number of solutions is desired.

procedure C ( n,k, sum : N);


begin
if k = n then
if sum + init[n] = totD2 then PrintIt( n,k );
else
if k = 0 then
if sum = totD2 then PrintIt( n,k );
else
bn := 0; C( n 1, k, sum );
bn := 1; C( n 1, k 1, sum + val[n] );
end{of C};
Algorithm 4.9: A CAT algorithm for the k-Partition problem.
We can draw two lessons from this example. First, a CAT algorithm is significantly
faster than a corresponding algorithm that, say, takes linear time per object generated. If
there are N = 2n objects, then the comparison is between a (N log N ) algorithm with a
c
uvic01, Frank
Ruskey, 19952001

64

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

(N ) algorithm. Secondly, in many applications some function needs to be applied to each


object as it is generated, and that function need not be computed from scratch for each
object. The function can often be updated in the same time that it takes to update the
object.
Ranking
In order to develop a lex ranking algorithm let us first consider an example. Suppose that we
wish to know the lexicographic rank of 35678 in C(9, 5). Since the ordering is lexicographic
we will count the number of combinations that are less than 35678 in lexicographic order.
Certainly
8
7all combinations beginning with a 1 or a 2 are lexicographically less. There are
with a 3 only
4 and 4 such combinations, respectively. Of the combinations that begin
5
the ones that begin 34 are guaranteed to be less than 35678. There are 3 combinations
that begin 34. Of the
that begin 35, the lexicographically smallest is 35678.
combinations

Thus the rank is 84 + 74 + 53 = 70 + 35 + 10 = 115.
Let Rn (a1 , a2 , . . . , ak ) denote the rank of a1 a2 ak as a combination of size k taken
from n objects. Following our previous discussion, the rank is the number of combinations
that begin with an element less than a1 plus the rank of the remaining string in an altered
combination set.

aX
1 1
ni
Rn (a1 , a2 , . . . , ak ) = Rna1 (a2 a1 , a3 a1 , . . . , ak a1 ) +
(4.6)
k1
i=1

The terminating condition is Rn (a1 ) = a1 1. Equation (4.6) can be iterated to obtain


(where a0 = 0)

aj 1
k1 aj+1X
X
n aj i

Rn (a1 , a2 , . . . , ak ) =
(4.7)
kj1
j=0

k1
X
j=0

i=1

n aj
kj

n aj+1 + 1

kj

k
X
n
n aj
=
1
.
k
kj+1

(4.8)

(4.9)

j=1

Expression (4.8) follows from (4.7) by an application of the identity (2.3). Expression
(4.9) follows from (4.8) by breaking the sum into two parts, applying the Pascals Triangle
identity, and cancelling like terms.
Thus, from (4.9), we see that if a table of binomial coefficients is available, then ranking
is O(n). Unranking can also be done in O(n) arithmetic operations if a table of binomial
coefficients has been pre-computed.
To rank combinations in relex order we clearly obtain

k
X
n aj
.
Rankn (a1 , a2 , . . . , ak ) =
kj+1
j=1

What about colex order? Just as for listing algorithms, colex order can lead to simpler,
more natural ranking algorithms. For A(n, k) a key observation is that, for a fixed value of
c
uvic01, Frank
Ruskey, 19952001

4.3. COMBINATIONS

65
0

21

56

15

?
35

10

20

?
3

10

0


Figure
4.2: The walk corresponding to the rank 100 = 56 + 35 + 4 + 3 + 2 combination of
9
in
colex
order.
5

k, the list for n is the initial nk strings of the list for n + 1. Thus the parameter n is not
needed for the ranking; one only needs the string a = a1 a2 a k . Clearly,
all combinations

whose largest element is smaller than ak precede a; there are akk1 of these. We are thus
lead to the following equation for the rank.

ak 1
RankC(a1 , a2 , . . . , ak ) =
+ RankC(a1 , a2 , . . . , ak1 )
k

k
X
aj 1
(4.10)
=
.
j
j=1

There is a very nice way to visualize colex ranking of bitstrings in B(n, k) in terms of
lattice walks on a k by n k grid. See Figure 4.2 for an example with k = 6 and n = 9.
Regarding
lower
x+y1 the
left corner as (0, 0), the vertical edge from (x, y) to (x, y + 1) is labeled
x+y1
=
y
x1 ; horizontal edges are unlabeled. The rank of a path from the lower left
corner to the upper right corner is the sum of the edge labels encountered. The illustrated
path corresponds to the bitstring 001110011 or the element 34589 of A(9, 5).
Keeping the lattice walk interpretation in mind, it is not difficult to derive an unranking

algorithm for colex order. See Algorithm 4.10, where the function C(n,k) returns nk .
procedure Unrank ( r : N);
local i,p : N;
begin
for i := k downto 1 do
p := i 1;
repeat p := p + 1
until C(p, i) > r;
r := r C(p 1, i);
ai := p;
end{of Unrank};
Algorithm 4.10: Unranking combinations (A(n, k)) in colex order.
c
uvic01, Frank
Ruskey, 19952001

66

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

Computing a table of binomial coefficients with parameters at most n and k requires


(kn) addition operations on rather large integers and this preprocessing dominates the
running time of the ranking and unranking algorithms weve presented so far for combinations. For many applications exponentially many rankings or unrankings are required, so
this preprocessing is a small part of the total time. However, if only a small number of
rankings or unrankings are done then the preprocessing time dominates. The precomputation of the table
can be avoided and the binomial coefficients computed on the fly by first
computing nk and then making use of the equations below.

n
n
n1
n n1
=
=
k
nk
k
k k1
To use these one must now do multiplies and divides (but only integer arithmetic is required). The resulting ranking and unranking algorithms use (n) arithmetic operations.

4.4

Permutations

Perhaps more algorithms have been developed for generating permutations than any other
kind of combinatorial object. For a survey and many references see Sedgewick [377].
Recall that in the n by n queens problem of the previous chapter each solution is a
permutation. In fact, that recursive algorithm will recursively generate all permutations in
lexicographic order if the references to arrays b and c are removed. Analyzing the resulting
algorithm is the subject of Exercise 21; it is CAT. We now discuss an iterative algorithm.
To iteratively generate permutations lexicographically scan from right to left until an
element smaller than its predecessor is found. That element is then minimally increased and
the remainder of the permutation is filled in as lexicographically minimal way as possible.
For example, 892157643 becomes 892163457. The general procedure is given in Algorithm
4.11.
procedure Next;
{Assumes that 0 = 0.}
begin
k := n 1;
while k > k+1 do k := k 1;
if k = 0 then done := trueelse
j := n;
while k > j do j := j 1;
k :=: j ;
r := n; s := k + 1;
while r > s do
r :=: s ;
r := r 1; s := s + 1;
end;
end{of Next};
Algorithm 4.11: Procedure Next for permutations in lexicographic order.
If n = 4 then the output of this algorithm is (read across)

c
uvic01, Frank
Ruskey, 19952001

4.4. PERMUTATIONS
1234
2134
3124
4123

1243
2143
3142
4132

1324
2314
3214
4213

67
1342
2341
3241
4231

1423
2413
3412
4312

1432,
2431,
3421,
4321.

The algorithm operates by transposing pairs of elements and can be analyzed by counting
the number of such transpositions. Let tn denote the number of transpositions used by this
algorithm in generating all n! permutations. Then t1 = 0, and if n > 1 then

n+1
tn = ntn1 + (n 1)
.
(4.11)
2
The reasoning behind this recurrence relation is as follows. There are n groups of permutations, each with a fixed value in the first position. Within those groups the number
of transpositions is tn1 . At the n 1 interfaces between those groups the number of
transpositions is b(n + 1)/2c. The ith interface has the form shown below.
i
n n 1 i + 2 i + 1 i 1
2
1
i+1 1
2
i 1
i
i + 2 n 1 n
The recurrence relation (4.11) can be solved by defining

n+1
.
sn = tn +
2
The recurrence relation then becomes

if n = 1
1
nsn1
if n even
sn =

n(sn1 + 1) if n odd,
which can be iterated to obtain

1
1
1
sn = n! 1 + + + +
.
2! 4!
(2b(n 1)/2c)!
P
The hyperbolic cosine is defined by cosh(x) = k0 1/(2k)!. Thus tn n! cosh 1
1.5308 n!, and so the algorithm runs in constant amortized time; it is CAT. Note that the
algorithm is memoryless.
A ranking function R for permutations in lexicographic order is developed next. What,
for example, is R(251436)? All permutations that start with a 1 precede 251436. There are
15! of them. We then want the rank of 51436 as a lexicographic permutation of {1, 3, 4, 5, 6}.
This is the same as the rank of 41325, obtained by subtracting 1 from each element greater
than 2. Thus R(251436) = 1 5! + R(41325). Continuing, R(41325) = 3 4! + R(1324),
R(1324) = 0 3! + R(213), and R(213) = 1 2! + R(12). Thus, the rank of R(251436) is
1 5! + 3 4! + 0 3! + 1 2! = 194.
In general,
0
R(1 2 n ) = (1 1) (n 1)! + R(10 n1
),
0
where i1
is i if 1 > i , and is i 1 if 1 < i . A straightforward implementation
of this recurrence relation uses O(n2 ) arithmetic operations, yielding a moderately efficient
ranking algorithm. Similarly, a O(n2 ) unranking algorithm will be derived later in this
section.

c
uvic01, Frank
Ruskey, 19952001

68

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

We call the string (1 1)(10 1) the inversion string string of , since its sum is
the number of inversions in . Given a permutation of {1, 2, . . . , n}, the inversion string,
P [] = p1 p2 pn , of can be defined as follows. Entry pi is the number of elements j
that satisfy j < i and j > i. In other words, pi is the number of elements that are less
than i and to the right of it. For example, P [351642] = 230210. Inversion strings are
useful since they preserve lexicographic order and free us from certain bookkeeping details
in the algorithms. Permutation is lexicographically less than 0 if and only if P [] is
lexicographically less than P [ 0 ]. Inversion strings are characterized by the property that
0 pi n i for i = 1, 2, . . . , n. The rank of in lex order can be simply expressed as
R() = p1 (n 1)! + p2 (n 2)! + + pn 0! =

n
X

pi (n i)!.

(4.12)

i=1

The process of converting a permutation to an inversion string (or inversion vector


or inversion table), or vice-versa, is easily accomplished with an algorithm that uses O(n2 )
arithmetic operations. By being somewhat more tricky the number of arithmetic operations
can be reduced to O(n log n). See Exercise 22.
It is straightforward to implement (4.12). The development of an unranking algorithm
is also not difficult. Some indexing is simplified by ranking in reverse colex order; see
Algorithm 4.12. The running time of this algorithm is O(n2 ), which again can be reduced
to O(n log n). Due to the size of the numbers involved, ranking and unranking are typically
done only for small values of n and then Algorithm 4.12 is superior to those algorithms that
are asymptotically faster. However, there is an O(n) arithmetic operation ranking algorithm
for permutations; it does not use lexicographic order and is very fast. See Exercise 23.
function Rank ( p : permutation ) : N;
local f ,i,j,c,r : N;
begin
r := 0;
f := 1;
for i := 2 to n do
c := 0;
for j := 1 to i 1 do
if p[j] > p[i] then c := c + 1;
r := r + c f ;
f := f i; {now f = i!}
return( r );
end{of Rank};
Algorithm 4.12: Ranking algorithm for permutations in reverse colex order.

4.5

Permutations of a Multiset

Recall that a multiset is a set with repeated elements. Throughout this section we assume
that the elements of the multiset are 0, 1, . . . , t, that the number of occurrences of i is ni ,
and that N = n0 + n1 + + nt . A string of length N with ni occurrences of symbol i
is said to be a multiset permutation with specification hn0 , n1 , . . . , nt i. An algorithm for
c
uvic01, Frank
Ruskey, 19952001

4.5. PERMUTATIONS OF A MULTISET

69

generating multiset permutations will generate combinations if t = 1 (i.e., the specification


is of the form hn0 , n1 i) and will generate permutations if all ni = 1 (i.e., the specification is
of the form h1, 1, . . . , 1i), thus unifying the results of the previous two sections.
As an example, in lexicographic order the 12 permutations of specification h2, 1, 1i are
0012, 0021, 0102, 0120, 0201, 0210, 1002, 1020, 1200, 2001, 2010, 2100.
It is quite straightforward to generate multiset permutations in lex or colex order, using
the knowledge gained previously in this chapter. The only challenge is to develop a CAT
algorithm. Let us assume that 0 occurs more frequently than any other symbol, n0
n1 , n2 , . . . , nt . We first apply PET #1 to develop an algorithm that eliminates all 0-paths
and then show that it is CAT.
(M1)
(M2)
(M3)
(M4)
(M5)
(M6)
(M7)
(M8)
(M9)
(M10)
(M11)
(M12)

procedure GenMult ( n : Z(0 . . . N ) );


local j : Z(0 . . . t);
begin
if n0 = n then PrintIt( [1..N ] )
else
for j {p | np > 0} do
n := j;
nj := nj 1;
GenMult( n 1 );
nj := nj + 1;
n := 0;
end{ of GenMult}

Algorithm 4.13: Generate permutations of a multiset in colex order. Given [1..n] are all
zeroes, the call GenMult(n) prints all strings [n + 1..N ] where is a multiset permutation of
specification hn0 , . . . , nt i.

Our algorithm, GenMult, is shown as Algorithm 4.13. The array is initialized to 0s


and then GenMult(N ) is called. The multiset permutation are produced in colex order.
Procedure GenMult can be translated directly into a conventional language like Pascal, C,
or Java except for the statement
for j {p | np > 0} do .
A nave implementation translates this into something like
for j := 0 to t do if n[j] > 0 then do ,
but the resulting analysis would give an amortized cost of (t). To eliminate the dependence
on t, we could maintain a linked list of those nj for which nj > 0. The nodes corresponding
to nj are deleted from the list at line (M8) when nj becomes 0 and are re-inserted at
line (M10). Assuming this linked list representation, the running time of the algorithm is
proportional to the number of nodes in its computation tree. The computation tree for
h2, 1, 1i is shown in Figure 4.3; this is the colex suffix tree of the set of permutations. Since
we are generating in colex order, level i in the tree corresponds to array index N i.
c
uvic01, Frank
Ruskey, 19952001

70

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

0
0

2
2

Figure 4.3: Computation tree of GenMult on input h2, 1, 1i.


The following two multinomial coefficient identities will prove useful in analyzing this
algorithm. The first identity is classic and is simply a generalization of Pascals triangle
(2.2); the second may be verified by induction on n0 .

t
X
N 1
N
=
(4.13)
n0 , . . . , ni1 , ni 1, ni+1 , . . . , nt
n0 , n1 , . . . , nt
i=0

This next identity is a generalization of (2.4).

n0
X
N
N i
N +1
=
n n0 + 1 n0 , n1 , ..., nt
n0 i, n1 , . . . , nt

(4.14)

i=0

The number of nodes of degree one in the recursion tree T of GenMult is equal to the
number of nodes on i-paths in T , where i > 0. The number of nodes at level N j + 1 (with
the root at level 0) on i-paths in T is the same as the number of multiset permutations of
specification
hn0 , n1 , . . . , ni1 , ni j, ni+1 , . . . , nt i.
For the example of Figure 4.3, the number of nodes on 2-paths at level 4 is 3 (corresponding
to 001, 010, and 100), there are no 2-paths at level 3, and the total number of degree one
nodes is 6 (these are the circled nodes). Thus, the total number of nodes of degree one is
given by the following expression.

ni
t X
X
N j
n0 , . . . , ni1 , ni j, ni+1 , . . . , nt
i=1 j=1

t
X
i=1

N 1
N
N ni + 1 n0 , . . . , ni1 , ni 1, ni+1 , . . . , nt

N
N

N n0 + 1 n0 , n1 , ..., nt

N
2
,
n0 , n1 , ..., nt

where n0 = max{n1 , n2 , . . . , nt }.
All other internal nodes of the tree have degree greater than one, so there cannot be
more of them than there are leaf nodes. Thus, the total number of nodes in the tree is at
N
most 4 n0 ,...,n
, the algorithm is CAT, independent of both t and N .
t
c
uvic01, Frank
Ruskey, 19952001

4.5. PERMUTATIONS OF A MULTISET

4.5.1

71

Combinations of a Multiset

The k-combinations of an n-set S are obtained by taking all subsets of the set S that
have k elements. What if S, instead of being a set, is a multiset with specification, say,
hn0 , n1 , . . . , nt i? Such k-subsets of a multiset have several applications, notably in statistics,
where they arise in permutation tests with repeated data values and as 2 n contingency
tables.
Let C(k; n0 , n1 , . . . , nt ) denote the set of all k-subsets of the multiset consisting of ni
occurrences of i for i = 0, 1, . . . , t. Each combination is itself a multiset. For example,
C(2; 2, 1, 1) = {{0, 0}, {0, 1}, {0, 2}, {1, 2}}. We show that all multiset combinations can
be generated by a CAT algorithm, as long as 0 < k < n0 + + nt . Observe that the
k-combinations of the multiset of specification h1, 1, . . . , 1i are precisely the k-combinations
of a (t + 1)-set.
Instead of listing the elements of each k-set, we record how many times each element
occurs. Thus we will generate all strings a0 a1 at , where a0 + a1 + + at = k and
0 ai ni for i = 0, 1, . . . , t. Because of this representation combinations of a multiset
are also sometimes called compositions with restricted parts. These strings will be listed in
colex order. For example, the strings representing C(2; 2, 1, 1) are listed 200, 110, 101, 011.
Let C(k; n0 , n1 , . . . , nt ) = |C(k; n0 , n1 , . . . , nt )|, and n = n0 + n1 + + nt . If i is
the number of times that t occurs in a multiset combination, then clearly we must have
0 i nt , as well as k n + nt i k.
Thus, classifying our solutions according to the value of at , we obtain the following
positive recurrence relation.
min(nt ,k)

C(k; n0 , n1 , . . . , nt ) =

C(k i; n0 , . . . , nt1 ).

(4.15)

i=max(0,kn+nt )

In our discussion the ni remain fixed, while k and t vary. Thus the C(k; n0 , . . . , nt )
notation is cumbersome and we shorten it to Ct (k). Hence (4.15) becomes
min(nt ,k)

Ct (k) =

Ct1 (k i)

(4.16)

i=max(0,kn+nt )

Observe the symmetry Ct (k) = Ct (n k) and the boundary values Ct (0) = Ct (n) = 1.
Our recursive algorithm for listing all k-subsets of a multiset is based on the recurrence
relation (4.16). After presenting the algorithm, we show that it is CAT; i.e. that the amount
of computation used is proportional to the number of combinations generated.
At each node in the recursion tree at level p, the algorithm decides how many copies of
p are included in the subset and array element a[p] is set to that number.
The algorithm for listing all k element subsets of a multiset with a total of n elements,
t + 1 of which are distinct, is given in Figures 4.14 and 4.15. The decision to use gen1 is
made if k n/2, otherwise gen2 is used. The algorithms take three parameters, k, t and
n.
The degree of a node is the number of different possible values that can be stored in a[p];
i.e., the number of terms in (4.16). From the algorithms gen1 and gen2 we observe that
nodes of degree one in the computation tree occur if k = n, or k = 0, in the cases of gen1
and gen2, respectively. Unfortunately, there can be many calls of degree one if k is close
to 0 or if k is close to n. In the procedure of Figure 4.14, the recursion is terminated when
c
uvic01, Frank
Ruskey, 19952001

72

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

0
0

1
1

0
2

Figure 4.4: Computation tree of gen1 on C(5; 1, 2, 2, 1, 1) or of a lex algorithm on


C(5; 1, 1, 2, 2, 1). Colex order is used in the algorithms, lex order in the analysis. The
crossed shaded edges indicate eliminated 0-paths.
k = 0. The initial call is gen1(k, 0, n) and a is initialized so that ai = 0 for i = 0, 1, . . . , t.
In the procedure of Figure 4.15, the recursion is terminated when k = n. The initial call is
also gen2(k, 0, n) but a is initialized so that ai = ni for i = 0, 1, . . . , t. The use of the min
and max expressions, and the inclusion of the parameter n are necessary to avoid making
recursive calls that will not result in a valid subset; i.e., to guarantee that the algorithm is
BEST.
procedure gen1 ( k, t, n : N);
local i : N;
begin
if k = 0 then PrintIt else
for i := max(0, k n + nt ) to min(nt , k) do
at := i;
gen1( k i, t 1, n nt );
at := 0;
end{ of gen1};
Algorithm 4.14: The procedure gen1 (to be used when k n/2).
In gen1 a call of degree 1 results if k = n; after that all descendant calls have degree
1. Similarly, in gen2 a call of degree 1 results if k = 0; after that all descendant calls have
degree 1.
We now analyze the algorithm gen1; the analysis of gen2 is analogous. The notation
of the analysis is simplified if we assume that the algorithm produces strings in lex order,
rather than colex order; this is simply a matter of indexing. We need the following
technical
n
Lemma which is a generalization of the fact that the binomial coefficients k are unimodal
in k.
Lemma 4.2 For fixed t the numbers Ct (k) are unimodal in k.
Proof: The numbers Ct (k) are the Whitney numbers (of the second kind) of the finite
chain product (n0 + 1) (n1 + 1) (nt + 1), where ni denotes a chain of i elements.
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

73

procedure gen2 ( k, t, n : integer );


local i : N;
begin
if k = n then PrintIt else
for i := max(0, k n + nt ) to min(nt , k) do
at := i;
gen2( k i, t 1, n nt );
at := nt ;
end{ of gen2};
Algorithm 4.15: The procedure gen2 (to be used when k > n/2).
According to Proposition 8.61 of Aigner [3], the sequence of level numbers of a finite
chain product is logarithmically concave. Since any logarithmically concave sequence is
unimodal, the proof is complete.
2
Let Ut (k) denote the number of calls to gen1(k,t,n) that have exactly one child. For
example, in Figure 4.4, U5 (5) = 13 (and C5 (5) = 12).
Lemma 4.3 If k n/2, then Ut (k) 2 Ct (k).
Proof: The lemma is clearly true if k = 0 or t = 0. So let k > 0 and t > 0; we argue by
induction on t.
Every node at the penultimate level has one child (since at must be k (a0 + +at1 )).
For a node higher in the tree, say at level p, to have degree one it must be the case that
ap+1 = np+1 , . . . , at = nt . Thus every non-penultimate node of degree one in the tree for
Ct (k) corresponds to some node of degree one in the tree for Ct1 (k nt ). For example,
in Figure 4.4, the thicker non-crossed edges show the tree for C4 (5 1), and each nonpenultimate degree one node in the tree for C5 (5) corresponds to a degree one node in the
tree for C4 (4). The number of nodes of degree one that are not at the penultimate level is
thus bounded by Ut1 (k nt ). The number of nodes at the penultimate level is Ct (k); the
number of degree one nodes at the penultimate level is typically fewer (as in Figure 4.4.
Hence we have
Ut (k) Ct (k) + Ut1 (k nt )
Ct (k) + 2Ct1 (k nt )
Ct (k) + Ct1 (k nt ) + Ct1 (k nt + 1)
2Ct (k).
Noting that k n/2 implies k nt (n nt )/2, the second inequality follows inductively.
The third inequality follows from the symmetry and unimodality of the Ct numbers. The
final inequality follows from the recurrence relation (4.16).
2

4.6

Trees

Trees play a pivotal role in graph theory and in computer science. There are many types
of trees and each type requires a somewhat different approach to generate. We will present
lexicographic algorithms for binary trees, ordered trees, rooted trees, free trees, and B-trees.
Two Gray codes for binary trees are discussed in the next chapter.
c
uvic01, Frank
Ruskey, 19952001

74

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


v
@
@

@v

v
A
A
A
A

A
A
v
v
A
A
A
A
A
A

Figure 4.5: Extended binary tree represented by the sequence 1110001100.


10101010
10101100
10110010
10110100
10111000
11001010
11001100

11010010
11010100
11011000
11100010
11100100
11101000
11110000

Figure 4.6: Lexicographic tree sequences for n = 4 (read down).

4.6.1

Binary Trees, Ordered Trees

Binary trees are just one of many objects counted by the Catalan numbers. In this section
we develop an algorithm for generating all binary trees with n vertices, as represented by
certain bitstrings. To represent a binary tree we first convert it to its extended equivalent
by adding n + 1 leaves. Then each internal node is labeled with a 1 and each leaf is labeled
with a 0. By performing a preorder traversal of the tree and omitting the final 0, a sequence
with n 1s and n 0s is obtained. Such sequences will be called tree sequences and are the
only representation of binary trees that will be used in this chapter; the set of all tree
sequences is denoted T(n) (see Chapter 2) and may be thought of as the set of all wellformed parentheses with n left parentheses and n right parentheses. Figure 4.5 shows the
extended binary tree that yields the tree sequence 1110001100 or well formed parentheses
((()))(()).
Our generation algorithm will list all tree sequences lexicographically. A tree sequence
is characterized by the property that there are exactly n 1s, n 0s, and in any prefix the
number of 1s is at least as great as the number of 0s. We call this the dominating property.
Thus to generate lexicographically we scan from right to left searching for the first 0 that
we can change to a 1 and still maintain the dominating property. This 0 is rightmost with
the property that a 1 is to its right. That 0 is changed to a 1 and the positions to the right
are filled in the lexicographically minimal way.
The general pattern is that
p 10 s

z }| {
a1 a2 ak1 0 11 1
a1 a2 ak1 1 |00 {z
0}
qp+2 00 s

q 00 s

z }| {
00 0
becomes
1010

10
,
| {z }
p1 100 s

c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

75

where q is the number of 0s in the rightmost block of 0s and p is the number of 1s in


the rightmost block of 1s. For example, the lexicographic successor of 11101011100000 is
11101100001010. An iterative implementation of this update is given in Algorithm 4.16.
procedure Next;
{Assumes a0 = 0}
local k, q, p, j : N;
begin
k := 2n;
while ak = 0 do k := k 1;
q := 2n k;
while ak = 1 do k := k 1;
if k = 0 then done := true else
p := 2n k q;
ak := 1;
for j := k + 1 to k + q p + 2 do aj := 0;
j := 2n 2p + 3;
while j 2n 1 do
aj := 1; aj+1 := 0; j := j + 2;
end {of Next};
Algorithm 4.16: Next for binary trees in lexicographic order.
Lets now analyze this algorithm. Note that p q for any tree sequence, since the
reverse complement (flip all bits and reverse the string) of a tree sequence is also a tree
sequence, and that the time to go from one tree to the next is O(p + q) = O(q). Also
since the reverse complement of a tree sequence is also a tree sequence, the number of tree
sequences having a fixed value of q is equal to the number of extended binary trees whose
leftmost leaf is at level q. We will show that the average value of this quantity is 3n/(n + 2)
and thus that the listing algorithm is CAT.1
Define T(n, k) to be the set of all bitstrings of length n whose leftmost k bits are 1s
and whose (k + 1)st bit is a 0. This corresponds to the number of extended binary trees
on n internal nodes with leftmost leaf at level k. Let T (n, k) be the number of bitstrings in
T(n, k). The numbers T (n, k) satisfy the following recurrence relation, which will be proven
a little later.

if k = 1
T (n, 2)
T (n, k + 1) + T (n 1, k 1) if 1 < k < n
T (n, k) =
(4.17)

1
if k = n
Also, a non-recursive expression is known.

k
2n k
T (n, k) =
2n k n k

(4.18)

Iterating (4.17) we obtain


T (n + 1, k + 1) =

n
X

T (n, j).

(4.19)

j=k
1

The exact amortized value of p + q + 1 is determined in exercise 39.

c
uvic01, Frank
Ruskey, 19952001

76

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

Thus
n
X

k T (n, k) =

k=1

n X
n
X

T (n, j)

(4.20)

T (n + 1, k + 1)

(4.21)

k=1 j=k

n
X
k=1

= T (n + 2, 3)

(4.22)

To obtain the average we divide this last expression by Cn and obtain the result below.
n
1 X
k T (n, k) =
Cn
k=1

=
=

T (n + 2, 3)
T (n + 1, 1)
2n+1
3

(4.23)

2n+1 n1
2n+1
1
2n+1
n

(4.24)

3n
n+2

(4.25)

This proves the following theorem.


Theorem 4.1 The average level number of the leftmost leaf of a binary tree is 3n/(n + 2).
Equivalently, a random walk in a random extended binary tree that starts at the root
and goes to the left or right subtrees with equal probability ends at a leaf in 3n/(n + 2)
expected number of steps.
To develop a recursive algorithm we could use the recurrence relation (2.15) for the
Catalan numbers but the resulting algorithm is not as simple as the one we obtain below.
The key idea is to add another parameter k to the problem. A constructive form of the
proof of (4.17) is the basis of our recursive algorithm, but we need to introduce some
additional notation. For bitstrings x and y = 1k 0x define operations flip and insert as
flip(y) = 1k1 01x and insert(y) = 1k+1 00x. These definitions are extended to lists of
bitstrings in the natural way.
To prove (4.17) classify the elements of T(n, k) according to whether they are of the
form = 1k 01x or = 1k 00x; i.e., according to the value of the (k + 2)nd bit. In the former
case flip(1k+1 0x) = and in the latter insert(1k1 0x) = ; elements of the argument sets
are counted by T(n, k + 1) and T(n 1, k 1), respectively. In other words,
T(n, k) = flip(T(n, k + 1)) ] insert(T(n 1, k 1)),

(4.26)

where ] denotes union of disjoint sets. Note that (4.26) proves the main case of (4.17).
This discussion leads to the recursive procedure T of Algorithm 4.17. To generate
T(n, k), set the first k + 1 symbols of x to 1k 0 and call T(n, k, k + 2). To generate T(n) no
initialization is necessary, just call T(n + 1, 1, 1).
Let t(n, k) denote the number of calls to procedure T with parameters n and k. Since
on every level of recursive calls, procedure T takes constant time, the time complexity of
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

77

procedure T ( n, k, pos : N);


begin
if k = n then PrintTree else
if k = 1 then
x[pos] := 1; T( n, 2, pos + 1 ); x[pos] := 0;
else
x[pos] := 1; T( n, k + 1, pos + 1 );
x[pos] := 0; T( n 1, k 1, pos + 1 );
end {of T};
Algorithm 4.17: Algorithm to generate the elements of T(n, k) in relex order.
the algorithm is proportional to t(n, k). The numbers t(n, k) satisfy the recurrence relation
given below.

if k = 1
1 + t(n, 2)
1 + t(n, k + 1) + t(n 1, k 1) if 1 < k < n
t(n, k) =

1
if k = n
The following lemma proves that t(n, k) is proportional to T (n, k) and thus that the
algorithm is CAT.
Lemma 4.4 For 1 < k n, t(n, k) 3T (n, k) 2, and t(n, 1) 3T (n, 1) 1.
Proof: We proceed by induction. If k = n, then
t(n, n) = 1 = 3 T (n, n) 2
If k = 1, then
t(n, 1) = 1 + t(n, 2)
1 + 3T (n, 2) 2
= 3T (n, 1) 1
If k = 2, then
t(n, 2) = 1 + t(n, 3) + t(n 1, 1)
1 + 3T (n, 3) 2 + 3T (n 1, 1) 1
= 3T (n, 2) 2
If 2 < k < n, then
t(n, k) = 1 + t(n, k + 1) + t(n 1, k 1)
1 + 3T (n, k + 1) 2 + 3T (n 1, k 1) 2
3T (n, k) 2
2
Ranking the elements of T(n) may be visualized conveniently in terms of lattice walks,
as was done for combinations. See Figure 4.7, which shows a n by n half grid and a walk
that starts at the upper right corner (n, n) and ends at the lower left corner (0, 0). At each
c
uvic01, Frank
Ruskey, 19952001

78

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

0
0 132

?
0
0

42 132

14

14

42

90

 ?
28

48

14

20

?
0

Figure 4.7: The walk, 11101011100000, corresponding to the rank 333 = 132 + 132 + 42 +
14 + 9 + 4 tree in T(8).
step of the walk you must either move down or to the left; these two steps correspond to
the two two terms in the recurrence relation (4.17) for T (n, k). A step down corresponds
to a 1 in a tree sequence and a step left to a 0. Again we attach numbers to vertical edges
but not horizontal edges. The edge from (n, k 1) to (n, k) is labeled
T (n, n k) =

nk n+k
.
n+k
k

Note that T (n, n k) is the number of walks from (n 1, k) to (0, 0). The rank of a
bitstring is simply the sum of the edge labels of the corresponding path. As was the case
for combinations, the values of the labels can be computed on the fly (Exercise 43). Thus
we have ranking and unranking algorithms that use O(n) arithmetic operations.

4.6.2

Rooted Trees, Free Trees

Recall that a rooted tree is a connected acyclic graph with a distinguished vertex called the
root and a free tree is a connected acyclic graph.
Labelled Rooted Trees
In proving Theorem 2.2 we established a bijection between the set of all functions f :
{2, 3, . . . , n} {1, 2, . . . , n + 1} and labeled trees on n + 1 nodes with root n + 1. Those
functions can be thought of as sequences a2 , a3 , . . . , an where 1 ai n + 1 for i =
2, 3, . . . , n. Such sequences are trivial to generate, rank, and unrank lexicographically by an
extension of our results on generating subsets. Basically, were counting in base n + 1 (See
Exercise 8).
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

79
n=1

n=2

q
q
q
q
q

n=3

q
q

q
q
q

q AAq

q
q
q

n=4

q AAq

q
q
q
q

q
qn=5 q

A
q Aq
q
qAAq
q

q S
q Sq
qAAq
q

q
q

q AAq
q

q
q
qAAq

q
q

q Aq
q S
q Sq
q

q
q
qAAqq
q qSSq
q q q

q
Q
qAAq Q
q
Qq

Figure 4.8: The rooted trees on at most 5 nodes.


Unlabeled Rooted Trees
In Figure 4.8 we show the rooted trees on n = 1, 2, 3, 4, 5 nodes. We have drawn these trees
in what will be referred to as a canonic manner. This means that subtrees with greater
height are drawn to the left, and that this definition is applied recursively.
To count the number an of rooted trees with n nodes we follow the discussion of Knuth
[218]. Obviously, a1 = 1. Given a tree with n > 1 vertices, suppose that there are ji subtrees
with i vertices. Since the order of the subtrees does not matter and the same subtree may
occur repeatedly we have
n1
Y

an =

j1 +2j2 +=n1 i=1

ai + ji 1
.
ji

(4.27)

Using this recurrence one can compute a table of the an as follows.


n
an

1
1

2
1

3
2

4
4

5
9

6
20

7
48

8
115

9
286

10
719

11
1842

12
4766

13
12486

14
32973

15
87811

One may be tempted to use the recurrence relation (4.27) to generate rooted trees since
it involves a summation over integer partitions of products of compositions, and there
exist CAT algorithms for generating partitions, product spaces, and compositions. However,
(4.27) is very awkward to use and a much simpler approach is available. The algorithm and
analysis for generating rooted trees we present is essentially due to Beyer and Hedetniemi
[30]. One way of representing a rooted tree is obtained by traversing the tree in preorder,
recording the level numbers of the nodes. This gives a sequence encoding e(T ) = l1 , l2 , . . . , ln
of T . The encoding also can be defined recursively. If |T | = 1 then e(T ) = 0; otherwise, let
the principle subtrees of T be T1 , T2 , . . . , Tm . Then
e(T ) = 0, e(T1 ) + 1, e(T2 ) + 1, . . . , e(Tm ) + 1,
where e(Ti ) + 1 is the sequence resulting from adding 1 to each element of the e(Ti ). A
canonic tree lexicographically maximizes e(T ) over all rooted trees isomorphic to T . For
c
uvic01, Frank
Ruskey, 19952001

80

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


r
HH
r
r
Hr
@
r
r
@r

@
r @r
r

r
HH
r
r
Hr

@
r @r r
@
r
@r
r

0121123342

0123432121

Figure 4.9: Two isomorphic rooted trees. The one on the right is canonic.
012345
012344
012343
012342
012341
012333
012332
012331
012323
012322

012321
012312
012311
012222
012221
012212
012211
012121
012111
011111

01234
01233
01232
01231
01232
01221
01212
01211
01111

(a)

(b)

Figure 4.10: (a) The 20 canonic sequences of length n = 6 in relex order (read down). (b)
The 9 canonic sequences of length n = 5 with elements contributing to x indicated in bold
and those contributing to y underlined.
example, see Figure 4.9. If T is canonic then e(T ) is said to be a canonic sequence. Let
h(T ) denote the height of a tree. In a canonic tree h(T1 ) h(T2 ) h(Tp ), and thus
max(e(T1 )) max(e(T2 )) max(e(Tp )).
We think of the nodes of the tree as being labeled in preorder with 1 as the root.
The algorithm lists canonic sequences in reverse lexicographic (relex) order. Given that
e(T ) = l1 , l2 , . . . , ln , let succ(e(T )) = s1 , s2 , . . . , sn denote the relex successor of e(T ) and
let
p = max{i : li > 1} and q = max{i : i < p, li = lp 1}.
In other words, p is the position of the rightmost element larger than 1, and q is the parent
of p. Note that p must be a leaf. We wish to decrease lp by as small amount as possible,
and then fill in the entries to the right in a lexicographically maximal manner. This is done
by setting si as follows (updating in the order i = 1, 2, . . . , n):

li
for i = 1, 2, . . . , p 1
si =
sip+q for i = p, . . . , n.
The subsequence lq , lq+1 , . . . , lp1 represents the subtree rooted at q. The update repeatedly copies this subsequence. Implementing this update in a straightforward manner (by
searching from the right, first for p and then for q) gives rise to an algorithm that is clearly
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

81

O(n) per tree generated. Is it, in fact, CAT? That depends on the value of n q, amortized
over all sequences. Unfortunately a bit of experimentation reveals that the amortized value
of n q is close to being linear in n (see Exercise 55). On the other hand, the amortized
value of n p appears to be bounded by a constant independent of n. This is proven below.
The observations of the previous paragraph suggest that we maintain some additional
data structure that will allow us to quickly determine q, given p. One possibility is to
maintain an array par, where par[i] is the parent of i. If li = 1, then par[i] = 1. The
following lemma is useful in proving a time bound on the algorithm.
Lemma 4.5 If l is a canonic sequence and li1 = li = 1, then lj = 1 for all j > i.
Proof: Note that if the sequence l1 , l2 , . . . , li2 , 1, 1, li+1 , . . . , ln is valid, then the sequence l1 , l2 , . . . , li2 , 1, li+1 , . . . , ln , 1 is also valid but is lexicographically greater unless
li+1 = = ln = 1.
2
(r1)
(r2)
(r3)
(r4)
(r5)
(r6)
(r7)
(r8)
(r9)
(r10)
(r11)
(r12)
(r13)
(r14)
(r15)
(r16)

procedure Next;
local i, q : N; global p : NATURAL
begin
while L[p] = 1 do p := p 1;
if p = 1 then done := true else
if L[p] = 2 and L[p 1] = 1 then
L[p] := 1; par[p] := par[p 1];
else
q := p par[p];
for i := p to n do
L[i] := L[i q];
if q + par[i q] < p
then par[i] := par[i q]
else par[i] := q + par[i q];
p := n;
end {of Next};
Algorithm 4.18: Generate rooted trees in relex order.

We now explain Algorithm 4.18. It is an iterative procedure Next that transforms the
current sequence L (we use capitals since lower case L is confusing in programs) into its
lexicographic predecessor. The arrays L and par are initialized so that L[i] = par[i] = i 1
for i = 1, 2, . . . , n. If L[p] = 2 and L[p 1] = 1 at the beginning of an iteration then
at the end of the iteration L[p] = 1 and all positions to the right also contain 1s. Thus
there is no need to reset p to n; we just leave its value unchanged. This means that p
must be left global. In the next subsection, when generating free trees, we will use Next
with p initialized to another value. The array par is easily updated. As the subsequence
L[q], . . . , L[p 1] is repeatedly copied (line (r11)) the values of par[q + 1], . . . , par[p 1]
remain the same (line (r13)) since they form all nodes of a subtree except the root. The
root nodes p, p + q, p + 2q, . . . must be handled separately as is done at line (r14). Line
(r12) tests whether i is one if the copied root nodes.
There are two loops that we must consider, the while loop at line (r4) and the for loop
that begins on line (r10). Let x be the number of times p is decremented by the while loop
c
uvic01, Frank
Ruskey, 19952001

82

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

at line (r4) and let y be one less than the number of times the body of for loop at line (r10)
is executed; this can be viewed as the number of increments of p from its current value up
to n. For example, if n = 5, then x = 6 and y = 2 (see Figure 4.10). Observe that
x y = n 1,
since p starts out at n and finishes at 1. We claim that the while loop at line (r4) is
executed at most twice. If, on the previous call to Next, lp = 2 and lp1 = 1, then (by
Lemma 4.5) lp2 > 1 and so p will be decremented exactly twice. On the other hand, if
on the previous call to Next lp 6= 2 or lp1 6= 1, then some subsequence, not containing
consecutive 1s, has been repeatedly copied. Thus, ln 6= 1 or ln1 6= 1. Hence x 2an , and
so y 2an n + 1 2an . We conclude that the algorithm is CAT. The preceding analysis
does not tell us a bound on the average value of n p, but its exact asymptotic value has
been determined by Kubicka [235].
Theorem 4.2 (Kubicka) The amortized value of n p + 1, taken over all canonic sequences,Pis 1/(1 ), where is the radius of convergence of the generating function
T (x) = ai xi .
Proof: The proof of this theorem is outside the scope of this book. We do, however, make
a few observations that provide some insight into the result. How many trees are there with
a given value of p? The number for which n p k is ank since the rightmost k leaves at
level 1 may be removed, yielding an arbitrary rooted tree with n k nodes. Thus the sum
of n p + 1 over all canonic sequences is a1 + a2 + + an . This implies that the generating
function for the complexity of the algorithm is
T (x) + xT (x) + x2 T (x) + =

1
T (x).
1x

The rest of the proof relies on some asymptotic manipulations.

The value of is approximately 0.3383219.... This theorem has the interesting consequence that if lines (r6-8) are removed from the algorithm, then it is still correct and it
is still CAT. With those three lines removed the algorithm depends on L only at line (r4)
where the test L[p] = 1 may be replaced with par[p] = 1. Thus the array L may be
removed and par used instead as the representation of a rooted tree.
Free Trees
Let tn denote the number of free trees with n nodes. The following formula for tn in terms
of the number of rooted trees (the an from the previous subsection) is known.

X
an/2 + 1
tn = an
ai aj + [[n even]]
.
(4.28)
2
i+j=n
ij

Unfortunately, the combinatorial decomposition upon which it is based is even less suitable
for generating than was the one that gave rise to (4.27) those minus signs are killers!
But we can use it to compute a table of the number of free trees.
n
tn

1
1

2
1

3
1

4
2

5
3

6
6

7
11

8
23

9
47

10
106

11
235

12
551

13
1301

14
3159

15
7741

16
19320

c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

012312 012212

012211

83

012121

012111

011111

Figure 4.11: The rooted versions of the free trees on 6 vertices, listed in relex order. Central
vertices are darkened.
The generation of free trees is more complicated than the generation of rooted trees,
due mainly to the absence of the root. Our strategy is to define some unique root for each
free tree and then use the level sequence representation of the previous subsection. These
sequences will be generated in relex order using the same general algorithm but we need to
somehow skip over those trees that are not representatives of a free tree. There are natual
candidates for a root, namely the centroid(s) or center(s) of the tree.
For generalizing the algorithm of the previous section it proves to be most convenient
to use a center of the tree as the root. Recall that the center(s) of a free tree may be found
by repeatedly removing all leaves until one or two vertices remain. If two vertices remain,
then they are adjacent and the tree is said to be bicentral. Furthermore, every longest path
in the graph passes through the central vertex (or vertices).
The algorithm described in this subsection is due to Wright, Richmond, Odlyzko and
McKay [461].
So we use the center of the tree as the root if there is a unique center, but what if T is
bicentral? We need a rule that will uniquely identify one of centers. The definition given
below will, at first glance, seem more complicated than is necessary (why not, for example,
just take the lex maximum equivalent rooted tree?). However, this definition results in
simple and efficiently implementable rules for getting the relex successor of a given free
tree. By Succ(e(hT i)) we denote the relex successor of free tree T ; i.e., it is the encoding
of the free tree whose rooted version follows e(hT i) in relex order. Define |T | to be the
number of vertices in T . Given two rooted trees S and T , define S T to mean that
either |S| < |T |, or |S| = |T | and e(S) lexicographically precedes e(T ), where e() is the level
number encoding introduced in the previous subsection.
Given an edge [u, v] joining two centers of T , let Tu and Tv denote the two rooted
subtrees that remain when [u, v] is deleted. The root of Tu is u and the root of Tv is v. We
root a free tree T at u if Tv Tu and root it at v otherwise. Given a free tree T , by hT i
we denote the rooted version of T with the root chosen as described above. This is called
the rooted version of T . In Figure 4.11 we show the rooted versions of the 6 free trees on 6
vertices.
Let hT i be a canonic rooted tree with principle subtrees T1 , T2 , . . . , Tm . Given an encoding e(S) of some rooted tree S, the following theorem gives a necessary and sufficient
condition for there to be a tree T such that e(S) = e(hT i).
Theorem 4.3 For n 3, the encoding e(S) of a rooted tree with principal subtrees T1 , T2 , . . . , Tm
is the encoding e(hT i) of the rooted version hT i of a free tree T if and only if
(A) m 2 (i.e., there is more than one principal subtree), and either condition (B1) below
or conditions (B2),(C),(D) hold.
(B1) max(e(T1 )) = max(e(T2 )) (i.e., T is unicentral), or
c
uvic01, Frank
Ruskey, 19952001

84

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

(a) 0123312111

(b) 0123311111

(c) 0123232123

Figure 4.12: Condition (B2) fails: (The height of T2 is reduced too much). (a) original tree
T . (b) succ(e(T )). (c) Succ(e(T )).

(a) 0122222121

(b) 0122222111

(c) 0122221222

Figure 4.13: Condition (C) fails: (|T1 | has too many nodes). (a) original tree T . (b)
succ(e(T )). (c) Succ(e(T )).
(B2) max(e(T1 )) = 1 + max(e(T2 )) (i.e., T is bicentral), and
(C) |T1 | n |T1 |, and
(D) if equality holds in (C), then e(T1 ) 0, 1 + e(T2 ), . . . , 1 + e(Tm ).
The theorem is simply a restatement of our definition of the rooted version of a free tree,
stated in terms of the level sequences. Item (A) says that the center is not a leaf, which is
obvious for n 3. Items (B1) and (B2) are another way saying that a center is a vertex
that minimizes the maximum distance to a node. If max(e(T1 )) = max(e(T2 )) then the tree
is unicentral and the central vertex is the root. If max(e(T1 )) = 1 + max(e(T2 )) then the
tree is bicentral; one center is the root and the other is the root of T1 . The last two items,
(C) and (D), deal with the bicentral case. Item (C) says that T1 has at least as many nodes
as the remainder of the tree. Item (D) says that the encoding of T1 is lexicographically
smaller than the encoding of the rest of the tree.
Our listing is in relex order so the initial tree is
0, 1, . . . , bn/2c, 1, 2, . . . , dn/2e 1,
which is the encoding of a path of length n. The final tree is 0, 1, . . . , 1 which is the encoding
of a star, K1,n1 .
Condition (A) can never fail, nor can condition (B1). For condition (B2) to fail e(T )
and succ(e(T )) must have the form:
e(T ) = 0, 1 + e(T1 ), 1, 2, . . . , h 1, 1, 1, . . . , 1
succ(e(T )) = 0, 1 + e(T1 ), 1, 2, . . . , h 2, h 2, . . . , h 2
For condition (C) to fail e(T ) and succ(e(T )) must have the form:
<|T1 |1

z
}|
{
e(T ) = 0, 1 + e(T1 ), 1, 2, . . . , h, 1, 1, . . . , 1
succ(e(T )) = 0, 1 + e(T1 ), 1, 2, . . . , h 1, h 1, . . . , h 1
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

(a) 0123231212

85

(b) 0123231211

(c) 0123222123

Figure 4.14: Condition (D) fails: (The encoding of T1 is too large). (a) original tree T . (b)
succ(e(T )). (c) Succ(e(T )).
For condition (D) to fail e(T ) and succ(e(T )) must have the form:
e(T ) = 0, 1, 2 + e(T2 ), . . . , 2 + e(Tm ), 1 + e(T2 ), . . . , 1 + e(Tm )
succ(e(T )) = 0, 1, 2 + e(T2 ), . . . , 2 + e(Tm ), 1 + e(T2 ), . . . , 1 + succ(e(Tm ))
These three cases are illustrated in Figures 4.124.14. In each case succ(e(T )) is not the
rooted encoding of a free tree. However, the same general strategy will produce Succ(e(T ))
from succ(e(T )) in each case. Clearly, T1 is going to have to change in Succ(e(T )), even
though it didnt change in succ(e(T )). We therefore apply the successor function Next (of
Algorithm 4.18) with p initialized to |T1 | + 1. The result is a tree that is lexicographically
smaller than succ(e(T )). Is it valid? Not necessarily: if L[1 + |T1 |] > 2, then a rooted tree
with only one principal subtree is produced. Note that all unicentral trees with a given T1
precede all bicentral trees with a given T1 . Therefore, if L[1+|T1 |] > 2, then we now need to
add a second principal subtree to transform it into a unicentral tree. This second principal
subtree should be lexicographically as large as possible, which means that it must be a path.
The addition of this path is accomplished by changing the last h elements to 1, 2, . . . , h,
where h = 1 + max(e(T1 )). The overall process of obtaining Succ(e(T )) is summarized as
Algorithm 4.19. Let L = e(T ).
L := succ(L);
if a condition of Theorem 4.3 fails then begin
L := sequence obtained by running Next with p initialized to |T1 | + 1;
if L[1+|T1 |] > 2 then L[nh+1 . . . n] := 1, 2, . . . , h;
Algorithm 4.19: Relex algorithm for generating free trees. Given L = e(hT i) it produces
Succ(e(hT i)).
Intuitively, it is plausible that this algorithm is efficient, since the trees T for which
succ(T ) violates one of the conditions have a very specific form, and we expect the number
of them to be asymptotically smaller than the number of free trees with n nodes. We can
detect failures by maintaining variables m1 = max(e(T1 )), m2 = max(e(T2 )), and n1 = |T1 |.
Clearly, these variables allow us to determine failures of conditions (B2) and (C) with simple
tests. Determining a failure of condition (D) is done by maintaining a variable c which is
the index of the first element of e(T2 ) which is different than the corresponding element of
e(T1 ). Let An be the time used to generate all free trees, aside from the time Bn used in
obtaining Succ(e(T )) from succ(e(T )) when failures occur.
The amortized time An /|Tn | is proportional to the amortized value of n p + 1, by the
same reasoning used in the case of generating rooted trees. From the asymptotic (2.18)
and Lemma 2.10, An 4tn . We now need to deal with Bn . Clearly the time to produce
c
uvic01, Frank
Ruskey, 19952001

86

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


s
H
EH

E HH

H
E

HH
E

HH

Es
s
s
S
A
##@
S
@
A
#

Ss
#
s
s
s @s

s
As
S
C
C
A
C
C
C
A
CS
C
A
C
C
C
A
C S C
A C C C
A

Figure 4.15: B-tree of order 4 with encoding 32223242323.

Succ(e(T )) from succ(e(T )) is O(n), so we need only show that the number of failures is
o(tn /n). However, proving this requires some delicate asymptotic manipulations that are
beyond the scope of this book. See [461] for the details.
Theorem 4.4 (Wright, Richmond, Odlyzko and McKay) Properly implemented, Algorithm 4.19 is a CAT algorithm for generating all free trees.

4.6.3

B-trees

B-trees are used extensively for the storage and retrieval of large amounts of data stored
on disk. In particular, they are used in many database applications. Their combinatorial
properties are not elegant, but algorithms for generating and ranking them are rather nice
and illustrate general useful techniques.
In this section we present a modification of an algorithm, due to Kelsen [209], for
generating all B-trees. Recall that a B-tree of order m is an ordered tree which satisfies the
following properties.
1. Every node has at most m children.
2. Every node, except the root, has at least m/2 children.
3. The root has at least 2 children.
4. All leaves occur on the same level.
We will assume throughout this section that m 3. As is typical, we generate a sequence
encoding of B-trees. Our encoding is the sequence of number of children of each internal
node, where the nodes are traversed bottom-up level-by-level and from right-to-left within
a level. See Figure 4.15 for an example.
We extend the definition of B-trees to what we call B(s, d) trees. The definition is the
same except that instead of all leaves occurring at the same level leaves may occur on two
levels; leaves at the lowest level are all in the left part of the tree. There are s leaves at the
lowest level and d leaves at the penultimate level. The B-trees with n leaves are just the
B(n, 0) trees. Figure 4.16 shows the trees in B(6, 2).
Let b(s, d) = |B(s, d)|. The addition of this second parameter to the problem helps
immensely, and is a recurrent theme in combinatorial generation. Weve seen an instance
c
uvic01, Frank
Ruskey, 19952001

4.6. TREES

87

pH

HH p
p

p
p Hp
pA p
p A p p A p p A p

pH
p HH p

p@ p
pp@ p
p A p
p A p p A p

222232

222322

p
H

HH p

p
Qp
p A p
pp@ p pp@ p

pP

p Q
p QP
p Pp
pp@ p pp@ p

33222

334

p
pQQp
p QQp pA p

A pQp p A p
p

p Q

pH

p @

p HpHp

Q
p p A pQp p A p

24222

244

p
H

HHp

p
Qp
p A p
A pQp
pA p
p
pQ

pP

p Q
p Pp
p QP
A pQp
pA p
p
pQ

424

42222

Figure 4.16: The eight trees in B(6, 2) (with m = 4).


d=
s=1
2
3
4
5
6
7
8
9
10

0
1
1
1
2
2
4
5
9
15
28

1
0
1
1
2
2
5
8
15
22
36

2
0
1
1
3
4
8
10
18
30
57

3
0
2
2
4
4
10
16
31
48
83

4
0
2
2
6
8
17
23
43
71
135

5
0
4
4
9
10
24
37
74
123
232

6
0
5
5
14
18
42
63
127
217
415

7
0
9
9
24
30
73
114
228
376
691

8
0
15
15
43
56
129
191
371
607
1116

9
0
28
28
73
90
208
309
599
975
1797

10
0
45
45
118
146
335
494
968
1608
3031

Table 4.1: The numbers b(s, d) when m = 4.


of it once before when generating binary trees; i.e., introducing k to get T(n, k). Table 4.1
contains the values of b(s, d) for m = 4.
A recurrence relation for b(s, d) and its combinatorial interpretation form the basis of
our recursive algorithm for generating B-trees. The trees will be generated in lex order.

1
if s = 1, d = 0

b(d
+
1,
0)
+
b(m/2,
d
+
1)
if
m is even, s = m

b(d + 1, 0)
if m is odd, s = m

b(d + 1, 0)
if dm/2e s < m
b(s, d) =
(4.29)

b(1, 0)
if 2 s < m, d = 0

min(sdm/2e,m)

b(s i, d + 1) if s > m, d 0

i=dm/2e

Each case of this recurrence relation is fairly easy to understand by considering the
underlying trees. Clearly if s = 1 and d = 0, then there is only one tree, namely the tree
with one node. If m is even, then b(m, d) = b(d+1, 0)+b(m/2, d+1) because any leftmost m
c
uvic01, Frank
Ruskey, 19952001

88

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

leaves must have only one parent (b(d + 1, 0)) or have two parents, both with m/2 children
(b(m/2, d + 1)). Similarly, s leftmost leaves must have a common parent if dm/2e s < m;
in this case b(s, d) = b(d + 1, 0). If 2 s < m and d = 0, then the parent of those s nodes
must be the root of the tree. In the general case of s > m and d 0, we simply classify
the trees according to the number i of children of the parent of the leftmost leaves that are
siblings. This discussion proves the recurrence relation (4.29).
The Pascal code to do the generation recursively is based on the recurrence relation
and its proof; see Algorithm 4.20. Some cases have been collapsed. No initialization is
necessary. To generate B(s, d) call Bgen(s, d, 1); to generate all B-trees with n leaves call
Bgen(n, 0, 1). The parameter p 1 to PrintIt indicates the number of nodes in the current
tree and is necessary since the number of internal nodes in a tree can vary.
procedure Bgen ( s, d, p : N);
local i : N;
begin
if s = 1 then PrintIt( p 1 ) else
if s > m and d 0 then
for i := dm/2e to min{s dm/2e, m} do
a[p] := i; Bgen( s i, d + 1, p + 1 );
else
if s = m and m is even then
a[p] := dm/2e; Bgen( m2, d + 1, p + 1 );
a[p] := s; Bgen( d + 1, 0, p + 1 );
end {of Bgen};
Algorithm 4.20: Generating B-trees in lexicographic order.
To show that this algorithm is CAT we will show that there are at most 4 successive
calls of degree one. By Lemma 2.5 we conclude that the listing algorithm is CAT. From
the recurrence relation, we observe that calls of degree one occur only if one of the four
conditions [A],[B],[C],[D] occurs, as shown in the table below.
[A]
[B]
[C]
[D]

condition
s = m is odd
dm/2e s < m
2 s < m and d = 0
m is odd and s = m + 1

result
b(d + 1, 0)
b(d + 1, 0)
b(1, 0)
b(dm/2e, d + 1)

Item [D] bears some explanation. The sum in (4.29) has one term if and only if
dm/2e = min(s dm/2e, m).
But we cannot have dm/2e = m unless m = 1, which cannot occur. If dm/2e = s dm/2e,
then s = 2dm/2e. If m is even then we get s = m, which is impossible since s > m. If m is
odd then we obtain s = m + 1.
Let us now consider what can happen from case [A] in order to obtain further degree
one nodes. From [A] we go to b(d + 1, 0) from which it is possible to obtain cases [A],[C], or
[D], but not [B]. If d = 0 then we are at a leaf. If d + 1 = m or 2 d + 1 < dm/2e, then we
go to b(1, 0) which is a leaf; this covers subcases [A] and [C]. The most interesting subcase
is [D], which occurs when d = m; the result is b(dm/2e, 1), which then goes to b(2, 0) and
c
uvic01, Frank
Ruskey, 19952001

4.7. SET PARTITIONS

89

then to a leaf. Similar exhaustive reasoning will lead us to the conclusion that the longest
path of degree one nodes has length 4.
Examples of such maximal paths, with m = 5, are
[A](5, 5) [D](6, 0) [B](3, 1) [C](2, 0) (1, 0)[leaf]
and
[B](3, 5) [D](6, 0) [B](3, 1) [C](2, 0) (1, 0)[leaf].
Algorithm 4.21 contains the corresponding ranking algorithm, which we present without
explanation. The structural similarity of the ranking and generating algorithms is obvious,
and is something to be strived for.
function Rank ( s, d, k : N) : N;
local i, r : N;
begin
if s = 1 then return( 0 );
if s > m then
r := 0;
for i := dm/2e to a[k] 1 do r := r + b(s i, d + 1);
return( r+ Rank( s a[k], d + 1, k + 1 ) );
if s = m and m is even then
if a[k] = m2 then return( Rank( dm/2e, d + 1, k + 1 ) );
else return( b(dm/2e, d + 1) ) + Rank( d + 1, 0, k + 1 );
return( Rank( d + 1, 0, k + 1 ) );
end {of Rank};
Algorithm 4.21: Ranking B-trees in lexicographic order.

4.7

Set Partitions

A partition of a set S is a collection of disjoint subsets of S whose union is S. The number


of partitions of an n-set is the Bell number Bn which is discussed in Chapter 2. Below we
list the 15 partitions of the set {1, 2, 3, 4}. We use a slash (/) to separate the subsets and
omit all commas and parentheses; 13/24 means {1, 3}, {2, 4}.
1234
123/4
124/3
134/2
234/1
12/34
13/24
14/23
1/2/34
1/3/24 1/4/23 2/3/14 2/4/13 3/4/12
1/2/3/4
Traditionally, the subsets of the union are referred to as blocks. In the list above partitions with the same number of blocks are listed on the same row. The
of partitions
nnumber

of a n-set into k blocks is the Stirling number of the second kind, k , which satisfy the
following positive recurrence relation, which may be proven by classifying n according to
whether it occurs in a block by itself or not (c.f. Equation (2.13)).
n1

if k = n
k1
n
n1
=
(4.30)
k
n1 if k = 1
n1
k
+
k
if
1
<
k
<
n
k1
k
c
uvic01, Frank
Ruskey, 19952001

90

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

It is awkward to work directly with the partitions; instead we use a sequence encoding
which has come to be known as a restricted growth function. This is a string a1 a2 . . . an
satisfying a1 = 0 and the restricted growth (RG) condition
ai 1 + max{a1 , . . . , ai1 } for i = 2, . . . , n.

(4.31)

There is a one-to-one correspondence between partitions of {1, 2, . . . , n} and restricted


growth strings (we will use string instead of function) of length n. Given a partition, order
its blocks B0 , B2 , . . . , Bk1 according to the smallest number in each block. Then set ai to
be the block in which i occurs. For example B0 = {1, 2, 4, 7}, B1 = {3, 8}, B2 = {5, 6, 9}
is such an ordering of blocks and the corresponding RG string is 001022012. Here are the
restricted growth sequences corresponding to the 15 partitions listed earlier.
0000
0001
0122
0123

0010
0121

0100
0112

0111
0120

0011
0102

0101
0012

0110

The restricted growth condition (4.31) leads immediately to a recursive procedure that
lists all RG strings whose largest value is less than k, as shown in Algorithm 4.22. Variables
n and k are global. The initial call is S(1,0). To generate all RG strings, set k := n. Variable
m keeps track of the largest value in a[1..l 1].
procedure gen( l, m : N);
local i : N;
begin
if l > n then PrintIt
else
for i := 0 to m do
al := i;
gen( l + 1, m );
if m < k 1 then
al := m + 1;
gen( l + 1, m + 1 );
end{of gen};
Algorithm 4.22: Procedure to generate RG sequences in lex order.
This algorithm is CAT because there are no calls of degree one except for the root as
long as k 2. See Figure 4.17 which shows the computation tree for n = k = 4. The
number of nodes in the tree is clearly the sum of the first n Bell numbers.
However, what if we only wanted the partitions into exactly k blocks? The recurrence
relation (4.30) leads to degree one nodes whenever n = k or k = 1. A casual inspection
of experimental data reveals that the condition n = k occurs more often than k = 1, so
we apply the PET (#1) technique and eliminate paths along which n = k. This yields
Algorithm 4.23, which generates RG sequences in a pseudo-colex order. The array a is
generated right-to-left, but because there are two recursive calls when an is set to k 1 the
resulting list is not exactly in colex order. The computation tree is not a prefix tree. To
initialize set a[i]:=i 1 for i = 1, 2, . . . , n and then call S(n,k). In order to understand the
algorithm note that when k 1 is placed at line (s9), it is the leftmost k 1.
c
uvic01, Frank
Ruskey, 19952001

4.7. SET PARTITIONS

91

a
aa

aa
aa

A
A

E
E
E

HH
H

H
H
H

J
J

J
J
J

J
J

S
C
CS
C S

0 1 2 3

Figure 4.17: Computation tree for n = k = 4.

(s1)
(s2)
(s3)
(s4)
(s5)
(s6)
(s7)
(s8)
(s9)
(s10)

procedure S( n, k : N);
local i : N;
begin
if n = k then PrintIt
else
for i := 0 to k 1 do
an := i; S( n 1, k ); an := n 1;
if k > 1 then
an := k 1; S( n 1, k 1 ); an := n 1;
end{of S};

Algorithm 4.23: Generation of set partitions with exactly k blocks in a pseudo-colex


order.

c
uvic01, Frank
Ruskey, 19952001

92

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

The algorithm takes time (n) if k = 1 and we will show that the amortized number of
calls for k > 1 is bounded by 2.5. Let t(n, k) denote the number of calls resulting from the
call S(n, k). Then

if k = n
1
1 + t(n 1, k)
if k = 1
t(n, k) =

1 + t(n 1, k 1) + k t(n 1, k) if 1 < k < n.


Lemma 4.6 If 1 < k n, then t(n, k)

5 n
2 k

1.



Proof: First note that n1 = nn = t(n, n) = 1, n2 = 2n1 1, and t(n,
1) = n for all

5 n
n 1. We now argue by induction on n. If k = n, then t(n, n) = 1 2 n 1 = 32 . If
2 < k < n, then
t(n, k) = 1 + t(n 1, k 1) + k t(n 1, k)

5k n 1
5 n1
1+
k
1+
2 k1
2
k

5 n
1.

2 k
If k = 2, then
t(n, 2) = 1 + t(n 1, 1) + 2t(n 1, 2)
= n + 2t(n 1, 2) {Iterate recurrence relation}
n3
X
= 2n2 +
(n i)2i
i=0
n2

= 52
n2
5 n1
(2
1) 1

2
5 n
=
1. {This is where the 5/2 comes from.}
2 2
Thus weve covered all cases 1 < k n and the lemma is proved.

Ranking
We now develop a ranking algorithm for RG sequences in lex order. Consider the example
001022012. Its rank is equal to the number of RG functions of length 9 with length 3 prefix
000, or length 5 prefixes 00100, 00101, or length 6 prefixes 001020, 001021, or length 8
prefix 00102200, or length 9 prefixes 001022010, or 001022011. Note that 00100 and 00101
are prefixes of exactly the same number of RG sequences of a given length; all that matters
is the length of the prefix and the maximum value occurring in it.
Define
mi = max{a1 , a2 , . . . , ai1 }

for i = 2, . . . , n;

this is the same as the parameter m in procedure gen of Algorithm 4.22. Also define R(n, m)
to be the number of sequences a1 a2 an for which a1 = m and the restricted growth
c
uvic01, Frank
Ruskey, 19952001

4.8. NUMERICAL PARTITIONS


n/m
1
2
3
4
5
6
7
8
9
10

0
1
2
5
15
52
203
877
4140
21147
115975

1
1
3
10
37
151
674
3263
17007
94828

2
1
4
17
77
372
1915
10481
60814

3
1
5
26
141
799
4736
29371

93
4
1
6
37
235
1540
10427

5
1
7
50
365
2727

6
1
8
65
537

7
1
9
82

8
1
10

9
1

Table 4.2: Restricted tail numbers R(n, m) for 1 n + m 10.


condition (4.31) is satisfied. These are sometimes called the restricted tail coefficients.
Thus R(n, 0) is the Bell number Bn . It now follows by our previous discussion that
Rank(a1 a2 an ) =

n
X

ai R(n i + 1, mi ).

(4.32)

i=2

For example,
Rank(001022012) = R(7, 0) + 2R(5, 1) + 2R(4, 2) + R(2, 2) + 2R(1, 2)
= 877 + 302 + 154 + 4 + 2 = 1339.
The R(n, m) numbers satisfy a simple recurrence relation which was used to construct
Table 4.2. If n = 1, then R(1, m) = 1, and if n > 1, then
R(n, m) = R(n 1, m + 1) + (m + 1) R(n 1, m).
To prove the recurrence relation consider a2 ; either 0 a2 m, in which case a2 an is
counted by R(n 1, m), or a2 = m + 1, in which case a2 an is counted by R(n 1, m + 1).
If R is precomputed then ranking takes O(n) arithmetic operations. Observe that the
mi can be computed in O(n) arithmetic operations. Unranking is also O(n), given the R
numbers. Computing the restricted tail numbers takes (n2 ) arithmetic operations.

4.8

Numerical Partitions

A numerical partition of a positive integer n is a sequence p1 p2 pk > 0 such


that p1 + p2 + + pk = n. Each pi is called a part. For example, 7 + 4 + 4 + 1 + 1 + 1
is a partition of 18 into 6 parts. The number of numerical partitions of n is denoted p(n)
and the number of partitions of n into k parts is denoted p(n, k); this is also the number of
partitions of n whose largest part is k. These numbers are studied extensively in number
theory and combinatorics. See Chapter 2 for further information on p(n) and p(n, k).
There is a simple recurrence relation that the p(n, k) numbers satisfy. This recurrence
will form the basis of a recursive algorithm for generating numerical partitions.
p(n, k) = p(n 1, k 1) + p(n k, k)

(4.33)
c
uvic01, Frank
Ruskey, 19952001

94

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

To prove (4.33) classify partitions according to whether the smallest part is a 1 or not. If it
is a 1, then removing the 1 leaves a partition of n 1 into k 1 parts. If it is greater than 1,
then reducing each part by 1 leaves a partition of n k into k parts. It is this second term
of the recurrence relation that causes trouble when trying to develop an efficient generation
algorithm since it seems to require (k) steps. This recurrence relation may be iterated to
obtain
min(k,nk)

p(n, k) =

p(n k, j).

(4.34)

j=1

Of course, (4.34) may be proven directly by interpreting the right-hand side as a sum over
the size j of the second largest part of a partition. One way to represent partitions is by
using the string p1 p2 pk ; we call this the natural representation. Another representation is
obtained by keeping track of how many times each part occurs; this is called the multiplicity
representation. If
p1 + p2 + + pk = v1 + + v1 + v2 + + v2 + + vl + + vl
|
{z
} |
{z
}
{z
}
|
m1

m2

ml

where v1 > v2 > > vl > 0 and mi > 0, then clearly p1 p2 pk = v1m1 v2m2 vlml . For
example, 71 42 13 represents the example partition of 18 given in the first paragraph of this
section. The multiplicity representation of the partition p1 + p2 + + pk is the string of
pairs [v1 , m1 ][v2 , m2 ] [vl , ml ]; e.g., our example is represented as [v1 , m1 ][v2 , m2 ][v3 , m3 ] =
[7, 1][4, 2][1, 3]. Below is a list of all integer partitions of 7 in lexicographic order in both
representations.
partition
1+1+1+1+1+1+1
2+1+1+1+1+1
2+2+1+1+1
2+2+2+1
3+1+1+1+1
3+2+1+1
3+2+2
3+3+1
4+1+1+1
4+2+1
4+3
5+1+1
5+2
6+1
7

natural
1111111
211111
22111
2221
31111
3211
322
331
4111
421
43
511
52
61
7

multiplicity
[1,7]
[2,1][1,5]
[2,2][1,3]
[2,3][1,1]
[3,1][1,4]
[3,1][2,1][1,2]
[3,1][2,2]
[3,2][1,1]
[4,1][1,3]
[4,1][2,1][1,1]
[4,1][3,1]
[5,1][1,2]
[5,1][2,1]
[6,1][1,1]
[7,1]

Our first algorithm uses the natural representation and is based upon (4.34). Interpret
p(n, k) as the number of partitions of n with largest part k, and the index of summation,
j, as the size of the second largest part. The algorithm lists all partitions of n whose
largest part is k in lexicographic order; see Algorithm 4.24. The initial call is P(n,k,1);
no initialization is necessary. To generate all partitions of n call P(2*n,n,1) and modify
PrintIt so that it doesnt print p[1] (or otherwise ignores p[1]).
c
uvic01, Frank
Ruskey, 19952001

4.8. NUMERICAL PARTITIONS

95

procedure P ( n, k, t : N);
local j : N;
begin
pt := k;
if n = k then PrintIt( t ) else
for j := 1 to min(k, n k) do P( n k, j, t + 1 );
end{of P}
Algorithm 4.24: Lexicographic generation of all partitions of n whose largest part is k.
Unfortunately the algorithm is not CAT when k = 1 since then there is only one partition
and it takes n recursive calls to create it. This bad behavior propagates and the algorithm is
not CAT for small values of k. However, the calls when k = 1 can be eliminated by applying
the PET (#1) technique, initializing p to be all 1s and restoring the value 1 as the recursion
backs up. Details may be found in Algorithm 4.25. To prove that this algorithm is CAT
note that every non-leaf node in the computation tree now has degree at least 2 or is the
parent of a leaf. The computation tree for all partitions of n = 7, which used to have 45
nodes, now has 26 nodes, and the tree for n = 20, which used to have 2714 nodes, now has
1117 nodes.
procedure P ( n, k, t : N);
local j : N;
begin
pt := k;
if n = k or k = 1 then PrintIt( t + n k ) else
for j := 1 to min(k, n k) do P( n k, j, t + 1 );
pt := 1;
end{of P};
Algorithm 4.25: CAT generation of all partitions of n whose largest part is k.
It is sometimes useful to be able to generate all partitions with a fixed number of parts or
with both a fixed number of parts and given largest part. We now develop an algorithm for
these problems. Define P(n, k, s) to be the set of all partitions of n into k parts with largest
part equal to s, and let p(n, k, s) = |P(n, k, s)|. Clearly, in order to have p(n, k, s) > 0 we
must have at least one part equal to s and at most k parts equal to s. Thus
s + k 1 n ks.
By classifying the partitions of P(n, k, s) according to the value of the second largest part,
call it j, we obtain the following recurrence relation, which has no zero terms; it is a positive
recurrence relation.
min(s,nsk+2)

p(n, k, s) =

p(n s, k 1, j)

(4.35)

j=max(1,d ns
e)
k1

The resulting procedure is Algorithm 4.26. As before we initialize the array to 1s and stop
the recursion if n = k or k = 1. The initial call is P(n,k,s,1). To generate all partitions
of n into exactly k parts call P(2*n,k+1,n,1) and ignore the contents of p[1] (which is
always n).
c
uvic01, Frank
Ruskey, 19952001

96

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


procedure P ( n, k, s, t : N);
local j, lower : N;
begin
pt := s;
if k = 1 or n = k then PrintIt
else
lower := max(1, d(n s)/(k 1)e)
for j := lower to min(s, n s k + 2) do P( n s, k 1, j, t + 1 );
pt := 1;
end{of P};
Algorithm 4.26: Generation of all partitions of n into k parts with largest part s.

What is the running time of Algorithm 4.26? It depends upon the value of the parameters and is certainly not CAT for some values. This is because another type of degree one
path in the computation tree is now possible, namely when n = ks (which gives rise to an
s-chain of length k). Even when generating all partitions into a fixed number of parts it is
not CAT; the maximum number of calls seems to occur when k = n/2. For P(2*n,n div

2+1,n,1) the number of recursive calls per partition appears to be ( n). The author is
unaware of any simple CAT algorithm for generating partitions with a fixed number of parts
using the natural representation. However, a rather complicated algorithm is presented in
the next chapter in Section 5.8.
We now develop an algorithm for listing numerical partitions in lexicographic order
when the multiplicity representation is used. The main idea behind the algorithm is that
at most only the last three [vi , mi ] pairs change from one partition to the next. The index
l indicates the last [vi , mi ] pair; i.e., vl is the smallest part in the partition. There are four
basic cases that must be considered as shown in the table below in the condition column;
note that they are mutually exclusive and exhaustive. For each case the lexicographically
next partition is shown in the action column. Each condition has two possible outcomes
depending whether there is a part equal to 1 in the lex successor or not.
condition
ml = 1 and vl2 > vl1 + 1

ml = 1 and vl2 = vl1 + 1

ml > 1 and vl1 > vl + 1

ml > 1 and vl1 = vl + 1

action
[vl2 , ml2 ][vl1 , ml1 ][vl , ml ]
[vl2 , ml2 ][vl1 + 1, 1][1, (ml1 1)vl1 + vl 1]
[vl2 , ml2 ][vl1 + 1, 1]
if vl = ml1 = 1
[vl2 , ml2 ][vl1 , ml1 ][vl , ml ]
[vl2 , ml2 + 1][1, (ml 1)vl1 + vl 1]
[vl2 , ml2 + 1]
if vl = ml1 = 1
[vl1 , ml1 ][vl , ml ]
[vl1 , ml1 ][vl + 1, 1][1, (ml 1)vl 1]
[vl1 , ml1 ][2, 1]
if vl = 1 and ml = 2
[vl1 , ml1 ][vl , ml ]
[vl1 , ml1 + 1][1, (ml 1)vl 1]
[vl1 , ml1 + 1]
if vl = 1 and ml = 2

The procedure Next of Algorithm 4.27 is iterative and simply implements the table
above. Initially we set [v1 , m1 ]:=[0, 0], [v0 , m0 ]:=[n + 1, 0], [v1 , m1 ]:=[1, n], and l:=1.
This algorithm is clearly CAT since there are no loops; the number of operations between
c
uvic01, Frank
Ruskey, 19952001

4.8. NUMERICAL PARTITIONS

97

procedure Next;
local sum : N; global l : N;
begin
sum := vl ml ;
if ml = 1 then
l := l 1; sum:=sum + vl ml ;
if vl1 = vl + 1 then
l:=l 1; ml :=ml + 1
else [vl , ml ]:=[vl + 1, 1];
if sum > vl then
[vl+1 , ml+1 ]:=[1, sum vl ]; l:=l + 1;
if l = 0 then done := true
end{of Next};
Algorithm 4.27: Next for lexicographic numerical partitions using multiplicity representation.
successive partitions is bounded by a constant. Usually loopless algorithms are not so easy
to come by; further examples will be found in the next chapter.
Ranking numerical partitions
Here we develop a ranking algorithm for partitions of n in lexicographic order (in the natural
representation). What is the rank of 7+4+4+1+1+1? All partitions of 18 with largest part
less than 7 precede it, as do all partitions of 18 with largest part 7 and second largest part
less than 4; this later number is the same as the number of partitions of 18 7 = 11 whose
largest part is less than 4. Continuing this process, all partitions with largest 2 parts 7 + 4
and third largest part less than 4 precede it, and so on.
Define p(n, k) to be the number of partitions of n whose largest part is at most k; in
other words,
p(n, k) =

k
X

p(n, j).

j=1

These numbers inherit the recurrence relation (4.33); in fact from (4.34), p(n, k) = p(n+k, k)
(combinatorial proofs are also readily derived). Continuing our example,
rank (7 + 4 + 4 + 1 + 1 + 1) = p(18, 6) + rank (4 + 4 + 1 + 1 + 1)
= p(18, 6) + p(11, 3) + rank (4 + 1 + 1 + 1)
= p(18, 6) + p(11, 3) + p(7, 3) + rank (1 + 1 + 1).
This suggest that we define si to be the sum of the parts with index at least i.
X
si =
pj
ji

The rank is then evidently given by the following equation.


X
X
rank (p1 + p2 + ) =
p(si , pi 1) =
p(si + pi 1, pi 1)
i1

i1

c
uvic01, Frank
Ruskey, 19952001

98

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

Computing the si values takes O(n) additions and if a table of the p numbers has been precomputed (a O(n2 ) computation), then evaluating the rank takes time O(n) (if arithmetic
operations are given unit cost).

4.9

Generalized Settings

4.9.1

Wilf s Generalized Setting

Many of the preceding examples relied upon a simple recurrence relation in two parameters.
Wilf in [448] and [449] gave a general setting for handling such recurrences. Extensions of
this idea may be found in Kelsen [209] and Williamson [453], [455].

4.9.2

The Generalized Setting of Flajolet, Zimmerman, and Cutsem

4.10

Listing solutions to problems solved by dynamic programming

This subsection is yet to be written. It will include some examples like the LCS problem
and its applications in computational biology.

4.11

Retrospective

Approach to Generation of Elementary Objects


1. Find a recurrence relation for the objects that
(a) Uses only multiply and add; no divide or subtract.
(b) Is strictly greater than zero in all cases.
(c) Admits a combinatorial proof in terms of some natural representation of
the objects.
2. Write a recursive procedure with the same structure as the recurrence relation.
This is usually easy. If necessary, rewrite the procedure so that the computation can be divided up in such a way that each recursive call is assigned a
constant amount of computation. This can be difficult, if not impossible.
3. To analyze the resulting algorithm count recursive calls. The original recurrence relation is modified by adding +1 to each non-constant case.

4.11.1

Why recusive and iterative algorithms have similar analyses

4.12

Ideals and Linear Extensions of Posets

In this section we present some algorithms for generating the linear extensions and ideals of
partially ordered sets. These algorithms can be used to solve various scheduling problems.
Unlike all of the previously presented algorithms, these algorithms are not or are not known
c
uvic01, Frank
Ruskey, 19952001

4.12. IDEALS AND LINEAR EXTENSIONS OF POSETS

99

WHAT WAS MEANT TO GO HERE???


Figure 4.18: A typical computation tree of a recursive algorithm generating strings in
lexicographic order.
to be CAT nor are they precisely lexicographic; however, they have a definite lexicographic
feel to them.

4.12.1

Varol-Rotem Algorithm for Linear Extensions

Let P be a poset on the set {1, 2, . . . , n} labeled so that 12 n is a linear extension of P.


Now suppose that a list Ln1 of all extensions of P {n} was available. This list could be
expanded to a list Ln of all extensions of P as follows. Let be an extension from the list
Ln1 and k be the maximum index for which k n. Assume that there is a fixed element
0 that is less than all other elements of the poset. Then, since j k n for all j > k, the
permutations
1 k k+1 j nj+1 n1 ,
as j varies from n to k, form a list of all extensions of P in which the elements of [n 1]
appear in the order . At the end of this process we have the permutation
1 k nk+1 n1 .
By now rotating to the left the elements nk+1 n1 we obtain the original permutation
with the n appended at the end (and out of the way). We may then proceed to the
successor 0 of in the list Ln1 and expand 0 in exactly the same way. Inductively, we
conclude that this recursive process will generate all linear extensions of P. Furthermore,
it is easily implemented as illustrated in Algorithm 4.28. Array is initialized to be the
identity permutation 12 n and the initial call is VR(1) Recall that x k y means that
poset elements x and y are incomparable. A non-recursive implementation is the subject of
Exercise 77.
procedure VR ( k : integer );
local i : integer;
begin
if k > n then PrintIt
else
VR( k + 1 );
i:=k;
while i1 k i do
i1 :=: i ;
VR( k + 1 );
i := i 1;
RotateL( i , i+1 , . . . , k );
end;
Algorithm 4.28: Recursive Varol-Rotem algorithm for generating linear extensions.
c
uvic01, Frank
Ruskey, 19952001

100

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

(1)

1(2)

````

123

```
```
`

(312)

132
@

123(4)

1324

@
@

13(42)

HH

3124

3142

HH
H

3(412)

Figure 4.19: Computation tree of Varol-Rotem algorithm on the example poset.


Example: For the poset below the
by the algorithm and their inverses.


1234
2
4

1324
1342
3124
3142

3412
1
3

table to its right shows the permutations produced


1
1234
1324
1423
2314
2413
3421

Figure 4.19 shows the computation tree associated with our example. The digits that
are in parentheses are those that get rotated as the computation backs up; i.e., think of
them being done in a postorder traversal of the tree. Observe that the rotations have the
property that the number of elements involved in the rotation is equal to the number of
siblings of that node (a node is a sibling of itself). This observation holds true in general,
and thus it follows that the sum of the costs of the rotations is equal to the number of nodes
in the computation tree. In the example computation tree there are 11 nodes and there are
11 numbers in parentheses. As an aside, note that if a linked representation is used, then
rotations take constant time.
What is the theoretical running time of this algorithm? We just argued that it depends
upon the number of nodes in the associated computation tree, which is clearly O(ne(P)).
Whether it is O(e(P)) depends on the number of nodes of degree one, which depends upon
the poset and the linear extension used to initialize the algorithm. Note that the number
of degree one nodes at level n 1 is equal to the number of extensions of P {n} for
which n1 n.
Let Q be the poset consisting of a chain x1 , x2 , . . . , xn and an element y incomparable
with each element of the chain. If the initial linear extension is yx1 x2 xn , then n(n
1)/2 + 1 rotations are used to produce the n + 1 linear extensions of Q. On the other
hand, if the initial linear extension is x1 x2 xn y, then only n rotations are required, and
the algorithm runs in time O(n). Thus, the algorithm is quite dependent upon the linear
c
uvic01, Frank
Ruskey, 19952001

4.12. IDEALS AND LINEAR EXTENSIONS OF POSETS

101

extension used to initialize the algorithm. However, the algorithm is highly recommended.
It is simple and quite fast in practice.
In the example the extensions and their inverses both get listed in lexicographic order,
but this is not always the case. Consider an antichain on three elements. Its extensions get
listed in the order 123, 132, 312, 213, 231, 321, which is not lexicographic, and neither are
its inverses 123, 132, 231, 213, 312, 321. There is a sense, however, in which the algorithm
is lexicographic. Let r() = r1 r2 rn be the string in which ri is the number of elements
of that are to the right of i and less than i. In our example, the successive values of r are
0000, 0010, 0011, 0020, 0021, 0022. The following lemma is easily proven by induction.
Lemma 4.7 If 1 , 2 , . . . is the list of extensions output by procedure VR, then the list
r(1 ), r(2 ), . . . is lexicographically increasing.
Since can be recovered from r(), the converse of Lemma 4.7 is also true.

4.12.2

Algorithms for Listing Ideals

Now consider the problem of generating all ideals (or filters) of a given input poset P. Recall
that an ideal I of a poset P is a subset of S(P) with the property that x I and y x
imply that y I. Given x S(P), the ideals of P may be classified according to whether
they contain x or not. An ideal that contains x must contain every element of x, the set
of elements below x. An ideal that doesnt contain x must not contain any element of x,
the set of elements above x. Stated formally,
J(P) = (x J(P x)) ] J(P x),

(4.36)

where x J(P x) means the set {x I : I J(P x)}.


This decomposition leads directly to Algorithm 4.29. The initial call is GenIdeal(P, ).
Note that the computation tree is an extended binary tree since both terms of (4.36) are
non-empty. Properly implemented, the amortized running time of this algorithm is O(n)
per ideal. Assume that S(P) = [n] (and thus that S(Q) [n]). Ideal I is implemented
as a bitstring b = b1 b2 bn , where bi = 1 if and only if i I. Then the three parameter
updates I:=I x, Q:=Q x, and Q:=Q x can be implemented in time O(n), given
bitstrings representing x and x. The set of all 2n bitstrings representing x and x can
be pre-computed and stored in time (and space) O(n2 ). Assuming that the selection of x at
line (I6) takes time O(n), the overall running time of Algorithm 4.29 is O(n2 + nN ), where
N = |J(P)|.
(I1)
(I2)
(I3)
(I4)
(I5)
(I6)
(I7)
(I8)
(I9)

procedure GenIdeal ( Q : Poset ); I : Ideal );


local x : PosetElement;
begin
if S(Q) = then PrintIt( I );
else
x:= some element of S(Q);
GenIdeal( Q x, I x );
GenIdeal( Q x, I );
end{ of GenIdeal};
Algorithm 4.29: Generic algorithm for generating ideals.
c
uvic01, Frank
Ruskey, 19952001

102

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

In the case were n is less than the word size of the computer, then the three updates may
be computed in constant time, and except for the selection of x at line (I6), the algorithm
will run in constant amortized time. This is an important consideration, to be kept in mind
as we discuss the theoretically faster variants below.
Specific algorithms are obtained by specifying precisely how the element x is to be chosen
at line (I6). We discuss two variants of this algorithm, one due to Steiner [404] and the
other to Squire [399].
Steiner Variant
In the Steiner variant, x is is chosen to be a minimal element of P. This has no discernable
effect on the running time of the algorithm but it does simplify the recursive call at line
(I7), which becomes GenIdeal(Q {x}, I {x}). The remaining concern is how to efficiently
select a minimal element. One possibility is to maintain, for each element x in the poset,
a value c(x), the number of elements that x covers; x is a minimal element if and only if
c(x) = 0. These c() values can be initialized in time O(n2 ) and updated in time O(n),
thereby preserving the O(n2 + nN ) running time of the generic algorithm.
Squire Variant
In the Squire variant, x is chosen to be the middle element in some linear extension of Q
a selection rule that leads to logarithmic cost per ideal.
We were somewhat sloppy in our analysis of Algorithm 4.29. The running time of
the algorithm is actually proportional to the sum of |S(Q)|, taken over all calls in the
computation tree. Imagine a global variable cost, initialized to 0, and updated at line (I3.5)
by cost:=cost + |S(Q)|. The total running time to produce all ideals is proportional to the
final value of cost.
Recall that a complete binary tree is an extended binary tree in which all leaves are at
the same level. We will show that the computation tree has many complete binary subtrees
and use this to get a bound on cost.
Lemma 4.8 In the computation tree of GenIdeal(P, ), with the Squire rule used to select
x, the call GenIdeal(Q, I) is the root of a complete binary subtree of height at least blg(1 +
|S(Q)|)c.
Proof: If x is chosen to be the middle element in a linear extension of Q then clearly

|S(Q)| 1
|S(Q)| 1
|S(Q x)|
and |S(Q x)|
.
2
2
From these bounds the lemma is easily proven by induction.

Imagine the cost s = |S(Q)| of the node (Q, I) of the computation tree as being distributed evenly over the nodes of the complete binary subtree, call it T = T (Q, I), of height
lg s, as guaranteed by Lemma 4.8. Then each node x of T is assigned a unit cost by the
call GenIdeal(Q, I), but x may be assigned costs by other calls. How much cost can be
assigned to x in total? Note that it can be assigned a cost only by an ancestor, and further,
only by an ancestor that occured at most lg n generations previously. Thus the total cost
assigned to a node is bounded above by lg n, which means that the total cost of the entire
computation tree is at most 2N lg n; i.e., the amortized cost per ideal is O(lg n).
c
uvic01, Frank
Ruskey, 19952001

4.13. EXERCISES

103

The only remaining detail is to explain how to implement the selection rule and update
the parameters in time |S(Q)|. An initial linear extension L of P may be computed in time
O(n2 ) using standard algorithms (cf. Section 2.11.3). We may, in fact, assume that the
elements of P have been labeled so that L = 1, 2, . . . , n. The extension L is stored as a
linked list, and is used to represent Q as the algorithm is running. Note that removing x
from L results in a linear extension of P x, so that we simply delete or add elements to L
as the recursion is proceeding. For each x, the sets x and x are precomputed and stored
as bitstrings. Parameter I is a bitstring. The structures Q and I are not explicitly passed
as parameters; we may think either as leaving them global or as passing pointers to them.
In either case the parameters are changed when the recursive calls are made, and restored
when a recursive call is completed. Note that we use a simple linear scan to find the median
element of the current extension L; a logarithmic search is not necessary.
Let us consider the update Q:=Q x in detail. We simply traverse L while updating
a local list T , initially empty, and equal to L x immediately before the recursive call is
made. Upon encountering an element k on L, we check x[k], the kth bit of the bitstring
representing x; if it is 1, then delete k from L and add it to the end of T , otherwise,
do nothing. At the end of this traversal, list L has been transformed into a new list, L0 ,
representing Q x. Now GenIdeal is called. After the call, T and L0 are merged, using the
standard linear time algorithm for merging sorted linked lists, to obtain the original list L.
These updates of L and T clearly take time O(|S(Q)|).

4.13

Exercises
Questions about lexicographic order

1. [1] True or false: In lexicographic order, for strings , , , and , if < and
< , then < ?
2. [1] Imagine a rather peculiar dictionary that contains exactly the odd numbers less
than 1010 listed in lexicographic order.Each number is written out in English without
using punctuation or the word and, or a for one. Thus, 105 is written as
onehundredfive and not ahundredfive or onehundredandfive; also 45 is written
as fortyfive and not forty-five. Assume the usual ordering of characters with the blank
() as the smallest character. What is the first number in the dictionary? What is
the last number in the dictionary? Answer the preceding two questions if the words
are listed in colex order.
3. [1] Under what conditions on a set of strings S are its lexicographic prefix tree and
its colex suffix tree identical? The tree of Figure 4.1 is such an example.
Questions about subsets
4. [1] The procedure Subset has the somewhat undesirable property that the call Subset(n)
does not generate the subsets of {1, 2, . . . , n}. Use colex order and make a few trivial
changes to Subset so that this undesirable property is eliminated.
c
uvic01, Frank
Ruskey, 19952001

104

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

5. [1+] Show that using Next of Algorithm 4.1 to generate the first m (1 m 2n )
subsets results in a CAT algorithm.
6. [1] Develop a O(n) unranking algorithm for subsets of an n-set in lexicographic order
as represented by bitstrings of length n (i.e., the set B(n)).
7. [1] Re-write Algorithms 4.1 and 4.2 so that they produce the actual elements of each
subset.
8. [1] The same ideas that were used to generate subsets can be used to generate what
are sometimes called product spaces. That is, suppose that we wish to generate all
t-tuples from A1 A2 At , where each Ai is a finite set, which may be thought
of as Ai = {0, 1, . . . , ni }, for some natural number ni . Given n1 , n2 , . . . , nt , show how
to generate the associated product space by a CAT algorithm.
9. [1+] An (n, m)-punctured partition (e.g., Damiani, DAntona and Naldi [79]) is a
string e1 e2 en such that the following three conditions hold.
m + 1 ei for i [n], and
e1 + e2 + + en = n, and
e1 + e2 + + ek = k if ek > 0.
(a) Show that the number of punctured partitions is (m + 1)n by demonstrating an
explicit bijection between (n + 1, m)-punctured partitions and (m + 1)-ary strings of
length n. (b) Develop a CAT algorithm to generate all (n, m)-punctured partitions.
Questions about combinations
10. [1] Give a simple proof by induction of Lemma 4.1.
11. [2] Develop and implement a CAT algorithm to generate the elements of B(n, k) where
PET #2 is used, but not PET #1. Exactly how many recursive calls are made?
12. [1+] The algorithm Next below generates the elements of B(n, k) in lexicographic orprocedure Next;
{Assumes 0 < k n and a0 < 0.}
local j : N;
begin
j := n;
while bj = 0 do j := j 1;
p:=n j;
Analyze the amortized
der.
while bj = 1 do j := j 1;
q:=n j p;
if j = 0 then done := /TRUE;
bj := 1;
for i := 1 to p + 1 do bi+j := 0;
for i := q 1 downto 1 do bni+1 := 1;
end {of Next};
running time of this algorithm by computing the sum of p + q + 1 = n j + 1 over all
bitstrings in B(n, k).
c
uvic01, Frank
Ruskey, 19952001

4.13. EXERCISES

105

13. This exercise is a continuation of the previous one. Observe that there may be some
bits to the right of aj that do not change, and yet Next scans over them and sets
them anyways. The scanning can be eliminated by storing the lengths of runs and the
resetting to the same value can be avoided as well. (a) [1+] Modify the algorithm so
that the amount of computation is O(t(n, k)), where t(n, k) is one-half of the number
of bits that change in a lexicographic listing of the elements of B(n,k).
(b) [1] Derive
a recurrence relation for t(n, k) and (c) [2] prove that t(n, k) is O( nk ).
14. [1+] Develop a CAT algorithm for generating F(n, k), the set of all strings in B(n, k)
that do not have two consecutive 1s. For example F(n, k) = {0101, 1001, 1010}.
15. [2] Find a short recursive algorithm for generating all those k-subsets of an n-set whose
sum is p, where each k-subset is represented as
Pa bitstring. In other words, generate
the elements of the set {b1 b2 bn B(n, k) | ni=1 ibi = p}. Is your algorithm CAT?
16. [2] Use a run-length encoding of subsets with a given sum to develop a loopless
algorithm for generating them.
17. [1+] Consider equivalence classes of bitstrings that are equivalent under reversal. For
example, {011, 110} is one equivalence class; each equivalence class has one or two
elements. We wish to generate the lexicographically smallest representative of each
class. With B(n) = n2 , let M(n) = {x B(n) : x xR }. What is |M(n)|? Give a
CAT algorithm to generate the elements of M(n).
18. [2] Consider equivalence classes of bitstrings with the fixed number of 1s that are
equivalent under reversal. Let M(p, q) = {x B(p + q, q) : x xR }. What is
|M(p, q)|? Develop a CAT algorithm to generate the elements of M(p, q).
19. [3] We now consider equivalence classes of bitstrings that are equivalent under reversal
and/or complementation. Let Y(n) = {x B(n) : x xR , x x, x xR }. What is
|Y(n)|? Develop a CAT algorithm to generate the elements of Y(n).
20. [R] Develop a CAT algorithm to generate all 0-1 matrices with given row and column
sums.
Questions about permutations
21. [1+] Prove that the n-queens program (Algorithm 3.1) with references to arrays b
and c removed results in a CAT algorithm for generating all n! permutations of [n].
22. [2] Implement a O(n log n) algorithm for ranking and unranking permutations in
lexicographic order.
23. [3] Develop an O(n) algorithm for unranking permutations. HINT: Do not base your
algorithm on lexicographic order; use an order derived from the algorithm in the first
section of Chapter 10.
24. [1] Develop and analyze algorithms for listing, ranking, and unranking k-permutations
of an n-set.
c
uvic01, Frank
Ruskey, 19952001

106

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

25. [1+] Develop a CAT algorithm for generating all up-down permutations in lexicographic order.
26. [1] Characterize the inversion strings of up-down permutations.
27. [1+] Defining E(n, k) to be the number of up-down permutations of [n] for which
1 = k, prove the recurrence relation
E(n, k) = E(n, k + 1) + E(n 1, n k).
28. [1+] Use the E(n, k) numbers of the preceding exercise to develop an O(n) ranking
algorithm for the inversion strings of up-down permutations in lex order.
29. [2] Using a combinatorial interpretation of the recurrence relation dn = (n1)(dn1 +
dn2 ), develop a CAT algorithm for generating all derangements of the set {1, 2, . . . , n}.
30. Let D(n, k) denote the number of derangements of [n] with k cycles. [1] Prove that
D(n, k) = (n 1)(D(n 1, k) + D(n 2, k 1)). [2+] Develop a CAT algorithm for
generating the associated set of derangements.
31. A k by n Latin rectangle is a k n matrix with entries from [n] such that (a) every
row is a permutation of [n] and (b) every column is a k-permutation of [n]. A Latin
rectangle is reduced if the first row is the identity permutation 12 n and the first
column is 12 k. The number of 2 n Latin rectangles is n!dn where dn is the
number of derangements of n; the number of reduced rectangles is dn1 . [1] Show
that that there exists at least one reduced Latin rectangle for all 1 k n. [R]
Develop a CAT algorithm to generate all reduced k by n Latin rectangles.
32. [R] Let M (n, k) denote the number of permutations of [n] that have k monotone
runs (sometimes called alternating runs). Prove that
M (n, k) = k M (n 1, k) + 2 M (n 1, k 1) + (n k) M (n 1, k 2)
and use this recurrence relation to develop a CAT algorithm for generating permutations with a fixed number of monotone runs.
33. [2+] Develop a CAT algorithm for generating all involutions, where each involution
is represented as a permutation in one-line notation.
34. [R] Develop a CAT algorithm for generating all permutations with a given number
of inversions. Develop a CAT algorithm for generating all permutations with a given
index.
35. [1+] Develop a CAT algorithm for generating all permutations with a given number
of cycles. [2+] Develop a CAT algorithm for generating all permutations with a given
number of left-to-right maxima.
36. [1+] A k-composition of an integer n is a solution to the equation x1 + x2 + +
xk = n in natural numbers. Develop listing, ranking, and unranking algorithms for
compositions.
37. [1+] Prove equation (4.14).
c
uvic01, Frank
Ruskey, 19952001

4.13. EXERCISES

107
Questions about trees

38. [1] Develop an iterative CAT algorithm for generating the elements of T(n, k).
39. [2] In reference to Algorithm 4.16, prove that the average value of p + q + 1 is C1 +
C2 + + Cn+1 .
40. [2] Use the cycle lemma to prove (4.18).
41. [1] Explain why T (n, k) is the number of ordered forests with n nodes and k trees.
42. [1] Prove an orthogonality relation for the T (n, k) numbers; i.e., if T is the matrix of
the T (n, k) numbers, then find T 1 such that T T 1 = T 1 T = I.
43. [1+] Show that the T (n, n k) numbers from the ranking algorithm for binary trees
can be computed on the fly so that the resulting algorithm uses O(n) arithmetic
operations O(n) space.
44. [2+] What is the average level number of the 2nd leaf of a random binary tree; of the
pth leaf as n ?
45. [1+] For a binary tree let T , let TL and TR denote its left and right subtrees. The
following order on binary trees has been called B-order: T < T 0 if
(a) T = and T 0 6= or
(b) TL < TL0 or
(c) TL = TL0 and TR < TR0 .
Show that Algorithm 4.16 generates binary trees in B-order.
46. [2] Let |T | denote the number of nodes in a binary tree. The following order on binary
trees has been called A-order (or natural order): T < T 0 if
(a) |T | < |T 0 | or
(b) |TL | = |TL0 | and |TR | < |TR0 |.
Develop a CAT algorithm for generating binary trees in A-order. [Hint: represent a
binary tree as a preorder sequence of the number of descendants of each node.]
47. An ordered forest of n + 1 nodes may be encoded by traversing the forest in preorder
and recording the (out)degree of each node as a sequence a = a1 , a2 , . . . , an . This is a
generalization of our encoding of extended binary trees, and we will omit the final 0 as
before. Let F (n0 , n1 , . . . , nt ) = F (n) denote the set of encodings of all ordered forests
that have ni nodes of out-degree i, for i = 1, . . . , t and n0 + 1 leaves. Let T (n) denote
the subset of F (n) consisting
P only of the encodings of trees. [1] Show that F (n) is
non-empty if P
and only if (1 i)ni 0. [1] Show that a T (n) if and only if, for
k = 1, . . . , n, 1ik ai k. [2] Develop a CAT algorithm for listing the elements of
T (n). The number of such ordered forests may be inferred from (2.16).
48. [2+] Give an O(n) algorithm that takes as input an ordered tree T and transforms it
into the equivalent canonic rooted tree. Note that this algorithm implies that we can
test for the isomorphism of two rooted trees in O(n) time.
c
uvic01, Frank
Ruskey, 19952001

108

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

49. [2] Prove that the array par of Algorithm 4.18 is generated in relex order.
50. [1+] Write an unranking algorithm for B-trees.
51. [2] Show that the algorithms for generating and ranking B-trees can be extended to
the following class of trees. Under what conditions is the generation algorithm CAT?
The more general case consists of trees with all leaves at one level, the maximum
(out) degree m and minimum degree l0 at the root, and minimum degree l at the
other internal nodes, where 1 < l0 l m.
52. [1] Compute the b(s, d) table when m = 5 and m = 6.
53. [R] Develop a CAT algorithm for generating AVL (height-balanced) trees.
54. [2] A red-black tree is an extended binary tree in which every node is either red or
black, every leaf is black, every black internal node has two red children, and every
path from a node to a descendant leaf contains the same number of black nodes. Such
trees are characterized by the property that the longest path from any node v to a
leaf is at most twice as long as the shortest path from v to a leaf. Develop a (CAT)
algorithm for generating a natural representation of red-black trees. [Hint: There is
a simple relation between red-black trees and B-trees with m = 4.]
55. [1+] This question refers to the section on generating rooted trees. Compute experimental evidence that the amortized value of n q is not constant, but that the
amortized value of n p is constant.
56. [1+] Prove that par is lexicographically decreasing in the procedure Next of Algorithm
4.18. from the section on rooted trees.
57. [3] Develop a recursive version of the Beyer-Hedetniemi algorithm for generating
rooted trees. The algorithm should also have the CAT property and need only use
the parent array, and not use the L array.
58. [3] Develop a CAT algorithm to generate all rooted trees in which no node has more
than two children.
59. [2] Implement Algorithm 4.19 so that it runs in constant amortized time.
60. [R] Develop a CAT algorithm to generate all rooted trees in which every non-leaf
node has at least two children. [R] Develop a CAT algorithm to generate all rooted
trees with a given degree sequence.
61. [1] Show that Wilfs generalized setting can be applied to well-formed parentheses
strings; i.e., to generate and rank the set T(n).
Questions about set partitions
62. [R] Is there a closed form expression for the numbers t(n, k) from the set partition
section?
63. [2] Investigate what happens when the recursion stops at k = 1 in Algorithm 4.23
(instead of when n = k). Is the resulting algorithm CAT?
c
uvic01, Frank
Ruskey, 19952001

4.13. EXERCISES

109

64. [1] Give a one line proof that

Pn1
i=1

i R(n i, i 1) = Bn 1.

65. [1] Develop an efficient algorithm for finding the rank r RG string of length n in
lexicographic order.
Questions about numerical partitions

66. [1] What numerical partition of 25 has rank 1000 in lexicographic order?
67. [1+] Write an unranking procedure that finds the rank r numerical partition of n in
lexicographic order. How many arithmetic operations does your procedure use in the
worst case?
68. [2] Write a loopless algorithm to generate numerical partitions in the multiplicity
representation in relex order and in colex order. Which order resulted in the simplest
algorithm: lex, colex, or relex?
69. [1+] Give an efficient algorithm to convert a partition in the natural representation
to its conjugate. Give an efficient algorithm to convert a partition in the multiplicity
representation to its conjugate.
70. [2] Write a short procedure to generate all partitions of n into exactly k parts in colex
order. Your recursive calls should be modeled after recurrence relation (4.33). Is the
resulting algorithm CAT?
71. [R] Determine the asymptotic number of nodes in the computation trees of Algorithms 4.24 and 4.25 when used to generate all partitions of n.
72. [1+] Develop a CAT algorithm for generating all partitions of n into odd parts (all
parts are odd) using the natural representation.
[1+] The number of partitions of n into odd parts is equal to the number of partitions
of n into distinct parts. Prove this equality.
[R] Develop a CAT algorithm for generating all partitions of n into distinct parts
using the natural representation.
73. [2+] A binary partition of 2n is a solution a0 , a1 , . . . , an , in numbers, to the equation
2n = a0 20 + a1 21 + + an 2n ; i.e., it is a partition of 2n into powers of 2. Give a CAT
algorithm for listing binary partitions in lex order.
74. [R] Develop a CAT algorithm for generating all compositions n = a1 + a2 + such
that (a) a1 = 1, (b) for j > 1, 0 nj 2nj1 , and (c) aj = 0 implies aj+1 = 0. [1]
Explain the connection between these compositions and level numbers of binary trees.
[1+] Explain the connection between these compositions and non-positive powers of
two summing to 1.
75. P
[R] A score
k vector of a tournament is a sequence a1 a2 an satisfying
k
i=1 ai 2 for k = 1, 2, . . . , n, with equality holding for k = n. Develop a CAT
algorithm for generating all score vectors.
c
uvic01, Frank
Ruskey, 19952001

110

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS


Questions about linear extensions and ideals

76. [2] Develop an algorithm for generating all linear extensions of a partially ordered set
in lexicographic order. [R] Is your algorithm CAT?
77. [1+] Implement the Varol-Rotem algorithm iteratively.
78. [2] Find an example where the final permutation produced by the Varol-Rotem algorithm is not the linear extension with the greatest number of inversions. [R]
Given an n element poset labeled so that 12 . . . n is a linear extension, is it NP-hard
to determine an extension that has the greatest number of inversions?
79. [R] For each poset P, is there an initial linear extension that causes the Varol-Rotem
algorithm to run in CAT time?
80. [1] Finish the inductive argument of Lemma 4.8.
81. 2 Explain how to compute all the principal upsets and principal downsets in time
O(n2 ).
82. [R] Find a CAT algorithm for generating the ideals of a poset.
Miscellaneous questions
83. [2] Develop a CAT algorithm for generating all contingency tables with fixed marginals.
That is, generate all r by c matrices of natural numbers with given row sums k1 , k2 , . . . , kr
and given column sums n1 , n2 , . . . , nc . HINT: base your algorithm on Algorithm 4.14.
84. [R] A degree sequence of a graph G is the sequence of degrees of the vertices of G arranged in non-decreasing order. There are several characterizations of such sequences.
Develop a CAT algorithm to generate all degree sequences of length n.
85. A seemingly much harder problem than the previous is to generate all degree sequences
of graphs with m edges (and no isolated vertices).These sequences are called graphical
partitions. [3+] Develop a positive recurrence relation (or relations) for the number of
graphical partitions. [2+] Based on the recurrence relation, develop a CAT algorithm
for generating all graphical partitions.
86. [R] Given two n-by-n matrices A = [aij ] and B = [bij ], a stable marriage is a permutation such that there does not exist a pair i, j with ai,(i) < ai,(j) and b(j),j < b(j),i .
Develop a CAT algorithm that takes as input A and B and then generates all stable
marriages.
87. [R] A n by n magic square is an arrangement of the integer of [n2 ] into a n by
n matrix so that all column sums, all row sums, and the two diagonal sums are all
equal. Develop a CAT algorithm for generating magic squares..
88. [R] There are many ways to fold a strip of n stamps, ranging from a zig-zag to a
spiral. For example, there are 2 ways to fold 3 stamps, and 5 ways to fold 4 stamps.
Devise a suitable representation for such foldings and develop a CAT algorithm for
generating the foldings.
c
uvic01, Frank
Ruskey, 19952001

4.14. BIBLIOGRAPHIC REMARKS

4.14

111

Bibliographic Remarks

Books
The books of Page and Wilson [296] and Rohl [347] contain algorithms for generating
various types of combinatorial objects, mostly in lex order. The book of Even [115] also
contains algorithms for listing combinatorial objects. The book of Skiena [390] describes
algorithms for listing permutations, multiset permutations, combinations, all implemented
in Mathematica. The Maple V package combinat includes functions to produce
exhaustive lists of permutation, combinations (of a multiset), compositions of an integer,
and other more esoteric objects such as cosets of a group.
Subsets and Combinations
The algorithm for generating combinations in lexicographic order is by Mifsud [280]; see
also Shen [385]. A comparison of algorithms for generating combinations was made by Akl
[5] who concludes that Mifsuds algorithm is fastest. Another survey was made by Carkeet
and Eades [51]. The idea of eliminating the while loop from Algorithm 4.3 was published
by Dvorak [94]. The ranking formula (4.10) is due to Lehmer [244].
Applications of a subset generating algorithm are given by Stojmenovic and Miyakawa
[408].
Permutations
According to [340], the first mention of an algorithm for generating permutations is by
L.L. Fisher and K.C. Krause in 1812 [129]. A fast algorithm for generating permutations
in lexicographic order is Ord [292] (see also Ives [189]) The analysis of the number of
transpositions used by Algorithm 4.11 is from [340].
A close variant of an inversion string is the inversion table, b1 b2 bn , of a permutation,
where bi is the number of elements to the left of i that are greater than i; see Knuth [219],
pg. 12. Also related is the inversion vector defined in Reingold, Nievergelt, and Deo [340]by
letting bi be the number of elements greater than i and to its left. Thus an inversion table
is an inversion vector indexed differently.
The following references contain algorithms for generating permutations: Golomb [153],
Knott [215], Knott [216], Orcutt [290], Rohl [348], Rohl [349], Roy [351], Lipski [250].
Algorithms for generating derangements are discussed in Akl [4].
A rosemary permutation is an equivalence class of permutations under rotations and
reversal; they can be thought of as Hamilton cycles on the labeled complete graph Kn .
The number of rosemary permutations of [n] is (n 1)!/2. The generation of rosemary
permutations was considered by Harada [169] and Read [334].
Bauslaugh and Ruskey [23] give CAT algorithms for generating up-down permutations
lexicographically and for ranking the inversion strings of up-down permutations in time
O(n). A CAT algorithm for generating the inversion strings of permutations with given
ups and downs in colex order was given in Roelants and Ruskey [430]. Roelants [428] gives
a CAT algorithm for generating involutions represented by what he calls I-sequences. The
trick for solving (4.11) is from Reingold, Nievergelt, and Deo [340].
A CAT algorithm for generating the permutations of a multiset in lexicographic order
may be found in Roelants van Baronaigien and Ruskey [360]. Knuth [222] contains an
c
uvic01, Frank
Ruskey, 19952001

112

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

algorithm for generating all permutations of a multiset in which the number of inversions
is bounded.
Savage and Barnes results get mentioned here.
There is an algorithm for ranking and unranking permutation in O(n) arithmetic operations, but the underlying order is not lexicographic; see [287].
Binary trees, ordered trees
A great many papers have be written about generating binary trees using various sequence
representations and many of these papers are closely related. Our discussion closely follows
Ruskey [362]. A survey is given by Makinen [263] and relationships among several of
these algorithms is demonstrated in Lucas, Roelants van Baronaigien and Ruskey [256].Lee,
Lee, and Wong [242] give a CAT algorithm for generating binary trees of bounded height.
Another algorithm for binary trees of bounded height may be found in Knuth [222] (pp. 153154). Other references on generating binary trees are Auger and Walsh [16], Bapiraju and
Rao [20],Bonnin and Pallo [36], Er [105], [107], [108], [111], [109], [112],Remy [341],Ruskey
and Hu [354], Knott [217], Liu [252], Makinen [261],[262], Pallo [297], [298], [300], Pallo
and Racca [302], Proskurowski [314], Proskurowski and Laiman [315], Ramanan and Liu
[327], Rotem and Varol [350], Roelants van Baronaigien and Ruskey [431], Ruskey [361],
Semba [379], Solomon and Finkel [395], Skarbek [389], Trojanowski [416], Vajnovszki and
Pallo [425], Zaks [466], Zaks and Richards [467], and Zerling [468].
A CAT algorithm for generating ordered forests with given degree sequence may be
found in Ruskey and Roelants van Baronaigien [360]; this topic is also considered by Zaks
and Richards [467] and Er [111].
An algorithm for listing all fixed size binary subtrees of a given binary tree is given in
Hikita [178]. Generating all ordered subtrees of a given ordered tree is discussed in Ruskey
[363] and Koda and Ruskey [226].
Free trees, rooted trees
The remarkable 1969 paper of Scions [376] discussed the generation of various types of
trees in lexicographic order. In particular, that paper introduces the encoding e(T ) of
Section 4.6.2 and observes (but does not prove) the CAT property of a lex algorithm for
generating those sequences. The rooted tree algorithm that we presented is due to Beyer
and Hedetniemi [30]; the free tree algorithm to Wright, Richmond, Odlyzko and McKay
[461].
Another paper that contains an algorithm for generating trees is that of Tinhofer and
Schreck [413].Recently, Li and Ruskey [248] have developed algorithms for generating rooted
and free trees that are based soley on the familiar parent array representation of trees. These
algorithms are recursive and can be adapted to generate rooted trees with degree and height
restrictions, and free trees with degree and diameter restrictions and they are CAT.
Kubicka [235] shows that the average value of nP
p + 1 is 1/(1 ), where is the radius
of convergence of the ordinary generating function ai xi . Algorithms for generating binary
rooted trees have been developed by Kubicka and Kubicki [234]. See also Pallo [300] and
Vajnovszki [423].
Additional algorithms for generating and/or ranking rooted trees are given in Wilf and
Yoshimura [452], Yoshimura [465], Kozina [232], and Dinits and Zaitzev [86]. Trojanowski
[417] considers the problem of ranking unordered binary trees.
c
uvic01, Frank
Ruskey, 19952001

4.14. BIBLIOGRAPHIC REMARKS

113

Other types of trees


Algorithms for ranking, unranking and listing 2-3 trees and B-trees are given by Gupta, Lee,
and Wong [162], [163], , and Belbaraka and Stojmenovic [24], but the approach developed
by Kelsen [209], and refined in this chapter, are superior.
Algorithms for listing and randomly generating hybrid binary trees are given in Pallo
[301]. An algorithm for ranking and unranking AVL (height-balanced) trees was given by
Li [249].
A rooted plane tree is a rooted tree embedded in the plane; i.e., the order of nodes within
subtrees is fixed, but the subtrees themselves can move circularly about the root. They can
be generated by using fixed density necklaces; see Exercise 7.
Set Partitions
Papers about generating and ranking set partitions in lex order are Djokic, Miyakawa,
Sekiguchi, Semba, and Stojmenovic [88], Er [110],,Semba [380], and Williamson [454]. Wells
[440] gives a listing algorithm and ranking function based on a combinatorial interpretation
of (2.12). Williamson [453] contains an algorithm for ranking partitions with a fixed number
of blocks.
Numerical Partitions
According to [340], an iterative lexicographic algorithm for generating numerical partitions
is due to K.F. Hindenburg in 1778.
The following references contain algorithms for generating numerical partitions: James
and Riha [192], Riha and James [344], Fenner and Loizou [120], [121], [122], McKay [275],
[276], [277], [278], Narayana, Mathsen and Saranji [288], Stockmal [407], White [446], [447],
Ord-Smith [294], [295]. Phillips [309], Schrak and Shimrat [374], Shen [386].
Knuth [222] (pp. 145-149) contains an algorithm for listing all partitions with a bounded
number of parts of bounded size. [223]
Linear Extensions and Ideals
The Varol-Rotem algorithm is from [433]; it and other algorithms are discussed and compared in Kalvin and Varol [201]. The recursive formualtion given here is believed to be
new. See also Knuth and Szwarcfiter [223]and Wells [440] for other algorithms that generate
all linear extensions. Knuth [221] develops an algorithm for listing all linear extensions of
forest posets in time O(M ), where M is the amount of change that the extensions undergo
in a lexicographic listing.
The Steiner algorithm is from Steiner [404]. The improvement of Squire is from [399].
Generalized settings
Lenstra and Rinnooy-Kan present recursive algorithms for generating various objects [245].
Extensions of Wilfs generalized setting were given by Williamson [453] and by Joichi,
White, and Williamson [200].
!!!! What does the paper of Bezem and van Leeuwen discuss? !!!!
c
uvic01, Frank
Ruskey, 19952001

114

CHAPTER 4. LEXICOGRAPHIC ALGORITHMS

Miscellaneous
Algorithms for generating stable marriages may be found in the book of Gusfield and Irving
[164].
There are a number of ways in which lex order and colex order is useful from a purely
combinatorial point of view. See, for example, Erdos and Szekely [104]. Other examples
include standard proofs of the Kruskal-Katona theorem (e.g., Stanton and White [403]).
Fillmore and Williamson [128] give ranking algorithms for the symmetries and colorations of an n-cube where the underlying order is lexicographic.

c
uvic01, Frank
Ruskey, 19952001

Chapter 5

Combinatorial Gray Codes:


Algorithmic Issues
In this chapter we introduce combinatorial Gray codes, which are lists of combinatorial
objects in which successive objects are close in some sense. These lists have many applications and also lead to interesting questions and results of a mathematical nature, many
of which are discussed in the chapter following this chapter, Combinatorial Gray Codes:
Graph Theoretic Issues. In this chapter we concentrate on algorithms for generating combinatorial Gray codes and on their applications. The fastest known algorithms for generating
certain objects are based on Gray codes; furthermore, they are the basis for an interesting
class of algorithms, the loopless algorithms.

5.1

Introductory Example

Consider, as a practical example, the hypothetical rotating device as shown in Figure 5.1.
One might imagine the drum in a photocopy machine or a magnetic storage device or the
axle of a vehicle, both real examples of the use of this idea. As the device turns it is
necessary to know its actual physical location. To help in this determination the device
contains a number of tracks (rings) each of which is broken down into sectors (pieces of
pie). Furthermore, there is a fixed set of sensors, one per track. Each (track,sector) pair
stores either a 0 or a 1. In the figure there are four tracks and 16 sectors; in general if
there are n tracks then there are 2n sectors. Interpret a white (track,sector) pair as a 0
and a shaded area as a 1. Since each sector intersects n tracks, it corresponds to some
binary number in the range 0, 1, . . . , 2n 1, a number that should be unique in order to
unambiguously identify the sector. In what order should those binary numbers be arranged
around the sectors? Figures 5.1(a) and (b) illustrate two arrangements and Figure 5.1(c)
illustrates the placement of the four fixed sensors. The arrangement (a) lists the numbers
in lexicographic order and arrangement (b) lists them in some other order, to be explained
later. On the first disk (a), the sensors would be reading 0001 and on the second disk (b),
0011.
If the rotation happens to stop so that the sensors are on the border between two
sectors, then the sensors will have trouble distinguishing between 0 and 1 on a track that
has different symbols across the border. If the symbols are the same across the border then
we assume that the sensor returns the correct value, but if they are different then the sensor
115

116

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


1
2
3
4

(a)

(b)










(c)

Figure 5.1: Position detection on a rotating device with two different labellings.
may return either value. In lex order the border between 0000 and 1111 displays a very
bad behavior because the sensors could be returning any bitstring b1 b2 b3 b4 whatsoever! We
consequently have absolutely no idea where the device is positioned.
What is desired is a listing of all 16 bitstrings of length 4 so that successive strings,
including the first and last, differ by exactly one bit. Then at the border the sensors can
return only one of two values, and those two values are exactly those of the two sectors that
share that border. Figure 5.1(b) shows such a listing. The construction of these lists for
general values of n is taken up and solved in Section 5.2.

5.1.1

Combinatorial Gray Codes

The listing just asked for is a specific example of a combinatorial Gray code. Here we give
a formal definition just to get it out of the way. Many other examples will be found in
this chapter and in the next.
Let S be a finite set of combinatorial objects and C a relation on S, which we call the
closeness relation. Then a Combinatorial Gray Code for S is a listing s1 , s2 , . . . , sN of the
elements of S, where |S| = N , such that (si , si+1 ) C for i = 1, 2, . . . , N 1. Sometimes
we also insist that (sN , s0 ) C; in this case, the code is said to be cyclic. There is a
graph G = G(S, C), the closeness graph, that is naturally associated with a combinatorial
Gray code. The vertices of this graph are the objects of S and edges connect those objects
that are close; in other words, G(S, C) is just the digraph of the relation C. In most
instances the closeness relation is symmetric and so G may be regarded as an undirected
graph. The question of finding a Combinatorial Gray Code for S with respect to C becomes
one of finding a Hamiltonian path (or cycle) in G. Conversely, a Hamilton path in G can
be viewed as a Gray code for S. In our introductory example, S is the set of all bitstrings
of fixed length, C is the relation of Hamming distance one, and G is the hypercube.

5.2

The Binary Reflected Gray Code

Suppose that we wish to list all 2n length n bitstrings in such a way that each bitstring
differs from its predecessor by a single bit, as required in our introductory example of
position detection on rotating devices. Such lists are called binary (Gray) codes, named
after Frank Gray who used them in a patent he obtained for pulse code communication.
c
uvic01, Frank
Ruskey, 19952001

5.2. THE BINARY REFLECTED GRAY CODE

117
0
0
..
.
0

Gn =

G0 = ,

Gn1

1
..
.
1
1

Gn1

Figure 5.2: The definition of Gn .


r

110

100
r
010

000

111

101
r

011

001

Figure 5.3: A 3-cube.


There is a very simple recursive algorithm for generating one particular Gray code, known
as the binary reflected Gray code (BRGC).
The binary reflected Gray code on n elements, Gn , is defined recursively by letting
G0 be the single element sequence containing the emptystring, and letting Gn be the list
obtained by prefixing each element of Gn1 with a 0 and then prefixing each element of
Gn1 with a 1.1 This definition is illustrated in Figure 5.2. Thus G1 = 0, 1, G2 = 00, 01,
11, 10, G3 = 000, 001, 011, 010, 110, 111, 101, 100, and G4 is the encoding used in Figure
5.1(b).
The n-dimensional hypercube, or n-cube, Qn , may be considered as the graph whose
vertices consist of all bitstrings of length n and where two vertices are joined by an edge
if they differ by a single bit. The problem of finding a Gray code corresponds to finding a
Hamilton path in Qn . Figure 5.3 illustrates a 3-cube. Observe that the binary reflected Gray
code actually defines a Hamiltonian cycle on the n-cube since the first and last bitstrings
only differ by a single bit.
There is an interesting connection between the Towers of Hanoi problem, the binary
reflected Gray code, and ordinary counting in binary. Most computer scientists are familiar
with the Towers of Hanoi problem, having been introduced to is as an introductory example
of recursive programming. The problem is to move n disks of distinct sizes from one peg to
another peg. Each disk has a hole in it and the pegs pass through these holes. Initially, the
disks are arranged in increasing size on one peg. There are two rules that must be obeyed
in moving the disks. First, only one disk at a time may be moved. Secondly, a larger disk
1

Recall that if L is a list, then L is its reversal.

c
uvic01, Frank
Ruskey, 19952001

118

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


Start

Move 1

Move 4

Move 2

Move 5

Move 3

Move 6

Move 7

Figure 5.4: Towers of Hanoi for n = 3.


Counting
321
000
001
010
011
100
101
110
111

Gray Code
210
000
001
011
010
110
111
101
100

Towers
1
2
1
3
1
2
1

Figure 5.5: n = 3: Counting, Gray Code, and Towers of Hanoi.

may not be placed on a smaller disk. Let Sn (a, b, c) denote a solution to the problem where
the disks are initially on peg a, are moving to peg b, and use peg c as an intermediary. If
n = 1 then simply move the disk from a to b. Otherwise, a solution may obtained by first
doing Sn1 (a, c, b), then moving the largest disk n from a to b, and then doing Sn1 (c, b, a).
A solution to the three disk Towers of Hanoi problem is shown in Figure 5.4.
Now suppose that the disks are numbered 1, 2, . . . , n where 1 is the smallest disk and
n is the largest disk. Let Tn denote the sequence of disks that are moved in solving the
n disk Towers of Hanoi problem. From the example above T3 = 1, 2, 1, 3, 1, 2, 1. From
the recursive solution to the Towers of Hanoi problem we see that T1 = 1 and in general
Tn+1 = Tn , n + 1, Tn . As a notational convenience we will now assume that the Gray code
bitstrings are indexed gn gn1 g1 . It turns out that the kth number of Tn is the index
of the bit that changes in the kth bitstring of Gn , and that this is equal to the position
of the rightmost 0, counting from the right, in the kth bitstring when counting in binary.
These correspondences are illustrated in Figure 5.5, which the reader should digest before
proceeding further.
The sequence Tn is called the transition sequence of the BRGC. Every Hamilton path on
the n-cube has a transition sequence and these paths are often specified by their transition
sequences.
We now develop algorithms for generating the binary reflected Gray code, as well as
ranking and unranking algorithms. Let rank denote the rank function for the BRGC, and
rank the rank function for the reversal of the BRGC (obtained by prepending with 1s first
and then 0s). The following two equations are clear from the recusive definition of the
c
uvic01, Frank
Ruskey, 19952001

5.2. THE BINARY REFLECTED GRAY CODE

119

BRGC.

0
rank(gn1 g1 )
rank(gn gn1 g1 ) =
n1
2
+ rank(gn1 g1 )

0
rank(gn gn1 g1 ) =
2n1 + rank(gn1 g1 )

rank(gn1 g1 )

if n = 0
if gn = 0
if gn = 1

(5.1)

if n = 0
if gn = 0
if gn = 1

(5.2)

The formula for rank is not really needed since


rank(gn gn1 g1 ) = 2n 1 rank(gn gn1 g1 ).
This means that the gn = 1 case of (5.1) could be written as 2n 1 rank(gn1 g1 ).
Nevertheless, it is convenient to have both (5.1) and (5.2) when deriving an iterative ranking
method, which we do next.
Let rank(gn gn1 g1 ) = (bn1 b1 b0 )2 . Then the recurrence relations (5.1) and (5.2)
for rank and rank may be interpreted as setting bn1 to 0 or 1, depending upon whether
the term 2n1 in the binary expansion is present or not. The repeated application of these
recurrence relations to a bitstring may then be thought of as sweeping a r (for rank) or r
(for rank) from left-to-right as illustrated below.
r
1
1
1
1
1
1
1
1
1
b8

g9
1
r
0
0
0
0
0
0
0
0
b7

g8
1
1
r
1
1
1
1
1
1
1
b6

g7
1
1
1
r
1
1
1
1
1
1
b5

g6
0
0
0
0
r
0
0
0
0
0
b4

g5
1
1
1
1
1
r
0
0
0
0
b3

g4
0
0
0
0
0
0
r
1
1
1
b2

g3
1
1
1
1
1
1
1
r
1
1
b1

g2
0
0
0
0
0
0
0
0
r
0
b0

g1
1
1
1
1
1
1
1
1
1
r

With gn+1 = 0, note that an r always has a 0 to its left and a r has a 1 to its left. Thus
there are four possibilities for the three symbols bi xgi where x is r or r, namely 0r0, 0r1,
1r0, and 1r1. The resulting value of bi1 is 0, 1, 1, 0 in those four cases, respectively. From
this we observe that bi1 = bi gi , where denotes exclusive-or. The bit below an r or r
is the exclusive-or of the bits left and right of the r or r.
The preceding discussion proves the following lemma.
Lemma 5.1 If rank(gn gn1 g1 ) = (bn1 b1 b0 )2 , then for i = 1, 2, . . . , n
bi1 = bi gi and gi = bi1 bi .
For our example, 358 = (101100110)2 , so the 358th Gray code bitstring is 111010101.
Note that Lemma 5.1 may be used to obtain a O(n) unranking algorithm (and constant
time when done in parallel).
c
uvic01, Frank
Ruskey, 19952001

120

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

Practical generation of the BRGC


The equation gi = bi bi1 suggests a rapid way of generating the BRGC as long as n is
not to large, in particular if n is at most the number of bits in a computer word. Let b
and g be words and use SR to denote shifting a word one position to the right. Since most
computers include and SR as machine operations, the update g:=b SR(b) operating
on words will update g in three machine instructions. Putting this update together in a
loop with b:=b + 1 will produce the BRGC very efficiently. In algorithm 5.1 we show a C
implementation that will produce the BRGC in the unsigned integer g (variable b is also
an unsigned integer).
b = 0;
do g = b ^ b++ >> 1;
while (b != 0);

Algorithm 5.1: A C implementation of the BRGC on computer words.

Recursively generating the BRGC


Figure 5.2 is a somewhat verbose way of presenting our recursion for the BRGC, although it
is the type of diagram most useful for presenting the BRGC on a blackboard in a classroom
setting. The recursion below is more compact and is in the style we use subsequently.2

if n = 0
C(n) =
(5.3)
C(n 1)0 C(n 1)1 if n > 0
Note that we are appending rather than prepending. In developing elegant natural
algorithms this has the same advantage that colex order had over lex order in the previous
chapter. In subsequent algorithms we will try to append. As in the previous chapter, the
recurrence relation leads immediately to an algorithm. Algorithm 5.2 assumes the existence
of an array of bits, b[1..n]. The initial call is brgcI(n); no intialization is necessary. Note
that brgcI calls cgrbI which produces the reversed list B(n). It is trivially obtained from
brgcI by inverting the order of the two lines of the else clause.
procedure brgcI ( n : N);
begin
if n = 0 then PrintIt
else
g[n] := 0; brgcI( n 1 );
g[n] := 1; cgrbI( n 1 );
end {of brgcI};
Algorithm 5.2: Indirect algorithm for generating the BRGC.
This algorithm is CAT and is perfectly suitable for many applications. However, it
suffers one drawback, namely that the processing routine PrintIt does not know which
bit was flipped. This defect can be remedied, but not without modifying the structure of
the algorithm. For Gray codes in general, it is often useful to know exactly what the list
2

Recall that denotes concatenation of lists and denotes concatenation of strings.

c
uvic01, Frank
Ruskey, 19952001

5.2. THE BINARY REFLECTED GRAY CODE

121

of small changes are that sequence through all objects. From the proof of correctness of
the BRGC we know that its the nth bit that flips between the two recursive calls. This
observation leads us to Algorithm 5.3. Algorithms of the first type we call indirect and those
of the second type are called direct. The generation algorithms of the previous chapter were
of the indirect style, mostly because the changes between successive objects could be large.
However, for combinatorial Gray codes we take the point of view that direct algorithms are
preferable.
Direct Gray Code Algorithm Superiority Principle: Try to develop direct
algorithms whenever possible. It will make your programs more flexible and easier
to analyze.
In Algorithm 5.3, the call flip(n) flips the nth bit and invokes PrintIt. The calling
sequence differs from the indirect case in that PrintIt is called once before calling brgcD(n).
Algorithm brgcD is also CAT; it is called exactly the same number of times on input n that
brgcI(n) and cgrbI are called, namely 2n 1 times. Note that we do not need a separate
reversed procedure cgrbD since it would be identical to brgcD. The procedure works correctly
no matter how the bitstring a is initialized; that is, the call brgcD(n) produces a Gray code
of all strings in n2 .
procedure brgcD ( n : N);
begin
if n > 0 then
brgcD( n 1 ); flip( n ); brgcD( n 1 );
end {of brgcD};
Algorithm 5.3: Direct algorithm for generating the BRGC.

A loopless algorithm: counting without loops


Recall from Section 4.2 that the most straighforward way of counting in binary results in
a CAT algorithm (Algorithm 4.1). That algorithm does a right-to-left scan for a 0, the
position of that 0 being the bit that should flip in the BRGC. Thus we can generate the
BRGC in constant amortized time per bitstring by counting in binary, but occasionally the
length of the scan will be proportional to n.
However, it is even possible to generate the BRGC in such a way that a constant amount
of computation is used in the worst case to go from one bitstring to the next. Such an
algorithm will be developed next. We know that if we count in binary and keep track of the
leftmost position that changes then we can generate the BRGC. But can we count without
doing the right-to-left scan for a zero? The answer is yes. We maintain an array [n 1..0]
which represents bn1 b1 b0 as follows. Define b1 = bn = 0, and for 0 i n 1,

i
if bi = 0 or bi1 = 1
i =
max{k | k = 0 and k > i} if bi = 1 and bi1 = 0.
In other words, i = i unless i is the rightmost 1 of a run of ones, in which case i is the
position of the 0 that immediately precedes the run of 1s. Observe that b may be recovered
from . Below we show 4 successive bitstrings b and the associated values of .
c
uvic01, Frank
Ruskey, 19952001

122

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


b

10

10

1
1
1
1

1
1
1
1

0
0
0
0

1
1
1
1

0
0
0
0

0
0
0
0

0
1
1
1

1
0
0
0

1
0
0
0

1
0
0
1

1
0
1
0

10
10
10
10

11
11
11
11

8
8
8
8

8
8
8
8

6
6
6
6

5
5
5
5

4
5
5
5

3
3
3
3

2
2
2
2

1
1
1
2

4
0
1
0

procedure Next;
local i : N;
begin
i := 0 ;
gi+1 := gi+1 ; {flip the bit}
0 := 0;
i := i+1 ;
i+1 := i + 1;
if 0 = n then done := true;
end {of Next};
Algorithm 5.4: Loop-free procedure Next for generating the BRGC.
The bit that flips in gn gn1 g1 is simply the bit in position 0 . There are four cases
to consider when updating , depending on the values of b0 and bi+1 , where i = 0 . These
cases and the associated updates are shown below.
values of b0
b0 = 1, bi+1
b0 = 1, bi+1
b0 = 0, bi+1
b0 = 0, bi+1

and bi+1
=0
=1
=0
=1

how to update
i :=i + 1; 0 := 0;
i :=i+1 ; i+1 :=i + 1; 0 :=0;
0 :=1;
0 :=1 ; 1 :=1;

It turns out that the four cases can be handled without any if statements as shown in
the remarkably simple Algorithm 5.4.

5.2.1

Applications of the BRGC

There are a suprising number of applications of the BRGC and other related Gray codes
and we mention some of these in this subsection. In particular the BRGC has applications
to solving certain puzzles, to hardware position detection, to satellite transmissions, to multiattribute file storage, to the construction of Brunnian links, and to circuit minimization.
Uniform Gray Codes
One problem with certain applications of the Binary Reflected Gray code in particular, and
generation algorithms based on backtracking in general, is that a small number of elements
do most of the changing (or moving or whatever the basic operation is). In the BRGC the
last bit changes on every other bitstring. In some cases we want a listing where the changes
are more uniformly distributed over all positions (or elements or whatever).
c
uvic01, Frank
Ruskey, 19952001

5.2. THE BINARY REFLECTED GRAY CODE

123

Figure 5.6: A six ring Chinese rings puzzle.










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Spinner











 


Slider

Housing

Figure 5.7: The Spin-out puzzle.

Puzzles
There are a number of puzzles whose solution involves the BRGC. Weve already seen one of
these, the towers of Hanoi. The oldest puzzle solved by the BRGC is known as the Chinese
Rings.
In the Chinese rings puzzle, the object is to remove the U-shaped piece from the rings.
As the puzzle is solved the rings are either on or off the U-shaped piece. If the rings are
successively numbered and the on-ness or off-ness of each rings is recorded as a bit, then
the solution of the puzzle sequences through that portion of the Gray code from 000000 to
111111.
Other puzzles are The Brain (Mag-Nif Inc.) and Spin-out (Binary Arts). The Spinout puzzle is illustrated in Figure 5.7. There are 7 spinners which take on one of two
orientations, either or . The figure illustrates the configuration , from
which two moves are possible. The first is simply to rotate the sixth spinner by 90 degrees to
obtain the configuration ; the second is to move the slider to the left in the
housing and then rotate the seventh spinner to obtain the configuration .
Given an initial configuration of , the puzzle is solved when the configuration is
. In the solved configuration the plastic piece upon which the spinners are
mounted (the slider) can be removed from its housing, thereby serarating the puzzle into
two pieces, as was the goal in the Chinese rings puzzle.
c
uvic01, Frank
Ruskey, 19952001

124

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


0 1 2

(a)

63
Name = A-M
Name = A-F,N-S
Name = A-C,G-I,N-P,T-V
Salary 50,000
Salary < 25,000 or > 75,000
Age < 40?
Cluster

0 1 2

(b)

63
Name = A-M
Name = A-F,N-S
Name = A-C,G-I,N-P,T-V
Salary 50,000
Salary < 25,000 or > 75,000
Age < 40?
Cluster

Figure 5.8: Multiattribute file storage: (a) Arranged according to the BRGC, (b) arranged
according to lex order. Blank area is 0 and shaded area is 1.
Karnaugh maps
A well-known method of minimizing circuits with a small number of gates is through the
use of Karnaugh maps. In such a map the possible inputs are given as the row and column
indices of a two dimensional table. Those indices are listed in Gray code order.
Codon rings
The 64 three letter code words, known as codons, that make up DNA can be encoded by
a base four number system in a natural way. Surprisingly, when the encoding numbers are
sequenced as a 4-ary reflected Gray code the resulting cyclic listing appears to have some
biological meaning. It is well known that DNA is composed of 64 codons, each composed of
a three letter string made up of one of four bases C (Cytosine), A (Adenine), G (Guanine),
U/T (Uracil/Thymine). Map C to 0, A to 3, G to 1, and U/T to 2. With each codon, there
is an associated anti-codon obtained by adding 2 (mod 4) to each digit. For example, the
anti-codon of CAG (012) is GUC (230). The number of hydrogen bonds per codon/anticodon pair is 9 minus the number of even digits in the codon.
Multi-attribute file storage
Imagine a database application in which records are to be stored in secondary storage as
a file and retrieved in response to incoming queries. The file F is a collection of ordered
k-tuples, each consisting of k attributes. Denote the attributes by A1 , A2 , . . . , Ak . In a
partial match query we are given a set of indices I and values vi for each i I. The query
should return all records for which Ai = vi for all i I.
Multiattribute hashing is a popular method of organizing the records. Each record R
has a hash value h(R) associated with it, where h(R) is a bitstring of size n. We try to
arrange the attributes so that no two records have the same hash value. This is generally
impossible, so some method of overflow must be implemented, but we ignore the possiblity
of overflow in the discussion below.
As a specific example suppose that each record contains three fields, Name, Salary, and
Age, and that there are about 64 records. We specify 6 binary attibutes as follows. Three
bits are allocated to Name, allowing 8 subcatagorizations: A-C, D-F, G-I, J-M, N-P, Q-S,
T-V, W-Z. Two bits are allocated to Salary, allowing 4 subcatagorizations: Salary < 25, 000,
25, 000 Salary < 50, 000, 50, 000 Salary < 75, 000, 75, 000 Salary. A single bit is
c
uvic01, Frank
Ruskey, 19952001

5.3. GRAY CODES FOR COMBINATIONS

125

allocated to Age, allowing 2 subcatagorizations: Age < 40 and Age 40.


A query is specified by a string q {0, 1, ?}k . The symbol ? indicates a dont care.
For example, the query ?1?1?? should return all records for which the name is in one of
the ranges A-C, G-I, N-P, or T-Z, and the salary is either less than $25,000 or greater than
$75,000.
Relative to a query q, a cluster is a contiguous collection of records satisfying q, delimited
by records not satisfying q. Assuming the records are stored in a file on a computer disk, a
reasonable measure of the cost of retreiving all records satisfying q is the number of record
clusters. This is because of the way computer disks are constructed reading sequentially
from a disk is fast, whereas finding a particular location on the disk is slow. Thus our aim
in storing the files on disk is to arrange them so that the number of clusters returned in a
typical query is minimized.
In Figure 5.8 we show the layouts for a 6 bit storage scheme in both the BRGC and in
lex order. For the example query ?1?1??, the BRGC organization leads to 4 clusters and the
lex organization leads to 8 clusters. It can be shown that the number of clusters returned
under the Gray code organization is never more than that returned under the lexicographic
organization, and that the lexicographic organization never returns more than twice as
many clusters as the Gray code organization. Thus it is always preferable to use the BRGC
organization.
Brunns Brunnian Link
In knot theory a link is a finite collection of knots. A Brunnian link is a link for which the
removal of any knot causes the link to become trivial; i.e, no remaining pair of knots is linked
and each knot is the unknot. In Brunns paper [43] there is a Brunnian link constructed
that has the same underlying structure as the BRGC. See Figure 5.9 which shows his
construction for n = 6. Note in particular the evident pattern 1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,4,
and the way in which the binary over-ness and under-ness gives rise to the BRGC along the
16 radial axes.

5.3

Gray Codes for Combinations

In this section we develop several Gray codes for combinations; that is, for k-subsets taken
from the set [n] = {1, 2, . . . , n}. For the most part, we identify each subset with a bitstring
of length n with exactly k ones; i.e., with an element of B(n, k) (defined in (4.1). The
algorithms developed can be easily modified to generate the elements of the other common
representation, A(n, k) (recall that A(n, k) is the set of monotonically increasing sequences
of length k with elements taken from [n]; see (4.2)).
There are many closeness conditions (or relations) that can be applied to bitstrings
with a fixed number of ones. In this section we consider five natural closeness relations.
These relations can be defined more generally on any set of strings in which the number of
occurances of each symbol is the same in each string. We therefore define them on the set
of permutations of a multiset.
[Trans] The transposition condition: The two permutations differ in exactly two
positions. The next three conditions are restrictions of this condition. Call the two
positions i and j. Example: 00211 and 10210. In this example the positions that
c
uvic01, Frank
Ruskey, 19952001

126

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

Figure 5.9: Bruns Brunnian link.


change are i = 1 and j = 5, and are underlined, as they will be in each of the example
below.
[H-Trans] The homogenous transposition condition: The permutations, say and
0 differ in exactly two positions i and j, and only symbols less than or equal to
min(i , j ) occur between those two positions. For example, if t = 1, then only 0s
occur between the 1 and 0 that are transposed. Example: 0000112 and 1000012.
[2-Trans] The one or two apart transposition condition: The two permutations differ
in positions that are one or two apart. That is, |i j| 2. Example: 00211 and
01201.
[A-Trans] The adjacent transposition condition: The two permutations differ in exactly two adjacent positions. That is, |i j| = 1. Example: 00211 and 02011.
[2A-Trans] The one or two adjacent transposition condition: The two permutations
differ by one or two adjacent transpositions. Example: 0012122 and 0102212, or 01201
and 02011. In the second example the transpositions are (2 3)(3 4) = (2 3 4).
Note that [A-Trans] is the strongest condition, [Trans] is weaker than [2-Trans] or [HTrans], and [2A-Trans], [2-Trans], and [H-Trans] are incomparable. For combinations, the
[A-Trans] condition cannot always be achieved, but the others can be achieved. There are
many other natural closeness conditions that could be considered; for example, permutations
differing by a 2-cycle or a 3-cycle acting on the positions (this is a restriction of 2A-Trans).
c
uvic01, Frank
Ruskey, 19952001

5.3. GRAY CODES FOR COMBINATIONS

127

[Trans]
Algorithms satisfying the transposition criteria are somtimes said to be revolving door
algorithms. Imagine two rooms A and B connected by a revolving door that operates only
if occupied by two persons, one from each room. As a person leaves one room, they enter
the other, so the door acts to swap two persons. Given k persons in the first room, the
problem is to sequence through all possible possible combinations of persons in the first
room so that each such combination occurs exactly once. This is precisely the same as the
question of generating combinations by transpositions.
The transposition closeness condition is handled in a manner similar to the way subsets
were done. That is, we rely on the classic recurrence relation for binomial coefficients, but
reverse the list corresponding to one of the terms. Thus our list is
n
0
1n
C(n, k) =

C(n 1, k)0 C(n 1, k 1)1

if k = 0,
if k = n,
if 0 < k < n.

(5.4)

Observe that the list defined above has the same form as (5.3), the definition of the
BRGC; the substantive difference is the addition of another parameter, k. Thus, taking the
BRGC list for n and deleting all strings not containing exactly k 1s results in the C(n, k)
list defined above. However, we still need to prove that it has the [Trans] property. Unlike
our proof for the BRGC, here we need to strengthen the inductive statment in order for the
proof to proceed. As will be the case in each such proof in this subsection, the strenghening
will precisely specify the starting and ending bitstrings on each recursively defined list.
Lemma 5.2 The list C(n, k), defined in (5.4), satisfies the following properties.
1. Successive combinations differ by a transposition of two bits; i.e., the list satisfies the
[Trans] condition.
2. f irst(C(n, k)) = 1k 0nk .
3. last(C(n, k)) = 1k1 0nk 1 if k > 0.
4. last(C(n, 0)) = 0n .
Note that the first and last bitstrings differ by a transposition. The following table
illustrates the various cases that can occur and should serve to convince the reader that the
lemma is true. 3
k=1

1<k <n1

k =n1

1000000
..
.

11111000000
..
.

1111110

0000010

11110000010
11100000011
..
.

1111011
..
.

11110000001

1111101

0000001
3

n 1

n 1

k1

Note that the k = n 1 case is subsumed by the 1 < k < n case.

c
uvic01, Frank
Ruskey, 19952001

128

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

The two swapped bits are underlined in the table. Note that they are in positions k 1
and n if 1 < k < n, and are in positions n 1 and n if k = 1. This observation leads us to
the direct implementation of Algorithm 5.5. The call gen(n,k) generates C(n, k) and the
call neg(n,k) generates C(n, k). If only a list of positions of transposed bits are desired,
then have swap(i,j) print the pair (i, j). If the bitstrings of C(n, k) are desired, then
swap(i,j) will swap the bits of b1 b2 bn that are in positions i and j before printing; also,
b must be initialized to 1k 0nk and printed before the call to gen. To produce the elements
of A(n, k), the call swap(n,n-1) in procedure gen should replace ak = n 1 with ak = n,
and the call swap(n,k-1) should replace ak1 = k 1 with n 1 and replace ak = n 1
with n; in neg these actions are reversed.
procedure gen ( n, k : N);
begin
if 0 < k and k < n then
gen( n 1, k );
if k = 1 then swap( n, n 1 ) else swap( n, k 1 );
neg( n 1, k 1 );
end {of gen};
procedure neg ( n, k : N);
begin
if 0 < k and k < n then
gen( n 1, k 1 );
if k = 1 then swap( n, n 1 ) else swap( n, k 1 );
neg( n 1, k );
end {of neg};
Algorithm 5.5: Direct transposition generation of B(n, k).
What is the running time of gen(n, k) of Algorithm 5.5? In answering this question
we begin to appreciate the potential beauty and power of Gray code algorithms. Only a
constant amount of computation is done at each recursive call. Every recursive call spawns
two others (or none), so the computation tree is an extended binary tree. Exactly one new
combination
is generated for each recursive call that is not a leaf. Thus the number of leaves

is nk + 1, one more than
n the number of internal nodes. We conclude that the total amount
of computation is O( k ); the algorithm is CAT.
To close our discussion of this [Trans] Gray code we note an interesting pattern which it
posesses, and use this pattern to develop a loopfree algorithm. Consider the corresponding
elements of A(n, k) that are generated, as illustrated in column 1 of Figure 5.10. We claim
that the value of the element ak always increases, that, for fixed ak , the value of ak1 always
decreases, and that in general, for fixed values of akj , . . . , ak , the value of akj1 always
either decreases or increases, depending on whether j is even or odd.
That this is true follows from a similar property of the BRGC. Below is a left-to-right
listing of the BRCG with set elements listed explicitly, bottom-to-top and in increasing
order. Note that along the rows, for fixed values below them, the order of elements is
alternately increasing () and decreasing ().
c
uvic01, Frank
Ruskey, 19952001

5.3. GRAY CODES FOR COMBINATIONS

129

.. . . . . . . . . . . . . . . . . . . .1. . . . . . . . . .
. . . . . . . . . . 1 . . . . . . . 1 . 221 . . . 1 . . . . .
. . . . . 1 . . . 122 . 1 . . . 1223333 . 122 . 1 . .
. . 1 . 221 . 3333221 . 444444443333221 .
.1223333444444445555555555555555

Call the n = 5 listing shown above L. Now imagine the construction of the list for n = 6.
It is obtained by taking a copy of L with an additional row of 32 periods on top, followed
by a copy of L with a row of 32 6s on bottom. The alternating property is thus preserved
inductively. Of course, the property holds for any sublist; in particular, for the subslists
consisting of subsets of fixed size, or falling within a fixed range.
This alternating lexicographic nature of the A(n, k) list leads us to the recursive Algorithm 5.6. To generate the elements of A(n, k), set ak+1 to be n + 1 and call gen( n ). It
is fairly straightforward to transform this into an iterative algorithm.
procedure gen ( n : N);
begin
if n = 0 then PrintIt else
if n is odd then
for j := n to an+1 1 do
an := j; gen( n 1 );
else
for a[n+1]-1 downto n do
an := j; gen( n 1 );
end {of gen};
Algorithm 5.6: A surprising indirect algorithm for generating A(n, k) by transpositions.
By a careful simulation of the implicit recursion stack it is possible to obtain a loop-free
algorithm ([32]), but a more direct approach is available. Let us examine the list A(n, k)
more closely. For a given combination, define p to be the minimum index for which ap > p.
The value of p changes by at most one from a combination to its successor.
If p = 1 and k is even then a1 should be decreased by one; if a1 = 1 after this decrease,
then p becomes p + 1, otherwise it remains unchanged. Otherwise, if k and p > 1 have
opposite parity, then ap2 and ap2 both increase by one and p becomes the maximum of 1
and p 2. Finally, if k and p have the same parity, then there are two cases depending on
the value of ap+1 . If ap+1 = ap + 1, then it decreases by one and ap becomes p. The new
value of p is one or two greater, depending on whether ap+1 > p + 1 or not. If ap+1 > ap + 1,
then ap1 becomes ap , ap is incremented, and p becomes the maximum of 1 and p 1.
Algorithm 5.7 is a loop-free procedure Next that transforms one combination to the
next. Instead of maintaining p, it proves simpler to maintain a variable j whose value is p if
it has the same parity as k, and whose value is p 1 if it has parity different from k. Array
a[1..k+1] is initialized to 1, 2, . . . , k, n + 1 and j is initialized to k. Then PrintIt and
Next are executed in a repeat loop with terminating condition a[k+1] < n+1. Note that
Next makes no reference to n or k; their only role is in the initialization and termination.
[H-Trans]
An early paper [96] described the problem as follows. Imagine a piano player who wishes
to play all chords, with k keys pressed, one after the other and with no repeats, on a piano
c
uvic01, Frank
Ruskey, 19952001

130

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


procedure Next; {j global and modified}
begin
if j < 1 then
a1 := a1 1;
if a1 = 1 then j := j + 2;
else
if a[j + 1] = a[j] + 1 then
a[j + 1] := a[j]; a[j] := j;
if aj+1 = aj + 1 then j := j + 2;
else
aj := aj + 1;
if j > 1 then
aj1 := aj 1;
j := j 2;
end {of Next};

Algorithm 5.7: Loopfree algorithm for generating combinations by transpositions.


with n contiguous keys. Furthermore, in going from one chord to the next, exactly one
finger is allowed to change position. The problem just described is solved by generating
all k combinations of an n set by transpositions. But now we impose the further natural
restriction that fingers never cross. This is exactly the problem of generating combinations
via homogenous transpositions.
term
n1The
homogenous transposition closeness condition is handled by expanding
n2 then2

of
the
classic
recurrence
relation
for
binomial
coefficients
(2.2)
into
+
k1
k1
k2 .
The following list was proposed by Eades and McKay [96] (but see Section 5.7).
n
0

10n1 010n2 0n1 1


E(n, k) =

E(n1, k)0 E(n2, k1)01 E(n2, k2)11

n
1

if
if
if
if

k=0
k=1
1<k<n
k=n

(5.5)

This list has a number of interesting properties which are embodied in the next lemma.
Lemma 5.3 The list E(n, k), defined in (5.5), satisfies the following properties.
1. Successive combinations differ by a transposition of two bits and all bits between those
that are transposed are zeroes; i.e., the transpositions are homogeneous the list
satisfies the [H-Trans] condition.
2. f irst(E(n, k)) = 1k 0nk .
3. last(E(n, k)) = 0nk 1k .
c
uvic01, Frank
Ruskey, 19952001

5.3. GRAY CODES FOR COMBINATIONS

131

The lemma may be proven by considering the list shown below for the 1 < k < n cases.
111k2 0nk1 0
..
.

E(n 1, k)0

0nk1 1k2 110


0nk1 1k2 101

..
E(n 2, k 1)01
.

k2
nk1
1 10
01
k2
nk1
1 00
11

..
E(n 2, k 2)11
.

nk1
k2
00
1 11
This list shows that the change at the first interface transposes the bits in positions
n 1 and n; the change at the second interface transposes the bits in positions k 1 and
n 1. We are thus led to the direct algorithm gen shown in Figure 5.8. Note that the first
and last bitstrings do not differ by a transposition.
procedure gen ( n, k : N);
begin
if 1 < k and k < n then
gen( n 1, k ); swap( n 1, n );
neg( n 2, k 1 ); swap( k 1, n 1 );
gen( n 2, k 2 );
else
if k = 1 then
for i := 1 to n 1 do swap( i, i + 1 ); {*}
end {of gen};
Algorithm 5.8: Direct generation of combinations by homogenous transpositions.
There is a corresponding procedure neg which simply executes the statements of gen in
reverse order and reverses the roles of the two procedures. Of course, after initializing the
string to 1k 0nk , the initial call is gen(n, k). The algorithm is CAT since (a) every iteration
of the for loop at line {*} produces another combination and (b) when the for loop is not
executed, three recursive calls are made and thus the underlying computation tree has more
leaves than internal nodes. The algorithm is quite efficient in practice if swap is defined as
a macro. The simple for loop, which is oft executed, and can be optimized, accounts for
this efficiency.
[2-Trans]
The one or two apart transposition closeness condition can be handled by the same listreversing technique that we used previously, but we need to add an additonal parameter,
which takes on one of two values. For p = 1, 2, let R(n, k, p) denote a listing of the bitstrings
in B(n, k) that starts at 0nkp 1k 0p and ends at 0nk 1k and which satisfies the [2-Trans]
condition. Of course, the list R(n, k, p) exists only if n k p. These lists may be defined
c
uvic01, Frank
Ruskey, 19952001

132

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

recursively as shown below.


n
0

1n1 0 R(n 1, k 1, 1)1


R(n, k, p) =

R(n 1, k, 1)0 R(n 1, k 1, 2)1

R(n 1, k, 1)0 R(n 1, k 1, 1)1

if
if
if
if

k=0
p = 1 and k = n 1
p = 1 and 0 < k < n 1
p = 2 and 0 < k < n 1

The list for p = 1 and k = n 1 could also be expressed as 1n1 0 1n2 01 01n1 ;
this is what causes the for loop in Algorithm 5.9. That this listing is correct follows from
consideration of the following table.
p=1
0nk2 01k1 1

p=2
0

0nk2 1k1 10

..
.
0nk2 1k1 10 0
0nk2 1k1 00 1
..
.

..
.
0nk2 01k1 1 0
0nk2 01k1 0 1
..
.

0nk2 001k1 1

0nk2 001k1 1

n 1

n 1

k1

Note that the transposition is in positions n 2 and n if p = 1 and is in positions n 1


and n if p = 2. This discussion leads directly to the Pascal procedure gen of Algorithm 5.9.
Also note that both transpositions are homogeneous and that the first and last bitstrings
do not differ by a homogeneous transposition.
procedure gen ( n, k, p : N);
begin
if p = 1 then
if k = n 1 then
for i := n 1 downto 1 do swap( i, i + 1 );
else
if 0 < k and k < n 1 then
neg( n 1, k, 1 ); swap( n 2, n ); gen( n 1, k 1, 0 );
else {p = 2}
if 0 < k and k < n 1 then
gen( n 1, k, 1 ); swap( n 1, n ); gen( n 1, k 1, 1 );
end {of gen};
Algorithm 5.9: Direct [2-Trans] generation of Combinations.
Figure 5.10 shows the lists arising from each of the algorithms presented in the last three
subsections as well as from the [A-Trans] subsection, which is to be found below.
[2A-Trans]
This closeness condition will be dealt with in Section 5.10 on linear extensions of posets,
where it forms a special case of a more general setting.
c
uvic01, Frank
Ruskey, 19952001

5.3. GRAY CODES FOR COMBINATIONS


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

2
3
3
2
4
4
4
3
3
2
5
5
5
5
4
4
4
3
3
2

3
4
4
4
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6

1
1
0
1
1
0
0
1
0
1
1
0
0
0
1
0
0
1
0
1

1
0
1
1
0
1
0
0
1
1
0
1
0
0
0
1
0
0
1
1

1
1
1
0
0
0
1
1
1
0
0
0
1
0
0
0
1
1
1
0

0
1
1
1
1
1
1
0
0
0
0
0
0
1
1
1
1
0
0
0

0
0
0
0
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0

0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1

1
1
1
0
0
1
1
1
0
0
0
1
0
0
1
1
1
0
0
0

1
1
0
1
1
0
1
0
1
0
0
0
1
1
0
1
0
1
0
0

133
1
0
1
1
1
1
0
0
0
1
1
0
0
1
1
0
0
0
1
0

0
1
1
1
0
0
0
1
1
1
1
1
1
0
0
0
0
0
0
1

0
0
0
0
1
1
1
1
1
1
0
0
0
0
0
0
1
1
1
1

0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1

0
1
0
0
1
1
1
1
1
0
0
1
1
1
0
0
0
1
0
0

0
0
1
1
0
1
1
1
0
1
1
0
1
0
1
0
0
0
1
0

1
0
0
1
1
0
1
0
1
1
1
1
0
0
0
1
1
0
0
0

1
1
1
0
0
0
0
1
1
1
0
0
0
1
1
1
0
0
0
1

1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
1
1
1
1

0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1

Figure 5.10: [Trans],[H-Trans],[2-Trans](R(6, 3, 1)).

0011
s

1001
s
@

@
@s
1010

s
0101@

1100
s

@
@s
0110

Figure 5.11: Non-Hamiltonian Adjacent Transposition graph.


[A-Trans]
The adjacent transposition closeness relation is that the two bitstrings differ by the interchange of two adjacent bits; that is, a 01 becomes a 10, or vice-versa. In this case it is
not always possible to have a Gray code. One exists if and only if n is even and k is odd.
Consider the case of n = 4 and k = 2. The underlying graph, which clearly has no Hamilton
path, is shown in Figure 5.3; in general this graph is denoted G(n, k).
The graph G(n, k) is bipartite. The vertices may be partitioned into two sets Even(n, k)
and Odd (n, k) depending upon the parity of their distance from the vertex 0nk 1k . Clearly,
an adjacent transposition changes the parity of a bitstring. Now consider the parity difference, D(n, k), which is defined to be Even(n, k) Odd (n, k). We determine this quantity
by using an involution on B(n, k). Define (b1 b2 bn ) to be
b1 b2 b2i2 b2i b2i1 b2i+1 bn ,
where i is the smallest index for which b2i1 6= b2i ; if there is no such index then the bitstring
c
uvic01, Frank
Ruskey, 19952001

134

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

is unmodified it is a fixed point. If the number of 0s and 1s are both odd, then there are
no fixed points. Otherwise the fixed points consist of a string of 00 and 11 pairs, followed
by a 1 or a 0 if n is odd. Furthermore, all of these fixed points have even parity since it
takes 4 adjacent transpositions to transform 1100 into 0011 (and 2 to transform 110 into
011). Thus the number of fixed points is equal to

0 if n even and k odd


bn/2c
D(n, k) =
if n odd or k even.

bk/2c
Hence D(n, k) 1 if and only if n is even and k is odd, or k = 0, 1, n 1, n.
Theorem 5.1 If n is even and k is odd, then there is a Hamilton path in G(n, k) that starts
at 1k 0nk and ends at 0nk 1k .
Proof: Assume that n is even and k is odd. The proof proceeds by induction on n and
k. The theorem is clearly true when k = 1 or k = n 1 (by moving the single 1 or 0 from
left-to-right through the remaining bits).
Let 1 < k < n. The proof is based on the recurrence relation given below, obtained by
iterating (2.2).

n
n2
n2
n2
=
+2
+
k
k2
k1
k

First, all n2
with prefix 11 are generated, followed by those beginning 01
k2 bitstrings
n2

or 10 (of which there are 2 k1 , followed by the n2
with prefix 00. The list starts with
k
k
nk
nk
k
the bitstring 1 0
and ends with the bitstring 0
1 . Inductively, those beginning 11 or
00 can be generated. Those beginning 11 are listed from 111k2 0nk to 110nk 1k2 , and
those beginning 00 are listed from 001k 0nk2 to 000nk2 1k . The complicated part of the
proof is in listing those bitstrings that begin 01 or 10. A special type of tree that is used in
this part of the proof is defined below.
Definition 5.1 A comb is a tree where each vertex has degree at most three and all vertices
of degree three lie along a single path which is called the spine of the comb. The paths that
are attached to the spine are called teeth.

4
Let us consider the specific case of n = 8 and k = 5. There are n4
=
k2
3 = 4
bitstrings with prefix 1010, and similarly there are 4 with each prefix 1001, or 0110, or
0101.
the four suffixes are 1110, 1101, 1011, and 0111. Denote the list of m
n4Inductively,

= k2 bitstrings prepended with 10 by p1 , p2 , . . . , pm , and when prepended with 01 by


q1 , q2 , . . . , qm . In our example, the p list is 101110, 101101, 101011, 100111, and the q list
is 011110, 011101, 011011, 010111. Note that
q1 , p1 , p2 , q2 , q3 , . . . , pm1 , pm , qm
is a path in G(n2, k1). This path is the spine of the comb. The bitstrings of B(n2, k1)
that begin 00 or 11 are attached as the teeth of the comb; those that begin 00 are attached
to q vertices, and those that begin 11 are attached to p vertices. The tooth attached to
a q vertex is obtained by moving its leftmost 1 to the right until it encounters another 1.
The tooth attached to a p vertex is obtained by moving the leftmost 0 to the right until it
c
uvic01, Frank
Ruskey, 19952001

5.4. PERMUTATIONS
q1

135
p1

p2

q2

q3

p3

p4

q4

-s
s -s
s -s
s
s

3



3

6


 -

s
s -s
s

+
s

s -s -
s
6 ?
6 ?
s
s
s
s
6


3




+
s

?
s
?
s

6 ?
s
s

3



s
s

?
6 ?
s


+

s

Figure 5.12: Path in the prism of combs for n = 8 and k = 5.

56
54

55

111100

53

111010
111001

49

50

51

52

110110
110101

45

46

47

48

110011

44

001111

43

011110
101110
101101
011101
011011
101011
100111
010111

39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
16
15
11

12

13

14

An efficient implementation of this proof a little tricky. See Hough and Ruskey [180] for
a CAT implementation.

17

18

19

There are two combs for C(n, k) depending on whether the bitstring starts 01 or 10. In
other words, we have the product graph of the comb and an edge. Let us call the comb with
prefix 10 the upper comb and the one with prefix 01 the lower comb. It is a simple matter
to find a Hamilton path in the two combs that starts at the upper vertex 10pm and ends
at the lower vertex 01q1 . Since pm = 10nk1 1k1 and q1 = 01k1 0nk2 the proof will
be finished. The Hamilton path in prism of combs for our example is illustrated in Figure
5.12.
When viewed along the spines the path sequence starts 10pm , 10qm , 01qm , 01pm . Thereafter, the patterns 01pi , 10pi , 10qi , 01qi , and 01qi , 01pi , 10pi , 10qi alternate as i decreases
from m 1 to 1. Of course, the teeth have to be generated along the way as well. This
finishes the proof.
2

40

41

42

q1
p1
p2
q2
q3
p3
p4
q4

57

58

59

60

encounters another 0. The following table lists the vertices of the spine for our example as
the leftmost column, and the bitstrings to the right are the teeth of the comb.

10

Permutations

c
uvic01, Frank
Ruskey, 19952001

5
4
3
2
1

The most natural closeness condition for permutations is that they differ by a transposition
of two elements. There are many algorithms for generating permutations by transpositions.
In this section we discuss two such algorithms.
The first algorithm, Algorithm 5.10, is very short and is recursive. It assumes that the
array is initialized to some permutation of 1, 2, . . . , n. The initial call is Permute(n).

5.4

136

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


procedure Permute ( m : integer );
var i : integer;
begin
if m = 0 then PrintIt;
for i := 1 to m do
i :=: m ;
Permute( m 1 );
i :=: m ;
end {of Permute};
Algorithm 5.10: Recursive permutation algorithm.

The number of transpositions use by this algorithm is the subject of Exercise 20, where
it is found to use more transpositions than the lexicographic algorithm, Algorithm 4.11 of
the previous chapter. However, it is a CAT algorithm.
If we further insist that the transposition be of elements in adjacent positions (just like
[A-Trans] in the previous section) then the problem is elegantly solved by the SteinhausJohnson-Trotter (SJT) algorithm ([405], [198], [418]).
The basic idea is a recursive sublist expansion. The largest symbol n sweeps from right
to left and then left to right, etc, through each of the (n 1)! permutations of 1, 2, . . . , n 1.
When the position of n becomes extreme (either at the right or left), then recursively an
adjacent transposition is done between two permutations in the list of permutations of
[n 1]. If this idea is used with n = 3,4, then we obtain the following list of permutations
(read down).
123

312

231

1234
1243
1423
4123

3124
3142
3412
4312

2314
2341
2431
4231

132

321

213

4132
1432
1342
1324

4321
3421
3241
3214

4213
2413
2143
2134

A recursive algorithm based on these ideas appears as Algorithm 5.11. Initially we set
= 1 = id and dir[i] := 1 for i = 1, 2, . . . , N . Variable dir[i] indicates whether element
i is currently moving right (+1) or left (1). The intial call is Perm(1), which generates
all permutations of [N ]. The algorithm is CAT because, except at the root, the degree
of each node in the computation has degree greater than one, and a constant amount of
computation is done at each node.
We now construct an iterative version Next of the recursive Algorithm 5.11 described
above. As before, each element, i, of the permutation will have a direction, dir[i], associated
with it. Initially, permutations (pi[1..n]) and 1 are set to be the identity permutation,
and all directions are 1, to the left. Call an element mobile if it is larger than the element
adjacent to it in the appropriate direction. The algorithm simply transposes the largest
mobile integer, call it m, with the element to the left or right of it according to the direction
dir[m]. To stop n from moving beyond the array bounds we set 0 and n+1 to both be
c
uvic01, Frank
Ruskey, 19952001

5.4. PERMUTATIONS

137

procedure Move( x, d : N);


local j : N;
begin
j := 1 [x]; [j] := [j + d]; [j + d] := x;
1 [x] := j + d; 1 [[j]] := j;
end {of Move};
procedure Perm ( n : N);
local i : N;
begin
if n > N then PrintIt
else
Perm( n+1 );
for i := 1 to n 1 do
Move( n, dir[n] ); Perm( n + 1 );
dir[n] := dir[n];
end {of Perm};
Algorithm 5.11: Recursive Pascal implementation of the SJT Algorithm.
n + 1. The directions of all integers in [N ] larger than m are changed and we are ready for
the next iteration. These ideas are implemented as Algorithm 5.12.
procedure Next;
{Assumes that 0 = n+1 = n + 1.}
begin
m := n;
while [ 1 [m] + d[m]] > m do
{Find largest mobile integer}
d[m] := d[m]; m := m 1;
[ 1 [m]] :=: [ 1 [m] + d[m]]; {update }
1 [[ 1 [m]]] :=: 1 [m];
{update 1 }
end {of Next};
Algorithm 5.12: Iterative Next implementation of the SJT algorithm (assumes 0 =
n+1 = n + 1).
We can place Next in a repeat loop with m = 0 as the terminating condition, as long as
1
the initial values of 1 = 1
= 1 are set. The loop of Algorithm 5.12 can be eliminated
by introducing another array 1 , . . . , n+1 , where j is the largest element smaller than j
which is mobile. The development of this loopless algorithm is the subject of Exercise 21.
It is easy to derive a ranking algorithm for the SJT algorithm based on the following
recurrence relation, where k is the position of n in (i.e., k = 1 (n)).

n k if Rank( n) is even
Rank() = n Rank( n) +
k 1 if Rank( n) is odd
For example, Rank(1324) = 4 Rank(132) + 4 1 = 7, since Rank(12) = 0 and so
Rank(132) = 3 Rank(12) + 3 2 = 1. A explicit Pascal implementation is given as
Algorithm 5.13. The algorithm uses (n2 ) arithmetic operations in the worst case and is
thus slower than the ranking algorithm for lexicographic order.
c
uvic01, Frank
Ruskey, 19952001

138

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


function rank ( n : N) : N;
local j, k, r : N;
begin
if n = 1 then return( ( ) 0 );
j := 1; k := 1;
{Determine k}
while [j] 6= n do
if [j] < n then k := k + 1;
j := j + 1;
r := rank( n 1 );
{Apply recursive formula}
if r is odd
then rank := n r + k 1
else rank := n r + n k;
end {of rank};
Algorithm 5.13: Ranking algorithm for SJT Algorithm.

Algorithm 5.14 unranks permutations in the SJT order. It sets both and the directions
dir[i] and is thus complete with respect to Next. It also uses (n2 ) arithmetic operations
in the worst case.
procedure UnrankSJT ( r : N);
local j, k, rem, c : N;
begin
for j := 1 to n do [j] := 0;
for j := n downto 1 do
rem := r mod j;
r := r/j;
if r is odd then
k := 0; dir[j] := +1;
else
k := n + 1; dir[j] := 1;
c := 1;
repeat k := k + dir[j];
if [k] = 0 then c := c + 1;
until c = rem;
[k] := j;
end {of UnrankSJT};
Algorithm 5.14: Unranking algorithm for SJT algorithm.

5.5

Gray Codes for Binary Trees

The two most common representations of binary trees are as well-formed parentheses strings
and the usual computer implementation as a collection of nodes, each consisting of two
pointers. We will develop Gray codes for each of these representations.
c
uvic01, Frank
Ruskey, 19952001

5.5. GRAY CODES FOR BINARY TREES

5.5.1

139

Well-formed Parentheses

Let T(n, k) denote the set of all bitstrings in T(n) with prefix 1k 0. These bitstrings correspond to binary trees with leftmost leaf at level k. If 1k 0s is an element of T(n, k) then the
1s in s will be called the free 1s (since their position within the bitstrings can vary). It can
be shown that there is no adjacent transposition ([A-Trans] ) Gray code for T(n, k) for even
n or for odd n > 5 and odd k (except when k = 0, 1, n 1, or n). A reason for considering
a Gray code for T(n, k) is that a Gray code for T(n + 1, 1) can be trivially transformed into
a Gray code for T(n) by ignoring the 10 prefix of every bitstring in T(n + 1, 1).
By the proof of (4.17) given in the previous chapter, the following expression gives a
recursive way of listing all the elements of T(n, k).

if k = 1
f lip(T(n, 2))
T(n, k) =
(5.6)
f lip(T(n, k + 1)) insert(T(n 1, k 1)) if 1 < k < n
n n
1 0
if k = n
In a slight abuse of notation, we let T(n, k) stand for both the set of bitstrings and the list
of bitstrings produced by (5.6). By the remarks above, the corresponding recurrence with
T replaced by T, and vice-versa, also holds. The reason for reversing the sublist in (5.6) is,
of course, to produce a Gray code list as per the following theorem.
Theorem 5.2 For any values of n and k, where 1 k n, the list T(n, k), as defined in
(5.6), is a Gray code list satisfying the [Trans] condition.
Proof: We will prove the theorem by mathematical induction on increasing values of n
and decreasing values of k with the hypothesis, P (n, k), that T(n, k), for 1 k n, is a
Gray code, and that (5.7) and (5.8) hold.

if k = 1
101100(10)n3
k
k
nk1
1 010 (10)
if 1 < k < n
f irst(T(n, k)) =
(5.7)
n n
1 0
if k = n
last(T(n, k)) = 1k 0k (10)nk for 1 k n

(5.8)

By definition, P (n, k) is true for n = 1 and for k = n. We have to show that if P (m, l)
holds for all m < n, and for all l > k when m = n then this implies P (n, k). Clearly, the
operations f irst and last commute with f lip and insert.
To show (5.7) observe for 1 < k < n that
f irst(T(n, k)) = f irst(f lip(T(n, k + 1)))
= f lip(last(T(n, k + 1)))
= f lip(1k+1 0k+1 (10)nk1 )
= 1k 010k (10)nk1
And for k = 1
f irst(T(n, 1)) = f lip(f irst(T(n, 2)))
= f lip(110100(10)n3 )
= 101100(10)n3
c
uvic01, Frank
Ruskey, 19952001

140

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


To show (5.8) observe for 1 k n that
last(T(n, k)) = insert(last(T(n 1, k 1)))
= insert(1k1 0k1 (10)nk )
= 1k 0k (10)nk

That T(n, k) is a Gray code will follow from the inductive assumption and from the
proper interface between the last bitstring of the first sublist last(f lip(T(n, k + 1))) and
the first bitstring of the second sublist f irst(insert(T(n 1, k 1))). If k = 1 then there
is no interface to consider, and for 2 < k < n 1,
last(f lip(T(n, k + 1))) = f lip(f irst(T(n, k + 1)))
= f lip(1k+1 010k+1 (10)nk11 )
= 1k 0110k+1 (10)nk2
= 1k 0110k1 00(10)nk2

and
f irst(insert(T(n 1, k 1))) = insert(f irst(T(n 1, k 1)))
= insert(1k1 010k1 (10)nk1 )
= 1k 0010k1 (10)nk1
= 1k 0010k1 10(10)nk2

n4
If k = 2, last(f lip(T(n, k + 1))) = 11011000(10)
and f irst(insert(T(n 1, k 1))) =

11001100(10)n4 . If k = n 1 then last(f lip(T(n, n)) = f lip(1n 0n ) = 1n1 0100n2 and

f irst(insert(T(n 1, k 2)) = 1n1 0010n2 . In both cases, as indicated by the underlined

bits, the two bitstrings differ on exactly two positions.


2
The recursive algorithm producing the list T(n, k) is simple enough to be given explicitly
as Algorithm 5.15. The global array x is initialized with 1k 02nk and the initial call is
T(n,k,k+2,true). The parameter pos keeps track of where the free 1s are to be positioned.
procedure T ( n, k, pos : N; dir : boolean );
begin
if k = 0 or k = n then PrintTree else
if k = 1 then
x[pos] := 1; T( n, k + 1, pos + 1, dir ); x[pos] := 0;
else
if dir then
x[pos] := 1; T( n, k + 1, pos + 1, not dir ); x[pos] := 0;
T( n 1, k 1, pos + 1, dir );
else
T( n 1, k 1, pos + 1, dir );
x[pos] := 1; T( n, k + 1, pos + 1, not dir ); x[pos] := 0;
end {of T};
Algorithm 5.15: Indirect Gray code algorithm for well-formed parentheses.
c
uvic01, Frank
Ruskey, 19952001

5.5. GRAY CODES FOR BINARY TREES

141

Let t(n, k) denote the number of calls to procedure T of Algorithm 5.15 with parameters
n and k. Since on every level of recursive calls, procedure T requires constant amount of
computation, the time complexity of the algorithm is proportional to t(n, k). The numbers
t(n, k) satisfy the recurrence relation given below.

if k = 1
1 + t(n, 2)
1 + t(n, k + 1) + t(n 1, k 1) if 1 < k < n
t(n, k) =

1
if k = n
It can be shown by an easy induction that t(n, k) 3T (n, k) 2 for 1 < k n and
t(n, 1) 3T (n, 1) 1 (see Lemma 4.4). This proves that t(n, k) is proportional to T (n, k)
and thus there are constant (on the average) changes necessary to produce the next string
in the list.
procedure gen ( n, k, p : N);
begin
if k = 1 then gen( n, 2, p ) else
if 1 < k and k < n then
neg( n, k + 1, p );
if k = n 1 then swap( n + 1 + p, n + 2 + p ) else
if k = 2 then swap( 4 + p, 6 + p )
else swap( k + 2 + p, 2k + 3 + p );
gen( n 1, k 1, p + 2 );
end {of gen};
Algorithm 5.16: Direct Gray code algorithm for well-formed parentheses.
With the proof of Theorem 5.2, it is not difficult to derive a direct algorithm as well,
and one is presented as Algorithm 5.16. The initial call is gen(n,k,0). Parameter p keeps
track of how far into the sequence we have proceeded as a result of recursive calls. The
array holding the well-formed parentheses string must be initialized according to (5.7). This
algorithm is also a CAT algorithm even though the call when k = 1 causes no call to swap.
This is because that call results in a call with k = 2, which does call swap.
Note that the bitstrings last(T(n, k)) and f irst(T(n, k1)) are adjacent, as are last(T(n, 2))
and last(T(n, 1)). Thus the following is a Gray code list for T(n).
T(n) = T(n, n) T(n, n 1) T(n, 2) T(n, 1)
This list starts with bitstring 1n 0n and ends with 101100(10)n3 . Note that these bitstrings
cannot be obtained from one another by a transposition.
As noted before, in the case of k = 1, the algorithm for T(n+1, 1) gives rise to yet another
Gray code listing of T(n) by ignoring the initial 10 prefix of the bitstrings in T(n + 1, 1).
In this case the list starts with the bitstring 1100(10)n2 and ends with 1010(10)n2 ; these
bitstrings are adjacent and hence a different list is produced from the one described in the
preceeding paragraph. Thus we obtain a Hamilton cycle in the underlying graph.
Both of these lists are different from the list in [316]. Since the algorithm for listing
T(n, k) runs in constant amortized time, the algorithms for listing T(n) also run in constant
amortized time.
c
uvic01, Frank
Ruskey, 19952001

142

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

5.5.2

Binary Trees

A rotation in a binary tree is the familiar operation used in maintaining balanced binary
search trees (e.g., Knuth [219]). Figure 5.13 shows a left and a right rotation at the node x.
Note the asymmetry with respect to x. We also speak of these two rotations as rotations of
the edge (w, x). We will develop an algorithm for generating all binary trees so that each
successive tree differs from its predecessor by a single left or right rotation. Note that a
rotation does not change the inorder numbering of the nodes.

xl
wl

J
AJ
J

wl

@
J
@
CJ
@
J
J
B J

right
=
left
=

@@

xl
J
@
AJ
@

J
J
J
B J
CJ

Figure 5.13: A left or right rotation at node x.


Our approach to the problem uses the paradigm of attaching alternating directions
to elements or positions in the object in order to obtain Gray codes of combinatorial
objects. The algorithm does not require the maintenance of any sequence representation
of the binary tree and can be implemented to run in constant time per tree on a pointer
machine, neglecting the time needed to actually print the tree.
The rotation graph Gn has vertex set consisting of all binary trees with n nodes. Two
vertices are connected by an edge if a single left or right rotation will transform one tree
into the other.
A Hamilton path of Gn is generated by using a recursive strategy. We show how to
construct a Hamilton path in Gn from a Hamilton path in Gn1 .
The proof uses the idea of an induced subtree of a binary tree. Let T be a binary tree of
size n. We define T [i..j] to be the tree formed by the nodes i, i + 1,. . . ,j, where the parent
of k, for i k j, in T [i..j] is defined to be the node l, for i l j, such that l is the
nearest proper ancestor of k in T . This is simply the tree formed by contracting any edge
in T whose endpoints are not both in the range [i..j]. See Figure 5.14.
Consider the effect of a rotation of the edge (x, y) in T on the tree T [i..j]. If both x and
y are in the range [i..j] then this rotation has the effect of rotating the same edge in T [i..j],
otherwise T [i..j] is unchanged. Notice that in the example of Figure 5.14 it is possible to
rotate the edge (x, y) in T [i..j] but not in T , because in T the nodes u and v are in the
way.
In the remainder of this section we only use induced subtrees with i = 1.
Given T [1..n 1], the only possible way to form a corresponding tree T is to place node
n as the root of T [1..n 1], as the right child of n 1 in T [1..n 1], or between any two
nodes along the right path of T [1..n 1]. See Figure 5.15.
From this observation we can derive the following result.
Theorem 5.3 The rotation graph Gn contains a Hamilton path.
c
uvic01, Frank
Ruskey, 19952001

5.5. GRAY CODES FOR BINARY TREES

143

d
Q

Q
Q
t

Q
T
A

Q
Qt
t
@
@

y t
@
@t
@

@
A A
@

@t
u d
@
@

@
A
A

@
@
@
v
@d
d

A
A

@
@
@ tx
d

@
@
A
A
@

i
@
@
t
d
@
A
A
@
@
@

t
@
@
A
A
@
A

@ tj

@
A

@
A
A
t
Q

T [i..j]
Q

Q
t
Qt
@
@

yt
@
@t
@

@
A A

@
@t
x
@
t
@

@
A
A

@
@
i t
@
@

@t
@
@
A
A

@
@

@tj

A
A

Figure 5.14: Example of induced tree.

c
uvic01, Frank
Ruskey, 19952001

144

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


.
t
@

@t
.
@

@t

.
.
@
A

.
@t
@
A

@t
.
n1
A

(a) T [1..n 1]

t
@

@t
@

@t
@

@t
n
A

t
@

@t
A
n1

(b) T

Figure 5.15: (a) Given T [1..n 1] there are 6 places where n could possibly be inserted to
get T . (b) The n has been inserted at the position of the double arrow.
Proof: The proof is by induction on n. Clearly there exists a Hamilton path in G1 since
there is only one binary tree with a single node.
Let n be any integer greater than 1. Inductively, there exists a sequence Pn1 =
T0 , T1 , . . . , Tk of all trees of size n 1 that forms a Hamilton path in Gn1 . Using Pn1 ,
we construct a sequence Pn of the trees of size n that forms a Hamilton path in Gn . The
initial tree in this sequence is formed by placing node n as the right child of n 1 in T0 . We
then perform precisely the same sequence of rotations used to generate Pn1 , but before
performing each rotation of Pn1 we first rotate n as follows:
1. If n is not the root, then repeatedly rotate the edge (parent(n), n) until n is the root.
These are left rotations at n.
2. If n is the root, then repeatedly rotate the edge (lef tchild(n), n) until n has no left
child. These are right rotations at n.
Finally, after performing the last rotation of Pn1 , we repeat the step above.
Clearly every tree T of size n is generated, since every possible shape of the subtree
T [1..n 1] is generated, and for each such shape all the trees of size n whose induced
subtree of the nodes 1 to n 1 has that shape have been generated.
Clearly it is possible to perform all of the rotations at n. Any rotation that does not
contain n as one of the endpoints of the rotated edge can also be performed. Such a rotation
is possible as long as n is not in the way, which is true since n is either the root of the
tree or the right child of n 1 whenever such a rotation of Pn1 is done.
2
This proof leads directly to an efficient algorithm for generating all binary trees by rotations. One such implementation is given in Figure 5.17. Procedure call List(t) generates
all binary trees T whose induced subtree T [1..t1] equals the induced subtree of the current
tree. This procedure is initially called with t pointing to node 2. The running time of this
c
uvic01, Frank
Ruskey, 19952001

5.5. GRAY CODES FOR BINARY TREES

n=2

1
@

n=3

n=4

2
3

3
4

3
1

2
2

1
2
@

3
2

4
2

2
@

1
@

2
@

4
@

4
@

1
@

1
@

3
@

1
@

1
@

145

4
@

2
1

Figure 5.16: List of trees for n = 2, 3, 4.

c
uvic01, Frank
Ruskey, 19952001

146

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

algorithm is determined by the number of recursive calls to List, i.e., by the size of the
recursion tree. From the proof above we know that the number of nodes in the recursion
tree is the sum of the first n Catalan numbers, since the list Pn of all Cn binary trees with
n nodes is created from the list Pn1 of all Cn1 binary trees with n 1 nodes, and so
on. This sum is bounded above by 1.5Cn (for n 6), as may be proven by induction from
the recurrence relation Cn = (4n 2)Cn1 /(n + 1). Asymptotically, the ratio of the first n
Catalan numbers to Cn is 4/3; see Exercise 22. Thus List runs in constant amortized time
(i.e., O(1) amortized time) per tree; it is a CAT algorithm.
procedure List( t : ptr );
begin
if t = null then PrintTree
else
List( t.sym succ );
if t.left = null then {increasing}
while LeftRotationPossible( t ) do
RotateLeft( t );
List( t.sym succ );
else {decreasing}
while RightRotationPossible( t ) do
RotateRight( t );
List( t.sym succ );
end {of List};
Algorithm 5.17: A procedure to generate trees by rotations.

5.6
5.6.1

Multisets
Permutations of a multiset

A most natural generalization of k-combinations of an n-set is that of a permutation of a


multiset. Combinations correspond to permutation of a multiset with two types of elements.
As in the section on combinations we consider permutations of a multiset under various
adjacency criteria, but the solutions attained are not so elegant.
We present two principle results. The first is an existence result, based on prefix trees,
showing the existence of a Gray code satisfying the [Trans] condition and the prefix property.
The second result is a direct CAT algorithm which satisfies the [H-Trans] condition but that
does not satisfy the prefix or suffix property. This algorithm is based on the Eades-McKay
algorithm for generating combinations by homogeneous transpositions [H-Trans].
A prefix tree Gray code
When we do not wish to specify the label at the root, we write simply n-tree. An n-tree
is also called a multiset tree. We regard multiset trees as being embedded in the plane
with the root on the left and leaves on the right. The ordering of subtrees is from top to
bottom. To each n-tree T there is a corresponding list L(T ) of all n-permutations; this list
is precisely the list of permutations printed by GenBag. Each permutation can be obtained
by traversing T from its root to a leaf and recording the labels of the nodes encountered.
c
uvic01, Frank
Ruskey, 19952001

5.6. MULTISETS

147

Definition 5.2 A multiset tree T is an interchange tree if successive permutations in list


L(T ) differ by an interchange of two elements.
The following examples illustrate the preceding definitions. For n = h2, 1i, there are
exactly 4 multiset trees. The list 001, 100, 010 is not L(T ) for any multiset tree T . For
n = h2, 2i, there is a unique multiset tree T such that L(T ) = 0011, 0110, 0101, 1010, 1001,
1100; however, T is not an interchange tree.
We call a node of a multiset tree a plus-node if its children are labeled in increasing
order and a minus-node if they are labeled in decreasing order. A node with only one child
is both a plus-node and a minus-node.
Definition 5.3 An alternating tree is a multiset tree in which every internal node is either
a plus-node or a minus-node and, at each level of the tree, reading top-to-bottom, plus- and
minus-nodes alternate.
We now prove a sufficient condition for a multiset tree to be an interchange tree and
use it to derive an interchange algorithm.
Theorem 5.4 Any alternating tree T is also an interchange tree.
Proof: Let = ac and 0 = bd be two successive permutations in L(T ), where
a, b, c, d {0, 1, . . . , t}, a 6= b, c 6= d, and , , , {0, 1, . . . , t} . In words, the first
symbols that differ are a and b and the next symbols that differ are c and d. The nodes
labeled a and b have a common parent; call the tree rooted at their parent an m-tree thus,
like n, m is a sequence that represents a multiset. Nodes c and d have different parents;
call the tree rooted at cs parent a p-tree and the tree rooted at ds parent a q-tree. There
are two symmetric cases, depending upon whether a < b or a > b. We discuss only the case
a < b.
Suppose a < b. Since children are labeled monotonically, mi = 0 for all a < i < b. This
implies pi = 0 and qi = 0 for all a < i < b. Since is common to both and 0 , pi = qi for
i 6= a and i 6= b; further, qa = pa + 1 and pb = qb + 1. Because T is alternating, the nodes
labeled c and d both have either the largest or the smallest labels among their respective
siblings.
In the smallest case, we have c = min{i | pi > 0} and d = min{i | qi > 0}. We argue
that d = a the proof that c = b is similar.
d
=

hd is the smallest siblingi


min{i | 0 i qi > 0}
=
hSince pi = qi for i < a, if some such qi > 0 then c = d; but c 6= di
min{i | a i qi > 0}
=
hqa = pa + 1 > 0i
a
The larger case is similar and is omitted. Hence, a = d and b = c in either case. Also,
p|c = q|d and thus, because T is alternating, = .
2
c
uvic01, Frank
Ruskey, 19952001

148

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

0
0

Figure 5.17: Recursion Tree (A) for n = h2, 1, 1i.

0
0

Figure 5.18: Recursion Tree (B) for n = h2, 1, 1i.

c
uvic01, Frank
Ruskey, 19952001

5.7. COMPOSITIONS

149

A [H-Trans] algorithm
Consider the [H-Trans] algorithm for generating the elements of B(n, k). In a previous
section we developed such an algorithm (Algorithm 5.8), where the initial string was 1k 0nk
and final string was 0nk 1k . Now suppose that Algorithm 5.8 was applied to the string
x1 x2 xk 0nk . Because all transpositions are homogeneous, the symbols x1 , x2 , . . . , xk
always retain their proper order; in particular, the final string will be 0nk x1 x2 xk . We
now have the germ of an idea for generating multiset permutations.
The algorithm is recursive an is reminiscent of the SJT algorithm for generating permutations. Imagine that we have a [H-Trans] listing of the permutations of type n1 , n2 , . . . , nt ,
call it
l1 , l2 , . . . , lm .
This listing is expanded into a listing of all permutations of type n0 , n1 , . . . , nt by expanding
each permutation, li , into a list of permutations, Li , as follows:
Li = 0n0 li , . . . , li 0n0 if i odd, and
Li = li 0n0 , . . . , 0n0 li if i even.
The Li lists are produced by using algorithm gen (i odd) or neg (i even), from the section
on generating combinations. If all ni = 1 then this algorithm has the same structure as the
SJT algorithm.
Although the main idea is simple, converting it into an efficient algorithm requires a
little care. See Algorithm 5.18, which calls neg and gen of Algorithm 5.8. In addition to n
and k, we pass another parameter t to gen and neg, which is not used directly, but is passed
onto swap. There are three
additional global arrays, sum, dir and off. Array sum[0..t]
Pj1
has jth entry sum[j] = i=0 ni . Array dir[0..t] contains boolean values initialized to
true, and is used to keep track of whether the the ni symbols i are moving right or left.
Array off[0..t] keeps track ofPthe number of blocks of higher symbols that are fixed
j1
at the left. Initially, off[j] =
i=0 nj , mirroring the fact that the initial permutation
1 tn t .
is 0n0 1nP
The initial call is BigGen(t). In general, the call BigGen(p) generates,
in the pi=0 ni locations starting at 1 + off[p], all multiset permutations of specification
hn0 , n1 , . . . , np i.

5.6.2

Combinations of a multiset

The same general strategy that was used to generate all permutations of a multiset by
using a prefix tree Gray code as presented in Section 5.6.1 can also be used to generate
the combinations of a multiset. We simply alternate the directions of the subtrees at each
level of the computation tree. Combinations of a multiset were generated in lexicographic
order in the previous chapter in Section 4.5.1. Recall that a k-combination of a multiset of
specification hn0 , n1 , . . . , nt i is represented as a string a0 a1 at that satifies a0 + a1 + +
at = k and 0 ai ni .

5.7

Compositions

A k-composition of n is a solution, in natural numbers, of the following equation.


x1 + x2 + + xk = n
c
uvic01, Frank
Ruskey, 19952001

150

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


procedure BigGen ( t : N);
begin
if dir[t] then gen( t, sum[t 1], sum[t] ) else neg( t, sum[t 1], sum[t] );
if t > 1 then BigGen( t 1 );
dir[t] := not dir[t];
if dir[t] then of f [t] := of f [t 1] + num[t 1] else of f [t] := of f [t 1];
end {of BigGen};
procedure swap ( t,x,y : integer );
local b,temp : integer;
begin
if t > 1 then BigGen( t 1 );
b := of f [t 1];
a[x + b] :=: a[y + b];
PrintIt;
end {of swap};
Algorithm 5.18: Procedures for generating multiset permutations.

For example, the four 2-compositions of 3 are


3 = 0 + 3 = 1 + 2 = 2 + 1 = 3 + 0.
Each solution can be thought
of as a way of placing n unlabelled balls into k labelled boxes.
n+k1
The number of solutions is
. We define a Gray code in which successive compositions
n
have the property that they differ by one in exactly two positions; that is, in the balls into
boxes interpretation, exactly one ball moves from one box to another.
Consider the list, Comp(n, k), of all k-compositions of n, as defined below. If k = 1,
then Comp(n, 1) = n and if k > 1, then Comp(n, k) is defined recursively as in list (a)
below.
Comp(n, k 1)0
Comb(n, k 1)010
Comp(n 1, k 1)1
Comb(n 1, k 1)011
Comp(n 2, k 1)2
Comb(n 2, k 1)012
Comp(n 3, k 1)3
Comb(n 3, k 1)013
..
..
.
.
Comp(0, k 1)n
(a)

Comb(0, k 1)01n
(b)

This list has a number of interesting properties which are embodied in the following
Lemma. Recall our convention that lower case Greek letters are strings and x, y are symbols.
Lemma 5.4 The list Comp(n, k) satisfies the following properties.
1. f irst(Comp(n, k)) = n0 0 = n0k1 .
2. last(Comp(n, k)) = 0 0n = 0k1 n.
3. If a and b are successive strings in Comp(n, k), then a can be written as a =
, x, , y, where b is either , x + 1, , y 1, or, , x 1, , y + 1, and where
is a string of 0s.
c
uvic01, Frank
Ruskey, 19952001

5.7. COMPOSITIONS
5
4
3
2
1
0
0
1
2
3
4
3
2
1
0
0
1
2
1
0
0
0
1
0
0
1
2
3

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

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

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1

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

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

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

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

11111000
11110100
11101100
11011100
10111100
01111100
01111010
10111010
11011010
11101010
11110010
11100110
11010110
10110110
01110110
01101110
10101110
11001110
10011110
01011110
00111110
00111101
10011101
01011101
01101101
10101101
11001101
11100101

11010101
10110101
01110101
01111001
10111001
11011001
11101001
11110001
11100011
11010011
10110011
01110011
01101011
10101011
11001011
10011011
01011011
00111011
00110111
10010111
01010111
01100111
10100111
11000111
10001111
01001111
00101111
00011111

Figure 5.19: The list (a) Comp(5, 4) (first two columns, read down) and the corresponding
list (b) Comb(5, 4) of the elements of B(8, 5) (last two columns).
This lemma is easily proven by induction on k. Property 3 states that in successive
strings there are two positions that differ by 1 and that only 0s occur between those two
positions. Every k-composition of n corresponds to a element of B(n + k 1, n). This
correspondence is obtained by turning each xi into a run of xi 1s and separating the k
runs of 1s by k 1 0s. For example 300123 corresponds to 11100010110111. In Figure
5.19 we show the list Comp(5, 4), together with the corresponding bitstrings in B(8, 5).
Applying this correspondence to the list definition (a) yields the list Comb(n, k) of elements
of B(n, k) as shown in (b). Because of property 3 of Lemma 5.4, the corresponding list of
bitstrings has the [H-Trans] property. In fact, it turns out that this listing is exactly the
same as the Eades-McKay listing discussed in Section 5.3.
Lemma 5.5 The list E(n + k 1, n), where E(n, k) are the Eades-McKay lists defined in
(5.5), and the list Comb(n, k) are identical.
Proof:

Algorithm 5.19 is an indirect implementation of Knuths algorithm; a direct version


is described in the following paragraph. Procedure gen has three parameters, n, k, and
dir, the last of which indicates whether the list is reversed or not. The initial call is
c
uvic01, Frank
Ruskey, 19952001

152

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

procedure gen ( n, k : N; dir : boolean );


local i : N;
begin
if k = 1 then
a[k] := n; PrintIt;
else
if dir then
for i := 0 to n do
a[k] := i; gen( n i, k 1, dir );
dir := not dir;
else
if n is odd then dir := true;
for i := n downto 0 do
a[k] := i; gen( n i, k 1, dir );
dir := not dir;
end {of gen};
Algorithm 5.19: Indirect implementation of Knuths algorithm.
procedure updown ( p, q : N);
begin
a[p] := a[p] + 1; a[q] := a[q] 1;
PrintIt;
end {of updown};
procedure gen ( n, k : N);
local i : N;
begin
if k > 1 then
for i := 0 to n 1 do
if i is odd then
neg( n i, k 1 ); updown( k, 1 );
else
gen( n i, k 1 ); updown( k, k 1 );
end {of gen};
procedure neg ( n, k : N);
local i : N;
begin
if k > 1 then
for i := n 1 downto 0 do
if i is odd then
updown( 1, k ); gen( n i, k 1 );
else
updown( k 1, k ); neg( n i, k 1 );
end {of gen};
Algorithm 5.20: Direct implementation of Knuths algorithm for compositions.
c
uvic01, Frank
Ruskey, 19952001

5.8. NUMERICAL PARTITIONS

153

gen(n,k,true). It is clear that this indirect implementation of the recursive definition of


Comp(n, k) translates into an algorithm whose computation tree has t(n, k) nodes, where

1 P
if k = 1
t(n, k) =
1 + nj=0 t(j, k 1) if k > 1
The k > 1 case may besimplified
to t(n, k 1) + t(n 1, k) (why?).With this simplification

the solution t(n, k) = n+k


to
this
recurrence relation is readily verified. The amortized
k1
running time is thus proportional to (n + k)/(n + 1), and the algorithm is not CAT if n is
small compared with k (i.e., if n = o(k). By eliminating 0-paths, it is possible to construct
a direct CAT algorithm.
Algorithm 5.20 is a direct implementation; the initial call is gen(n,k). The direct
algorithm is CAT, since every recursive call is matched with a call to updown, which increments the element in one position, decrements an element in another position and then
calls PrintIt. Further algorithmic questions are pursued in the exercises.

5.8

Numerical Partitions

Recall that a numerical partition can be thought of as a solution, in natural numbers, of


the equation
x1 + x2 + + xn = n,
where
x1 x2 xn 0
The number of parts of the partition is the number of non-zero xi . What are the appropriate
closeness conditions for defining Gray codes of numerical partitions? Taking our clue from
the previous section, define two numerical partitions to be close if there are two entries
that differ by 1. The first column of following table shows the partitions that are close to
53221.
p
53221
432211
43321
44221
52222
532111
5332
54211
5422
62221
63211
6322

p
54211
6421
5431
5422
55111
63211
44311
53221
44221
541111
532111
53221

The table also shows the conjugate 54211 and the partitions that are close to it (in the
second column). We observe that the partitions in both columns are conjugates of each
other. This is no accident as the following lemma states.
c
uvic01, Frank
Ruskey, 19952001

154

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

@
I
@

r
r
r

@
@

@
r
r 3 @3@
@3@

@ @
@
@ @ @
3
@

@
@

@
@
@ @
@
@ @ @

@ @

@ @
@

Figure 5.20: Peaks and valleys.


16

214

22 12
@

23
@

@
@

@
@

@
@

313

32

321
@

@
@

@
@
@

@
@

412

42

Figure 5.21: The closeness graph G(6, 4) of L(6, 4).


and q
are also close.
Lemma 5.6 If p and q are partitions of n that are close, then p
Proof: To prove this lemma consider the Ferrers diagrams of close partitions p and q.
Draw the Ferrers diagram of p pointing up as illustrated in Figure 5.20. That figure
represents the partition 5 + 4 + 2 + 1 + 1 or its conjugate 5 + 3 + 2 + 2 + 1. To each distinct
part of the partition there corresponds one peak, indicated by the small diamonds (3).
Each peak has a pair of valleys on either side; there is one more valley than peak. Valleys
are indicated by dots (). Two partitions are close if they can be obtained, one from the
other, by moving a square at a peak down into a valley. This operation is the same whether
we are considering partitions or their conjugates.
2
Define L(n, k) to be the set of all partitions of n whose largest part is at most k. Thus
L(n, n) is the set of all partitions of n. The closeness graph G(n, k) of L(n, k) has vertices
which are the members of L(n, k) and edges between those vertices that are close. Figure
5.21 shows the closeness graph of L(6, 4). Note that 1n is always a pendant vertex of G(n, k)
for all 0 < k < n.
Our strategy is to try to find a Hamilton path in the closeness graph. Since 1n is a
pendant vertex, any Hamilton path must start at 1n , which is the lexicographically least
c
uvic01, Frank
Ruskey, 19952001

5.8. NUMERICAL PARTITIONS

155

numerical partition. We will try to make the Hamilton path end at the lexicographically
largest partition, which is k t r where t and r are the quotient and remainder when n is
divided by k; i.e., n = kt + r with 0 r < k. Savage [369] has shown that such a Hamilton
path always exists except when (n, k) = (6, 4). In that case, the path cannot end at 42, but
must end at 22 12 , 23 , 313 , 32 or 412 , as Figure 5.21 clearly shows. Note that generating
P(n, k) reduces to generating L(n k, k) by appending a k; i.e., P(n, k) = kL(n k, k).
To show the existence of a Hamilton path in the the general closeness graphs we use
recursively constructed lists, some going forwards and some going backwards, as was done for
the previous Gray codes of this chapter, but the definitions are quite a bit more complicated
in this case. Part of the complication arises because of the (6,4) case and those other cases
X that depend upon (6,4). Define
X = {(6, 4), (6, 6), (14, 4), (15, 5), (12, 7)}.
All cases of X, depend on (6,4) and they must be handled separately as shown in Figure
5.22. Note that L(15, 5) is defined in terms of M (15, 4).
In a slight abuse of notation we will use L(n, k) to stand for both the set defined earlier
and the list of partitions to be defined below. It proves convenient to define another set M
which will also be treated as a list. The construction of the Gray code involves a mutual
recursion on both L and M. Define M(n, k) to be the set of all partitions of n with either
largest part at most k or largest part k + 1 and second largest part less than k. In other
words,
M(n, k) = L(n, k) (k + 1)L(n k 1, k 1).
For example, M (4, 2) = {22 , 212 , 14 , 31}. Just as L had special cases, so does M. Define
Y = {(11, 5), (13, 6), (18, 4), 20, 5)}
These lists for these special cases are shown in Figure 5.23.
Heres how the list L is defined:

1n

List(i, j)

L(n, n)

M(n, k 1)

k(k1)L(n 2k + 1, k 1)

kkL(n 2k, k)
L(n, k) =
M(n, k 1) k(k1)

L(n, k 2)

(k1)L(k 1, k 3)

kL(k 2, k 3)

(k1)(k2)1 (k1)(k1) k(k2)

L(n,
k 2)

(k1)L(n k + 1, n k + 1)

kL(n k, n k)

if
if
if
if
if
if

n=k=0
n > 0 and k = 0
k=1
(i, j) X
n<k
n 2k

if n = 2k1
if n = 2k2 and n > 4

if k n < 2k 2

For the sake of brevity, this definition differs a little from our usual conventions about
recursive decompositions. In some cases as sublist may be non-existent. For example, if
c
uvic01, Frank
Ruskey, 19952001

156

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

L(6, 4) =
16
214
313
22 12
23
321
32
42
412

L(6, 6) =
L(6, 3)
42
412
51
6

L(14, 4) =
114
2112
22 110
23 18
24 16
25 14
26 12
27
325 1
425
424 12

324 13
323 15
423 14
422 16
322 17
4218
4110
3111
3219
32 18
4317
42 16
42 214
42 22 12
42 23
432 22
432 212
432 14
43215
4322 13
4323 1
32 24
32 23 12
32 22 14
32 216
33 15
33 213
33 22 1
34 12
34 2
433 1
42 32
42 321
42 313
43 12
43 2

L(15, 5) =
M (15, 4)
5432
542 2
542 12
54313
54321
5423
5422 12
54214
5416
55L(5, 5)

L(12, 7) =
112
2110
319
22 18
23 16
24 14
25 12
26
324 1
323 13
322 15
3217
32 16
32 214
33 13
32 22 12
32 23
33 21
34

432 2
42 22
42 212
42 14
4315
43213
432 12
4322 1
424
423 12
422 14
4216
418
517
5215
5314
522 13
523 1
5322
53212
532 1
42 31
43
543
5421
52 2
52 12
5413
6L(6, 4)
7L(5, 4)
651
66
75

Figure 5.22: The L lists for the special cases in X.

c
uvic01, Frank
Ruskey, 19952001

5.8. NUMERICAL PARTITIONS

157

M (11, 5) =

M (18, 4) =

M (20, 5) =

L(11, 4)
532
542
5412
5313
5321
523
522 12
5214
516
6L(5, 3)
641
551

L(18, 3)
434 2
433 22 1
434 12
433 213
433 15
432 2L(6, 2)
42 L(10, 2)
43111
43219
432 18
4322 L(7, 2)
4L(14, 2)
5L(13, 3)
42 33 1
43 32
43 321
43 22 L(2, 2)
42 32 2L(2, 2)
42 322 L(3, 2)
42 32 14
42 3215
42 317
43 16
43 214
44 313
44 L(2, 2)

L(20, 4)
5M (15, 4)
6L(14, 2)
6322 L(7, 2)
632 18
63219
63111
64L(10, 2)
632 2L(6, 2)
633 L(5, 3)
643L(7, 3)
642 L(6, 3)
643 2
643 12
52 42 12
52 42 2
52 4L(6, 3)
53 L(5, 5)

M (13, 6) =
L(13, 5)
6L(7, 5)
7L(6, 3)
742
7412
751
661

Figure 5.23: The M lists for the special cases in Y .

c
uvic01, Frank
Ruskey, 19952001

158

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

n = k, then the sublist L(n k, n k) doesnt exist; this is not a problem because the
other two sublists (L(n, k 2) and L(n k + 1, n k + 1)) do exist. To adhere to our old
convention, n = k would have to be split off as a separate subcase. Similar exceptions occur
for (n, k) {(4, 3), (2, 2)}, but these may be considered as base cases.
Heres how the list M is defined:

if k = 0

1
if k = 1

List(i,
j)
if (i, j) Y

L(n, k 1)
if 2k + 1 n < 3k 1

k,
k

1)
kL(n

(k + 1)L(n k 1, k 2)
(k + 1)(k 1)L(n 2k, k 2)
M(n, k) =

kkL(n 2k, k)

L(n,
k 1)
if n 3k 1

kM(n k, k 1)

(k + 1)L(n k 1, k 1)

kk(k 1)L(n 3k + 1, k 1)

kkkL(n 3k, k)
only if n > 3k 1
The indirect algorithm that arises from the recursive definitions of the lists L and M is
CAT as is now argued. Where are the degree one calls? First note that M has no degree
one calls; the degree of each call is 4 or 5 (or 0 in the case of a leaf). In the definition of L
degree one calls occur only if k > n, but then a call L(n, n) occurs which has degree three.
Thus any call of degree one is followed by a call of degree greater than one, from which we
conclude that the number of nodes in the computation tree is O(|L(n, k)|).
Theorem 5.5 (Savage) Successive partitions on the lists L and M defined above are close.
Unless (n, k) = (6, 4), the first partition on the list is the lexicographically smallest and the
last partition on the list is the lexicographically largest.
The proof of this theorem is a routine and somewhat tedious induction. The beauty
and difficulty of the result lies in the recursive definitions of the lists L and M. However,
there is one aspect of the proof that is of vital interest to us if we wish to construct a direct
algorithm: The proof explicitly identifies the two positions of the partition that change.
This allows us to easily construct a direct algorithm. Let us illustrate by examining one
case of the proof and the code in a direct algorithm that results from that case. Consider
the case k n < 2k 2 in the definition of L; i.e.,
L(n, k 2) (k 1)L(n k + 1, n k + 1) kL(n k, n k)
The list L(n,k-2) starts at 1n and ends at (k2)(k2)1 if n = 2k3 and at (k2)(nk+
2) if n < 2k 3. Note that these partitions are adjacent to f irst(L(n k + 1, n k + 1)) =
(k 2)(n k + 2). At the second interface last(L(n k + 1, n k + 1)) = (k 1)1nk+1
and f irst(L(n k, n k)) = k1nk are also adjacent. At the first interface the changes
occured in positions 1 and 3 if n = 2k 3 and in positions 1 and 2 if n < 2k 3. At the
second interface the changes occured in positions 1 and n k + 2.
In implementing a direct procedure we could write procedures L(n,k,p) and rL(n,k,p)
to produce L(n, k) and L(n, k), respectively. Since the partition is created left-to-right the
c
uvic01, Frank
Ruskey, 19952001

5.9. SET PARTITIONS

159

parameter p is necessary to keep track of the indexing offset as the recursion proceeds. The
resulting code is shown as Algorithm 5.21. Completing the implementation is a straightforward, if somewhat long, exercise.
if k n and n < 2k 2 then
L( n, k 2, p );
if n = 2k 3 then UpDown( p + 1, p + 3 ) else UpDown( p + 1, p + 2 );
rL( n k + 1, n k + 1, p + 1 );
if n > k then
UpDown( p + 1, p + n k + 2 );
L( n k, n k, p + 1 );
Algorithm 5.21: Case k n < 2k 2 of the recursion for L.

5.9

Set Partitions

Let S(n) be the set of all set partitions of {1, 2, . . . , n} and let S(n, k) denote those partitions
into k blocks. Recall that Bn = |S(n)| is a Bell number. We say that two partitions are
adjacent if they differ in only two blocks and those blocks differ by a single element. For
example {1, 2, 5}, {3, 4, 6} and {1, 2, 4, 5}, {3, 6} are adjacent since the two blocks differ by
the single element 4. This definition of closeness allows for the creation (or deletion) of a
block of size one; e.g., {1, 2, 5}, {3, 4, 6} and {1, 2, 5}, {3, 6}, {4} are adjacent.
As in the previous chapter we represent set partitions using RG (restricted growth)
sequences. Consider two RG sequences that differ in only one position i, say with the two
different values x and y. Then the corresponding partitions are adjacent; the only difference
is that in one partition i is in block x and in the other i is in block y. However, this is not
the only way that partitions can be adjacent; consider, for example, what happens when
the smallest element of one block moves to another block.
If a is an RG sequence of length n, define
m(a) = max{a(1), a(2), . . . , a(n)}.
Clearly, if a and b are adjacent, then |m(a) m(b)| 1. Now let
l1 , l2 , . . . , lBn1
be a list of RG sequences of length n 1, whose corresponding partitions are adjacent.
Expand li into
Li = li 0, li 1, . . . , li m, li (m + 1) if i is odd, and
Li = li (m + 1), li m, . . . , li 1, li 0 if i is even,
where m = m(li ). This recursive construction is illustrated for n = 1, 2, 3, 4 in Figure
5.24(a). We now claim that L1 , L2 , . . . , LBn1 is a list of all RG sequences of length n
and that successive sequences represent adjacent partitions. The basic structure of the
construction is the same as that used in Algorithm 4.22 and thus the list contains all RG
sequences. The underlying computation tree is the same as that shown in Figure 4.17,
except that certain subtrees have been rotated.
c
uvic01, Frank
Ruskey, 19952001

160

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


0

00

000
001

01

012

011

010

0000
0001
0012
0011
0010
0120
0121
0122
0123
0112
0111
0110
0100
0101
0102

(a)

00

000
001

01

011

012

010

0000
0001
0011
0012
0010
0110
0111
0112
0122
0123
0121
0120
0100
0101
0100

(b)

Figure 5.24: Gray codes for RG functions n = 1, 2, 3, 4: (a) Knuth list, (b) differing in only
one position.
Within the Li lists successive partitions are adjacent since they differ only by element
n moving to another block. If i is even then last(Li ) = li 0 and f irst(Li+1 ) = li+1 0 are
adjacent since li and li+1 are. Similarly, if m = m(li ) = m(li+1 ), then last(Li ) = li (m + 1)
and f irst(Li+1 ) = li+1 (m + 1) are adjacent. The remaining cases occur when m = m(li ) =
m(li+1 ) 1. Now suppose that m = m(li+1 ) + 1; the case m = m(li+1 ) 1 is symmetric.
So last(Li ) = li (m + 1) and f irst(Li+1 ) = li+1 m. In both of these partitions n is in a block
by itself and thus they are adjacent.
If the definition of m is changed from m = m(li ) to m = min{k 2, m(li )}, then the list
produced is of all partitions into at most k blocks.
These lists can be generated by a CAT algorithm. The basic structure is like that of Algorithm 4.22, except that we need to determine whether the for loop (and the gen(l+1,m+1)
call) generates a[l] in increasing or decreasing order. This is easily determined by looking
at the old value of a[l]. The algorithm is CAT for exactly the same reasons that Algorithm
4.22 was CAT.
RG sequences differing in only one position
The list that we have just described has the somewhat undesirable property that successive
RG sequences may differ in more than one position, even though the partitions that they
represent are adjacent. A simple modification of the construction results in lists where
successive RG strings differ in only one position, and by at most 2 in that position. As
before, assume that l1 , l2 , . . . , lBn1 is a list of all RG strings having the required properties.
We only need to modify our previous expansion as follows. Let m0 = m(li+1 ). If i is odd,
then
Li = li 0, li 1, . . . , li m, li (m + 1) if m0 m, and
Li = li 0, li 1, . . . , li (m 1), li m, li (m + 1) if m0 = m 1.
c
uvic01, Frank
Ruskey, 19952001

5.9. SET PARTITIONS

161

If i is even then
Li = li (m + 1), li m, . . . , li 1, li 0 if m0 m, and
Li = li (m + 1), li m, li (m 1), . . . , li 1, li 0 if m0 = m 1.
The construction for n = 1, 2, 3, 4 is illustrated in Figure 5.24(b).
Currently, there is no CAT implementation for generating these lists. In general it is
impossible to generate RG strings so that successive strings differ by 1 in one position
(see Exercise 50).
Partitions with a fixed number of blocks
Generating partitions with a given number of blocks is somewhat more complicated. We define two lists of RG sequences representing partitions of [n] into k blocks, denoted S(n, k, 0)
and S(n, k, 1). These definitions have similarities to the [2-Trans] lists defined in an earlier
subsection on Gray codes for combinations. The lists possess the following properties.
f irst(S(n, k, 0)) = f irst(S(n, k, 1)) = 0nk 012 (k 1)
last(S(n, k, 0)) = 0nk 12 (k 1)0
last(S(n, k, 1)) = 012 (k 1)0nk
The construction of the lists depends upon the parity of k as shown in the tables below,
first for k even and then for k odd.
S(n, k, 0) even k

S(n, k, 1) even k

S(n 1, k 1, 0)(k 1)
S(n 1, k, 1)(k 1)
S(n 1, k, 1)(k 2)
..
.

S(n 1, k 1, 1)(k 1)
S(n 1, k, 1)(k 1)
S(n 1, k, 1)(k 2)
..
.

S(n 1, k, 1)1
S(n 1, k, 1)0

S(n 1, k, 1)1
S(n 1, k, 1)0

S(n, k, 0) odd k

S(n, k, 1) odd k

S(n 1, k 1, 1)(k 1)
S(n 1, k, 1)(k 1)
S(n 1, k, 1)(k 2)
..
.

S(n 1, k 1, 0)(k 1)
S(n 1, k, 1)(k 1)
S(n 1, k, 1)(k 2)
..
.

S(n 1, k, 1)1
S(n 1, k, 1)0

S(n 1, k, 1)1
S(n 1, k, 1)0

The lists S(5, 3, 0) and S(5, 3, 1) are illustrated in Figure 5.25.


A simple inductive argument proves the following lemma.
c
uvic01, Frank
Ruskey, 19952001

162

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

0
1
1
0
0
1
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
1
0

0
0
1
1
1
1
0
2
2
2
0
1
1
1
1
0
2
2
2
2
2
2
0
1
1

1
1
1
1
0
0
0
0
1
2
2
2
2
2
2
2
2
1
0
0
1
2
2
2
2

2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
0
0
0
0
0
0

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

0
0
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
1
0
0
1
1
1
1
1

0
1
1
0
0
1
1
1
1
0
2
2
2
2
2
2
0
1
1
1
1
0
2
2
2

1
1
1
1
0
0
0
2
2
2
2
1
0
0
1
2
2
2
2
2
2
2
2
1
0

2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
0
0
0
0
0
0

Figure 5.25: The lists S(5, 3, 0) and S(5, 3, 1).

c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS

163

Lemma 5.7 The lists S(n, k, 0) and S(n, k, 1) of RG sequences have the property that successive sequences differ in only one position.
Note that the partition corresponding to f irst(S(n, k)) is
{1, 2, . . . , n k}, {n k + 1}, . . . , {n}
and to last(S(n, k, 0)) and last(S(n, k, 0)) are
{1, 2, . . . , n k 1, n}, {n k}, . . . , {n 1} and
{1, k + 1, k + 2, . . . , n}, {2}, . . . , {k}.
Thus first and last are not adjacent. It is an open question as to whether they can be made
adjacent (and still preserve the property of Lemma 5.7).

5.10

Linear Extensions of Posets

In this section we present a CAT algorithm of Pruesse and Ruskey [321] for generating
all linear extensions of a poset so that successive extensions differ by one or two adjacent
transpositions; i.e., so as to satisfy the [2A Tran] condition. Before getting into the nittygritty of the algorithm, we first provide some motivational material, showing that many
interesting types of combinatorial objects may be viewed as linear extensions of particular
posets.

5.10.1

Combinatorially Interesting Posets

Binary Trees
Consider posets Bn whose Hasse diagrams look like 2 by n grids tilted 45 degrees. An
example of B5 is shown in Figure 5.26. Regard the n elements of the lower chain as
being labelled with a left parenthesis and of the upper chain as being labelled with a right
parenthesis. Then each linear extension is a well-formed parenthesis string. The structure
of the poset guarantees that there will be at least as many left parentheses in any prefix
as right parentheses, which is the essential condition for a string of parentheses to be wellformed. Conversely, it is clear that any well-formed parentheses string corresponds to a
linear extension of Bn .
Young Tableau
In a more general vein, imagine a Ferrers diagram rotated to face up as illustrated in
Figure 5.26. Consider each element to be labelled in the order 1, 2, . . . , n that they are listed
in a linear extension. Such a labelling gives a Young Tableau of the shape of the Ferrers
diagram. Conversely, any Young Tableau gives a linear extension of the poset. Thus an
algorithm for listing linear extensions can be used to generate Young Tableau of a given
shape.
Permutations of a Multiset
Let P be a poset consisting of t+1 disjoint chains of sizes n0 , n1 , . . . , nt . Then each extension
of P may be viewed as a permutation of a multiset of specification hn0 , n1 , . . . , nt i. In
particular, if t = 1 then the extensions correspond to combinations.
c
uvic01, Frank
Ruskey, 19952001

164

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


s
s @ s
@
s
s @ s @s
@
@ s @ s @s
@
@
@s @s
@
@s

s
s @ s
@ s @ s
@ @
@s @ s
@ s @@s
@
@s

s
s

Figure 5.26: (a) Poset whose linear extensions correspond to binary trees. (b) Poset whose linear
extensions correspond to Young tableau of shape 6,4,4,1.
6
4

12

@
3

11

10

Figure 5.27: Poset whose linear extensions correspond to set partitions of type (2,2,2,3,3).
Alternating Permutations
The fence poset...
Partitions of Given Type
Let P (n0 , n1 , . . . , nt ) be the set of partitions of the set {1, 2, . . . , n} into blocks of sizes
n0 , n1 , . . . , nt , where n = n0 + n1 + + nt . For example, the partition
{{2, 8}, {3, 5}, {6, 12}, {1, 10, 11}, {4, 7, 9}}

(5.9)

is in P (2, 2, 2, 3, 3). We have listed the elements in order and the blocks of equal size
by the magnitude of their smallest element. The problem of generating the elements of
P can be reduced to that of generating the linear extensions of a certain forest poset.
The cover relations of the poset consist of chains of sizes ni together with chains of the
maximal elements of the previous chains which have equal values of ni . For our example above the Hasse diagram is shown in Figure 5.27. The linear extension corresponding to our example is 7, 1, 3, 10, 4, 5, 11, 2, 12, 8, 9, 6. The inverse of this permutation is
2, 8, 3, 5, 6, 12, 1, 10, 11, 4, 7, 9, which is just our original partition (5.9) but without the curly
braces.
To be a little
Pi1more precise, assume that the ni are listed such that n0 n1 nt ,
and let si = j=1 ni . The cover relations of the poset are indicated by the t + 1 chains
Ci = si + 1, si + 2, . . . , si + ni

and

Di = si + 1, si+1 + 1, . . . , sj + 1,
where ni1 < ni = ni+1 = = nj < nj+1 .
There is a formula for the number of extensions of a poset whose Hasse diagram is a
forest; see Exercise 37.
c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS

165

s b1 b2 a1 a2

a2

b2

s b1 a1 b2 a2
@
@
@s b1 a1 a2 b2

a1 b1 b2 a2 s
s

a1

b1
P

@
@
@s a1 b1 a2 b2

s a1 a2 b1 b2

G(P)

Figure 5.28: A poset and its transposition graph.

5.10.2

Generating Linear Extensions Fast

The Prism of G0 (P) is Hamiltonian


For T S(P), we let P\T denote the poset on the set S(P)\T with the relations set
R(P) (S(P)\T )2 . Suppose a and b are incomparable elements of S(P) such that a has
the same relationship to all other elements of S(P) as b; more precisely, suppose that, for
all c S(P), c a if and only if c b, and a c if and only if b c. Then a and b are
called siblings. For posets P and Q, if R(P) R(Q) is antisymmetric, then we let P + Q
denote the poset on the set S(P) S(Q) with the relation set which is the transitive closure
of R(P) R(Q). For example, P + abc is the poset on the set S(P) {a, b, c} with the
relation set which is the transitive closure of R(P) {(a, b), (b, c)}. If P + Q = P, then we
say P induces Q. For example, if P + abc = P, then {(a, b), (b, c)} R(P), and every linear
extension of P has a b c; therefore, we say P induces abc. For element disjoint total
orders , , , , we let ( + ) denote the poset + .
Consider the graph which has E(P) as its vertex set, such that two vertices are adjacent
in the graph whenever the corresponding linear extensions differ by a single transposition.
This graph is called the transposition graph of the poset P and is denoted G(P). The
subgraph of G(P) on the same vertex set but containing only the edges which correspond
to adjacent transpositions is called the adjacent transposition graph and is denoted G0 (P).
Generating the linear extensions of P by (adjacent) transpositions is equivalent to finding
a Hamiltonian path in the graph G(P) (G0 (P)). Figure 5.28 shows a poset and its transposition graph. If and are linear extensions of P, then by D(, ) we denote the distance
in G(P) from to , and by D0 (, ) we denote the corresponding distance in G0 (P).
Transposition graphs are bipartite and connected. If the partite sets of G(P) are not
the same size, then there is no Hamiltonian cycle through the graph; if the difference in the
size of the partite sets is more than one, there is no Hamiltonian path through the graph
and thus, the linear extensions of P cannot be generated by transpositions. In Figure 5.28,
the partite sets have a size difference of two, so the linear extensions of that poset cannot
be generated by transpositions.
Recall the the prism of a graph G is the graph G K2 obtained by taking two copies of
G, and adding the edges which correspond to an isomorphism between the two copies. To
differentiate between the copies of G, we will prefix the vertices of one copy of G with +
and the other with . For example, Figure 5.29 shows G(P) K2 , where P is the poset
c
uvic01, Frank
Ruskey, 19952001

166

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES


+b1 b2 a1 b1

b1 b2 a1 a2

s
AA
A

s
AA

As

As
s

+a1 a2 b1 b2

s
AA

s
AA

As

As
s

a1 a2 b1 b2

Figure 5.29: The graph G(P) K2 .

Figure 5.30: Part of a Hamiltonian cycle through G0 (P) K2 .


shown in Figure 5.28.
Lemma 5.8 If a and b are siblings in P, then G(P)
= G(P + ab) K2 .
Proof: Observe that E(P) = E(P + ab) E(P + ba). For any linear extension l of P,
transposing a and b in l yields another linear extension of P. Therefore, the operation which
transposes a and b in a linear extension provides an isomorphism between G(P + ab) and
G(P + ba).
2
If e(P) = 1 (i.e., if P is a total order), G(P) K2 is an edge. For the purpose of
inductively showing the existence of Hamiltonian cycles, we consider this graph to have a
Hamiltonian cycle, since it has a Hamiltonian path such that the endpoints are adjacent.
The Graph G0 (P) K2 is Hamiltonian
The proof that G0 (P) K2 is Hamiltonian forms the basis of the efficient algorithm to be
presented in the next section. That this is true for a certain kind of poset, called a B-poset,
was shown by Pruesse and Ruskey [319], and this result will be used in the proof of the
general case.
c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS

167
y10r
y9 r
y8 r
y7 r
y6 r

y5 r
y4 r
y3 r
y2 r
y1 r

BBr
x2 = b

@
@rx = a
1

Figure 5.31: A B-poset.


Definition 5.4 A B-poset is a poset P whose elements can be partitioned into two disjoint
chains, x1 x2 xn and y1 y2 ym , such that yj 6 xi for all i and j, where
1 i n and 1 j m.
An example of a B-poset is shown in Figure 5.31. Note that = x1 x2 . . . xn y1 y2 . . . ym
is a linear extension of any B-poset. The extension is called the canonical linear extension
of a B-poset. Define mr(xi ) to be the largest index j such that xi k yj ; if xi y1 , then
mr(xi ) = 0. For the B-poset of Figure 5.31, mr(a) = 4 and mr(b) = 7.
The following lemma was proved in [319].
Lemma 5.9 Let P be a B-poset. Then there exists a Hamiltonian cycle in G0 (P)K2 which
uses the edge [+, ].
Figure 5.29 shows the graph G0 (P) K2 , where P is the B-poset shown in Figure 5.31.
The edges corresponding to the isomorphism between the two copies of G0 (P) have been
omitted for the sake of clarity. One can think of traveling up a vertical edge as transposing
b = x2 with its neighbor on the right, and traveling along a horizontal edge as transposing
a = x1 with one of its neighbors. A Hamiltonian path between + and is shown in
Figure 5.30. All B-posets used in the remainder of the paper have n = 2; we call these
2B-posets.
A graph similar to that shown in Figure 5.30 arises whenever a k y1 ; we call this the
typical case. If a y1 then G0 (P) is a path; we call this the atypical case. In other words,
the typical case occurs when mr(a) > 0 and the atypical case occurs when mr(a) = 0.
Theorem 5.6 For every poset P, the graph G0 (P) K2 is Hamiltonian.
Proof: The proof of the theorem is by induction on |S(P)|. For the base cases of the
induction, P is the poset on zero or one elements; in both of these cases G0 (P) K2 is an
edge.
Suppose |S(P)| > 1. If P has a unique minimum element a, then G0 (P)
= G0 (P\{a}),
0
and by the inductive hypothesis G (P\{a}) K2 is Hamiltonian.
Otherwise, P has at least two minimal elements, let a and b be two of them. By the
inductive hypothesis, the graph G0 (P\{a, b}) K2 has a Hamiltonian cycle H 0 . Replace
c
uvic01, Frank
Ruskey, 19952001

168

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

each signed linear extension +i on H 0 with abi ; replace each linear extension i with
bai . The result is a cycle 1 , 2 , . . . , M , where M = 2 e(P\{a, b}), in G0 (P) K2 , which
visits exactly those linear extensions in which a and b precede all other elements of S(P).
That is, this cycle visits all the linear extensions of
X
P0 = P +
ax + bx.
xS(P)\{a,b}

The poset P 0 is P extended so that a and b are covered by every other element of Min(P).
For each i = xi yi i , where xi = a, yi = b or xi = b, yi = a, the poset P + xi yi + i
is a B-poset. By Lemma 5.9, there is a Hamiltonian path through G0 (P + xi yi + i ) K2
from +i to i . We substitute the occurrence of i with this path in H 0 , for each odd i.
For each even i, we substitute the reverse of this path for the occurrence of i . Call the
resulting walk H.
To prove that H is a Hamiltonian cycle through G0 (P) K2 , it is necessary to show that
every vertex on the cycle H is a linear extension of P; this is true, since E(P 0 ) E(P),
and hence each B-poset generated is an extension of P. It is also necessary to show that for
each linear extension l of P, +l and l both occur exactly once on H. Suppose l induces
the order xy on {a, b} and the order on S(P)\{a, b}. Then xy is a linear extension of P 0 ,
and l is a linear extension of the B-poset P + xy + ; also, every other B-poset generated
either does not induce the order xy or does not induce the order . Therefore, +l and l are
generated only during the generation of E(P + xy + ); i.e., each +l and l are generated
exactly once.
2
Observe that the reference to Lemma 5.9 in the preceding proof was not strictly necessary
because the B-posets that occur are all 2B-posets. In the typical case, the cycle of Figure
5.30 could be used; in the atypical case the cycle is obvious (move b to the right as far
as possible, change signs, and then move the b back to the left). If mr(b) is even, the cycle
of Figure 5.30 is slightly different and uses the edge [+ab, ab], where = y1 y2 ym .
These cycles are used in the algorithm of the following section.
The Algorithm
The proof of Theorem 5.6 is constructive. In this section, we present the recursive algorithm implicit in the inductive proof. The algorithm runs in constant amortized time; i.e.,
generating all the linear extensions of a poset P takes time O(e(P)).
We first give an overview of the algorithm and use a small example to give a general
understanding of how it works. We then give the details of the algorithm and a proof of its
correctness.
The algorithm is an in-place algorithm; it maintains an array le in which contains the
current linear extension, and maintains a variable IsPlus which keeps track of the sign
(+ or ). We go from one linear extension to the next by making changes to the array
or reversing the sign.
The main procedure used by the algorithm, which we call GenLE, is recursive and basically follows the path indicated in Figure 5.30. Every level of the recursion has an associated
pair of minimal elements of the current subposet. For example, in the poset shown in Figure 5.28, a1 , b1 are a pair of minimal elements of P1 = P, and a2 , b2 are a pair of minimal
elements of P2 = P1 \{a1 , b1 }. These pairs are determined by some preprocessing which will
be described later.
c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS


Procedure Call

169
Linear Extension
+a1 b1 a2 b2

GenLE(2)
GenLE(1)
Move(b1 , )
Switch(0)
Move(b1 , )
Switch(1)
GenLE(1)
Switch(0)
Switch(2)
GenLE(2)
GenLE(1)
Move(a1 , )
Switch(0)
Move(a1 , )
Switch(1)
GenLE(1)
Switch(0)

+a1 a2 b1 b2
a1 a2 b1 b2
a1 b1 a2 b2
b1 a1 a2 b2
+b1 a1 a2 b2
+b1 a1 b2 a2

+b1 b2 a1 a2
b1 b2 a1 a2
b1 a1 b2 a2
a1 b1 b2 a2
+a1 b1 b2 a2

Figure 5.32: The trace of the calling sequence for the poset of Figure 1.
The procedures Move and Switch are used to change the current linear extension. They
operate in O(1) time as follows:
Switch(i): If i = 0, then the sign is changed; that is, IsPlus is changed. If i > 0, then ai
and bi are transposed.
Move(x,left): This call transposes x with the element on its left.
Move(x,right): This call transposes x with the element on its right.
Each time a new linear extension l of Pi is generated by the call GenLE(i) (i.e., each time
Move or Switch is called), GenLE(i1) is called; the call GenLE(i1) moves a1 , b1 , . . . , ai1 , bi1
in all possible ways through l, while maintaining the order ai1 bi1 (or bi1 ai1 , depending on their order at the point of calling GenLE(i 1)). If i = 1, then GenLE(i 1)
does nothing.
For example, starting with +a1 b1 a2 b2 and executing the calling sequence GenLE(2),
Switch(2), GenLE(2) on the poset shown in Figure 5.28 leads to the trace of calls shown in
Figure 5.10.2.
We now follow with the details of our implementation. The reader should refer to the
Pascal procedure GenLE of Figure 5.23.
The implementation of the algorithm maintains four global arrays: array le is the
linear extension; array li is its inverse. Arrays a and b store the elements ai and bi . In our
discussion of the algorithm, ai and bi are considered to be fixed at the outset and unchanging
c
uvic01, Frank
Ruskey, 19952001

170

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

throughout the run of the algorithm. The arrays will be maintained so that a[i] always
contains the value of the leftmost of the ith pair, and b[i] contains the rightmost. Also,
the current sign, either plus (+) or minus (), is maintained.
The boolean function Right(x) is used to determine whether the element x can move
to the right. It operates in O(1) time as follows:
Right(b[i]): Returns true only if b[i] is incomparable with the element to its right in
the array le.
Right(a[i]): Returns true only if a[i] is incomparable with the element to its right in
the array le and the element to the right is not b[i].

procedure PreProcess
begin
i := j := 0;
Q := P;
while S(Q) 6= do
if Q has exactly one minimal element x then
j := j + 1; le[j] := x;
Q := Q\{x};
else
Let a0 , b0 be any two minimal elements of Q;
i := i + 1; j := j + 2;
a[i] := a0 ; b[i] := b0 ;
le[j 1] := a0 ; le[j] := b0 ;
Q := Q\{a0 , b0 };
MaxPair := i;
end {of PreProcess};
Algorithm 5.22: Preprocessing a poset by stripping off minimal pairs.
We now describe our preprocessing. We successively strip off pairs ai , bi of minimal
elements for i = 1, 2, . . . until there are no elements left. If a unique minimum element is
encountered then it is simply deleted and does not become part of a pair. Let MaxPair be
the index of last pair of minimal elements we strip from P, the remainder of P being a total
ordering, or empty. This preprocessing is detailed in Algorithm 5.10.2. Note that MaxPair
is not uniquely determined by the poset, but that it depends on the order in which the
elements are stripped from P. The most straightforward way to implement this algorithm
is using a breadth-first-search. The running time is proportional to the size of the poset
description, O(n + m) where m is the number of cover relations.
We say the linear extension l is in proper order up to i if for all 1 j i, the elements
aj and bj are adjacent in l, and l induces the orders a1 a2 . . . ai ah and a1 a2 . . . ai bh for all h,
where i < h MaxPair. The initial linear extension of the listing must be properly ordered
up to MaxPair; the preprocessing of Algorithm 5.10.2 does this.
Assuming that Right( b[MaxPair+1] ) is false, the initial call is simply GenLE( MaxPair
+ 1 ); this is the same as the following procedure calls, which we call the calling sequence.
GenLE( MaxPair ); Switch( MaxPair ); GenLE( MaxPair );
c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS

171

The algorithm consists of executing the preprocessing, setting IsPlus to plus (+), and
then executing the calling sequence.
A Pascal procedure implementing GenLE is given in Figure 5.23. We now prove the
following theorem.
Theorem 5.7 The algorithm GenLE generates the linear extensions along a Hamiltonian
path in G0 (P) K2 .
Proof: In order to prove the theorem, we first prove the following claim.
Claim: Let the linear extension in array le be = ai bi , and let be properly ordered
up to i. Then for each linear extension l E(P + ai bi + ), GenLE(i) generates each of +l,
l exactly once. Furthermore, if i = 1, then the last extension generated is , and if i > 1
then the last extension generated is + 0 , where 0 differs from by a transposition of ai1
and bi1 .
Proof of Claim: The proof proceeds by induction on i. If i = 1 the recursive calls GenLE(0)
do nothing, Switch(0) just changes the sign, and a1 is induced by P.
It is easy to confirm that the algorithm in Figure 5.23, when stripped of its recursive calls,
and in which Switch just changes the sign, simply follows the path indicated in Figure 5.29.
In this case, GenLE(1) just finds a Hamiltonian path from + to through G0 (Q) K2 ,
where Q is the 2B-poset P + a1 b1 + .
If i > 1, then assume without loss of generality that the sign in storage when GenLE is
invoked is +. There are , , such that + = +ai1 bi1 ai bi . Because of the way
the preprocessing selects the pairs aj , bj , we are assured that P induces the order (ai + bi )
(of course, could be empty).
As mentioned before, the basic structure of the algorithm, stripped of recursive calls,
follows the Hamiltonian path in a 2B-poset as indicated in Figure 5.29, where Switch now
transposes ai1 and bi1 ; therefore it generates +E 0 = +E(P + (ai1 + bi1 )(ai bi + )).
Each linear extension in +E 0 is properly ordered up to i 1.
As each such linear extension +l = +ai1 bi1 (or +l0 = +bi1 ai1 ), where
E(ai bi + ), is generated, GenLE(i 1) is called on +l. By the inductive hypothesis,
that call generates E(P +ai1 bi1 +) (or E(P +bi1 ai1 +), respectively), starting
at +l and ending at +l0 if i > 2, and ending at l if i = 2. Since there are an even number
of vertices in the product of the graph with a edge, there are an even number of calls to
GenLE(i 1). Thus if i > 2 then the sign of the final permutation is unchanged, and if
i = 2, then the relative ordering of ai1 and bi1 is unchanged. The union over all such
is E(P + ai1 bi1 + ai bi + ) E(P + bi1 ai1 + ai bi + ) = E(P + ai bi + ).
2
Let a, b be a[MaxPair], b[MaxPair] respectively, and suppose the calling sequence is
executed on the preprocessed poset P. By the Claim, the first call to GenLE generates
E(P + ab); then a and b are transposed, and then E(P + ba) is generated. Therefore,
E(P) is generated, and the Theorem is proved.
2
In analyzing the time complexity of the algorithm, we assume that Right, Switch, and
Move can be implemented in constant time. This is easily accomplished as long as the
inverse, li, of le is maintained. Each call to Move and Switch generates one more linear
extension. Observe that the call GenLE(i) generates at least two calls to GenLE(i 1). Each
c
uvic01, Frank
Ruskey, 19952001

172

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

iteration of a while-loop or for-loop in the algorithm executes a Move, thereby generating


a linear extension. The only occasion in which GenLE can be recursively called and no
linear extension generated is when i = 0, and this happens at most once per linear extension generated. Therefore, the algorithm runs in constant time per linear extension, when
generating E(P); the algorithm is CAT. By suppressing the linear extensions which are
prefixed with , we generate E(P) in constant amortized time. Another way to think of
the preceding argument is to consider the underlying computation tree, where each internal
node is a recursive call and each leaf is a linear extension. The total amount of computation can be divided up so that each node is assigned a constant amount of computation.
Since each internal node has at least two children, the number of leaves is greater than the
number of internal nodes and therefore the total amount of computation is proportional to
the number of leaves.
Observe that the generation of the minus () vertices only occurs when i = 1 in
Algorithm GenLE. This suggests that i = 1 be treated as a special case and that minus
() vertices be omitted entirely by simply skipping to the next plus (+) vertex. If
this is done, then it saves some computation but the same list of extensions is produced as
before, and successive extensions can differ by a large number of transpositions.
If one only wants to compute the number of extensions, then some computation can
be saved by only computing the number of vertices at the i = 1 level of the recursion,
and not generating the extensions explicitly; i.e., never moving a1 and b1 . The number of
vertices (extensions) can be determined from mr(a) = mra and mr(b) = mrb; the number is
(mr(a) + 1)[mr(b) + 1 mr(a)/2]. Furthermore, mr(a) and mr(b) change by at most unity
from one extension to the next, since only adjacent transpositions are used. This leads to
an algorithm whose running time is O(e(P \ {a1 , b1 })). In general, we have
2 e(P \ {a1 , b1 }) e(P) n(n 1) e(P \ {a1 , b1 }).
The lower bound is attained when a1 c and b1 c for all elements c of P \ {a1 , b1 }. The
upper bound is attained when a1 and b1 are maximal, as well as minimal.
Gray Codes for Linear Extensions
We now show that linear extensions can be listed so that successive extensions differ by at
most a few adjacent transpositions. We first show the existence of such listings, and then
how to modify the results of the previous sections to show that the set of linear extensions
of a poset can be listed so that successive extensions differ by only one or two adjacent
transpositions. Let us say that an ordering 1 , 2 , . . . , e(P) of the extensions of P has
delay C if D0 (i , i+1 ) C for all 0 i < e(P), where 0 = e(P) . Thus we are going to
show the existence of a delay 2 ordering of E(P). Furthermore, such a listing can be done
in constant amortized time. The existence of a delay 3 ordering is not difficult to show.
A poset P has a delay k ordering if and only if G0 (P)k is Hamiltonian. A result of
Sekanina [378], proved in the next chapter, is that the cube of every connected graph is
Hamiltonian. Since G0 (P) is always connected, G0 (P)3 is Hamiltonian and a delay 3 ordering
exists.
The graph G0 (P) is not always 2-connected; otherwise the existence of a delay 2 ordering
would by implied by a result of Fleischner [134] which states that the square of every 2connected graph is Hamiltonian.
Even though G0 (P) is not in general 2-connected, the posets with 2-connected transposition graphs are easy to characterize. First, consider the question of which transposition
c
uvic01, Frank
Ruskey, 19952001

5.10. LINEAR EXTENSIONS OF POSETS

173

graphs have pendant vertices. If P consists of two disjoint chains, then G0 (P) has two
pendant vertices; if P is a B-poset and is not the disjoint union of two chains, then G0 (P)
has one pendant vertex; otherwise G0 (P) has no pendant vertices.
Lemma 5.10 For every poset P, the graph H(P) is 2-connected, where H(P) is G0 (P)
minus any pendant vertices.

procedure GenLE ( i : N);


local mrb, mra, mla, x : N; typical : boolean ;
begin
if i > 0 then
GenLE( i 1 );
mrb := 0; typical := false;
while Right( b[i] ) do
mrb := mrb + 1;
Move( b[i], ); GenLE( i 1 );
mra := 0;
if Right( a[i] ) then
typical := true;
repeat
mra := mra + 1;
Move( a[i], ); GenLE( i-1 );
until not Right( a[i] );
if typical then
Switch( i 1 ); GenLE( i 1 );
if mrb is odd then mla := mra 1 else mla := mra + 1;
for x := 1 to mla do
Move( a[i], ); GenLE( i 1 );
if typical and mrb is odd
then Move( a[i], )
else Switch( i 1 );
GenLE( i 1 );
for x := 1 to mrb do
Move( b[i], ); GenLE( i 1 );
end {of GenLE};
Algorithm 5.23: Pascal procedure GenLE to generate linear extensions.
This may be proven by showing that every pair of incident edges of H(P) is on a 4, 6,
or 8-cycle. This lemma does not help us in finding an efficient algorithm for listing a delay
2 ordering of E(P). Instead, we prove it by applying Theorem 5.6 and the following lemma.
Lemma 5.11 If G is bipartite and G K2 is Hamiltonian, then G2 is Hamiltonian.
Proof: Let G be a bipartite graph on n vertices, and let (v1 , x1 ), (v2 , x2 ), , (v2n , x2n ) be
a Hamiltonian cycle through G K2 , where vi V (G) and xi V (K2 ) = {1, 2}, for all
i, 1 i 2n. Consider the sequence of vertices S = v2 , v4 , . . . , v2n .
c
uvic01, Frank
Ruskey, 19952001

174

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

Since G is bipartite, so is G K2 ; thus the vertices of S are all the vertices of one partite
set of G K2 . Also, for a vertex u of G, (u, 1) and (u, 2) are adjacent are are therefore in
different partite sets of G K2 . Therefore each vertex of G appears exactly once in S. For
each i, the vertices vi and vi+2 are either of distance one in G (if xi 6= xi+2 ) or of distance
two in G (if xi = xi+2 ). Therefore S is a Hamiltonian cycle in G2 . 2
Theorem 5.8 The linear extensions of any poset can be generated with delay 2 in constant
amortized time.
Proof: We run the Algorithm GenLE given in Figure 8, but instead of suppressing the linear
extensions with a negative sign, we suppress every other linear extension; i.e., if we generate
the list l1 , l2 , l3 , l4 , l5 , . . ., then we output the list l1 , l3 , l5 , . . .. By the proof of Lemma 5.11,
this is a delay 2 listing of the linear extensions. It has the same running time as GenLE,
i.e., constant amortized time. 2
In the remainder of this section we discuss how to use the algorithm to compute P (x < y)
and h(x). We use the version of GenLE that generates each extension exactly twice, where
each successive extension differs by an adjacent transposition from its predecessor. We first
discuss how to compute P (x < y) for every pair x, y.
Let us define an xy-run to be a maximal sequence of successive extensions where x
precedes y. We maintain two arrays of integers, call them S and T . The value of S[x, y]
is the sum of the lengths of the previous xy-runs. The value of T [x, y] is the iteration at
which the current xy-run started. At each iteration, exactly one adjacent pair, say xy, is
transposed. If this occurs at the t-th iteration, then S[x, y] is incremented by t T [x, y] and
T [y, x] is set to t. At the termination of the algorithm, P (x < y) is S[x, y] divided by 2e(P).
Since only a constant amount of update is done at each iteration the total computation is
O(n2 + e(P)).
To compute h(x) we proceed in a similar fashion. An x-run is a maximal sequence of
extensions in which x occupies the same position. Here the value of S[x] is the weighted
sum of the lengths of the previous x-runs and T [x] is the iteration at which the current
x-run started. At each iteration exactly one adjacent pair, say xy, is transposed. If this
occurs at the t-th iteration, then for z = x, y, S[z] is incremented by p[z] (t T [z]) and
T [z] is set to t, where p[z] is the position that z occupied in the extension. At termination
the value of h(x) is S[x] divided by 2e(P).

5.11

Ideals of Posets

In this section we show the existence of a Gray code of ideals where each ideal differs from
its predecessor by the flip of one or two bits. Our strategy is the same as in the previous
section; we show that the prism of the underlying closeness graph is Hamiltonian. This
Gray code can be generated in time O( i(P)), where is the maximum number of upper
covers of any element of the poset P.
Theorem 5.9 Let x be minimal in P. The graph J(P) has a Hamilton cycle that contains
the edges [+, ] and [+, +x].
Proof:Our proof is by induction on |P| = 1. If |P| = 1 then J(P) e is the 4-cycle
+, +x, x, . Otherwise, assume that |P| > 1, and let x be minimal.
c
uvic01, Frank
Ruskey, 19952001

5.12. LOOPLESS ALGORITHMS

175

Inductively, there is a Hamilton cycle


+ = X1 , X2 , . . . , Xp =
in J(P/x).
If x is the minimum, then the Hamilton cycle
+, X1 {x}, X2 {x}, . . . , Xp {x},
satisfies the conditions of the theorem.
Otherwise, let x and y be minimal elements of P. There are Hamilton cycles
+ = X1 , X2 , . . . , Xp = in J(P/x) and
+ = Y1 , Y2 , . . . , Yq = in J(P \ x),
with X2 = Y2 = {y}. The Hamilton cycle
+ = Y1 , X1 {x}, Xp {x}, Xp1 {x}, . . . , X2 {x},
Y2 , Y3 , . . . , Yq1 , Yq =
satisfies the conditions of the theorem.

5.12

Loopless Algorithms

An algorithm for generating combinatorial objects is loopless if ...


There is often a natural connection between combinatorial Gray codes, CAT algorithms,
and loopless algorithms.
These algorithms are interesting, not only because of the seeming simplicity of the computations, but because such algorithms would be preferred for hardware implementation,
particularly in those cases where timing is important.

5.12.1

Binary Trees

The algorithm to generate the sequence of trees can also be implemented on a pointer
machine in O(1) worst case time per rotation.
The algorithm requires each node to store the usual pointers to its parent, left child and
right child in the tree, and in addition two more pointers and a direction bit. The direction
bit for node i indicates whether i is currently rotating left on its way to becoming the root
of T [1..i] ( i.e. direction(i) = up ) or whether i is currently rotating right on its way to
becoming the right child of i 1 ( i.e. direction(i) = down ).
We need only show that the next node to be rotated can be located in O(1) time.
At any given time in the generation procedure each node i is currently in the process
of rotating either up or down the right path of T [1..i 1]. We say i is unfinished if at
the current time i has not yet completed its rotations along the right path of T [1..i 1].
Otherwise we say i is finished.
Initially all the nodes are unfinished except for node 1.
At any point in the generation procedure, if T is the current shape of the tree, then the
next node that is to be rotated, node j say, is the largest ( rightmost ) unfinished node. All
c
uvic01, Frank
Ruskey, 19952001

176

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

nodes greater than j are either proper ancestors of the root of T [1..j] or in the right subtree
of j.
After the rotation at node j in T produces a tree T 0 , every node i, i > j commences
to rotate along the right path of T 0 [1..i 1] in the appropriate direction. But before and
after each such rotation at i, all trees T 00 whose subtree T 00 [1..i] is identical to the current
induced subtree of the nodes from 1 to i are generated.
Consider a rotation at node j at some point during the generation procedure. Then the
state of every node i, i < j, remains unchanged but the state of every node i, i > j, changes
from finished to unfinished. The node j, which was unfinished before this rotation, may be
either finished or unfinished after this rotation.
Thus our algorithm can easily identify the next rotation to be performed by maintaining
two lists, the list of finished nodes and the list of unfinished nodes. These two lists partition
the nodes of the tree. On each list the nodes will appear in decreasing order. Each node
i has a pointer next to the node following i on the list containing i. The next pointers are
used for both the list of finished and unfinished nodes, since a node can be on only one list.
This facilitates the updating of the lists needed after each rotation. We also maintain for
each node i a pointer to its inorder successor i + 1.
The next node to be rotated is simply the first node on the list of unfinished nodes.
These two lists of nodes can easily be updated in constant time after each rotation by making
O(1) pointer changes. The details are given in the code for this generation algorithm which
appears in Appendix A.

5.13

Finding a Hamiltonian cycle

So far, even though many problems can be modelled as finding Hamiltonian paths in certain
graphs, we have been able to avoid explicitly building the graph before trying to find a
Hamiltonian path in it. Unfortunately, we cannot expect to always be so lucky; so in
this section we describe how to find a Hamilton path if one exists. We will describe two
algorithms, one that works well for dense graphs and another that works well for cubic
graphs.
The problem of determining whether a graph has a Hamilton cycle is NP-complete, so
we dont expect deterministic polynomial time algorithms that work on all graphs. A bruteforce backtracking approach can be employed, but the ugly reality of exponential growth
limits the usefulness of backtracking we therefore describe two other approaches. These
algorithms work well in practice (on graphs of moderate size).

5.13.1

Extension-Rotation Algorithms

Here we try a probabilistic approach to finding a Hamilton cycle in undirected graphs that
are fairly dense. Roughly speaking, these algorithms will almost always find a Hamilton
cycle in a graph that has one, as long as m n log n.It is also true that almost all graphs
on that many edges have a Hamilton cycle.Thus we would expect the algorithm to succeed
on the n-cube, but not necessarily on planar graphs.
The algorithms are based on two simple operations on a path P in the graph, called
rotation and extension. In an extension the path is simply lengthened by adding a new
vertex to the end of P . In a rotation the length of P is unchanged; an existing edge is
removed and new one is added. Rotations and extensions are illustrated in Figure 5.33.
c
uvic01, Frank
Ruskey, 19952001

5.13. FINDING A HAMILTONIAN CYCLE

177

extension

rotation
w

Figure 5.33: The extension and rotation operations.


(P1)
(P2)
(P3)
(P4)
(P5)
(P6)
(P7)
(P8)
(P9)

P := some vertex of G, call it v0 ;


repeat
v := last(P );
if v is adjacent to v0 and P is a H-cycle then return( P );
(v, w) := an edge of E;
if w 6 P then P := P, w;
if w P then P := the path obtained by adding the edge (v, w)
and deleting the first edge on the subpath from w to v.
until false;

Algorithm 5.24: Extension-rotation heuristic algorithm for finding a Hamilton cycle.


See Algorithm 5.24. Some variation of this algorithm can occur, mostly depending
on how the edge is chosen at step (P5). The first problem to be overcome is making the
algorithm finite, as it is possible that the edge removed on a previous iteration is reinserted at
a later iteration, resulting in the same path occuring twice. We will discuss three variations
of the algorithm, due to (a) Posa, (b) Angluin and Valiant, and (c) Karp.
In Posas version, the rotation operation at lines (P7-8) is constrained so that no two
paths of the same length have the same final vertex. For example, the removed edge is not
again added unless an extension has occured in the interim.
In the Angluin and Valiant version, an edge that is used in P is removed from the
underlying graph. Thus each edge is ever considered at most once. Furthermore, they
select the edge (v, w) so that w is chosen uniformly at random from N (v), the current
neighborhood of v.
In the Karp version of the algorithm a more sophisticated probabilistic selection rule
is used. The first step is a randomized preprocessing that creates for each vertex v a set
PN(v) of its neighbors.
Let p be the probability that an edge is in E (if this is not known,
simply take p = m/ n2 ). Membership in PN(v) is determined by the following selection
rule, applied to each edge (v, w) E(G):
v
v
v
v

PN(w),
PN(w),
6 PN(w),
6 PN(w),

w
w
w
w

PN(v)
6 PN(v)
PN(v)
6 PN(v)

with
with
with
with

probability
probability
probability
probability

p/4
(1 p/2)/2
(1 p/2)/2
p/4

For each vertex v, a subset, OLD(v) of PN(v), is maintained. Then w at step (P5)
c
uvic01, Frank
Ruskey, 19952001

178

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

Figure 5.34: Implicated edges on Hamilton cycles in cubic graphs.


is chosen uniformly at random, either from OLD(v) or PN(v) \ OLD(v); which of the two
sets of used is determined by taking OLD(v) with probability |OLD(v)|/(n 1). Initially
OLD(v) = for each vertex v. If w is not selected from OLD(v), then w is added to OLD(v)
at the end of the iteration.
Why do we have three separate algorithms? In each of the succeeding cases the probability of success is greater, at least if certain distributional assumptions about the input graph
are made. Precise statements about the success of the algorithms can be made but these are
outside of the scope of this book. Appropriate references may found in the Bibliographic
Remarks at the end of this chapter.

5.13.2

Cubic graphs

A graph that is regular of degree two has a Hamilton cycle if and only if it is connected.
Finding a Hamilton cycle in a cubic (regular of degree three) graph is NP-complete but a
simple backtracking approach is remarkably effective on graphs of modest size.
Cubic graphs have the nice property that if two edges of the form (u, v), (v, w) are on
a Hamilton cycle then there are two other edges (x, y), (y, z) that are also on the cycle
and an edge (v, y) that is not on the cycle. See Figure 5.34. These implicated edges can
lead then to further implicated edges and so on. The upshot of this observation is that
backtracking algorithms for finding Hamilton cycles in cubic graphs can be made to be
remarkably efficient on graphs of size, say 200, a size that is totally impossible on graphs of
higher minimum degree.

5.14

Exercises
Questions about the BRGC

1. [1] What is the rank of 1n in the BRGC? Note the solution to this problem gives the
number of moves to solve the Chinese Rings puzzle, or The Brain puzzle, or the
Binary Arts SPIN-OUT puzzle.
2. [2] Prove that any two strings in the Binary Reflected Gray Code list whose Hamming
distance is at least m have ranks that differ by at least d2m /3e.
3. [2] Let b and c be two bitstrings of length n and assume that rank (b) = 2k + rank (c).
Prove that the Hamming distance between b and c is 1 if k = 0, and is 2 if 0 < k <
2n1 . This result has implications for routing algorithms on the hypercube.
c
uvic01, Frank
Ruskey, 19952001

5.14. EXERCISES

179

4. [1+] In the BRGC, let rank(gn gn1 g1 ) = (bn1 b1 b0 )2 . The Hamming distance between gn gn1 g1 and bn1 b1 b0 is some integer k between 0 and n 1.
Prove that the number of length n bitstrings
n1 at Hamming distance k apart from their
corresponding string in the BRGC is 2 k .
5. [2] The transition sequence Tn for the BRGC produces a Gray code, no matter the
vertex an a2 a1 of Qn from which it starts. Restate Lemma 5.1 so that it applies to
such a Gray code, but from the generic vertex an a2 a1 , instead of 0n .
6. [2] The transition sequence of the BRGC could be regarded as an infinite sequence
T = t1 , t2 , . . ., whose first 2n 1 entries give Tn . Find a simple expression for the sum
of the first k elements of T . The expression may use the function (m), the number
of bits in the binary expansion of m.
7. [1] What is the number of shortest paths from 0n to 1n in the n-cube?
8. [2] Show that Qn is Hamilton-laceable by showing thePexistence of a Hamilton path
from 0n to b1 b2 bn for any bitstring b1 b2 bn with
bi odd.
9. [2+] The BRGC can be thought of as a permutation of the elements of B(n), or,
alternatively, of the elements of {0, 1, . . . , 2n 1}. For example, in one line notation
for n = 3 the permutation is 01326754, or in cycle notation is (0)(1)(2 3)(4 6 5 7).
The purpose of this exercise is to determine the cycle lengths of elements in this
permutation. Let G(s) be the rank s element of the BRGC. Define c(s) = min{k |
Gk (s) = s and k > 0}. Show that c(s) = 2dlg(1+blg sc)e , for s > 0.
10. [1+] Prove that any algorithm that solves the Towers of Hanoi problem must make
at least 2n 1 moves. [1] What happens in the Towers of Hanoi problem if we add the
additional requirement that no two disks of the same parity may lie atop each other?
[2] Devise an efficient divide-and-conquer algorithm that starts from an arbitrary valid
configuration of disks. How many moves does your algorithm take?
11. [2] Prove that the following iterative algorithm solves the Towers of Hanoi problem
and that it can be implemented so that only a constant amount of computation is
done between moves. The algorithm moves disks from peg A to peg C.
1. Order pegs A,B,C if n even and A,C,B if n odd
2. repeat
3.
Move disk 1 according to order given in step 1.
4.
If all disks on destination peg then stop.
5.
Move smallest allowed disk that is greater than 1.
6. until false.

12. [2] Prove that the following two lines of Java code solves the towers of Hanoi problem.
Each line of output has the form 0-->2, meaning move the top disk on peg 0 to peg
2.
for (int m=1; m < (1 << n); m++)
System.out.println( (m&m-1)%3 + "-->" + ((m|m-1)+1)%3 );
c
uvic01, Frank
Ruskey, 19952001

180

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

13. [2+] Prove that for 1 j n and 1 k 2n , that the jth letter of the kth word of
n nj 1
the BRGC is equal to 2n222
nj1 k/2 modulo 2.
14. [2] Develop a CAT algorithm for generating F(n), the set of all bitstrings of length n
with no substring 11, in Gray code order. Successive strings should differ by a single
bit flip.
Questions about Combinations

15. [2] Prove that the number of calls to gen and neg in Algorithm 5.8 is
dk/2e

1+3

X
i=1

n 2i
nk1

16. [1+] Regarding Algorithm 5.8 as generating the elements of A(n, k), on each iteration
some ai changes. Prove that the sum of all such indices i over the entire algorithm is

n+1
k+1

,
k
k
and thus that the
average position of the element that changes is (n + 1)/(n k +
1) (k + 1)/ nk .
17. [1+] Show that the following recursion defines a list of combinations that that we
have already encountered. This recursion is from the first editon of Nijenhuis and
Wilf [289].
A(n, k) = A(n 1, k)0 A(n 2, k 2)11 A(n 2, k 1)01
18. [1+] We call this the piano players problem. Recall the chord playing motivation at the beginning of Section ??? on generating combinations by homogeneous
transpositions. The problem, as stated, is not very realistic since it assumes that
the players fingers can stretch arbitrarily far apart. Suppose that we assume that
the chord is being played on one hand and that the fingers on that hand can stretch
over p keys. Can all such restricted chords be played so as to satisfy the [H-Trans]
condition? To be more precise, prove that there is a listing of the strings of the set
P(n, k, p) = {a1 a2 ak | 1 a1 < a2 < ak n, and ak a1 p} so that each
successive string differs in only one position. What is |P(n, k, p)|?
19. [1] The R(n, k, p) lists can be defined for p > 2. Show that they exist, as long as
n k p.
Questions about permutations
20. [1+] How many transpositions does Algorithm 5.10 use in generating all permutations
of [n]?
c
uvic01, Frank
Ruskey, 19952001

5.14. EXERCISES

181

21. [2] Develop a loopless version of Algorithm 5.12 for generating all permutations by
adjacent transpositions.
22. [1] Show that the parity difference of An , the set of alternating permutations of length
n, is 1 if n is even and is 0 if n is odd.
23. [R] For n odd show that An can be generated by transpositions. The case of n even
follows from the results of [365] or [319].
24. [2] Show that there is a Hamilton path, call it A(n), in the hypercube Qn from 0n
to 1n if and only if n is odd. Show that there is a Hamilton path, call it Z(n), in Qn
from 0n to 1n1 0 if and only if n is even. Explain why these paths can be generated
by a CAT algorithm.
25. [2+] Using the results of the previous exercise, develop a Gray code for M(n) = {x
B(n) : x xR }, the set of lexicographically smallest bitstrings of the equivalence
classes of bitstrings equivalent under reversal. Successive words should differ by a
single bit. Show two types of paths: T(n) from 0n to 1n and a cyclic path N(n).
Explain why these paths can be generated by a CAT algorithm.
26. The Butterfly Network can be be thought of as a graph with vertices ??? and edges
???. Give a simple algorithm for listing the vertices along a Hamiltonian path in the
Butterfly Network.
27. [1+] Modify Algorithm 5.10 so that it generates all derangements of [n]. The algorithm does not have to be BEST, nor do successive derangements have to differ by
single transpositions.
28. [1+] Modify the Johnson-Trotter algorithm to develop a CAT algorithm for generating
all (n 1)!/2 rosary permutations of [n].
29. [1+] Give an example of a set of multiset permutations that have a cyclic Gray code
under transposition, but which do not have the prefix property (permutations with a
common prefix appear consecutively).
30. [R] Is every bipartite graph obtainable as the transposition graph of some set of
multiset permutations?
Questions about numerical partitions

31. [2+] Give a direct CAT implementation of the recursive definition of L and M for
generating numerical partitions.
Questions about trees

32. [2] Develop a ranking algorithm for the Gray Code that generates binary trees by
rotations.
c
uvic01, Frank
Ruskey, 19952001

182

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

33. [3] It is remarkable that sometimes a lexicographic listing is also a Gray code listing;
this exercise gives one such naturally occuring example (another example is numerical
partitions using the multiplicity representation). Let T be the binary tree which
consists of a single path of left children. Any binary tree may be constructed by
moving up this path toward the root, performing some number of rotations at the
successive nodes on this left path. Let ci be the number of rotations performed at
the node on the left path at height i 1. (a) Show that the sequence c1 , c2 , . . . , cn
uniquely
Pn identifies a tree and that these sequences are characterized by the property
that i=j ci n j for 1 j n. (b) Let C be the set of all such sequences and
let them be ordered lexicographically. Show that two successive sequences in C differ
in one or two adjacent positions. (c) Develop a loopless algorithm for generating the
sequences of C in lex order.
34. [R] Can well-formed parenthesis strings be generated by homogeneous transpositions? What about 2-apart transpositions?
35. [2] Give a small example showing that there does not exist a Gray code, using
diagonal flips, of the triangulations of an arbitrary polygon.
Questions about Linear Extensions
36. [1] Prove that if P is a poset with a pair of siblings, then G(P) is Hamiltonian. [[R]]
Under the same assumption, prove the stronger result that G0 (P) is Hamiltonian.
37. [2] Let F be a forest poset on elements v1 , v2 , . . . , vn , and let d(vi ) be the number of
descendants of vi in the forest (counting an element
as a descendant of itself). Prove
Qn
that the number of extensions is e(F ) = n!/ i=1 d(vi ).
Miscellaneous questions
38. [R+] Consider the graph Gk whose vertex set consists of all bitstrings of length 2k +1
in which each bitstring has k or k + 1 ones. The edges of Gk connect those vertices
at Hamming distance one. Find a Hamiltonian path in Gk for all k 0. [This is a
well-known problem, sometimes known as the middle two levels problem, which, as
of this writing, is unresolved.]
39. [3] Implement the Eades-Hickey-Read algorithm to run in constant amortized time
and O(n) space.
40. [1+] Modify Algorithm 5.20 so that it outputs the elements of B(n + k 1, n) by
homogeneous transpositions.
41. [2] Consider the algorithm for generating the k-compositions of n by transpositions,
modified to list the elements of B(n + k 1, n) (as in the previous exercise). Let
s(n, k) be the number of 0s between the two bits that get transposed, summed over
all combinations. Show that s(n, 1) = 0 and, if k > 1,

0
if n odd
s(n, k) = s(n 1, k) + s(n, k 1) +
k 2 if n even.
c
uvic01, Frank
Ruskey, 19952001

5.14. EXERCISES

183

Solve this recurrence relation. Show that the corresponding recurrence relation for
the Eades-McKay listing is r(n, 1) = r(n, n) = 0 and, if 1 < k < n,
r(n, k) = n k 1 + r(n 2, k 2) + r(n 2, k 1) + r(n 1, k).
Solve this recurrence relation. Of course, s(n, k) = r(n + k 1, n) in view of Lemma
5.5.
42. [1+] Derive a simple recursive formula for the rank of a k-composition of n in the
order of the Knuth list Comp(n, k).
43. [2+] Show that permutations can be generated by a tree of transpositions (Slater),
and that there is a CAT algorithm (Jiang).
44. [2+] Implement the SJT algorithm in a loopless manner using the hint at the end of
the subsection describing the SJT algorithm.
45. [R] Can the elements of T(n) be generated so as to satisfy the [2-Trans] closeness
criteria: |i j| 2?
46. [R] Can the elements of T(n) be generated so as to satisfy the [H-Trans] closeness
criteria: no 0s between bi and bj ?
47. [R+] Does G(P) have a Hamilton path whenever D(P) = 0?
48. [2+] Show that multiset permutations of type 0n0 1n1 tnt can be generated by a
CAT algorithm so that successive permutations differ by an adjacent transposition
whenever there are at least two odd ni . (Stachowiak)
Questions about set partitions
49. [R] Can RG sequences be generated so that successive sequences differ in only one
position, including the first and last partitions?
50. Consider the graphs G(n) and G(n, k) whose vertices are the RG sequences in S(n)
and S(n, k), and where an edge joins those sequences that differ by 1 in exactly one
position. [1+] Show that this graph is connected and bipartite. [1+] Define the index
of an RG sequence to be the sum of values in the sequence. Find a recurrence relation
for D(n, k), the number of RG sequences in S(n, k) of evenPindex minus the number
of odd index. Determine the value of D(n, k). Let D(n) = nk=1 D(n, k). Show that

1 if n mod 6 = 3, 4
0 if n mod 3 = 2
D(n) =

+1 if n mod 6 = 0, 1
[R] Find an involution proof of the equation above. [R] Find a Hamilton path
in G(n) if n mod 12 is not 4,6,7,9. These cases are ruled out since 012 (n 1) is a
pendant vertex.
51. [2] Show that J(P)2 is Hamiltonian for any poset P.
c
uvic01, Frank
Ruskey, 19952001

184

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

52. [R] Develop a CAT algorithm for generating the ideals of an arbitrary poset.
53. [R] Let R = (r1 , r2 , . . . , rm ) and C = (c1 , c2 , . . . , cn ) be nonnegative integral vectors,
where C is montone and ri n for i = 1, 2, . . . , m. The Gale-Ryser theorem asserts
that a m by n (0,1)-matrix with row sums R and column sums C exists if and only
if C R , where R indicates the conjugate of R (regarded as an integer partition).
Let G(R, C) be the graph whose vertices are all such (0,1)-matrices. Two vertices are
adjacent if they differ only by a 2 by 2 sub-matrix, one of which is the identity I2
and the other of which is obtained by flipping the bits of I2 . Ryser ??? showed that
G(R, C) is connected. Investigate the Hamiltonicity properties of G(R, C).
54. [2] Refering to the previous exercise, and assuming that c1 = c2 = = cn , explain
why G(R, C) is Hamiltonian and that a Hamilton cycle can be generated by a CAT
algorithm. HINT: How may solutions are there?
55. [R] Let G be a planar graph. A two-switching in a planar embedding of G is the
flip of some subgraph of G (explain this more !!!). Define the embedding graph
Em(G) to be the graph whose vertices are the different planar embeddings of G,
where two embeddings are joined by an edge of Em(G) if and only if they differ by a
two-switching. Whitney (reference) proved that E(G) is always connected. Show by
example that it is not necessarily bipartite. Explore the Hamiltonicity properties of
the embedding graph.
56. [R] Consider the set of all perfect matchings on the n by m grid. Any grid with
a perfect matchings must contain a 4-cycle that contains two matched edges. By
switching the matched and unmatched edges on the 4-cycle another perfect matching
is obtained. Define the switching graph Sw(n, m) to be the graph whose vertices are
the perfect matchings on an n by m grid, and whose edges are join vertices that differ
by a switching. Is Sw(n, m) connected? Investigate the Hamiltonicity properties of
the switching graph.
57. [R] The number of labellings of a free tree T with n nodes is n! divided by the size
of the automorphism group of T . Show that there is Gray code of such labellings,
where successive labellings differ by an exchange of labels on some edge. Can such a
Gray code be generated efficiently?

5.15

Bibliographic Remarks

The Binary Reflected Gray Code


United States patent number 2632058 was granted to Frank Gray (assignor to Bell Telephone Laborartories, Inc.) on March 17, 1953 after having been applied for on November
13, 1947 ([160]). For a history of the Gray code see Heath [175].
Papers about the Binary Reflected Gray Code and its applications: Arazi [11], Chang,
Lee, and Du [52], Cohn [66], Er [106], Flajolet and Ramshaw [130], Gilbert [150], Kirschenhofer and Prodinger [210], Richards [343], Sharma and Khanna [384], [382], [383], van Zanten
[432], Cummings [75]. Diaconis and Holmes [84] discuss applications of combinatorial Gray
codes to statistics.
c
uvic01, Frank
Ruskey, 19952001

5.15. BIBLIOGRAPHIC REMARKS

185

Games and Puzzles


The classic Towers of Hanoi puzzle actually has nothing whatever to do with Hanoi
(except, perhaps, that the French were involved in Indochina at the time), but was created
by the French mathematician and puzzle designer Edouard Lucas in 1883.
For more on the Towers of Hanoi, the excellent article of Poole [313] is a good starting
point. A great many papers have been written about the Towers of Hanoi problem and its
extensions; see for example, Lu [257], Minsker [282], Wu and Chen [462], Veerasamy and
Page [434], Chapter 51 of Dewdney [83], and Section 7.6 of Parberry [304]. Many other
references may be found in [313]. The simple iterative solution given in Exercise 11 is from
Buneman and Levy [46].
The SPIN-OUT puzzle is described in Pruhs [323].
Combinations
[Trans] Our approach follows Bitner, Ehrlich, and Reingold [32], but see also Misra [284],
Tang and Liu [410], [251]. [H-trans] The first published account of a list satisfying this condition is due to Ehrlich [101]; furthermore, Ehrlich provides a loopless implementation of his
algorithm. However, his list is much more complicated to define than the one we presented,
which was first published by Eades and McKay [96] , although Chase [57] attributes such
a list to Donald E. Knuth and William R. Bauer! [2A-trans] The list formulation here is
believed to be new, but has its roots in the papers of Chase [57] and McCarthy and Jenkyns
[194]. [2-trans] There are three known proofs of that B(n, k) can be generated by adjacent
transpositions if and only if n is even and k is odd (or k = 0, 1, n 1, n), discovered independently by Buck and Wiedemann [44], Eades, Hickey, and Read [95], and Ruskey [364].
Other papers about Gray codes for combinations are Lam and Soicher [237] , Chase [57].
The first algorithm to satisfy the homogeneous transposition condition was discovered by
Lathroum and implemented by Chase [56]. Ehrlich [101] gave a loopless algorithm that
can start at any string of the form 0p 1k 0npk ; a nice description of this algorithm may be
found in the book of Even [115]. !!!! Check on these !!!!
Gray codes for Lyndon words are discussed by Cummings [74] (for more on Lyndon
words see Chapter ??).
!!!! What does Luneburg [258] discuss??? !!!!
The book of L
uneburg [260] contains much material on Gray codes. In particular, he
has developed algorithms for doing arithmetic where integer i is represented by unrank (i).
See Sedgewick [377] for an excellent survey of algorithms for generating permutations by
transpositions. The Steinhaus-Johnson-Trotter algorithm is from Steinhaus [405], Johnson
[198], and Trotter [418]. The discussion of multiset permutations follows Ko and Ruskey
[227]. Other algorithms for generating permutations of a multiset by transpositions may be
found in Hu and Tien [185] , Sag [368], Bratley [40] and Chase [55]. Korsh and Lipshutz [230]
develop a loopless algorithm for listing permutations of a multiset, but in a non-standard
representation.
Set Partitions
A Gray code for set partitions is discussed in Kaye [208]; it is based on a Gray code that
is attributed to Donald Knuth by Wilf [451]. The lists of partitions into a fixed number of
blocks as defined in Section 5.9 are from Ruskey [353]. Another Gray code for set partitions
c
uvic01, Frank
Ruskey, 19952001

186

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

into a fixed number of blocks is attributed to Brian Hansche in Fill and Reingold[126]. !!!
Ehrlich [101] loopless algorithms ??? Other Gray codes for generalized restricted growth
strings are developed in Ruskey and Savage [358].
Binary Trees
The Gray code for well-formed parentheses is from Ruskey and Proskurowski [356] (see also
[316]). Another Gray code for well-formed parentheses may be found in Vajnovszki [424].
By counting the sizes of the partite sets in T(n, k) Ruskey and Miller [355] showed that
there is no adjacent transposition (i.e., [A-Trans]) Gray code for T(n, k) for even n or for
odd n > 5 and odd k (except when k = 0, 1, n 1, or n).
The Gray code for binary trees (Section 5.5) is from Lucas, Roelants van Baronaigien,
and Ruskey [256] (see also Lucas [255]). The solution to Exercise 33 is from Roelants van
Baronaigien [427].
The rotation graph was used by Pallo [297], who showed that the directed version
obtained by using only left rotations is a lattice. This result was anticipated by Huang
and Tamari [183]. Further properties of this lattice are given in Pallo [299]. The problem
of determining the diameter of the rotation graph Gn was considered by Sleator, Tarjan,
and Thurston [392]. Lucas [255] showed that the rotation graph has the Hamilton path
generated in Section 5.5; she also showed the much harder result that the rotation graph is
Hamiltonian. Knuth [222] (pp. 150-155) presents an algorithm for generating the rotation
lattice (or graph).
Compositions
The Gray code for compositions presented in Section 5.7 is due the Knuth (unpublished) and
is discussed by Klingsberg [214]. The relationship between this list and the Eades-McKay
list was noted in Ruskey [353].
Partially Ordered Sets
A Gray code for the ideals of forest poset is discussed in Koda and Ruskey [226]. Their
algorithm is a generalization of the Binary reflected Gray code and can be implemented as
loopless algorithm. Gray codes for the ideals of interval orders, both of all sizes and of a
given size, have been given by Habib, Nourine, and Steiner [165].
A loopless implementation of the Pruesse-Ruskey algorithm has been developed by Canfield and Williamson [49]. Extensions of the Pruesse-Ruskey algorithm to basic words of
antimatroids is carried out in Pruesse and Ruskey [320].
Carla Savage [369] has developed an algorithm for listing partitions so that each partition
differs from its predecessor by increasing one part by one and decreasing another part by
one (where parts of size 0 are allowed). These results have been extended to the following
classes of partitions: (a) partitions of n into parts of size at most k, (b) partitions of n into
distinct parts of size at most k; see Rasmussen, Savage and West [333].
Miscellaneous Gray Codes
An excellent survey of results on combinatorial Gray codes is given by Savage [371]
Prokurowski, Ruskey, and Smith [317].
c
uvic01, Frank
Ruskey, 19952001

5.15. BIBLIOGRAPHIC REMARKS

187

Rall and Slater [325].


Squire, Savage, and West [397] have developed a Gray code for the acyclic orientations
of a graph.
Permutatation generation papers (some may belong in lex.tex): Boothroyd
[37], [38], [39], Bratley [40], Coveyou and Sullivan [72], Eaves [97], Fike [125], Hall and
Knuth [168], Heap [174], Howell [181], [182], Langdon [238], Ord-Smith [291], [293], Peck
and Schrack [305], Pleszczy
nski [312], Rohl [348], Robinson [346], Sag [368], Tompkins [415],
Wells [439].
Rosemary permutations are discussed in Harada [169] and Read [334].
Combination generation papers: Akl [6]. Bitner, Ehrlich and Reingold [32]. Chase [56].
Ehrlich [100] Ehrlich [101] Kurtzberg [236] Lehmer [243] Lehmer [244] !!! some belong in
lex chapter? !!!
loopless Algorithms
Loopless algorithms for generating permutations, set partitions with at most k blocks, set
partitions with exactly k blocks, r-subsets of n where l r u, compositions and subsets
are developed in Ehrlich [101], who seems to have originated the term loopless.
Let T be a tree with root r. Koda and Ruskey [226] give a loopless algorithm for listing
all subtrees of T that are rooted at r.
Dershowitz [80] gives a loopless algorithm for generating permutations. Fenner and
Loizou [121] give loopless algorithms for generating numerical partitions. Canfield and
Williamson [49] give a loopless implementation of the Pruesse-Ruskey algorithm for generating linear extensions. Roelants [427] gives a loopless algorithm for generating a certain
sequence representation of binary trees. Roelants and Neufeld [429] give a loopless algorithm
for generating a certain representation of subsets with a given sum.

c
uvic01, Frank
Ruskey, 19952001

188

CHAPTER 5. COMBINATORIAL GRAY CODES: ALGORITHMIC ISSUES

c
uvic01, Frank
Ruskey, 19952001

Chapter 6

Combinatorial Gray Codes: Graph


Theoretic Issues
In several countries, and in particular England, there has developed a systematic methodology for ringing a series of bells, typically mounted in a church tower, but sometimes rung
with hand-held bells. This art of bell ringing is sometimes called change-ringing or
campanology and has its own peculiar terminology. Suppose that there are n distinct
bells; they are arranged in descending order of pitch. Bell 1 is called the treble, bell n the
tenor. A change is the ringing of each of the bells in some order, each bell rung exactly
once. A round is the change obtained by ringing the bells in the order 1, 2, . . . , n. An extent
consists of n! + 1 changes subject to the following three inviolable rules.
1. The first and last change are both rounds.
2. No change is repeated. Thus except for rounds each possible change is rung exactly
once.
3. From one change to the next, no bell moves more that one position in its order of
ringing.
The following rules are viewed as being desirable but not absolutely necessary.
4. No bell occupies in the same position for more than two successive changes.
5. Each bell (except perhaps the treble) does the same amount of work. The work
that a bell does is the number of position changes that it undergoes.
6. The sequence of changes is palindromic in a sense to be made clear later.
From these descriptions, it is clear that an extent consists of all n! permutations of [n]
subject to various rules. We have already seen lists of permutations that satisfy the first
three rules. The list produced by Johnson-Trotter algorithm does, but does not satisfy
rule 4 because a bell may occupy the same position for many changes. Because of rule 3,
each transition may be regarded as an involution, that is, as a product of disjoint 2-cycles
(transpositions).
The natural setting for examining questions of campanology is within the context of
finding Hamilton cycles in certain Cayley graphs. The first four rules are the specification
189

190 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


of a combinatorial Gray code for Sn . The following theorem was proven by Rapaport [332],
and shows the existence of an extent satisfying the first 4 rules.
Theorem 6.1 The Cayley graph Cay(Sn : X), where X is the three element generating set
given below, is Hamiltonian.
X = {(1 2), (1 2)(3 4)(5 6) , (2 3)(4 5)(6 7) }
Campanology is a surprising source of combinatorial Gray codes. Combinatorial Gray
codes give rise to many interesting mathematical problems, and most of these problems can
be converted into questions about graphs. In this chapter we explore the interplay between
combinatorial Gray codes and graph theory. The central concept is that of a Hamilton path
or cycle in a graph. The subject of Hamiltonicity is vast and impossible to cover in a single
chapter or even a book, so we concentrate on those graphs most relevant to combinatorial
Gray codes. These are the graphs whose vertices are combinatorial objects with edges
defined in some natural way. These graphs often exhibit a high degree of symmetry and
tend to be sparse.
We begin our discussion with the many fascinating properties of the hypercube. Succeeding sections discuss results that dont rely on symmetry, and then those that do rely
on symmetry particularly Cayley graphs.

6.1

The Hypercube

The queen of graphs is the hypercube. In the the previous chapter we showed that it is
Hamiltonian and how to quickly generate the vertices along a particular Hamilton cycle.
In this section we will explore further some of its other properties, concentrating on those
that relate to Hamiltonicity. These results are all constructive, but developing algorithms
for generating the corresponding lists remains largely unexplored.
Lemma 6.1 The graph Qn is Hamilton-connected.
Proof: !!! This proof remains to be written. !!! Does this follow from the result that any
Cayley graph over an Abelian group is Hamilton-connected? There is a nice proof of this
in Savage, Squire, and West [397].
2

6.1.1

Monotone Gray Codes

In the BRGC sometimes a 0 changes to a 1 and other times a 1 changes to a 0. Let us


record a x for a 0 to 1 change and a y for a 1 to 0 change. For n = 4 we obtain the sequence
= xxyxxyyxxxyyxyy.
Let us say that the weight, w(), of such a sequence is the number of xy or yx pairs that
occur. For our example w() = 7. Over all possible Hamilton paths on the n-cube, what is
the maximum value that w could attain? An upper bound is easy to obtain. Consider the
contiguous subsequence of induced by the changes between 0n and 1n . The number of
xs in minus the number of ys is n. Thus
w() 2n n 1.

(6.1)
c
uvic01, Frank
Ruskey, 19952001

6.1. THE HYPERCUBE

191
00000
00001
00011
00010
00110
00100
01100
01000

11000
10000
10001
10101
10100
10110
10010
11010

01010
01011
01001
01101
00101
00111
01111
01110

11110
11100
11101
11001
11011
10011
10111
11111

Figure 6.1: A monotone Gray code for n = 5.


It is in fact possible to construct a Gray code where the bound is attained when n is
odd. The following list for n = 5 arises from the Gray code shown in Figure 6.1; it attains
the bound.
x|xyxyxyxyx|xyxyxyxyxyx|xyxyxyxyx|x
Consider any subsequence of alternating x and ys, as delimited by in the sequence
above. The corresponding subsets have k or k + 1 elements for some k. A monotone Gray
code is one in which all the k-subsets precede any of the k + 2 subsets. The existence of
monotone Gray codes was proven by Savage and Winkler [372], and we give their proof
below.
Let Gn (i) denote the subgraph of Qn induced by A(n, i) A(n, i + 1); i.e., by all the
i and i + 1-subsets. If is a permutation of [n] and A [n], then (A) denotes the set
{(a) | a A}. This notation is extended in the natural way to sequences of subsets:
(A1 , A2 , . . . , Am ) = (A1 ), (A2 ), . . . , (Am ).
Thinking of Qn as a lattice under the subset relation, let us say that a chain = x0
x1 xn = [n] in Qn is an antipodal chain. The number of such chains is n! (see
Exercise 7). Furthermore, if y0 y1 yn is another antipodal chain then there is a
permutation of [n] such that yi = (xi ) for i = 0, 1, . . . , n, namely the one obtained by
taking (xi \ xi1 ) = yi \ yi1 for i = 1, 2, . . . , n.
Theorem 6.2 (Savage-Winkler) There is a monotone Gray code in Qn that can be written
sign(n1)

P0 , P11 , P2 , P31 , . . . , Pn1

such that each Pi is a path in Gn (i) of the form


Pi = yi , . . . , xi+1 ,
where {xi }ni=0 and {yi }ni=0 are antipodal chains.
Proof: The proof proceeds by induction on n. If n = 1, then the Hamilton path is
P0 = , {1} = y0 , x1 . Assume that the theorem is true for n; we prove it for n + 1. If
A [n] we denote by A0 the set A {n + 1} and extend this notation componentwise to
sequences of subsets.
c
uvic01, Frank
Ruskey, 19952001

192 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


Let P0 , P1 , . . . , Pn1 be as defined in the statement of the theorem. For n+1 we will show
sign(n)
how to construct a Gray code R = R0 , R11 , . . . , Rn
in Qn+1 satisfying the theorem.
Define Ri as follows.

if i = 0
P0
0 )1 , P
(Pi1
if
0<i<n1
Ri =
i

0
1
(Pn1 )
if i = n
Since R contains the subsets of P0 , P1 , . . . , Pn1 (all subsets not containing n+1) together
0
with the subsets of (P00 ), (P10 ), . . . , (Pn1
) (all subsets containing n + 1) and each subset
occurs exactly once, R contains all vertices of Qn+1 exactly once. We need to show that
sign(i)
sign(i+1)
the interfaces between the Ri
and Ri+1
lists have Hamming distance one and that
each Ri is a path in Gn+1 (i).
0 )1 and P is ((x0 ), y ). Since (x0 ) =
For 0 < i < n the interface between (Pi1
i
i
i
i
yi {n + 1} they have Hamming distance one. The other interfaces are between Ri and
1
1
Ri+1
, or between Ri1 and Ri+1 . Between Ri and Ri+1
the interface is (xi+1 , xi ), which has
1
0 , y 0 ),
Hamming distance one. Between Ri and Ri+1 the interface is (x0i+1 , x0i ) = (yi+1
i
which has Hamming distance one.
The initial interface between P0 and P11 is (x1 , x2 ), which has Hamming distance one.
0
0
0
The final interface is either between Pn1 and (Pn1
) or between (Pn2
) and (Pn1
)1 .
0
0
0
Between Pn1 and (Pn1 ) the interface is (xn , (xn )) = ([n], [n + 1]). Between (Pn2 )
0
0
and (Pn1
)1 the interface is ((x0n1 ), (x0n )) = (yn1
, yn0 ). In either case, the interfaces
have Hamming distance one.
For the path R, from the definition of the Ri , the two antipodal chains are
x0 , x1 , . . . , xn , [n + 1]
and
y0 , (y00 ), (y10 ), . . . , (yn0 ).
2
We make now a few observations about the above proof. The number of nodes in the
path Pi of Qn is 2 n1
i . In the first antipodal chain, the recursive construction implites
that xi = [i]. When n is odd, the monotone Gray code ends at [n + 1]; when n is even, it
0
ends at (yn1
).
Cube free Hamilton cycles in the cube
The first 2n1 vertices of the BRGC for n are a Hamilton path in a Qn1 , as are the last
2n1 vertices. It is natural to ask whether there is a Hamilton cycle in Qn that does not
include a Hamilton path in Qk for any 1 < k < n. Perhaps surprisingly, the answer is yes.
As far as we are aware, no one has addressed the problem of developing an algorithm
for generating such a path.
A collection of results about the hypercube
To close this section we simply list some interesting results and conjectures about the ncube.Many of these results involve Gray codes, either directly or indirectly.
c
uvic01, Frank
Ruskey, 19952001

6.1. THE HYPERCUBE

193

1. For n even there is a Hamilton decomposition of the n-cube. A Hamilton decomposition is a partitioning of the edges of the cube such that each partite set (term?) is a
Hamilton cycle. [See Alspach, Bermond, and Sotteau [8] for more information on this
problem.]
2. The n-cube is pancyclic; i.e., it possesses cycles of all possible even lengths. The ncube is Hamilton-connected; i.e., it possesses Hamilton paths between every pair of
vertices of opposite parity. This result is due to Saad and Scults [367].
3. For n 5, there is a Hamilton cycle in Qn that does not have any subpath that is
a Hamilton path of a Qr for 2 r n 1. This result is due to Mills [281]; for a
simpler proof see Ramras [329].
4. Imagine the following game (one player) played on the n-cube (may be easier to
visualize as the Boolean algebra lattice). Place 2n tokens arbitrarily at the vertices
of the cube; a vertex may get more than one token. Let v be a node containing at
least two tokens and w a node adjacent to v. A token may be moved from v to w
but at a cost of one token, charged to v. There is a way to play the game to that
a token occurs at the node corresponding to 0n . This result is due to Fan Chung.
[REFERENCE???]
5. All trees are bipartite. Let us say that a tree is balanced if the number of nodes in its
two partite sets are equal. Havel [173] conjectures that all balanced trees of maximum
degree three and with 2n nodes are spanning subgraphs of the n-cube. A weaker
conjecture of Alan Wagner [REFERENCE???] asserts that all trees of maximum
degree three that possess a perfect matching with 2n nodes are spanning subgraphs
of the n-cube.
6. The genus of the n-cube is 1 + (n 4)2n3 . See Ringel [345].
The crossing number of the n-cube is UNKNOWN ???.
The thickness of the n-cube is 1 + bn/4c. See Kleinert [213].
The bandwidth of a graph G with vertices 1, 2, . . . , n is
b(G) = min

max {|(v) (w)|}

Sn [vw]E(G)

A related quantity is obtained by taking the sum rather than the maximum.
X
s(G) = min
{|(v) (w)|}
Sn

[vw]E(G)

k
P
The bandwidth of the n-cube is b(Qn ) = n1
k=1 bk/2c . The minimum sum of an
n-cube is s(Qn ) = 2n1 (2n1 1). See Harper [171], [172].
7. A natural question to ask is: How many edges can be removed from a n-cube and still
approximately preserve the proximity of vertices? There is a spanning subgraph G of
Q = Qn of maximum degree d(n + 1)/2e (and average degree (n + 1)/2) such that, for
all vertices u and v,
dG (u, v) 2 + dQ (u, v).
c
uvic01, Frank
Ruskey, 19952001

194 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


This result is due to Liestman and Shermer [247].
8. Among all bipartite graphs, n-cubes are characterized by the property that there are
d! shortest paths between vertices that are distance d apart. This result is due to
Foldes [136]. !!! look up more recent result in J. Graph Theory !!!
9. Let P be a perfect matching in Qn . I conjecture that there is a Hamilton cycle in
Qn that contains every edge in P . Examples can be found of maximal matchings that
cannot be extended to Hamilton cycles.
10. Venn diagram problem.
11. The number of spanning trees of Qn is
n
n
1 Y
(Qn ) = n
(2i)( i ) .
2

(6.2)

i=1

This may be found from the Matrix Tree Theorem. Harary, Hayes and Wu [170]
attribute this result to A.J. Schwenk (Spectrum of a graph, Ph.D. dissertation, University of Michigan (1973)).
12. The snake-in-a-box problem asks what is the length of the longest induced path
in Qn . Klee has proven that the length is asymptotic to 2n /n. !!! Check on this...
REFERENCE ???
13. Defining dom(G) to be the largest value k for which there exists a partition of V into
k blocks such that each block is a dominating set (a dominating set is a set of vertices
that are adjacent to every other vertex). Mike Fellows in his Ph.D. thesis shows that
dom(Qn ) is asymptotic to n.

6.2

Hamiltonicity Results for Graphs

The closeness graphs of many types of combinatorial objects are vertex-transitive and the
existence of algorithms can often be inferred from general results on vertex-transitive graphs.
We mention several such results.
There is a certain natural sense in which reasonable closeness criteria always lead to
Gray codes. By reasonable we simply mean that the underlying graph is connected. But if
this is the case then we can get a Gray code in which successive objects differ by at most
three iterations of the closeness operator. The formal statement is as follows.
Theorem 6.3 (Sekannina) The cube of any connected graph is Hamiltonian
Proof: It is sufficient to prove the theorem for trees. Proceed by induction on the height
of the tree, where some node r is chosen arbitrarily to be the root. We actually prove the
stronger statement that T 3 has a Hamilton path starting at r and ending at any specified
child x of r. The statement is clearly true if the height is 0 or 1. So let T be a tree of
height at least 2. Let T1 , T2 , . . . , Tp be the principle subtrees of T , i.e., the rooted trees that
remain when r is removed. Assume, without loss of generality, that x is the root of Tp , and
c
uvic01, Frank
Ruskey, 19952001

6.3. VERTEX-TRANSITIVE GRAPHS

195

Figure 6.2: Illustration of the 4-cycle construction.


let Pi be a Hamilton path through Ti3 that starts at the root ri of Ti and ends at a child of
ri (or at ri if Ti is a single node). Then
r, P11 , P21 , . . . , Pp1
is a Hamilton path in T 3 that starts at r and ends at x = rp .

That proof was relatively simple. Much harder to prove is the following result of Fleishner, which we state but do not prove.
Theorem 6.4 (Fleishner) The square of a 2-connected graph is Hamiltonian.

6.2.1

A useful lemma

If G is an undirected graph, then vertex-disjoint cycles C and C 0 are said to share a 4-cycle
if there is a 4-cycle u, v, x, y such that [u, v] C and [x, y] C 0 . A cycle-decomposition of
G is a collection of vertex-disjoint cycles in G such that every vertex of G is on some cycle.
The following simple lemma is the basis of many proofs of Hamiltonicity. It is most often
applied when p = 2 (and can be viewed as an iterative application of the p = 2 case).
Lemma 6.2 (The 4-cycle construction) If C1 , C2 , . . . , Cp form a cycle-decomposition
of G such that C1 and C2 share a 4-cycle, Cp1 and Cp share a 4-cycle, and Ci1 and Ci
share a pair of 4-cycles for i = 3, . . . , p 1, then G is Hamiltonian.
Proof: This proof is best understood by considering Figure 6.2. Note that two of edges
on the pair 4-cycles can be adjacent on the same cycle Ci .
2

6.3

Vertex-Transitive Graphs

Not all connected vertex trasitive graphs are Hamiltonian. Four such graphs are known.
Three of them are shown in Figure 6.3. The first graph is known as the Peterson graph,
which is well-known to every student of graph theory as being non-Hamiltonian and vertextransitive. The second graph is obtained from it by replacing each vertex with a triangle.
The third graph is known as the Coxeter graph. The fourth example, which is not shown,
is obtained from the Coxeter graph by replacing every vertex by a triangle, in the same
manner that the second graph is obtained from the Peterson graph.
c
uvic01, Frank
Ruskey, 19952001

196 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES

Figure 6.3: Three of the four known non-Hamiltonian vertex-transitive graphs. The second
is obtained from the first by replacing each edge with a triangle. The fourth (not shown) is
obtained from the third in a similar manner.
sz) Does every vertex-transitive graph have a Hamilton path? Aside
Question 6.1 (Lova
from the four known counter-examples, are all other vertex-transitive graphs Hamiltonian?
Faber-Moore graphs. Arrangement graphs.

6.4

Anti-Gray Codes

In some applications a list of combinatorial objects is required where successive objects


differ by a large amount. For example, one might a want a list of all permutations so that
successive permutations differ in all positions; i.e., each permutation is a derangement of
the previous one. As another example, one might want a list of all length n bitstrings in
which successive bitstrings differ in n 1 positions. We will simply list some of the known
results. Algorithmic issues are usually not a big concern.

6.4.1

Permutations

Below is a list of all 4! permutations of [4] in which successive permutations differ in all
positions.
1234
4123
2341
3412
1324
4132
3241
2413
3124
4312
1243
2431

3214
4321
2143
1432
2314
4231
3142
1423
2134
4213
1342
3421
c
uvic01, Frank
Ruskey, 19952001

6.5. HAMILTON CYCLES IN CAYLEY GRAPHS

197

The key to the construction of this table (and its generalization to arbitrary values of
n) lies in the permutations of [n 1] = [3] shown in bold. These are the the permutations
of [n 1] = [3] that are output by the Steinhaus-Johnson-Trotter algorithm, but any listing
of permutations in which successive permutations differ by adjacent transpositions will do.
The other permutations of the table are obtained by appending n, repeatedly rotating the
permutation to the right, and finally arranging those rotations in an order that leaves n in
the second position (the underlined 4s).
Why does this construction work? Clearly, all n! permutations are produced, and those
that differ by right rotations differ in all positions. We need only consider what happens
before and after every n-th permutation. The interfaces there are of the form
xn1 n x1 x2 xn3 xn2
x01 x02 x03 x04 x0n1 n,
where x01 x02 x0n1 differs from x1 x2 xn1 by an adjacent transposition. For n 4, note
that xi ends in a position that is at least two away from i, and thus that it must be in
different position than x0i , even if it participated in the transposition. The construction
does not work for n = 3, because a derangement listing is impossible if n = 3.
~
The above construction show the existence of a Hamilton cycle in Cay(D
: Sn ), where
D is the set of derangements of [n]. In fact, we used the subset of derangements D =
, 2 , . . . , n , (1 2) n2 , (2 3) n2 , . . . , (n1 n) n2 , where is the right rotation (1 2 n).
Savage [370] permutation result.
Odd graphs? Vertices are combinations, edges join those vertices with empty intersection.

6.4.2

Subsets

Culberson and Rawlins result.

6.5

Hamilton Cycles in Cayley Graphs

We begin this section with two easy observations.


Lemma 6.3 Every Cayley digraph is vertex-transitive.
Lemma 6.4 Every Cayley digraph is strongly-connected.
To prove Lemma 6.3, given a, b G, we wish to show that there is an automorphism
mapping a to b which preserves adjacencies. Use (x) = (ba1 )x. To prove Lemma 6.4, in
view of Lemma 6.3, it is sufficient to show the existence of a path from the identity, 1, to
any other group element g. This is easy since there is, by definition of a generating set, a
sequence of generators g1 , g2 , . . . , gk whose product is g. These lemmata imply that every
undirected Cayley graph is vertex-transitive and connected.
There are two ways to specify a Hamilton cycle in a Cayley graph. First, we can list
the group elements as they occur along the cycle. Such lists will be delimited by the angle
brackets h and i. Secondly, we could successively list the generators along the cycle (more
generally, any path can be specified by a list of generators). Such lists will be delimited by
the square brackets [ and ] as a natural extension of the notation for edges. For example,
in the Cayley graph Cay({(1 2), (2 3)} : S3 ), the Hamilton cycle may be specified either as
h123, 132, 312, 321, 231, 213i, or as [(2 3), (1 2), (2 3), (1 2), (2 3), (1 2)].
c
uvic01, Frank
Ruskey, 19952001

198 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES

4321
2431

3412
1342

3241
2314

4132

3124

1234

2143
1423

4213

~
Figure 6.4: The non-Hamiltonian directed Cayley graph Cay({(1
2)(3 4), (1 2 3)} : S4 ).
Conjecture 6.1 Every connected Cayley graph is Hamiltonian.

6.5.1

Directed Cayley Graphs

Although it is believed that all undirected connected Cayley graphs are Hamiltonian,
there are examples of directed connected Cayley graphs that are non-Hamiltonian, such
~
as Cay({(1
2)(3 4), (1 2 3)} : S4 ) as illustrated in Figure 6.4. This example can also be specified as G = ha, b | a2 = b3 = (abab2 )2 = idi and is isomorphic to the direct product C2 A4 .
~
As another example, Rankin [331] shows that Cay({(2
4 6 5 3), (1 6 3)(2 4 5)} : A6 ) is not
Hamiltonian.
s) The graph Cayley graph of Cn Cm is Hamiltonian if
Theorem 6.5 (Trotter-Erdo
and only if there are positive integers p and q such that gcd(p, n) = 1 = gcd(q, m) with
gcd(m, n) = p + q.
The Trotter-Erdos [419] result is also said to be implicit in the work of Rankin [331].
Even though an Abelian directed Cayley graph can fail to have a Hamilton cycle, they all
possess a Hamilton path. The proof follows the same general reasoning as that of Theorem
6.7, the difference being that we cant use the inverse of generator. In the toroidal spanning
grid graph we simply snake back and forth from top to bottom to get the Hamilton path.
Lemma 6.5 Every Cayley digraph has a Hamilton path.
He is a compendium of Hamiltonicity results for Cayley graphs of particular orders.
These results are strong in the sense that they apply to any generating set for the group,
but are weak in the sense that groups with these orders are rather restricted. For example,
it is clear that every Cayley graph over a group of prime order is Hamiltonian, since all
such groups are cyclic.
Theorem 6.6 Let p and q be prime. Every Cayley digraph on a group of order p (p > 2),
pq, 4q (q > 3), p2 q (2 < p < q), 2p2 , 2pq, 8p, or 4p2 is Hamiltonian.
c
uvic01, Frank
Ruskey, 19952001

6.5. HAMILTON CYCLES IN CAYLEY GRAPHS


x1

x2

xM 2

199

x1

xM

x2

xM 2

xM
s1

s1
s1

s1
s1

s
x1

x2

x1
M 2

1
x1
1 x2

x1
M

xM 1

Figure 6.5: Hamilton cycles in Cayley graphs over Abelian groups.

6.5.2

Undirected Cayley Graphs

Theorem 6.7 Every Cayley graph over an Abelian group is Hamiltonian.


Proof: Let G be an Abelian group and let S be a generating set for G. We proceed by
induction on n = |S|. If n = 1, say with S = {s}, then
(1, s, s2 , . . . , sm1 = [s, s, . . . , s]
is a Hamilton cycle in Cay(S : G), where m is the order of s.
If n > 1, then let s S and T = S \ {s}. Furthermore, let H = hT i, the subgroup
generated by T . Inductively, there is a Hamilton cycle [x1 , x2 , . . . , xM ] in Cay(T : H), where
M = |T |.
Let X = [x1 , x2 , . . . , xM 2 ]. If m is odd, then the cycle is
[X, s, X 1 , s, X, s, X 1 , s, X, . . . , s, X, xM 1 , s1 , s1 , . . . , s1 , xM ].
If m is even, then the cycle is
1 1
1
[X, s, X 1 , s, X, s, X 1 , s, X, . . . , s, X 1 , x1
M , s , s , . . . , s , xM ].

The coset Hs generates the quotient group G/H. Hence, the cosets of H are H, Hs, Hs2 , . . . , Hsm ,
where m = |G|/|H| 1. These cycles are illustrated in Figure 6.5, which shows the underlying spanning toroidal grid structure of the Cayley graph. The horizontal direction
represents H and the vertical direction G/H. Each row corresponds to a coset of H, the
first row to H itself.
2
In fact, Cayley graphs over Abelian groups have much stronger Hamiltonicity properties,
such as Hamilton-connectedness.
Theorem 6.8 (Chen-Quimpo) Every connected Cayley graph over an Abelian group is
Hamilton-connected.

6.5.3

Cayley graphs over Sn

In this subsection we consider Cayley graphs whose vertex set consists of all n! permutations
of [n] and whose generators are transpositions.
With each set S of transpositions we may associate an undirected graph G(S), obtained
by treating each transposition (x y) as the edge [x, y] E(G(S)). Under what conditions
on G(S) is it the case that S generates the symmetric group Sn ? The answer is given in
the following theorem.
c
uvic01, Frank
Ruskey, 19952001

200 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES

2341

2431

2314

3214

2134

2143

3421

3124
1234

3241

1324
3142

2143

3412
1243

1342

1423
1432

4123

4132

4213

4231

4123

4321

Figure 6.6: The Cayley graph Cay({(1 2), (2 3), (3 4)} : S4 ).


Theorem 6.9 A set S of transpositions generates Sn if and only if G(S) is connected.
Proof: If G = G(S) is connected then between every pair of vertices x and y there is a
path x = x1 , x2 , . . . , xm = y in G. By the SJT (Steinhaus-Johnson-Trotter) algorithm, the
transpositions (x1 x2 ), (x2 x3 ), , (xm1 xm ) generate all permutations of {x1 , x2 , . . . , xm }.
In particular, the transposition (x y) is generated and thus the set S generates the transposition of any two elements. Since every permutation is the product of transpositions
(Lemma 2.7), every permutation is generated by S.
Conversely, if G is disconnected, say with x and y in different connected components,
then (thinking of the permutations in one-line notation and the transpositions acting on the
positions) there is no way to transpose an element in position x with an element in position
y. Therefore, not all of Sn can be generated.
2
Thus, in considering Sn , as generated by transpositions, we need only consider minimally
connected graphs G(S); i.e., we need only consider trees. A tree of transpositions is a tree
T whose nodes are labelled 1, 2, . . . , n, where n is the number of nodes in the tree.
Figure 6.6 shows the Cayley graph on S4 whose generating set is given by the tree of
transpositions that is a path on four vertices, labelled sequentially. Figure 6.7 shows another
Cayley graph on S4 , this one with generating set given by the tree of transpositions that is
a star. In general, a tree of transpositions always yields a Hamiltonian Cayley graph.
Theorem 6.10 (Slater) Let T be a tree of transpositions with n 4 nodes and s and d
be elements of [n]. Starting at an arbitrary permutation 1 , the graph Cay(T : Sn ) has a
Hamiltonian path 1 , 2 , . . . , n! , such that n! (d) = s.
Proof. The proof is by induction on n. The base cases for n = 4 are illustrated in Figure
6.5.3.
If n > 4 then let [p, q] be an edge of T , where p is a leaf, and let T 0 denote T with
vertex p removed. We may assume that d 6= p since T has at least two leaves. Now let
c
uvic01, Frank
Ruskey, 19952001

6.6. EXERCISES

201

Figure 6.7: The Cayley graph Cay({(1 2), (1 3), (1 4)} : S4 ).


1 (p) = i1 , i2 , . . . , in be a sequence of the nodes of the tree (i.e., a permutation of [n]) where
in 6= s. Let Lz (T 0 , x, y) denote a list 1 , 2 , . . . , (n1)! of permutations of [n] in which z
always occupies position p, successive permutations differ by a transposition from T 0 , and
(n1)! (x) = y. Inductively, such lists L exist with any initial permutation 1 for which
1 (p) = z and y 6= z (unless x = p).
Then there is a list of the following form that satisfies the conditions of the theorem.
Li1 (T 0 , q, i2 )
Li2 (T 0 , q, i3 )
..
.

Swap(i1 , i2 )
Swap(i2 , i3 )

Lin1 (T 0 , q, in1 )
Lin (T 0 , d, s

Swap(in1 , in )

At the interfaces between the L lists the permutations differ only by the transposition
(p q).
2
The proof of Slater is slightly different in that his final condition is n! (d) = s, rather
than n! (d) = 1 (s).

6.6

Exercises
Questions about the hypercube
c
uvic01, Frank
Ruskey, 19952001

202 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


base case listings go here
Figure 6.8: Base case listings.
1. [1+] Show that the subgraph of Qn induced by those vertices whose values in binary
are 0, 1, . . . , k has a Hamilton path, for all 0 k 2n 1. Develop a CAT algorithm
for generating such a Hamilton path.
2. [1+] Show that Qn is Hamilton laceable.
3. [1+] For n = 2, 3, 4, 5 show explicitly the monotone Gray codes in Qn that are produced by the Savage-Winkler proof.
4. [R] Give a bijective proof of the formula (6.2) for (Qn ), the number of spanning
trees of the n-cube.
5. [2] The cube-connected cycle CCCn is the graph obtained from Qn by replacing each
vertex with an n-cycle, the ith vertex of the cycle connected to the ith dimension edge
of the original vertex. More formally, V (CCCn ) consists of all pairs (d; x) where x is
a length n bitstring, 0 d n 1, and
E(CCCn ) = {[(d; x), (d + 1 (mod n); x)] | x V (Qn ) and 0 d < n}
{[(d; x), (d; y)] | [x, y] E(Qn )}
Show that CCCn is Hamiltonian. [2] Show that CCCn is a Cayley graph.
6. The d-dimensional butterfly graph BGdn is a graph whose vertex set consists of all
pairs (r; x) where 0 r < n, x nd , and whose edge set E(BGdn ) is
{[(r; x), (r + 1 mod n; y)] | Ham(x, y)}.
The graph BG23 is shown below (vertices with r = 0 are repeated for the sake of
clarity). The row labels are r, the column labels are x.
000

100

010

110

011

101

011

111

0
1
2
0

[2] Show that BGdn is Hamiltonian. [2] Show that BGdn is a Cayley graph.
7. [R] The transition sequence Tn from the BRGC gives rise to successive pairs of the
form (1, k) or (k, 1) which, regarded as the edges of an undirected graph, is a n-star.
In this case we call the n-star, the graph of position changes. Is there a Hamilton path
(or cycle) on Qn in which the graph of position changes is a path in which successive
vertices are labelled 1, 2, ..., n? What are necessary and sufficient conditions for a
graph to be graph of position changes of a Hamilton path on Qn ? [1] What is the
relationship between this problem and the previous one?
c
uvic01, Frank
Ruskey, 19952001

6.6. EXERCISES

203

8. [2] Every Hamilton path in Qn gives rise to a transition sequence S obtained by recording the position that changes. Recall that Tn = 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, . . . was the
transition sequence of the BRGC. Prove the following characterization: The sequence
S = s1 , s2 , . . . , sN is a transition sequence if and only if, for all 1 i j N = 2n 1,
the subsequence si , si+1 , . . . , sj contains at least one digit an odd number of times.
(For a Hamilton cycle add the condition that S, sN contains each symbol an even
number of times.)
9. [2] Find the 9 non-isomorphic Hamilton cycles in Q4 .
10. [2] Show the existence, in Q5 of a Hamilton cycle none of whose subpaths induce any
r-cubes, r = 2, 3, 4.
11. [R] Find a Gray code on Qn where the difference in successive strings, regarded as
binary numbers, is as small as possible. Results for the maximum difference or the
total difference would both be interesting.
12. [R] Regard the hypercube Qn with its vertices labelled by the subsets of [n] in the
usual manner. Is there a Hamilton path in Qn with the property that any subset
A [n] is preceded on the path by all subsets of A, with at most one exception? If
there is an exception, then it must occur immediately following A. For example, in
Q4 ,
, 1, 12, 2, 23, 3, 34, 4, 24, 124, 14, 134, 13, 123, 1234, 234
is such a path (the exceptions are underlined).
13. [1+] Give an example of a distributive lattice whose Hasse diagram is Hamiltonian
but that does not have a monotone Gray code.
14. In the n-disk Towers of Hanoi problem (Exercise ???) record for each disk the peg 1,
2, or 3 on which it is impaled and call the resulting string over [3] a tower string. Form
a graph Hn whose vertices are the tower strings and whose edges join permutations
differing by a valid move. [1] (a) How many tower strings are there as a function
of n? [1+] (b) Draw H3 . [2] (c) Discover a recursive construction of Hn and use it
to prove that Hn is Hamiltonian. [3] (d) Find a closed form expression for the total
sum of lengths of shortest paths between all ordered pairs of vertices in Hn .
15. The following generalization of the Towers of Hanoi problem is due to Bob Jamison.
A GTHP (Generalized Towers of Hanoi Problem) is a 4-tuple (P, G, s, t) where P is
a poset (the allowed disk placements on a single peg), G is a graph (the pegs), and s
and t are vertices of G (the starting and ending peg). The graph G could be directed
or undirected; its edges indicate whether a disk can move from one peg to another.
In the classic case P is a total order, meaning no disk may be placed on top of a
smaller disk, and G is a triangle, meaning that, subject to the constraint imposed by
P , a disk can be moved from one peg to any of the other two pegs. [R] Investigate
GTHP and try to prove some general results.
The following variants have been discussed in the literature. (a) The graph G is a path
of length 3 and s and t are the two pendant vertices. [1] Devise a recusive algorithm
for this case and determine how many moves it uses. Can you prove that youve used
c
uvic01, Frank
Ruskey, 19952001

204 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


the least number of moves? (b) The graph G is a directed triangle. Here there are
two possiblities for s and t. [1] Devise a simple recusive algorithm for this case and
determine how many moves it uses. There is a solution that uses o(4n ) moves. (c)
The graph G is the complete graph Kn (the case n = 4 has been
often considered).

[2] For the case when n = 4 devise an algorithm that uses O(n2 2n ) moves. (d) The
poset P is A2 A2 A2 ; that is, the posets consists of pairs (x, y) such that x
and y are incomparable, but are comparable with every other element of the poset.
[1] Devise an efficient recursive algorithm to solve the problem in this case.
16. [2] Show that there is a derangement ordering (anti Gray code) of nk if and only if
k > 2 or n = 1.
Questions about Cayley graphs

17. [1] Prove that every Cayley graph is 2-connected.


18. [1+] Show that the graph of Figure 6.4 is not only non-Hamiltonian, but that it does
not even possess a Hamilton path.
19. [2] The graph Cay({(1 2 3 4 5), (1 2)(3 4)} : A5 ) is known as the Buckyball. Prove
that the Buckyball is Hamiltonian.
20. [R] Consider the undirected Cayley graph on S7 with generators g1 = (2 3)(4 5)(6 7),
g3 = (1 2)(4 5)(6 7), g5 = (1 2)(3 4)(6 7), and g7 = (1 2)(3 4)(4 5). Determine whether
there is a Hamilton cycle in G of the form
a1 Xa2 Y a3 Xa4 Y a840 Y
where ai {g5 , g7 } and X = g1 g3 g1 g3 g1 and Y = g3 g1 g3 g1 g3 . This is a special form
of an extent known as Stedman Triples.
21. [4] Show that the undirected Cayley graph Cay(, : Sn ) is Hamiltonian. [2] A
doubly-adjacent listing of permutations is a Hamilton cycle in Cayley graph with
generators (i i + 1) for 1 i < n in which successive edges are either of the form
(j j + 1), (j + 1 j + 2) or the form (j j + 1), (j 1 j). What is the relationship between
the previous problem and that of finding a doubly-adjacent listing of Sn ?
Miscellaneous Questions

22. [2] How many Hamilton paths are there on a k by n grid graph, for k = 2, 3? [3]
What about k = 4 (give a recurrence relation for the number)?
23. [2+] Show that the cube of any connected graph is Hamilton-connected.
24. [1] Find a small connected graph G for which G2 is not Hamiltonian. There is a seven
vertex example.
c
uvic01, Frank
Ruskey, 19952001

6.7. BIBLIOGRAPHIC REMARKS

205

25. [3] Let G be an undirected graph. The acyclic orientation graph, AO(G), has as its
vertices the acyclic orientations of G and edges joining those vertices that differ by
the reversal of the orientation of a single edge. Show that the square of AO(G) is
Hamiltonian. Show that the prism of AO(G) is Hamiltonian.
26. [R] Prove or disprove the conjecture of Dillencourt and Smith [85] that aside from
the stellated tetrahedron, any simplicial polyhedron (all faces triangles) with vertex
degree at most 6, is 1-Hamiltonian. A graph is 1-Hamiltonian if deletion of any vertex
results in a Hamiltonian graph.
27. [R] Are there sets Sn of binary strings of length n whose distance from each other in
lex order and in least distance order are not proportional to each other? For example,
if Sn consists of all binary strings of length n, then in lex order we have distance sum
2 2n and in Gray code order we have distance sum 2n , so here the distance sums are
proportional.

6.7

Bibliographic Remarks

An excellent survey of results on combinatorial Gray codes is given by Savage [371]. This
survey mentions many of the topics that were covered in this chapter.
Hypercubes
A survey of results on hypercubes may be found in Harary, Hayes and Wu [170]. Hamilton
path between vertices of opposite parity (Latifi and Zheng [240]). Ranking the symmetries
and colorations of an n-cube is done in Fillmore and Williamson [128]. The number of
(nonisomorphic) Hamilton cycles in Qn is known only up to n = 5. For 1 n 5 the
numbers are 1,1,1,9,237675. Papers containing results about the number of Hamilton cycles
are Douglas[89] and Smith[393], but determining the asymptotic number is far from resolved.
Papers about Gray codes whose graph of transitions are restricted include Bultena and
Ruskey [45] and Wilmer and Ernst [456].
Cayley Graphs
The interesting connection between Hamiltonicity of Cayley graphs and campanology is
explored in a series of papers by A.T. White [441], [442], [443], [444].
A good survey of Hamiltonicity results on Cayley graphs is given in Witte and Gallian
[458] and its update by Curran and Gallian [78]. Other results about the Hamiltonicity of
Cayley graphs may be found in Alspach [9], Marusic [267].
Recent results and references in the broader survey of Hamiltonicity results in general
graphs may be found in Gould [158].
Kompelmakher and Liskovets [228] and Slater [391] showed that Cay(T : Sn ) is Hamiltonian, where T is a tree of transpositions; Tchuente [411] showed that the graph is Hamiltonlaceable.
Papers about the Hamiltonicity properties of Cayley graphs on vertex sets of interest
to combinatorists include Conway, Sloane, and Wilks [69],
The Chen-Quimpo theorem is from [58].
c
uvic01, Frank
Ruskey, 19952001

206 CHAPTER 6. COMBINATORIAL GRAY CODES: GRAPH THEORETIC ISSUES


Other Objects
Squire [400] determines necessary and sufficient conditions for there to be a Gray code of
q-ary strings that have some given forbidden contiguous substring.
Ruskey [366] showed that the linear extensions of disjoint chains could be generated by
transpositions; that they could be generated by adjacent transpositions was established by
Stachowiak [401].
Gray codes for the acyclic orientations of a graph are discussed in Squire, Savage, and
West [397], Squire [398], and in Pruesse and Ruskey [322].
The basic word graph of an antimatroid has vertices that are the basic words of an antimatroid and edges between those words that differ by a transposition of adjacent symbols
in the words. The feasible set graph of an antimatroid has vertices that are the feasible sets
of an antimatroid and edges between those sets that differ by a single element. Pruesse and
Ruskey [320] show that the square of the basic word graph is Hamiltonian and that the
square of the feasible set graph is also Hamiltonian.
The shuffle-exchange network SEn is a cubic graph over n2 . A generic vertex b1 b2 bn1 bn
in SEn is adjacent to b2 bn1 bn b1 , bn b1 b2 bn1 , and b1 b2 bn . Feldman and Mysliwietz [119] showed the existence of a Hamilton path in SEn ; Annexstein and Kuchko [10]
showed how to rank this path.
Hamiltonicity and its variants
Karaganis [206] shows that the cube of a connected graph is Hamilton-connected. Hendry
and Volger [176] show that the square of a connected S(K1,3 )-free graphs with at least three
vertices is vertex-pancyclic. Faudree and Schelp [118] showed that the square of a block is
panconnected.
Graphs with combinations as vertices
Let T be a graph on vertex set [n]. Define GT (n, k) to be the graph whose vertex set consists
of all k-subsets of [n] and whose edges connect subsets that differ by a transposition of
elements specified by an edge of T . If P is a path with vertices labeled sequentially, then
GP (n, k) is the adjacent transposition graph considered in Section 5.3, and Section 5.3
gives a Hamilton path in GP 2 (n, k). If the case that C is a cycle labelled sequentially, Enns
[102], building on partial results of Joichi and White [199], gives the following necessary
and sufficient conditions for GC (n, k) to contain a Hamilton cycle or a Hamilton path.
A Hamilton circuit exists in GC (n, k) if and only if neither n and k are both even, nor
k = 2 or n 2 for k > 7; a Hamilton path exists if and only if n and k are not both
even. That GKn (n, k) is Hamiltonian was demonstrated in Section 5.3 That GKn (n, k) is
Hamilton-connected was demonstrated by Jiang and Ruskey [195]
Other objects not explicitly covered in this chapter
Wang and Savage [438] show the existence of Gray codes of binary necklaces of fixed density.
Udea [422] also shows the existence of Gray codes of binary fixed density necklaces and primitive necklaces; however, his representation is non-standard, necklaces are not represented
by their lexicographically smallest rotation.
Chinburg, Savage, and Wilf [64] show ??? Bhat and Savage [31] show ???

c
uvic01, Frank
Ruskey, 19952001

Chapter 7

DeBruijn Cycles and Relatives


Dominoes is a simple game, enjoyed by children and adults alike. There are 28 domino
pieces, each of which consists of an unordered pair of numbers taken from the set {0, 1, 2, 3, 4, 5, 6}
and printed or stamped as dots on a 1-by-2 piece of wood or plastic. Each of two players
takes turns alternately placing dominoes, subject to the restriction that the new domino
must abut a domino with the same number (there are various other rules that we ignore). A
sample game which used all pieces is shown in Figure 7.1. As is customary, dominoes with
identical numbers are placed perpendicular to the other dominoes. Now imagine a solitary
player who simply wants to create a configuration like that shown in the figure. We consider
a couple of questions. First, does it matter which pieces have already been placed? Can you
blindly place the pieces and still know that a successful completion is assured? Secondly,
note that we have created a kind of Gray code, in the sense that successive dominoes satisfy
a natural closeness condition, but trying to model the problem as one of finding a Hamilton
path in a graph with 28 vertices is not as straightforward as you might imagine. Give it a
try!
Heres a more serious topic. The following circular bitstring has a rather curious property.
00011101

(7.1)

Consider the set of all of its contiguous substrings of length three. There are eight such
substrings and they are all distinct: 000, 001, 011, 111, 110, 101, 010, 100. In other words,
they are all 8 bitstrings of length three. In general it is natural to wonder whether there is
a circular k-ary string of length k n with the property that all of its k n length n contiguous
substrings are distinct. Such circular strings do indeed exist and have come to be known
as De Bruijn Cycles. De Bruijn cycles have found use in coding and communications, as
pseudo-random number generators, in the theory of numbers, in digital fault testing, in the

qqq qqq qqq q q q q qqq qqq qqq qqq qq qq q q q q q q


qq qq
qqq qqq qqq q q q q q q q q qqq qqq q q

qq
q q qqqqq qqqqq q q q q qqq qqq qq qq qq qq qq qq q q q q qqq
qqq qqq
qqq
qq

qqqqq

qqqqq q q q
qqqqq qqq qq

Figure 7.1: Example (maximal) domino game.


207

qqq q
qqq qq

q
q
q q q qq q q q q q qqq
qqq

208

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

design of Sanskrit memory wheels1 , and by illusionists in mind-reading effects.


What ties these two topics together is that they are more naturally modelled as questions
about Eulerian cycles in graphs rather than about Hamilton paths in graphs. One of our
main aims in this chapter is to show that De Bruijn cycles always exist, and how to generate
them quickly. Along the way we encounter other important combinatorial objects, such as
necklaces, Lyndon words, and primitive polynomials over finite fields.

7.1

Eulerian Cycles

Certain combinatorial Gray code questions are more naturally posed as Eulerian cycle
questions rather than as Hamiltonian cycle questions. Recall that an Eulerian cycle in a
(multi)graph is a cycle that includes every edge exactly once. There is a simple characterization of Eulerian graphs, namely as given in Lemma 2.6: a connected (multi)graph is
Eulerian if and only if every vertex has even degree.
Now back to the domino problem. Consider the complete graph K7 with vertices labelled
0, 1, 2, 3, 4, 5, 6 and with the self-loops {i, i} added to each vertex i. There are 28 edges in
this graph and each edge corresponds to a domino. An Eulerian cycle corresponds to a
maximal domino game like that shown in Figure 7.1. Eulerian cycles clearly exist since
each vertex has even degree 8.

7.1.1

Generating an Eulerian cycle

Let G be an directed Eulerian multigraph with n vertices. In this section we develop an


algorithm that will generate an Eulerian cycle in in G. Along the way we will discover a nice
formula for the number of Eulerian cycles and a CAT algorithm for generating all Eulerian
cycles of G.
If G is a directed multigraph then G is the directed multigraph obtained by reversing
the directions of the edges of G (i.e., for each edge (u, v) of G, there is a corresponding edge
(v, u) in G.
There is a simple algorithm for finding an Eulerian cycle in G given an in-tree rooted at
some vertex r. We simply start at r and successively pick edges subject to the restriction
that an edge of T is not used unless there is no other choice. A simple implementation of
this rule is contained in Algorithm 7.1, and an example of its use is to be found in Figure
7.2.
At step (E1) we find a spanning out-tree T of G that is rooted at r. In general there
may be several spanning out-trees rooted at r and it doesnt matter which one is used;
depth-first search is a convenient and efficient way to find such a tree. The tree T is a
spanning in-tree of G. We now modify (at line (E2)) the adjacency lists of G so that the
unique edge (u, v) T on the list for u is at the end of the list. We now say that the
adjacency lists are extreme with respect to T . The remaining lines simply extract edges
from the adjacency lists, destroying the lists in the process.
The running time of this algorithm is O(m) where m is the number of edges in G. Since
G is Eulerian, m n. The depth-first search at line (E2) runs in time O(n + m). The
1

The nonsense Indian word yam


at
ar
ajabh
anasalag
am is a mnemonic way of remembering the sequence
(7.1), where an accented vowel represents a 1 and an unaccented vowel represents a 0. This word was used
by medieval Indian poets and musicians as an aid in remembering all possible rythms. (There are 10 bits
because the last two have been wrapped around.)

c
uvic01, Frank
Ruskey, 19952001

7.1. EULERIAN CYCLES

(E1)
(E2)
(E3)
(E4)
(E5)
(E6)
(E7)

209

Use depth-first-search to find a spanning out-tree T of G rooted at r.


Compute adjacency lists adj of G that are extreme with respect to T .
u := r;
while adj[u] 6= null do
v := adj[u].vert; adj[u] := adj[u].next;
Output( (u, v) );
u := v;
Algorithm 7.1: Algorithm to find an Eulerian cycle in a directed multigraph.

Figure 7.2: Example of finding an Euler cycle in a directed multigraph. (a) A directed
multigraph G, with spanning in-tree found by depth-first search of G shown in bold, and (b) extreme
adjacency lists with respect to T , with edges of T thicker. The cycle produced by this example is
r, d, a, b, c, d, c, a, r.

c
uvic01, Frank
Ruskey, 19952001

210

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

computation of G takes time O(m) the remaining computation (line (E3-E6)) takes time
O(m) since the body of the while loop at line (E3) is executed exactly m times.
Theorem 7.1 Given a directed Eulerian multigraph G, Algorithm 7.1 outputs a list of edges
along an Eulerian cycle of G.
Proof: Since G is Eulerian, the path P produced by the algorithm must end at r. Imagine
that there is some edge (v, w) that is not in P . Since the algorithm terminated it must
be the case that v 6= r. Clearly, any edge on the adjacency list for v that follows (v, w)
must also not be in P . Thus, because the edge lists are extreme with respect to T , we may
assume that (v, w) is in T . Since G P is balanced, there is an edge (u, v) also not in P ,
which again we can take to be in T . Continuing in this manner we obtain a path of edges
in (G P ) T that terminates at r. But then, since G P is balanced, it must contain an
edge (r, q), contrary to the terminating condition of the algorithm.
2
How many Eulerian does a connected, balanced multigraph G have? In answering this
question we regard an Eulerian cycle as being a circular list of edges; the edge that starts
the list is immaterial. The answer is provided by our algorithm. Clearly, different in-trees
T produce different cycles, as do different adjacency lists that are extreme with respect to
T . A graph G has (G) different spanning in-trees rooted at a given vertex r and there are
(d+ (v) 1)! ways of arranging the adjacency list of v so that it is extreme with respect to
T . Thus it is plausible that the number of Eulerian cycles in G is
Y
(G)
(d (v) 1)!.
(7.2)
vV

To prove (7.2) we must show that we can recover the adjacency lists and tree T from an
Eulerian cycle C. Fix an edge (r, s) to be the first on the cycle. Define the adjacency list
for vertex v to simply be the edges of the form (v, w) in the order that they are encountered
on C. With these adjacency lists lines (E3-E8) will produce the cycle C. To finish the proof
we need to show that the the collection of edges
S = {(v, v 0 ) E | v 6= r and (v, v 0 ) is the last occurence of v on C}
is an in-tree rooted at r. The set S contains n 1 edges; we must show that it forms no
cycles. Assume to the contrary that such a cycle X exists and let (y, z) be the first of its
edges that occur on C, and let (x, y) be the previous edge on X. Unless y = r, there is some
edge (y, z 0 ) that follows (x, y) on C, in contradiction to the way (y, z) was chosen. But we
cannot have y = r either since then (y, z) = (r, z) would be in S.
How fast can we generate all Eulerian cycles in a graph? We need to generate permutations of edges on adjacency lists. There are many CAT algorithms for generating
permutations (e.g., Algorithms ???, ???, and ???). We also need to generate spanning trees
of a graph. This topic is taken up in Chapter ???. There are CAT algorithms for generating
spanning trees of undirected graphs, but what about spanning in-trees of directed graphs?
!!! Is this a simple reduction or a research problem ???

7.1.2

An Eulerian Cycle in the Directed n-cube

Given the central role played by hypercubes in the previous chapters, it is fitting that the
next problem is one that can be modeled on the n-cube. We must admit, however, that it is
c
uvic01, Frank
Ruskey, 19952001

7.1. EULERIAN CYCLES

211

a rather distant relative of De Bruijn cycles. The solution of the following problem of Bate
and Miller [22] is useful in circuit testing. Generate a cyclic sequence of n2n bitstrings of
length n with the following three properties: (a) Each distinct bitstring must appear exactly
n times; (b) each bitstring must differ by exactly one bit from the previous bitstring; (c)
each possible pair of successive bitstrings must appear exactly once. An example of such a
sequence for n = 3 is shown below.
000, 001, 011, 001, 101, 111, 101, 011, 000, 100, 110, 110,
101, 100, 000, 010, 110, 111, 011, 111, 110, 010, 011, 010,
The three occurences of 011 are underlined. Note that the three bitstrings which follow
~ n be the directed n-cube; every edge of the n-cube Qn is replaced
them are all distinct. Let Q
by two directed edges, one in each direction. The following theorem is obviously true since
any digraph for which the in-degree of each vertex is equal to its out-degree is Eulerian.
Our interest in this theorem lies in its proof, which shows an explict construction of the
Eulerian cycle without building the graph.
~ n for all n > 0.
Theorem 7.2 There is an Eulerian cycle in the directed n-cube Q
Proof: We argue by induction on n. The cycle will be expressed as a list of n2n + 1
vertices, starting and ending at 0n . The list for n = 1 is 0, 1, 0.
Let L denote the list of bitstrings of the Eulerian cycle for n 1. Produce from this a
list L0 obtained by doing the following steps in order.
1. Append a 0 to each bitstring of L.
2. Replace the first occurence of a non-zero bitstring X0 by the 3 bitstrings X0, X1, X0.
3. Concatenate a second copy of L with a 1 appended to each bitstring.
4. Concatenate to the list a final bitstring of 0s.
~ n is recursively thought of as two
It is easy to visualize what this algorithm is doing if Q
~ n1 , where one copy contains those vertices that end with a 0 and
copies E0 and E1 of Q
the other with those that end with a 1. The only other edges are the 2-cycles of the form
X0, X1. It is these 2-cycles that get added to the Eulerian cycle by step 2; call the result
E00 . They are added the first time X is encountered in E0 , except if X is 0. The 2-cycle
00, 01 is used to join E00 and E1 .
2
For n = 2 the cycle produced is
00, 10, 11, 10, 00, 01, 11, 01, 00.
The table below shows the Eulerian cycle for n = 3.
c
uvic01, Frank
Ruskey, 19952001

212

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES


n=2

E00 0

00
10
11
10
00
01
11
01
00

000
100,101,100
110,111,110
100
000
010,011,010
110
010
000

E1 1

001
101
111
101
001
011
111
011
001
000
How can we develop and efficient algorithm based on this proof? The main difficulty
comes from deciding which is the first occurence of a non-zero string. A bitstring b is
uniquely identified in the list by the bit that changes, call it c, in obtaining the successor
of b, call it b0 . If, given b and c, we can specify c0 , the bit that changes in obtaining
the successor of b0 , then we will be able to produce the entire list. Initially b = 0 = 0n
and c = 1; these can also be used for the terminating conditions. Below we give rules for
obtaining c0 from b and c. These rules will be justified in the paragraphs to follow. In these
rules x denotes a single bit (a dont care), X is a generic bitstring, and 0 is a string of
0s.
A. If c = n and b = X0 then c0 = 1 if X = 0 and c0 = n if X 6= 0.
B. If c = n and b = Xx1 then c0 = 1 if X = 0 and c0 = n 1 if X 6= 0.
C. If c = n 1 and b = 01x then c0 = n.
D. If c = n k (k 1) and b has more than k trailing 0s, then c0 = n.
E. If none of the rules A-D above apply, then recursively apply them to the string consisting of the first n 1 bits.
The last transition is from 0n1 1 to 0n . Thus when the sequence E00 or E1 is completed,
the next transition is in position n. So if b = 01x and c = n, then c0 should be n. This
explains rule C.
Whenever bit n changes from 0 to 1 (b = X0 and c = n), then it changes back to 0
0
(c = n) unless X = 0. If X = 0, then a new sequence on n 1 bits is starting and c0 should
be 1. This explains rule A.
In expanding E0 to E00 and a bitstring is generated for the first time, c0 should be n.
This happens whenever bit n1 is about to change from a 0 to a 1 (b = X00 and c = n1),
or whenever bit n 1 is a 0 and a pattern on the first n 2 bits is about to appear for the
first time. Recursively, this gives rise to the patterns b = X000 and c = n 2, and more
generally to b = X0k+1 and c = n k. This explains rule D.
We now consider the case in which bit n changes from 1 to 0. This happens at the
penultimate bitstring (i.e., when b = 0n1 1 and c = n). It also occurs when finishing up
a two cycle; that is, when bit n has changed from 0 to 1 and is now immediately changing
from 1 to 0. The sequence on n 1 bits must now be resumed. This implies that rule D
was used, followed by rule A, and that the following bitstrings have just been generated.
c
uvic01, Frank
Ruskey, 19952001

7.2. NECKLACES

00

213

01

11

01

11

10

10

10

11

00

00

01

00

01

10

11

Figure 7.3: The six two-color necklaces with 4 beads. The necklace strings are shown in
bold. Each equivalence class of strings under rotation is boxed.
b

X0 0 . . . 0 0
X1 0 . . . 0 0
X1 0 . . . 0 1
X1 0| .{z
. . 0} 0

nk
n
n
?

current
b0 , c0

k1

The interrupted sequence on n 1 bits must now be resumed. The two lines where
c = n may be ignored, as may bit n. The first bitstring of the four should determin c0 . This
explains rule B.
Rules A-D cover all cases where c = n or c0 = n. If bit n is not involved, then we are
~ . This explains rule E.
generating the Eulerian cycle in Qn1
A straighforward implemenation of these rules overcomes the exponential space obstacle,
reducing it to O(n). Time is also clearly O(n) per bitstring produced. Unfortunately, the
rules dont lend themselves to a CAT algorithm since about n2 2n1 applications of the rules
(including the recursive applications) are required to generate the Eulerian cycle.
However, a circuit may be designed, based on a refinement of these rules, which produces
each bitstring in constant time from its predecessor, by operating in parallel, a similar circuit
used for each position.

7.2

Necklaces

Mathematically, a necklace is usually defined as an equivalence class of strings under rotation. This definition is not exactly in accord with our intuition about what constitutes a
real necklace, since we expect to be able to pick up a necklace and turn it over. However, we
stick with the mathematical tradition and thus regard 001101 as being a different necklace
than 001011, even though one may be obtained from the other by scanning backwards (and
then rotating). Our principle goal in this section is to develop an efficient algorithm for
generating necklaces.
Figure 7.3 shows the six two-color necklaces with 4 beads.
Recall that k = {0, 1, . . . , k 1}, that nk is the set of all k-ary strings of length n, that
c
uvic01, Frank
Ruskey, 19952001

214

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

k is the set of all k-ary strings, and that +


k = k \ {}. Define an equivalence relation
+

on k by if and only if there exist u, v k such that = uv and = vu. Instead


of defining a necklace as an equivalence class we choose to define it as the lexicographically
least representative of some equivalences class of the relation . The set of all necklaces is
denoted N and Nk (n) denotes the set of all necklaces of length n over a k-ary alphabet.
def

Nk (n) = { nk | for all }

(7.3)

For example N2 (4) = {0000, 0001, 0011, 0101, 0111, 1111}. The cardinality of Nk (n) is
denoted Nk (n).
Recall that a string is periodic if = k where is non-empty and k > 0. If is
aperiodic, then it is called the periodic reduction of . An aperiodic necklace is called a
Lyndon word. The set of all Lyndon words is denoted L and Lk (n) denotes the set of all
k-ary Lyndon words of length n.
def

Lk (n) = { Nk (n) | is aperiodic}


For example, L2 (4) = {0001, 0011, 0111}. The cardinality of Lk (n) is denoted Lk (n).
A word is called a pre-necklace if it is the prefix of some necklace. The set of all
pre-necklaces is denoted P and the set of all k-ary pre-necklaces of length n is denoted
Pk (n).
def

Pk (n) = { nk | N(n + m, k) for some m 0 and m


k }
For example P2 (4) = N2 (4) {0010, 0110}. The cardinality of Pk (n) is denoted Pk (n). We
also define Wk (n) to be the number of pre-necklaces of length at most n. These numbers
will prove useful in analyzing the algorithms developed below. We define
def

Wk (n) = 1 +

n
X

Pk (i).

(7.4)

i=1

Let = a0 a1 an1 be a string that can be written = xy = yx where neither of x


or y is empty. In other words, is equal to one of its non-trivial circular shifts. Suppose
x = a0 a1 am1 . Then xy = yx implies the equation ai = ai+m , where index addition is
taken modulo n. Iterating the equation we obtain
ai = ai+jm for all 0 i, j < n.
By Lemma 2.2 on page 16, jm (mod n) for 0 j < n gives us the multiples of m modulo
n, so that
{jm (mod n) : j = 0, 1, . . .} = {0, d, 2d, . . . , n d},
where d = gcd(m, n). We have now proven the following lemma.
Lemma 7.1 If = xy = yx then
= (a0 a1 ad1 )n/d ,

(7.5)

where n = || and d = gcd(|x|, n).


Note the following corollary.
c
uvic01, Frank
Ruskey, 19952001

7.2. NECKLACES

215

Corollary 7.1 If = xy = yx with x 6= , y 6= , then is periodic.


We now count the objects under consideration. In the expressions below is the Euler
totient function and is the Mobius function .
Theorem 7.3 The following formulae are valid for all n 1, k 1:
1 X n d
k ,

Lk (n) =
n
d

(7.6)

d\n
n

Nk (n) =

1X
1 X gcd(j,n)
k
=
(d)k n/d ,
n
n
j=1

Pk (n) =

n
X

(7.7)

d\n

Lk (i).

(7.8)

i=1

Proof: Let Ak (n) be the number of k-ary aperiodic strings of length n. Since every string
can be expressed as an integral power of some aperiodic string, it follows that
X
Ak (d).
kn =
(7.9)
d\n

Now apply Mobius inversion (2.11) to obtain


X
Ak (n) =
(n/d)k d .
d\n

Since every circular shift of an aperiodic string is distinct, Ak (n) = n Lk (n), thereby
proving (7.6). In the special case where p is a prime (7.6) or (7.7) imply Fermats Little
Theorem: k p1 1 (mod p).
To prove (7.7) we use Burnsides lemma . Necklaces are obtained by having the cyclic
group Cn act on the set of k-ary strings. Let denote a left rotation by one position.
Then the group elements are j for j = 0, 1, . . . , n 1. We need to determine the number
of strings for which j () = . By Lemma 7.1 this can occur only if = t where
L and || = gcd(j, n). The number of such strings is k gcd(j,n) . Thus the number of
equivalence classes is k gcd(j,n) summed over all j = 1, 2, . . . , n (noting that 0 = n ) divided
by |Cn | = n. The second equality follows from equation (2.10).
Equation (7.8) will be proven later.
2
It is also worth noting that, since every necklace has the form t where L,
X
Nk (n) =
Lk (d).

(7.10)

d\n

Equation (7.10) can also be used to prove (7.7); see exercise 5.


Below is a table of these numbers for the most important case, k = 2. Note that 2n /n is
sandwiched between L2 (n) and N2 (n), a property that holds for all values of n, and more
generally, for all k 2.
c
uvic01, Frank
Ruskey, 19952001

216

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES


k = 2, n = 6
000000
000001
000010
000011
000100
000101

N
L

000110
000111 L
001001 N
001010
001011 L
001100

L
L

001101
001110
001111
010101
010110
010111

L
L
N

011011 N
011101
011110
011111 L
111111 N

k = 3, n = 4
0000
0001
0002
0010
0011
0012
0020
0021

N
L
L
L
L
L

0022
0101
0102
0110
0111
0112
0120
0121

L
N
L

0122
0202
0210
0211
0212
0220
0221
0222

L
L
L

L
N

1111
1112
1121
1122
1212
1221
1222
2222

L
L
L
L

N
L
L
N
L
N

Figure 7.4: Output of the FKM algorithm (read down columns).


n
L2 (n)
d2n /ne
N2 (n)
P2 (n)
W2 (n)

1
2
2
2
2
3

2
1
2
3
3
6

3
2
3
4
5
11

4
3
4
6
8
19

5
6
7
8
14
33

6
9
11
14
23
56

7
18
19
20
41
97

8
30
32
36
71
168

9
56
57
60
127
295

10
99
103
108
226
521

11
186
187
188
412
933

12
335
342
352
747
1680

13
630
631
632
1377
3057

14
1161
1171
1182
2538
5595

15
2182
2185
2192
4720
10315

A simple and elegant algorithm was proposed in Fredricksen and Maiorana [140] and
Fredricksen and Kessler [139] to generate the sets Pk (n) and Nk (n). We will refer to this
algorithm as the FKM algorithm.
For a given n and k, the FKM algorithm generates a list, Pk (n), in lexicographic order,
where as usual we use the same notation for both the list and the set. The list Pk (n) begins
with the string 0n and ends with (k 1)n . For a given in Pk (n), the successor of ,
succ(), is obtained from = a1 a2 an as follows.
Definition 7.1 For < (k 1)n , succ() = (a1 a2 ai1 (ai + 1))t a1 aj , where i is the
largest integer 1 i n such that ai < k 1 and t, j are such that ti + j = n and j < i.
We also define the predecessor function pred, where if succ() = , then pred() = .
It is shown in [140] that the successor function succ sequences through the elements of
Pk (n) in lexicographic order and that succ() is a necklace if and only if the i of Definition
7.1 is a divisor of n. We will prove these assertions later in this section.
Figure 7.4 shows the output of the FKM algorithm for n = 6, k = 2, and n = 4, k = 3.
Lyndon words are followed by an L; periodic necklaces are followed by a N and the
periodic reduction of each necklace is underlined. An iterative implementation is given as
Algorithm 7.2.
For k , let lyn() be the length of the longest prefix of that is a Lyndon word.
c
uvic01, Frank
Ruskey, 19952001

7.2. NECKLACES

217

for j := 0 to n do aj := 0;
PrintIt( 1 );
i := n;
repeat
ai := ai + 1;
for j := 1 to n i do a[j + i] := a[j];
PrintIt( i );
i := n;
while ai = k 1 do i := i 1;
until i = 0;
Algorithm 7.2: The original iterative FKM Algorithm (note: a[0] = 0.)
This function is well-defined since k L. More formally,
def

lyn(a1 a2 an ) = max{1 p n | a1 a2 ap Lk (n)}.

(7.11)

The next theorem provides useful characterizations of necklaces and Lyndon words.
Theorem 7.4 The following conditions characterize the sets N and L.
= xy N if and only if xy yx, for all x, y.
= xy L if and only if xy < yx, for all non-empty x, y.

(7.12)

Proof: [Nk (n)] This is just a restatement of the definition (7.3).


[Lk (n)] Suppose = xy = yx with xy yx and x 6= and y 6= . If xy > yx then
6 Nk (n), so 6 Lk (n). If xy = yx then by Corollary 7.1, is periodic, so 6 Lk (n).
Conversely, if Nk (n) is periodic, say = t with t > 1, then = xy = yx where x =
and y = t1 .
2
Lemma 7.2 If N, then t N for t 1.
Proof: For t > 1, if t = xy, then yx has the form t1 where = . Since is a
necklace . Thus
t = ()t ()t = t1 ,
and so t must also be a necklace.

Lemma 7.3 If L and = for some and with non-empty, then for any t 1
(a) t P, and
(b) t N if and only if || = 0.
Proof: (a) Since is a necklace, both t and t+1 are necklaces by the Lemma 7.2. Thus
t is a pre-necklace and is a necklace if = . If 6= then = < , since L.
Therefore,
t = ()t = ()t > ()t = t ,
so that t is not a necklace.

2
c
uvic01, Frank
Ruskey, 19952001

218

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

Lemma 7.4 Let = a1 a2 an be a string and p = lyn(). Then P if and only if


ajp = aj for j = p + 1, . . . , n.
Proof: If ajp = aj for j = p + 1, . . . , n, then = t for some t 1 where =
a1 a2 ap L and is a prefix of . Thus P by Lemma 7.3.
Conversely, assume that P, and let j be the smallest index j > p such that ajp 6= aj .
We will derive separate contradictions, depending whether ajp < aj or ajp > aj . By the
definition of j, the string has the form t aj an for some t 1 where = a1 a2 ap
and is a proper prefix of . For some , = where the first symbol of is ajp .
Since P there is a string such that N. If ajp < aj , then
t1 aj an < t1 aj an ,
which implies that is not a necklace, a contradiction.
If ajp < aj then consider the string = t aj ; we will use (7.12) to show that L,
in contradiction to the definition of p = lyn(). Write = xy with x 6= and y 6= . We
consider two cases: (a) t is a prefix of x and (b) x is a prefix of t .
[Case (a)] Here x = t u and y = vaj where uv = . Since uv is a proper prefix of
there is a w 6= such that = uvw. Note that the first symbol of w is ajp . By (7.12),
uvw < vwu < vaj . Thus
xy = uvw t1 aj < vaj t u = yx.
[Case (b)] Here x = p u and y = v q aj where uv = and t = 1 + p + q. If u = or v = ,
then we may assume without loss of generality that p 1 and u = . Thus
xy = p q aj < q aj p = yx,
a contradiction. Thus we may assume that neither of u or v is empty and that = uv < vu
since L. Now if q = 0, then since uv < vu < vaj ,
xy = uv p aj < vaj p u = yx,
again a contradiction. The only remaining case is q > 0. But then
xy = p uv q aj = u(vu)p (vu)q vaj < v(uv)q aj p u = v q aj p u = yx,
a contradiction.

The following theorem leads to a recursive version of the FKM algorithm. Its proof is
inherent in the proof of the previous lemma. This theorem is very useful. We are tempted
to call it the Fundamental Theorem of Necklaces!
Theorem 7.5 Let = a1 a2 an1 Pk (n 1) and p = lyn(). The string b Pk (n)
if and only if anp b k 1. Furthermore,

lyn(b) =

p
n

if b = anp
if anp < b k 1.

c
uvic01, Frank
Ruskey, 19952001

7.2. NECKLACES

219

procedure gen( t,p : N );


local j : N;
begin
if t > n then PrintIt( p )
else
a[t] := a[t p]; gen( t + 1, p );
for j := a[t p] + 1 to k 1 do
a[t] := j; gen( t + 1, t );
end {of gen};
Algorithm 7.3: Recursive FKM Algorithm (note: a[0] = 0.)
Algorithm 7.3 is the recursive FKM algorithm. It follows directly from the Theorem
7.5. The initial call is gen(1,1). We assume, as for the iterative FKM algorithm, that
a0 = 0. Various types of objects may be produced, depending on PrintIt(p), as shown in
the table below.
Sequence type
PrintIt(p)
Pre-necklaces (Pk (n)) Println( a[1..n] )
Lyndon words (Lk (n)) if p = n then Println( a[1..n] )
Necklaces (Nk (n))
if n mod p = 0 then Println( a[1..n] )
De Bruijn sequence
if n mod p = 0 then Print( a[1..p] )
The call Println( a[1..n] ) prints the array a[1], a[2], . . . , a[n] on a separate line. Each
time PrintIt is called a new prenecklace is produced. Since the parameter p to PrintIt
is the value of lyn(a[1..n]), a Lyndon word is produced exactly when p = n. By part (b) of
Lemma 7.3 a necklace is produced whenever p divides n. De Bruijn cycles will be discussed
in the following section.
We can now also understand why Algorithm 7.2, the iterative FKM algorithm, is correct.
Consider again the successor function succ. It is clear that the rightmost position i that can
change is the largest index i for which ai < k 1. We then increment ai . What was not so
clear was how the remainder of the sequence was to be completed in the lexicographically
smallest way. Theorem 7.5 provided the answer.
How fast is the FKM algorithm? We analyze the recursive version; the same conclusions
hold for the iterative version. Call the number of nodes in the computation tree Wk (n).
From the structure of the algorithm, Wk (n) is equal to the number of prenecklaces of length
at most n, as expressed in (7.4).
From the expressions (7.6) and (7.7) we obtain the following bounds.
1 n
1
1
(k (n 1)k n/2 ) Lk (n) k n Nk (n) (k n + (n 1)k n/2 )
(7.13)
n
n
n
We have
n
n
X
X
1 i
Pk (n) =
Lk (i)
k,
(7.14)
i
i=1

i=1

The equality in (7.14) follows because every prenecklace is obtained as a prefix of , where
is some Lyndon word. Hence
Wk (n) 1 =

n
X
j=1

Pk (i)

j
n X
X
1
j=1 i=1

ki
c
uvic01, Frank
Ruskey, 19952001

220

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

100

110

101

111

011

000

010

001

Figure 7.5: The De Bruijn graph for k = 2 and n = 4.


Thus
j
n
n XX 1 i
Wk (n) 1
n
k
Nk (n)
k
i
j=1 i=1

In [357] it is shown that this last expression converges to (k/(k 1))2 as n . Thus
the asymptotic running time per necklace (or per Lyndon word) is constant; the necklace
Algorithms 7.2 and 7.3 are both CAT.

7.3

De Bruijn Sequences

An example of a De Bruijn sequence was given at the beginning of this chapter. All De
Bruijn sequences arise as Eulerian cycles in a certain graph which we introduce below. The
FKM algorithm can be used to generate the lexicographically least Eulerian cycle.
The De Bruijn graph , Gk (n) has vertex set consisting of all k-ary strings of length n1;
i.e., it is n1
. There is a directed edge, labelled b, from d1 d2 dn1 to d2 dn1 b for
k
each b k . Thus the out-degree of each vertex is k and the number of edges is k n . Figure
7.5 shows G2 (4). An Eulerian cycle in Gk (n) is called a De Bruijn cycle. More precisely,
the sequence of k n edge labels is a De Bruijn cycle. A De Bruijn cycle is characterized by
the property that each element of nk occurs exactly once as a substring on the cycle.
There is a very simple modification of the FKM algorithm that will produce a De
Bruijn sequence, as indicated by the appropriate line in the table in the previous section.
The idea is to successively concatenate the reduction of each necklace as it is produced by
the FKM algorithm. Thus we are concatenating all Lyndon words of length divisible by n
in lexicographic order. At first glace it appears most amazing that this algorithm should
work. Lets first look at the sequences it produces for the examples given in Figure 7.4.
Heres the resulting De Bruijn cycle for n = 6 and k = 2.
0 000001 000011 000101 000111 001 001011 001101 001111 01 010111 011 011111 1
Heres the resulting cycle for n = 4 and k = 3.
0 0001 0002 0011 0012 0021 0022 01 0102 0111 0112
0121 0122 02 0211 0212 0221 0222 1 1112 1122 12 1222 2
c
uvic01, Frank
Ruskey, 19952001

7.3. DE BRUIJN SEQUENCES

221

These are, in fact, the lexicographically smallest De Bruijn cycles.


We now prove that the algorithm is correct. The algorithm is so elegant that one
would hope the same of the proof. Unfortunately, it is a rather uninspiring case analysis.
To understand the difficulties involved the reader should, before reading the proof, try to
figure out the location of a few specific strings in the output of the algorithm. For example,
in the list for k = 3 and n = 9, where does 220121012 appear? For k = 3 and n = 8, where
does 22012201 appear?
Theorem 7.6 The list of successive periodic reductions of necklaces as produced by the
FKM algorithm forms a De Bruijn cycle.
Proof: Let D denote the list of k-ary symbols produced by concatenating the successive
periodic reductions of necklaces from the FKM algorithm. Since (7.9) may be written as
X
kn =
d Lk (d),
d\n

the list D contains the correct number of digits. We now argue that each k-ary string of
length n occurs as a substring of D, which will finish the proof.
Note that the first two outputs of the algorithms are 0 and 0n1 1, and the last two
outputs are (k 2)(k 1)n1 and (k 1). Thus D has a prefix 0n and a suffix (k 1)n ,
from which it follows that all strings of the form (k 1)p 0np (where 0 p n) occur as
substrings in D. All other strings have the form
= (k 1)p (xy)t
where 0 p < n, t 1, yx L, xy contains some non-zero symbol, and the first symbol of
x is not k 1. We will classify the possibilities for according to whether p = 0, whether
t = 1, and whether y = .
Case 1 [p = 0, t = 1, y = ]: Here = x with x L. Trivially x appears as substring
in D since x is output by the algorithm.
Case 2 [p = 0, t > 1, y = ]: Here = xt with x L. The string x is output by
the algorithm, and the next string output by the algorithm is xt1 S(x), where S(x) is the
necklace the lexicographically follows x. Thus the string xt occurs in D since xxt1 S(x) is
a substring of D.
Case 3 [p = 0, t = 1, y 6= ]: Here = xy and yx L. The string yx is output by the
algorithm; what output follows it? Note that succ(yx) = yz for some string z since x is not
all (k 1)s (and y(k 1)n|y| L). What is the periodic reduction of yz? We claim that
it is a string with prefix y, which will finish this case. Observe from Theorem 7.5 that if
L, then r is the lexicographically smallest necklace of length r|| with prefix . Thus
it cannot be that yz = r and is a proper substring of y, since yx is a lexicographically
smaller necklace.
Case 4 [p = 0, t > 1, y 6= ]: Here = (xy)t and yx L. The string yx is output by the
algorithm, and the next string output by the algorithm is S((yx)t ) = (yx)t1 yS(x), which
is aperiodic. Thus D contains the substring yx(yx)t1 yS(x); a string which contains as
substring.
Case 5 [p > 0, t = 1, y = ]: Here = (k 1)p x with x L. The string D contains
= pred (x)(k 1)p since L. The next string output by the algorithm has prefix x, and
so D contains the substring pred (x)(k 1)p x.
c
uvic01, Frank
Ruskey, 19952001

222

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

Case 6 [p > 0, t > 1, y = ]: Here = (k 1)p xt with x L. The string D contains


= pred (x)(k 1)nd where d = |x|, since L. Let n = md + r where 0 r < m.
If r > 0 (i.e., d does not divide n), then succ() = xm S(y) L where y is the string
consisting of the first r symbols of x. Since m t, the string occurs on D. If r = 0, then
following the algorithm outputs x, followed by xm1 S(x). Thus D contains the string
pred (x)(k 1)nd xxm1 S(x) which in turn contains .
Case 7 [p > 0, t = 1, y 6= ]: Here = (k 1)p xy with p 1 and yx L. Note that
= neck () is either (a) = xy(k 1)p or (b) = y(k 1)p x. In case (a) L. We
may now proceed exactly as in Case 5, with xy playing the role of x. That is to say, D
will contain the string pred (xy)(k 1)p xy. In case (b), if is aperiodic, then is output
by the algorithm and the following string output is of the form y(k 1)p z, so appears on
D. In case (b) if is periodic, then y(k 1)p must be the periodic reduction, where, say,
= [y(k 1)p ]q . The algorithm outputs pred(y)(k 1)n|y| , followed by y(k 1)p , followed
by [y(k 1)q1 S(y(k 1)p ), and thus D contains .
Case 8 [p > 0, t > 1, y 6= ]: Here = (k 1)p (xy)t with p 1, t > 1, and yx L.
Note that = neck () must be = y(xy)t1 (k 1)p x L. The next string output by the
algorithm is y(xy)t1 (k 1)p S(x), so again occurs as a substring of D.
2
From our previous analysis of the FKM algorithm we know that the total amount of
work, aside from outputting the symbols, in producing this cycle is (k n /n). Thus the time
required to produce the De Bruijn cycle is dominated by the time to output the digits; i.e.,
it is (k n ) which is best possible.

7.4

Computing the Necklace of a String

Given a string, it is often useful to compute its necklace. Such applications arise in several
diverse areas such as graph drawing, where it is used to help determine the symmetries of
a graph to be drawn in the plane.
Recall that for any string = a1 a2 . . . aN N
k its necklace, neck () is the lexicographically smallest of its circular shifts. The question naturally arises as how to efficiently
compute the necklace given the string. In this section we present an O(N ) algorithm for
the task.
First we consider the problem of factoring a word as specified in the following theorem.
Theorem 7.7 (Chen, Fox, Lyndon) Any word +
k admits a unique factorization
= 1 2 m ,
such that i L for i = 1, 2, . . . , m and
1 2 m .
Here are two examples of Lyndon factorizations.
011 011 00111 0 0 and 0102 0102 01 0 0
It is easy to see that such a factorization exists, since each letter is a Lyndon word and
any two Lyndon words x and y for which x < y can be concatenated to get another Lyndon
word xy. Uniqueness is also not hard to show. See Exercises 12 and 13.
There is also a version of this theorem that deals with factorizations into necklaces.
c
uvic01, Frank
Ruskey, 19952001

7.4. COMPUTING THE NECKLACE OF A STRING

223

Theorem 7.8 Any word +


k admits a unique factorization
= 1 2 m ,
such that i N for i = 1, 2, . . . , m and
1 > 2 > > m .
Here are two examples of necklace factorizations, using the same words as above.
011011 00111 00 and 01020102 01 00
Duval [92] has developed an elegant, efficient algorithm for factoring a word. Our version
of the algorithm is essentially based on Theorem 7.5. The output of the algorithm consists
of indices 0 = k0 , k1 , k2 , . . . , km = N such that
i = aki1 +1 , . . . , aki
Informally, the idea of the algorithm is to keep extending n and updating p until a value
an > anp is encountered. Then a1 ap is the longest prefix of that is in L and t =
a1 an1 where pt + || = n 1 with || < p. The words
, , . . . , , , causing output ki = i|| for i = 1, 2, . . . , t
| {z }
t

are the first t factors in the Lyndon factorization of . Note that is the prefix of a Lyndon
word that is lexicographically less than . Now apply the same algorithm to and the
remainder of ; i.e., to an an+1 aN . The details may be found in Algorithm 7.4.
D

k := 0; a[N + 1] := 1;
while k < N do
n := k + 2; p := 1;
while a[n p] a[n] do
if a[n p] < a[n] then p := n k;
n := n + 1;
repeat PrintIt( k ); k := k + p;
until k n p;
PrintIt( N );
Algorithm 7.4: Duval algorithm for factoring a string.

By moving the PrintIt(k) statement at line (D8) outside and just before the repeat
loop (i.e., between lines (D7) and (D8)), the algorithm will produce the necklace factorization.
What is the running time of Duvals algorithm? Note that the comparison anp = an at
line (D4) is the most often executed statement. Let us assume that we are doing a necklace
factorization and that there are m factors. Let ki , ni , pi be the values of k, n, and p at the
end of the ith iteration of the outer while loop. The total number of comparisons done at
line (D4) on the i-th iteration is ni ki1 1. But by (D9), ni ki pi so that
m
m
X
X
(ni ki1 1) = km +
(ni ki 1)
i=1

i=1

N m+

m
X

pi

i=1

2N m
c
uvic01, Frank
Ruskey, 19952001

224

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

Thus the total number of comparisons done at line (D4) is at most 2N and the running
time of the algorithm is therefore O(N ).
How to find a necklace
To find the necklace of a word , use as the input to the necklace factorization algorithm
(or modify the algorithm to do arithmetic mod n). Suppose that t is the necklace of ,
where L. Then t will occur as t factors in the Lyndon factorization of . We need
simply wait until a necklace factor of length || appears. When n k > N (tested after
line (D9)), the string a[k + 1..k + N ] is the necklace of .

7.5

Universal Cycles

In this section we give a brief introduction to Universal Cycles. These are a very interesting generalization of DeBruijn cycles introduced by Chung, Diaconis and Graham [65].
Many types of combinatorial objects are represented by strings of fixed length; suppose that
there are N total objects and each representation has length n. The problem is to find a
(circular) string D of length N such that every representative occurs exactly once as a contiguous substring of D. For De Bruijn cycles we had N = 2n and the representations were
all bitstrings of length n. But what about combinations, permutations, set and numerical
partitions, etc?
k-permutations of an n-set
Suppose that we try to extend the De Bruijn cycle idea from subsets (bitstings of length
n) to k-permutations of an [n]. That is, we would like a circular string of length (n)k over
the alphabet [n] such that each k-permutation occurs exactly once as a substring. It is
natural to define a digraph G(n, k + 1) whose vertices are ([n])k , the k-permutations of
[n], and where a generic vertex a1 a2 ak has n k + 1 outgoing edges whose endpoints
are a2 ak b for b [n] \ {a2 , , ak }. For example if k = 1 then G(n, 2) is the complete
directed graph; i.e., there is an edge between every pair of distinct vertices. Note that
G(n, k) is vertex-transitive.
One Eulerian cycle in G(4, 2) is shown below.
123414243213
Lemma 7.5 For 1 k < n, the graph G(n, k) is Eulerian.
Proof: It is easy to see that the in-degree of each vertex is also n k + 1. We need only
show that G(n, k) is strongly connected. Given a = a1 a2 ak ([n])k , note that there
is a cycle of length k in G(n, k) that contains every circular permutation of a, namely the
one obtained by repeatedly using edges of the form x x. We now show that there is
a path from a to a with any two of its adjacent elements transposed. To do this, we need
only show the existence of a path from a to a2 a1 a3 ak . Let x 6 {a1 , a2 , . . . , ak }. Then
there is a path

a1 a2 a3 ak a2 a3 ak x a3 ak xa1

xa1 a3 ak a1 a3 ak a2 a2 a1 a3 ak
c
uvic01, Frank
Ruskey, 19952001

7.5. UNIVERSAL CYCLES

225

as claimed, where denotes a path of some length. This implies that there is a path from
a to b = b1 b2 bk where b1 < b2 < < bk and {b1 , b2 , . . . , bk } = {a1 , a2 , . . . , ak }. We
now show that there is a path from b to 12 k. Since G(n, k) is vertex-transitive, that
will finish the proof. Let i be the smallest value for which bi 6= i and bi+1 > i + 1; if there
is no such value, then we are done. The following path is in G(n, k).

b1 b2 bk bi+1 bk 1 i bi+2 bk 1 i(i + 1) 12 i(i + 1)bi+2 bk .


Continuing in this manner we eventually reach 12 k.

It would be interesting to develop a fast algorithm to output an Eulerian cycle whose


existence is guaranteed by the lemma.
If n = k then G(n, k) consists of n!/2 2-cycles of the form 1 2 n 2 n 1 , and
is thus not Eulerian. Another way to view this is as follows. Consider n = 3. The substring
123 must occur, but what symbol follows the 3? It must be 1. And then 2 and then 3. But
123123 does not satisfy our criteria since, for example, 321 does not occur as a substring.
The problem occurs because we have insisted on [n] as our alphabet. Consider the string
145243
This contains the substring 431 which we consider to represent the permutation 321 (which
we missed before). Below are the six substrings of length 3 and the corresponding permutations of [3].
substring
permutation

145
123

452
231

524
312

243
132

431
321

314
213

More formally, we say that permutations of natural numbers, and 0 , both of length
n, are order isomorphic if i < j if and only if i0 < j0 for all 1 i, j n. Our problem is
to find a string of numbers whose substrings are order isomorphic to the n! permutations
of [n].
Heres a string that works for n = 4.
123412534153214532413254
How could we construct such a string? Do they always exist? How many extra symbols
must be used? Taking our clue from De Bruijn sequences we define a graph and look for
Eulerian cycles in that graph. !!! need to put in the rest of the stuff from the CDG
paper !!!.
Let N (n) be the number of extra symbols that are necessary to construct a U-cycle for
permutations. Chung, Graham and Diaconis conjecture that N (n) = n + 1; that n + 1
N (n) n + 6 is known.
Set Partitions
abcbccccddcdeef
Murasaki Universal Cycles?
c
uvic01, Frank
Ruskey, 19952001

226

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

Combinations
Heres a U-cycle for n = 8 and k = 3, where the underlying set is 8 .
02456145712361246703671345034601250135672560234723570147
Lemma 7.6 If there is a U-cycle for A(n, k) then k divides

n1
k1 .

Chung, Graham and Diaconis conjecture that this necessary condition is also sufficient
as long as n is large enough, but there are also conjectures to the contrary!

7.6

Polynomials over finite fields

Polynomials whose coefficients are the elements of a finite field have many applications in
mathematics and engineering. Of particular interest are what are known as irreducible
polynomials and primitive polynomials. Somewhat surprisingly, we can use our algorithm
for generating Lyndon words as the basis of an algorithm for generating all such polynomials of a given degree. This section will be brief and most proofs will be omitted as the
mathematics necessary is non-trivial and would take more room than we have here. Several
excellent introductions to finite fields are mentioned in the bibliographic remarks. Nevertheless, the careful reader should have little trouble turning the discussion here into a
functioning program, particulary if written in a language for symbolic calculations, such as
Maple. There is another connection between polynomials and the material presented earlier
in this chapter, namely that a primitive polynomial ca be used to efficiently generate a de
Bruijn sequence.
Let p(x) be a monic polynomial over GF(q). Recall that GF(q) refers to the field of
integers mod q and that q must be a prime power. The polynomial p(x) is irreducible if it
cannot be expressed as the product of two polynomials of lower degree.
If is a root of a degree n polynomial p(x) over GF(q), then the conjugates of
n1
are , q , . . . , q . Each conjugate is also a root of p(x). Irreducible polynomials are
characterized in the following theorem.
Theorem 7.9 Let p be a degree n polynomial over GF(q) and a root of p. The polynomial
p is irreducible if and only if the conjugates of are distinct.
An element of GF(q) is primitive if k = q n 1 is the smallest non-zero value for which
k = 1. A degree n polynomial p(x) over GF(q) is primitive if it is irreducible and contains
a primitive element of GF(q n ) as a root. In other words, to test whether an irreducible
polynomial p(x) is primitive.....
The number of primitive polynomials over GF(q) is known to be
Pn (q) =

1
(q n 1)
n

Here is a small table for q = 2.


n
Pn (q)

1
1

2
1

3
2

4
2

5
6

6
6

7
18

8
16

9
48

10
60

11
176

12
144

13
630

14
756

15
1800

16
2048

c
uvic01, Frank
Ruskey, 19952001

7.6. POLYNOMIALS OVER FINITE FIELDS

227

The number of irreducible polynomials of degree n over GF(q) is given by the so-called
Witt formula
1X
Iq (n) =
(n/d)q d
n
d\n

This is the same as the number of Lyndon words, Ik (n) = Lk (n). This remarkable coincidence cries out for an explanation and a purpose of this section is to supply one. The key
to the correspondence is Theorem 7.9.
If is a generator of GF(q n ) {0}, then = k for some k. The conjugates of are
thus
k , (k )q , . . . , (k )q

n1

Which ks will give rise to s that are the roots of irreducible polynomials? Since the
conjugates are all distinct, they are the ks for which k, qk, q 2 k, . . . , q n1 k are all distinct
mod q n 1. Thinking of k as a length n base q number, the conjugates are all the circular
shifts of k. For them to be distinct k has to be a q-ary length n Lyndon word.
Lyndon word
000001
000011
000101
000111
001011
001101
001111
010111
011111

k
1
3
5
7
11
13
15
23
31

irreducible polynomial
1000011
1010111
1100111
1001001
1101101
1011011
1110101
1110011
1100001

order e
63
21
63
9
63
63
21
63
63

Given k the order of the polynomial is (q n 1)/gcd(q n 1, k). The primitive polynomials
are those of order q n 1.
The polynomial p(x) = x6 + x + 1 is known to be primitive over GF(2). Let be a
root of p(x). Thus 6 = 1 + . We now illustrate the correspondence on the Lyndon words
000001 and 001011; corresponding to = 1 and = 11, respectively. We now compute
(note that (1 + )2 = 1 + 2 ). First, with = 1.
r(x) = (x+)(x+ 2 )(x+ 4 )(x+ 8 ) (x+ 16 )(x+ 32 )
= (x2 +(+ 2 )x+ 3 )(x+ 4 )(x+ 2 (1+))
(x+ 4 (1+)2 )(x+ 2 (1+)2 (1+)2 (1+))
= (x2 +(+ 2 )x+ 3 )(x+ 4 )(x+ 2 + 3 ))
(x+ 4 (1+ 2 ))(x+ 2 (1+ 2 )2 (1+))
= (x+)(x+ 2 )(x+ 4 )(x+ 2 + 3 ))(x+1++ 4 )(x+ 2 (1+ 4 )(1+))
= (x+)(x+ 2 )(x+ 4 )(x+ 2 + 3 ))(x+1++ 4 )(x+1+ 3 )
= (x+(+ 2 )x+x2 )(x+ 4 )(x+ 2 + 3 ))(x+1++ 4 )(x+1+ 3 )
= (x+(+ 2 )x+x2 )(x+ 4 )(x+ 2 + 3 ))(x+1++ 4 )(x+1+ 3 )
= x6 +x+1
c
uvic01, Frank
Ruskey, 19952001

228

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

output

r3

r2

r1

input

r1

Figure 7.6: The LFSR corresponding to the polynomial x4 + x + 1.


Now with = 11 (note that 64 = 1).
r(x) = (x+ 11 )(x+ 22 )(x+ 44 )(x+ 88 )(x+ 176 )(x+ 352 )
= (x+ 11 )(x+ 22 )(x+ 44 )(x+ 24 )(x+ 48 )(x+ 32 )
= (x+ 5 (1+))(x+ 4 (1+)3 )(x+ 2 (1+)7 )
(x+(1+)4 )(x+(1+)8 )(x+ 2 (1+)5 )
= x6 +x5 +x3 +x2 +1
Theorem 7.10 Every string of length n, except 0n occurs in a linear recurring sequence
generated by a primitive polynomial over GF(pn ).
For example, the polynomial p(x) = x4 + x + 1 is primitive over GF(2). It generates a
linear recurring sequence defined by the recurrence relation
cn = cn3 + cn4 .
Using the initial conditions c1 , c2 , c3 , c4 = 1, 0, 0, 0 we obtain the sequence
100010011010111
Inserting another 0 into the block of n 1 0s produces a De Bruijn sequence, but not all
De Bruijn sequences are obtained in this manner.

7.6.1

Linear Feedback Shift Registers

A linear feedback shift register, or LFSR, is a device like that illustrated in Figure 7.6 On
each clock cycle the bit in cell rk1 is output and fed back into some subset of the cells,
depending on the placement of the feedback lines. There is one feedback line corresponding
to each non-zero term of its polynomial. If the feedback line is present, then the new value
of ri is ri1 rk1 , otherwise it is just ri1 . The value of r1 is taken to be zero, which
means that the exclusive-or gate feeding into r0 is redundant; it is shown for the sake of
consistency.
do { if ((x <<= 1) >> k) x = x & ONES ^ PP;
} while (x != INITIAL);

Algorithm 7.5: C code for iterating an LFSR.


Algorithm 7.5 generates the De Bruijn sequence in the least significant bit of the unsigned
integer x, whose initial value is INITIAL. The algorithm is simulating the action of the LFSR.
c
uvic01, Frank
Ruskey, 19952001

7.7. EXERCISES

229

Unsigned integer PP is a bitstring encoding of a primitive polynomial, 0011 in the example


above. Constant ONES is a bitstring of k 1s in the lower order bits. This code essentially
generates each new value of x in constant time. The only problematic operation is the shift
right by k positions. On most modern computers this shift should be executed in one or
two machine instructions.

7.6.2

Another look at the BRGC

We may identify a bitstring b0 b1 bn1 with a degree n 1 polynomial over GF(2) under
the bijection
b0 b1 bn1

n1
X

bi xi .

i=0

Under this bijection a right shift corresponds to multiplying by x and componentwise


exclusive-or to addition mod 2. Thus if g(x) is the polynomial corresponding to the bth
word in the BRGC, then
g(x) = (1 + x)b(x).
Since the algebraic inverse of 1 + x is the polynomial 1 + x + + xn1 (check this!),
the procedure for converting from the BRGC to binary corresponds to multiplication by
1 + x + + xn1 . That is,
b(x) = (1 + x + + xn1 )g(x).

7.7

Exercises

1. [1] What is the length of a maximal domino game for a general value of n (dots taken
from [n])?
2. [2+] Show that there is a string s0 s2 s2n!1 of length 2n! over the alphabet [n] such
that
{si si+1 si+n!1 : i = 0, 2, . . . , 2n!},
where the index arithmetic is taken mod 2n!, is the set of all n! permutations of [n].
~ n have?
3. [R] How many Eulerian cycles does the directed n-cube Q
4. [R] Develop a CAT or, better yet, loopfree algorithm for generating an Eulerian
~ n.
cycle in Q
5. [2] Use (7.10) to prove (7.7).
6. [2+] Let N (n0 , n1 , . . . , nt ) denote the number of necklaces composed of ni occurrences
of the symbol i, for i = 0, 1, . . . , t. Let n = n0 + n1 + + nt . Prove that
N (n0 , n1 , . . . , nt ) =

1
n

X
d\ gcd(n0 ,...,nt )

(d)

(n/d)!
(n0 /d)! (nt /d)!
c
uvic01, Frank
Ruskey, 19952001

230

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES


In particular, if t = 1, then
1
N (r, n r) =
n

X
d\ gcd(r,nr)

n/d
(d)
r/d

gives the number of necklaces with r black beads and n r white beads.
7. [1+] Find a simple one-to-one correspondence between length 2n necklaces with n
black beads and n white beads and rooted plane trees with n edges.
8. [2+] Derive a formula for the number of binary Lyndon words of length n and weight
r. The weight of a string of digits is the sum of those digits. [2+] Let eLk (n) be
the number of length n Lyndon words of even weight, oLk (n) be the number of odd
weight,
P and dLk (n) be the difference dLk (n) = eLk (n) oLk (n). Show that dL2 (n) =
n1 (d)2n/d where the sum is over all odd d\n. Show that dL2 (2n) = oL2 (n).
9. [1] For odd n, show that eN2 (n), the number of binary necklaces with an even number
of 1s, is equal to oN2 (n), the number of binary necklaces with an odd number of 1s.
10. [1+] What bitwise operations preserve necklaces (or prenecklaces, or Lyndon words)?
I.e., is the intersection of necklaces a necklace? What about union and exclusive-or?
11. [1+] Modify both the iterative and the recursive versions of the necklace generating
algorithms so that the produce necklaces in relex order, instead of lex order. Which
algorithm was easier to modify?
12. [2] Prove that w L if and only if w < v for all uv = w where v 6= . I.e., a word is
a Lyndon word if and only if it is strictly less than all of its proper suffixes.
13. [2] Prove that if x and y are both Lyndon words and x < y then xy is also a Lyndon
word. Finish the proof of the Chen, Fox, Lyndon Theorem 7.7 by showing that the
Lyndon factorization is unique.
14. [3] Define an involution on {0, 1}n by
(x1 . . . xn ) = x1 . . . xn1 xn ,
where xn denotes the complement of the bit xn , and let (x) denote the rotation of
string x one position left. Show that the calls: Print(0n ); Gen( 0n1 1 ); generate all
necklaces of length n in two colors, where Gen is the procedure shown below.
procedure Gen ( x : necklace );
begin
Print( x );
x := (x);
while IsNecklace(x) do begin
Gen( x );
x := 1 (x);
end;
end {of Gen};
c
uvic01, Frank
Ruskey, 19952001

7.7. EXERCISES

231

15. [2] Let = where and are as in the previous exercise. In other words,
(b1 b2 bn ) = b2 bn b1
Define an equivalence relation between bitstrings x and y of length n as follows:
x y if and only if there is a number k such that k (x) = y. Show that the number
of such equivalence classes is
1 X n/d
2 (d).
2n
d\n
d odd

There is a one-to-one correspondence between such equivalence classes and what are
known as vortex-free tournaments.
16. [R] Let V(n) denote the set of lexicographically least representatives of length n of
the equivalence classes of , as defined in the previous exercise. For example V(n) =
{00000, 00010, 00100, 01010}. Develop an efficient, and preferably CAT, algorithm to
generate V(n).
17. [2] How many n-bead necklaces are there, composed of white and black beads, and
with no two adjacent black beads? How many such necklaces are aperiodic? Prove the
following, a kind of Fermats Little Theorem for Fibonacci numbers: If p is a prime,
then Fp+1 + Fp1 1 (mod p). [R] Given a forbidden pattern P (a bitstring
00 in the first part of this problem), how many necklaces of length n are there without
P as a substring? How fast can you compute the number?
18. [R] Develop an efficient ranking algorithm for prenecklaces, necklaces, and Lyndon
words in the order that they are generated by the FKM algorithm.
19. [R] Develop an efficient ranking algorithm for some De Bruijn sequence. If the De
Bruijn sequence arising from the FKM algorithm is used then the results of the previous exercise should be useful.
20. [1+] Prove that if Pk (n) then (k 1)n Nk (n). This means that every prenecklace is also the prefix of a Lyndon word, unless is a string of (k 1)s of length
greater than 1.
21. [2] Prove: P if and only if xy yz and x z for all x, y, z such that = xyz
with |x| = |z|.
22. [R] Find a Gray code (successive words differ by one bit) of Lyndon words when k = 2
or prove that no such code exists.
23. [3+] Let n > 1 and p, q > 1. Show that Lq+4 (n) Lq (n) is even and hence that
if p q (mod 4), then Lq (n) Lp (n) (mod 2). Use this result to determine the
parity of Lq (n).
P
24. [2] Prove that Lpq (n) =
gcd(i, j)Lp (i)Lq (j) where the sum is taken over all i and
j such that lcm(i, j) = n.
c
uvic01, Frank
Ruskey, 19952001

232

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

25. [2] Find a class of words that causes the Duval algorithm to use 2N o(N ) comparisons
for infinitely many values of N .
26. [R] For a binary alphabet, what is the maximum number of comparisons that can
be used by the Duval algorithm on a string of length N ? For a k-ary alphabet?
27. [R] Develop a CAT algorithm for generating all necklaces where the number of
beads of each color is fixed. The number of such necklaces was determined in Exercise
6. Even the two color case is open.
28. [R] Develop a CAT algorithm for generating the lexicographically least representatives of the equivalence classes of k-ary strings of length n that are equivalent under
rotation or reversal. In other words, the dihedral group is acting on the strings and
not just the cyclic group. Such equivalence classes are sometimes called bracelets.
29. [2] What about 2-dimensional analogues of De Bruijn cycles? These are sometimes
called De Bruijn torii. (a) Consider the set of 2 by 2 tiles where each square of the
tile is colored black or white. There are 16 such tiles. Can they be placed in a 4
by 4 arrangement so that their borders match, where the border wrap-around in the
manner of a torus? (b) Now let each tile be colored with three colors, say white, gray,
and black. There are 81 tiles. Can they be placed in a 9 by 9 arangements so that
the colors along their borders match?
30. [2] For those who know about context-free languages: Use a closure property to prove
that N and L are not context-free languages. Use the pumping lemma to prove
that N and L are not context-free languages.
31. [2] Let I(n, k) be the set of permutations Sn such that k = 1, the identity
permutation, and I(n, k) = |I(n, k)|. Thus I(n, 2) counts the number of involutions
in Sn . Show that
X n 1
(d 1)!I(n d, k).
I(n, k) =
d1
d\k

[R] Develop a CAT algorithm for generating the elements of I(n, k).
32. [1] Modify algorithm 7.5 so that it outputs a De Bruijn sequence.

7.8

Bibliographic Remarks

~ n is from Bate and Miller [22].


The Eulerian cycle in Q
An early paper containing an algorithm for constructing a DeBruijn cycle is Martin [266].
More recent papers about generating De Bruijn cycles include Fredricksen and Kessler [142],
Fredricksen [141], Ralston [326], Huang [184], and Xie [463]. It is rather remarkable that
there is a simple closed form formula for the number of distinct De Bruijn cycles.
(k!)k

n1

/k n .

This formula was proven by Van Aardenne-Ehrenfest and De Bruijn [426], but was anticipated by Flye-St. Marie [135]. It is even possible to count the number of Eulerian
c
uvic01, Frank
Ruskey, 19952001

7.8. BIBLIOGRAPHIC REMARKS

233

cycles (directed or undirected) in a graph. See Fleishners book [132], which contains everything you could want to know about Eulerian cycles. Generating all Eulearian cycles
was considered by Fleishner [133].
Interesting material about De Bruijn cycles may be found in Stewart [406].
The algorithm ??? for generating necklaces is from Fredricksen and Maiorana [140]
and Fredricksen and Kessler [139]. Independently, Duval [93] developed a version of the
algorithm that generates Lyndon words. Duval [92] is our source for the material of the
section on finding the necklace of a string.
The formula (7.7) has been attributed by Comtet [68] to the 1892 paper of Jablonski
[190]. It is credited to Colonel Moreau of the French Army by Metroplis and Rota [279].
Cummings and Mays [76] determine the parity of the Witt formula.
Papers about generating primitive and irreducible polynomials include L
uneburg [259],
and Gulliver, Serra and Bhargava [161]. The relationship between irreducible polynomials
and Lyndon words is explored in Golomb [155], L
uneburg [259], [260], and Reutenauer [342].
Tables of primitive polynomials may be found there and in Peterson and Weldon [307] and
Serra [381].
An analysis of the FKM algorithm is given in Ruskey, Savage, and Wang [357]. Duvals
nearly identical algorithm is analyzed in Berstel and Pocchiola [29]. The recursive version
of the FKM algorithm (Algorithm 7.3) is believed to be new.
A CAT algorithm for necklaces where the number of 0s is fixed may be found in Ruskey
and Sawada [359]. A CAT algorithm for binary bracelets was developed by Sawada [373].
More material on Lyndon words may be found in Lothaire [253].
Universal cycles were introduced in Chung, Diaconis and Graham [65]. Later developments may be found in Jackson [191] and Hurlbert [186], [187].
There is a delightful chapter called The Autovoracious Ourotorus about De Bruijn
cycles and generalizations in Stewart [406].

c
uvic01, Frank
Ruskey, 19952001

234

CHAPTER 7. DEBRUIJN CYCLES AND RELATIVES

c
uvic01, Frank
Ruskey, 19952001

Chapter 8

Orderly Algorithms
For complicated objects progress in combinatorial generation can be measured in terms of
whether it is more expedient to generate the objects via a program or to read them from
storage on an external device. An interesting case in point is the set of unlabelled graphs.
Ron Read computed all such graphs in 1960s and made them available on magnetic tape.
This tape proved to be quite popular. Now, however, no one uses this tape any longer
because there is a program called makeg, developed by Brendan McKay that produces
the graphs more quickly and conveniently than they can be read from a tape or any other
external storage device.
In fact in 1993, he generated all 90,548,672,803 of the 12 vertex
graphs on at most 12
2 /2 edges. The computation took 5277 hours of computer time
distributed over a network of computers.
There are three things that drive such progress: (a) better algorithms, (b) faster hardware (including the use of parallelism), and (c) better implementations (i.e., code optimization).
There are many types of combinatorial objects that do not seem to have nice recursive decompositions that translate into efficient listing algorithms, such as those that we
encountered in Chapters 4 and 5. Nevertheless there may be enumerative results about the
number of such objects. Consider the example of unlabelled graphs. They can be counted
by using Polya theory, resulting in a sum over integer partitions with a multiplicative factor
of 1/n. However, this formula seems to be of little use in actually generating the graphs.
In general we may wish to list members of equivalence classes of combinatorial objects.
For example graphs under the equivalence relation of isomorphism, as mentioned above.
There is a general technique for developing algorithms that list equivalence classes, particularly equivalence classes that arise from symmetry conditions, called orderly algorithms. In
orderly algorithms there must be a systematic way of constructing larger objects recursively
from smaller objects. For example, graphs can be constructed by adding edges, starting with
the empty graph. The main idea in orderly algorithms is to carefully pick the representative
of each equivalence class and the order in which larger objects are constructed from smaller
objects. Our final aim is a backtracking algorithm to generate the objects. Typically the
resulting backtracking algorithms have the property of having successes at all nodes; i.e.,
they are BEST algorithm. This property is certainly one to be strived for; unfortunately,
the amount of work done at a node is often exponential in the size of the individual objects
(at least in the worst case) so we dont necessarily obtain CAT algorithms. But enough of
this general rambling; lets look at a few specific examples.
235

236

CHAPTER 8. ORDERLY ALGORITHMS


1

hGi

P (G)

Figure 8.1: An unlabelled graph, its canonic labelling, and its parent.

8.1

Undirected Graphs

In this section we develop an algorithm for generating all unlabelled graphs. With each
unlabelled graph G we associate a unique labelled graph hGi, which is said to be canonic.
If two labelled graphs G and H are isomorphic, then hGi = hHi. The algorithm outputs all
the canonic labelled graphs. By ignoring the labels of the output canonic graphs, we obtain
all unlabelled graphs.
Orderly algorithms are a particular type of BEST backtracking algorithm. Our strategy
is to impose a tree structure on the set of canonically labelled graphs and construct our
bactracking so it has this tree as its computation tree.
If G is a graph and e is a nonedge of G, the G e denotes the graph obtained by adding
e to the edge set of G. Similarly, G e denotes the graph obtained by removing e from the
edge set of G.
We begin by trying to associate a single graph, call it the parent, P (G), with G such
that G = P (G) e for some edge e. To do this it is necessary to decide which edge to
remove from G to get P (G). An unambiguous choice can be made if we do two things.
(A) Fix an ordering of the set of all possible edges of labelled graphs with vertex set [n].
(B) Label the vertices of G in some unique way; i.e., so that isomorphic graphs get the
same labelling. This is the canonic labelling refered to above.
With respect to the ordering (A) and labelling (B), the vertex e to be removed is the largest
one in G. The most natural choices for these orderings are as follows.
(A0 ) Each edge is a 2-subset of n. Use the lexicographic ordering of A(n, 2), as in Section
4.3. In other words
{1, 2} < {1, 3} < < {1, n} < {2, 3} < < {n 1, n}.
(B 0 ) For any labelling of the vertices of G, the edges of G can be listed
e1 < e2 < < eq .
Among all labellings Sn , choose the one that lexicographically minimizes the
string (e1 , e2 , . . . , eq ).
Figure 8.1 shows a canonic labelling and its parent.
Now that P (G) is well-defined, observe that it imposes a tree structure, Tn , on the set
of all graphs with n vertices. See Figure 8.2 for an example with n = 4. The labelling of
c
uvic01, Frank
Ruskey, 19952001

8.1. UNDIRECTED GRAPHS

237
r r
@
r @r

r
r

1r 3r
r

2 4

r
r

r
r

r
r

r
r

r
r

@
r
@
r

r
r

r r
@
r @r

r r
@
r @r

r r
@
r @r

r
r

r
r

@
r
@
r

r
r

Figure 8.2: The tree of unlabelled graphs on four vertices.


vertices shown at the root is maintained throughout the tree. If the edges are selected in
lexicographic order,q then
this tree is traversed
in preorder from the top of the page towards
q q
q
q is output before q q . The reader should study this figure carefully.
the bottom; e.g.,
q@
@
Our object is to now develop a backtracking algorithm whose computation tree is Tn .
Consider P (G). What are its children? They are obtained by adding an edge e, larger
than any other edge of P (G). The resulting graph G = P (G) e may or may not satisfy
condition (B 0 ); i.e., it may or may not be canonic. If it is canonic, then G is a valid graph
and can be output. If it is not canonic, then G can be ignored. Clearly every canonic
child of P (G) will be produced by this process. Furthermore, no child can be produced
more than once because the added edges e are all different. Why is the algorithm correct?
The key observation is that the parent of a canonic graph is canonic. For other types of
combinatorial objects, its not always easy (or possible) to choose definitions of canonicity
and parenthood that have this crucial property.
procedure Gen ( G : graph; x : edge );
local y : edge;
begin
Print( G );
for all y > x do
if Canonic( G y ) then Gen( G y, y );
end {of of Gen};
Algorithm 8.1: Orderly algorithm (recursive version).
The incarnation of the preceding discussion is the deceptively simple Algorithm 8.1. The
initial call is Gen(G, e) where G is the graph containing the single edge e = {1, 2}. As is
usual in backtracking, it is most expedient to leave the major data structure, which in this
case is G, global, as is illustrated in Algorithm 8.2. In both algorithms the most recently
added edge x is passed as a parameter.
What are
the resource requirements of this algorithm? The height of the backtracking
tree Tn is n2 and only a constant amount of space is needed at each node (for edges x and
y). Thus space usage is O(n2 ). The time required depends on the following three factors.
The number of graphs on n vertices.
c
uvic01, Frank
Ruskey, 19952001

238

CHAPTER 8. ORDERLY ALGORITHMS


procedure Gen ( x : edge );
local y : edge;
begin
Print;
for all y > x do
G := G y;
if Canonic then Gen( y );
G := G y;
end {of Gen};
Algorithm 8.2: Orderly algorithm with G global.

The number of times Canonic fails.


The running time of Canonic.
The number of graphs on n vertices is known to be asymptotic to
1 (n)
2 2 .
n!
The running time of Canonic is essentially the time it takes to test whether two graphs
are isomorphic. Naively implemented, its running time is (n2 n!). The number
of times

n
Canonic fails is unknown, but at any node of the tree it can fail at most 2 times, so the
1 (n)
total time is O( n2 n!
2 2 ). Thus we can say that overall running time of the algorithm is
n (n)
O( 2 2 2 ), but this is extremely pessimistic. For the makeg program mentioned in the first
paragraph of this chapter, the canonicity tester is another program nauty that is extremely
fast on a random graph so much so, that makeg appears to be a CAT algorithm!
Enumerating graphs
Using Polya theory, one can derive a formula for the number of unlabelled graphs. The
derivation of this formula is outside the scope of this book, but we can give a little insight.
Consider the expression
j
Y kj
Y
Y (r,t)j j
n!
1 X
j k( 2k )
2k+1
Q j
s2k+1
(sk sk1
)
s
s[r,t] r t
2k
2k
k
n!
k k jk !
r<t

(j)

The sum is over (j) which represents all numerical partitions of n, where jk is the number
of occurances of k. We can evaluate the sum by making use of our algorithm for generating
numerical partitions with the multiplicity representation. In the formula s1 , s2 , . . . are
indeterminates, (r, t) denotes the greatest common divisor of r and t, and [r, t] denotes
their least common multiple.
The number of graphs with n vertices and m edges is the coefficient of xm in the
polynomial gn (x) given below:
gn (x) = Z(Sn(2) , 1 + x)
This means to use the substitution sk = 1 + xk .
c
uvic01, Frank
Ruskey, 19952001

8.2. TOURNAMENTS

239
1r

-r2

#
#

c
c
c

c
c
r
-r
]
JJ

J
r

1r

-r2
J

JJ
r

#
#

c
c

rH
-r


JH

H
jr
J4 6

JJ
r

rH
*r

JH
H
jr

J 6

JJ
r

c
c
rH
-r
Y 
JHHr


J 6

J^
Jr

rH
-r
]
JJ H


jr
H

J 6

Jr

Figure 8.3: Tree of tournaments for n 4.

8.2

Tournaments

In this section we develop an orderly algorithm for generating all unlabelled tournaments.
We follow the same pattern as laid out in the previous section. It is necessary to define
appropriate notions of canonicity and parenthood.
Recall that a labelled tournament is a directed graph that has either the edge [i, j] or
has the edge [j, i], but not both, for each 1 i < j n. We construct tournaments by
adding a vertex at a time, rather than an edge at a time as was done for graphs. First we
order the possible edges of a labelled tournament. Define a colex ordering c1 , c2 , . . . , c(n)
2
on those edges [i, j] where i < j.
[1, 2] < [1, 3] < [2, 3] < [1, 4] < < [1, n] < < [n 1, n]
Define a lex ordering l1 , l2 , . . . , l(n) on those edges [i, j] where i > j.
2

[2, 1] < [3, 1] < [3, 2] < [4, 1] < < [n, 1] < < [n, n 1]
A tournament is represented by a string of edges
e = e1 e2 e(n) where ei = ci or ei = li
2

As the canonic representative of each unlabelled tournament we choose that labelling that
lexicographically minimizes e. The parent of a canonic tournament T with n vertices is
obtained by removing vertex n from T . Figure 8.3 shows the resulting tree of tournaments
for n 4. Let Xp denote the following cartesian product.
Xp = {[1, p], [p, 1]} {[2, p], [p, 2]} {[p 1, p], [p, p 1]}
Another way of thinking of e above is as an element of X1 X2 Xp .
c
uvic01, Frank
Ruskey, 19952001

240

CHAPTER 8. ORDERLY ALGORITHMS

Our orderly algorithm for tournaments is Algorithm 8.3. The call Gen(T, p), given a
p 1 vertex canonic tournament T , generates all children of T that have at most n vertices.
The statement for all e Xp is assumed to select strings in Xp in lexicographic order. The
initial call is Gen([1, 2],3).
procedure Gen ( G : tournament; p : N);
local e : EdgeString;
begin
if p n then
Print( G );
for e Xp { in lex order } do
if Canonic( G e ) then Gen( G e, p + 1 );
end;
end {of Gen};
Algorithm 8.3: Orderly algorithm for tournaments on at most n vertices.
Why does this algorithm work? First, note that if vertex n is removed from a canonic
tournament, then a canonic tournament results. This means that every tournament is
produced at least once. Secondly, since the parent of a tournament is unique, no tournament
can be produced more than once.

8.3

Restricted Classes of Graphs

We have now seen examples of orderly algorithms for generating two types of graphs. In one
instance the augmentation operation added an edge, in the other instance the augmentation
operation added a vertex. In this section we develop orderly algorithms for generating
restricted classes of graphs, using one or the other of those augmentation operations.

8.3.1

Bipartite Graphs

8.3.2

Cubic Graphs

Let Cn denote the number of cubic graphs on n vertices.


n 4 6 8 10 12
14
16
18
20
Cn 1 2 5 19 85 509 4060 41301 510489

8.4

22
7319447

24
117940535

Posets

In this section we discuss an orderly algorithm for generating non-isomorphic posets.

8.4.1

Dedekinds Problem

[!!! Recent results of Wiedemann !!!]


c
uvic01, Frank
Ruskey, 19952001

8.5. COSET ENUMERATION

8.5

Coset Enumeration

8.6

Exercises

241

1. [1] What is the canonic labelling of the following graph G? What is its parent P (G)?
2. [1] What is the canonic labelling of the following tournament G? What is its parent
P (G)?
3. [R] Develop an orderly algorithm for generating all non-isomorphic unicyclic graphs.
An undirected graph is unicyclic if it contains a single cycle; such graphs are obtained
by adding an edge to a tree.
4. [R] Use and orderly algorithm to determine the number of non-isomorphic antimatroids with n feasible sets for small values of n. Do the same for greedoids.

8.7

Bibliographic Remarks

Read [335] was the paper that popularized the name and method.
Other papers about orderly algorithms include Read [337], Colbourn and Read [336],
Cameron, Colbourn, Read, and Wormald [48], Royle [352], White and Williamson [445],
McKay [271]. The basic algorithm for generating unlabeled graphs can be modified so that
it uses polynomial space (see Colbourn and Read [67]), and so that polynomial delay occurs
between the output of successive graphs (see Godberg [151]).
McKay and Royle develop an orderly algorithm that is used to generate all cubic graphs
on up to 20 vertices [272]. The best method for generating cubic graphs, and that described
in Section ???, is due to Brinkmann [41]. Orderly algorithms for generating k-colorable
graphs, in particular bipartite graphs, may be found in Koda [224].
The generation of graphs with transitive automorphism group is consider in McKay and
Royle [273] (up to 26 vertices).
The algorithm for generating posets in Section ?? is due to Culberson and Rawlins [73].
Other papers about generating posets are Erne and Stege [113], and ..... Koda [225]
discusses orderly algorithms for generating all lattices and finite topologies.
Redelmeier [339] develops an algorithm for generating all non-isomorphic polyominoes.
Walsh [436] develops an orderly algorithm for generating all nonisomorphic maps with a
given number of vertices and edges and of a given genus. [Is a map simply a graph embedded
in a surface?]
The recent thesis of Goldberg [152] disusses many complexity issues associated with
orderly algorithms and the types of problems to which they are applied.
Other references about graph generation include Baraev and Faradjev [21], Faradjev
[117], McKay [270], Yap [464], Petrenjuk and Petrenjuk [308].
A variety of algorithms for generating combinatorial objects subject to group actions
may be found in Laue [241].
Group theorists have long used the Todd-Coxeter algorithm for generating coset representatives of groups presented as generators and defining relations. See for example, Todd
and Coxeter [414] and Canon, Dimino, Havas, and Watson [50]. Allison [7] has developed
an algorithm for the case where the group is presented in terms of permutations.
c
uvic01, Frank
Ruskey, 19952001

242

CHAPTER 8. ORDERLY ALGORITHMS

c
uvic01, Frank
Ruskey, 19952001

Chapter 9

Subgraph Generation
!!!! Put in some sort of nifty introductory example. (E.G., Why do electrical
engineers want to generate spanning trees) !!!!
In this brief chapter we consider generation questions where the input is a graph and
the output consists of all subgraphs of a specified type. We consider the problem of listing
all spanning trees, all cliques, and so on.
In the previous chapter on orderly algorithms we saw the utility of imposing a rooted
tree structure on the set of objects to be generated; in fact, this is a theme throughout the
book. It will also be a theme of this chapter, but the aim of the tree is somewhat different.
In the previous chapter we were concerned with not generating isomorphic instances of
already generated objects, here the tree is simply a guide that will hopefully lead us to an
efficient algorithm.

9.1

Spanning Trees

Every connected graph has a spanning tree and the selection of a spanning tree satisfying
some optimization criteria is an oft occuring problem in discrete optimization. For simple
criteria, such as total edge weight, well-known efficient algorithms are known this is just
the minimum spanning tree problem. For other criteria, we must examine all spanning trees
and just pick the best.

9.1.1

A naive algorithm

The number of spanning trees is given by the Matrix Tree Theorem (Theorem 2.4). We can
generate spanning trees by using the basic Contraction-Deletion idea.

9.1.2

A CAT algorithm

In the remainder of this section we present a CAT algorithm for generating spanning trees
that is due to Shioura and Tamura [387]. Let G = (V, E) be a graph and assume that the
vertices and edges have been ordered according to a depth-first search (DFS) of the graph
as described below. The vertex set is V = {v1 , v2 , . . . , vn }, where v1 , v2 , . . . , vn is the order
in which vertices are encounted in the DFS. The edge set is E = {e1 , e2 , . . . , em }, where
e1 , e2 , . . . , en1 are the tree edges of the graph in the order that they are encountered in
DFS; the non-tree edges are also ordered according to when they are encounted in the DFS.
243

244

CHAPTER 9. SUBGRAPH GENERATION

procedure Gen ( G : graph; T : tree );


var y : edge;
begin
if |G| = 1 then PrintTree( T ) else
if G connected then begin
e some non-loop edge of G;
Gen( G e, T {e} );
Gen( G e, T );
end; end {of Gen};
Algorithm 9.1: Contraction-Deletion algorithm for generating spanning trees.
a

d
e

Figure 9.1: The connected graph K4 e with edges labelled.


Let T 0 the the depth-first search tree; it is rooted at v1 . Figure ?? shows a graph with a
numbering arising from a DFS.

9.1.3

A Spanning Tree Gray Code

Can spanning trees be generated by single edge replacements? This is the most natural
Gray code question for spanning trees. The answer is yes. In this subsection we provide a
proof of this assertion and give some hints about how to generate this Gray code in a CAT
manner.
Given a graph G, the tree graph of G, denoted T (G) has as vertices the spanning trees
of G, with edges joining those vertices that differ by a single edge replacement. Figure 9.1
shows a graph, with its spanning trees shown in Figure 9.2 and its tree graph shown in
Figure 9.3. Note that t1 , t2 , . . . , t8 is a Hamilton cycle in T (G).
To prove that T (G) is hamiltonian we prove that it has a stronger property, uniform
Hamiltonicity. A graph X is positively Hamiltonian if every edge of X is included in some
Hamilton cycle, and is negatively Hamiltonian if every edge of X is avoided by some Hamilton cycle. A graph that is both positively and negatively Hamiltonian is uniformly Hamiltonian.
The basis of the algorithm and the proof of uniform Hamiltonicity is the decomposition
of the set of spanning trees into two disjoint sets, those that contain some specified edge
and those that dont. Let e be an edge of a multigraph G(V, E). Recall that G e is the
graph obtained from G by removing the edge e, and that G e is the graph obtained from
G by contracting the edge e.
(G) = (G e) + (G e)
c
uvic01, Frank
Ruskey, 19952001

9.1. SPANNING TREES

245

t1

t2

t3

t4

t5

t6

t7

t8

Figure 9.2: The spanning trees of K4 e.

t7

t6

t2

t1

t3

t4

t8

t5

Figure 9.3: The tree graph of K4 e.

c
uvic01, Frank
Ruskey, 19952001

246

CHAPTER 9. SUBGRAPH GENERATION

Theorem 9.1 The tree graph T (G) of a graph G is uniformly Hamiltonian if T (G) contains
at least two cycles.
Proof:

9.2

Reverse Search

9.3

Cycles

9.4

Cliques

9.5

Maximal Independent Sets

9.6

Cutsets

Let G = (V, E) be a graph or a digraph, with s, t V . A cut is a set of edges of the form
(X, X) = {(u, v) E | u X, v X},
where X V and X = V \ X. A cut (X, X) is an (s, t)-cut if s X and t X. A
(s, t)-cutset is an edge minimal (s, t)-cut.
Cuts and (s, t)-cuts have a range of applications, such as switching functions, sensitivity analysis of optimization problems, vertex packing, and network reliability calculations
([318]).

9.7

Chromatic Polynomials

9.8

Convex Polytopes

Generating all vertices and faces.

9.9

Exercises

1. [1] Show that T (K3 ) is K3 . Draw T (K4 ). Find simple graphs Gn for which T (Gn ) is
isomorphic to Kn ?

9.10

Bibliographic Remarks

Spanning Trees
Read and Tarjan [338] show the spanning trees, cycles, and simple cycles of a graph can all
be generated in space O(n + m) and time O(n + m + mN ), where n is the number of vertices
in the graph, m is the number of edges, and N is the number of spanning trees, cycles, or
simple cycles, respectively. According to the 1970 book of Moon [285], Many papers have
been written giving algorithms, of varying degrees of usefulness, for listing the spanning
trees of a graph. He mentions the early papers of Feussner [123], [124], and the papers
of Wang [437], Duffin [90], [166], Mayeda and Seshu [269], Mukherjee and Sarker [286],
c
uvic01, Frank
Ruskey, 19952001

9.10. BIBLIOGRAPHIC REMARKS

247

Chen [59], Berger [27], and Char [54]. More recent algorithms may be found in Minty [283],
Kamae [202], Kishi and Kajitani [211], Fang [116], Wojciechowski [459], Cherkasskii [61],
Winter [457], Hakimi and Green [167], Chen [60], Jayakumar, Thulasiraman, and Swamy
[193], and Gabow and Myers [147]. The later paper also considers the problem of generating
the spanning trees of directed graphs. Gabow [146] discusses the generating of weighted
spanning trees in increasing order.
The best current algorithms for generating spanning trees are those of Kapoor and
Ramesh [205], Shioura and Tamura [387], and Smith [394]. Their algorithms run in time
O(N + m + n) and space O(mn) for a graph with n vertices m edges and N spanning trees.
The Shioura and Tamura algorithm is an instance of reverse search. Smiths algorithm
generates spanning trees in a Gray code order; successive trees differ by a single edge
replacement.
The proof that tree graphs are uniformly Hamiltonian is from Holzmann and Harary
[179]; this proof applies more generally to the bases of a matroid. Earlier papers on this
same topic are Cummins [77] and Kishi and Kajitani [212].
Colbourn, Myrvold and Neufeld develop a O(n3 ) (arithmetic operations) algorithm for
ranking the spanning trees of a graph.

Other stuff
Johnson [197] gives an algorithm for generating all circuits of a graph. Cai and Kong [47]
give fast algorithms for generating all cliques of certain perfect graphs.
Avis and Fukuda [17] give an algorithm for generating all vertices of a convex polytope.
A generalization of this procedure called Reverse Search [18] resulted in fast algorithms
for listing other types of combinatorial objects.
Fukuda and Matsui [144] give an algorithm for generating all perfect matchings in a
bipartite graph. An algorithm to enumerate all common bases in two matroids is presented
in Fukuda and Namiki [143].
Eppstein [103] gives an O(n) algorithm for listing all bipartite subgraphs of a graph of
bounded arboricity.
Here we mention some algorithms for the important class of planar graphs. Syslo [409]
gives an algorithm for listing all cycles of a planar graph. Listing all triangles (3-cycles) was
considered by Itai and Rodeh [188]. Listing of triangles, 4-cycles, and cliques was considered
in Chiba and Nishizeki [62] (see also Chapter 8 of Nishizeki and Chibe [63]); their algorithm
specialized to planar graphs run in linear amortized time. See also Matula and Beck [268]
and Papadimitriou and Yannakakis [303].
The number of triangles, 4-cycles and cliques in planar graphs are all O(n) so that these
problems are of a somewhat different character than those weve usually considered, since
our typical problem has exponentially many instances in terms of m and n.
Tsukiyama, Ide, Ariyoshi, and Shirakawa [420] generate maximal independent sets as
does Loukakis [254]. The maximal independent sets of trees are generated by Chang, Wang,
and Lee [53]. The maximum independent sets of bipartite graphs are generated by Kashiwabara, Masuda, Nakajima, and Fujisawa [207] in amortized O(n) time per set.
!! paper by Szwarcfiter on perfect graphs? !!
c
uvic01, Frank
Ruskey, 19952001

248

CHAPTER 9. SUBGRAPH GENERATION

Cutsets
There have quite a few papers written about generating all cutsets. Algorithms for listing
all cutsets in undirected graphs may be found in Abel and Bicker [1], Bellmore and Jensen
[25], and Tsukiyama, Shirakawa, Ozaki, and Ariyoshi [421] The best of these algorithms is
that found in [421], which requires O(|E|) computation per cutset generated.
Algorithms for generating all minimum cardinality cuts have been proposed by Ball
and Provan [19], Gardner [149], and Picard and Queyranne [310]. Ramanathan and Colbourn [328] generate what they call nearly minimum cardinality cuts. Kanevsky [203]
generates vertex cutsets of minimum cardinality in linear time per cutset. According to the
abstract, he generates all M minimum size separating sets of a k connected graph in time

(min{max{M nk, knm min{k, n}}, max{M n, k 2 n3 }. A simple unanalyzed algorithm for
generating all cutsets of a graph has been given by Ahmad [2]; this paper contains some
other references from the reliability literature. A unifying framework for cutset problems is
to be found in Provan and Shier [318].

c
uvic01, Frank
Ruskey, 19952001

Chapter 10

Random Selection
In cases where exhaustive enumeration is impractical it is often useful to generate a large
number of objects at random. Unranking algorithms can be used but they suffer from a
number of drawbacks
They typically involve large integers and thus require the used of exended precision
arithmetic packages.
The bit complexity is usually (n2 ). This is because they require (n) operations on
integers of size exponential in n.

10.1

Permutations

We start with a simple example. There are many applications that require the production
of random permutations, from simple programs that shuffle cards to sophisticated simulation routines. Let rand (1, n) be a procedure that produces a integer selected uniformly
at random from {1, 2, . . . , n}. Here is a simple two line procedure to produce a random
permutation of 1, 2, . . . , n:
for i := 1 to n do [i] := i;
for i := 2 to n do [i] :=: [rand(1, i)];
The first line sets to be the identity permutation; any other permutation could have
been used. Why does this work? We argue by induction on n. Note that the integer n
does not move until the nth iteration of the second loop. Thus we may assume that the
procedure produces a random permutation 1 , 2 , . . . , n of 1, 2, . . . , n 1 in the first n 2
iterations; i.e., that each permutation of 1, 2, . . . , n1 has probability 1/(n1)! of occuring.
In the final iteration n then gets set to a value, say k, with probability 1/n. Thus, each
permutation of 1, 2, . . . , n has probability 1/(n 1)! 1/n = 1/n! of occuring.
Note that stopping the second loop at k instead of n will produce a random k-permutation
of an n-set. Also note that the first line could set to be any permutation of 1, 2, . . . , n.

10.2

Combinations

The problem of producing a random subset S of {1, 2, . . . , n} is easily solved by adding each
i to S with probability 1/2 for i = 1, 2, . . . , n. Similarly, it is trivial to produce a random
249

250

CHAPTER 10. RANDOM SELECTION

element of a product space uniformly at random.


The production of a random combination is considerably more subtle. Heres a very
elegant algorithm due to Knuth. Why does this algorithm work? !!! Is this really
wothwhile presenting??
Algorithm 10.1 is another very elegant algorithm, this one due to Floyd.
function Sample ( n, k ) : Set;
if k = 0 then return( )
else begin
S := Sample(n 1, k 1);
t := rand(1, n);
if t 6 S then S := S {t} else S := S {n}
return( S );
end;
Algorithm 10.1: Generate a random k-subset of [n].
Why does this algorithm work?
The running time of Floyds algorithm will depend upon how the set S is maintained.
There are a number of options that might be considered:
One could simply use a Boolean array b[1..n], where b[i] is true if and only if i S.
The running time is O(n) since that is the time it takes to initialize the array.
If k is small compared with n, then O(n) is an unacceptable running time. There is
a tricky technique whereby an array can be used without intializing the array, which
would reduce the time to O(k), but the storage requirement is still O(n).
A dictionary data stucture could be used; that is, a data structure that supports
the operations of Insert and Member. Assuming that we also wish to output the items
in order, various balanced tree schemes seem appropriate. Then the running time is
O(k log k) and the storage is O(k).

10.3

Permutations of a Multiset

We can adapt the ideas of the previous section to generate the permutation of a multiset of
specification hn0 , n1 , . . . , nt i.

10.4

Necklaces

Let us first consider the problem of generating a Lyndon word uniformly at random. The
idea is to generate a random word and check it for aperiodicity as in the following probabilistic algorithm:
repeat
w := random element of nk ;
until w is aperiodic;
c
uvic01, Frank
Ruskey, 19952001

10.5. NUMERICAL PARTITIONS

251

Since the ratio of aperiodic words to all words is asymptotically one, the expected
number of iterations is also one. To produce a random necklace we need to weight the
divisors of n by ???

10.5

Numerical Partitions

From sci.math.research April 1998:

In article <6hksce$upk$1@isn.dac.neu.edu>, meleis@coe.neu.edu (Waleed Meleis) writes:


|> Hello, Im looking for an efficient way to compute a random partition of a
|> fixed integer n such that every distinct partition is equally likely.
Start by precomputing the number p_k(m) of partitions of m with each term <= k,
for m = 1 .. n and k = 1 .. m. This can be obtained by the recursion
p_k(m) = sum_j = 0^floor(m/k) p_k-1(m-jk) for k <= m
(noting that p_k(m) = p_m(m) for k > m). In the process, store the
partial sums s(k,m,l) = sum_j = 0^l p_k-1(m-jk) for k <= m, l <= m/k.
This computation takes O(n^2 ln(n)) arithmetic operations.
Now the following pseudocode procedure produces a random partition of m
with each term <= k, where m <= n. So to get a random partition of n you
would use randpart(n,n).
function randpart(m,k: integer): partition;
if k > m then return randpart(m,m)
else if k = 1 then return m 1s
else
choose j in 0, 1, ..., floor(m/k) with probabilities
P(j) = p_k-1(m-jk)/p_k(m);
return j ks + randpart(m-jk, k-1)
end
Note that the step of choosing j can be done as follows: generate a random
number X in [0,1] and use binary search to find the least j so that
s(k,m,j) >= X p_k(m).
Robert Israel
Department of Mathematics
University of British Columbia
Vancouver, BC, Canada V6T 1Z2

israel@math.ubc.ca
(604) 822-3629
fax 822-6074

c
uvic01, Frank
Ruskey, 19952001

252

CHAPTER 10. RANDOM SELECTION

10.6

Set Partitions

10.7

Trees

10.7.1

Well-formed Parentheses

The Cycle Lemma can be used to generate a random well-formed parenthesis string.

10.8

Graphs

10.9

Exercises

1. [1] Show that if the interchange of the algorithm for generating a random permutation
is changed to [i] :=: [rand(n)], then the procedure no longer produces random
permutations (for n 3).
2. [1+] Show how to compute a random derangement of [n] in O(n) arithmetic operations.

10.10

Bibliographic Remarks

The algorithm for generating a random permutation is due to Durstenfeld [91].


Floyds algorithm for generating random combinations is described in Bentley [26].
Constant time initialization of arrays is discussed in Lewis and Denenberg [246].
Wilf [450] discusses the random selection of free tress. Tinhofer [412] presents a survey
of methods for generating various types of trees and graphs uniformly at random.
The following papers discuss the random generation of binary trees: Arnold and Sleep
[12], Remy [341] (in O(n) time),Atkinson and Sack [13], Johnsen [196], Korsh [229], and
Martin and Orr [265].
Furnas [145] discusses the random generation of binary rooted
trees. Atkinson and Sack [15] develop a O(n3 h) algorithm to generate all binary trees on n
nodes of height h. Atkinson and Sack [14] discuss the generation of combinatorial objects
in parallel.
Dixon and Wilf [87] generate unlabelled graphs uniformly at random. Wormald [460]
discusses the random selection of regular graphs; for moderate degrees a superior algorithm
is given in McKay and Wormald [274].
The number of non-singular n-by-n matrices over a finite field of elements is
(n 1)(n ) (n n1 ).
Randall [330] shows how to efficiently select such a matrix at random.
Broder [42] contains an algorithm for selecting a random spanning tree.
Flajolet, Zimmerman and van Cutsem [131] develop a systematic approach to the random generation of labelled combinatorial objects.
Sinclairs book [388] contains much material on generating combinatorial objects at
random.
Other useful references include the book of Devroye [82] on generating non-uniform
random variables, and Hickey and Cohen [177] on generating random strings in a context
free language. Also, Lapointe and Legendre [239].
c
uvic01, Frank
Ruskey, 19952001

10.10. BIBLIOGRAPHIC REMARKS

253

In Generating Triangulations at Random (Proceedings of the 4th Canadian Conference


on Computational Geometry, pp.305-321) Epstein and Sack give an O(n4 ) algorithm for
generating a triangulation of a polygon with n vertices, uniformly at random.
Given as input a regular language L as specified by a non-deterministic finite automaton
or a regular expression, Kannan, Sweedyk, and Mahaney [204] present a nO(lg n) expected
time algorithm for generating a random string in L of length n.

c
uvic01, Frank
Ruskey, 19952001

254

CHAPTER 10. RANDOM SELECTION

c
uvic01, Frank
Ruskey, 19952001

Bibliography
[1] U. Abel and R. Bicker, Determination of all minimal cut-sets between a vertex pair
in an undirected graph, IEEE Trans. Reliability R-31 (1982), 167171.
[2] S. Hasanuddin Ahmad, Enumeration of minimal cutsets of an undirected graph, Microelectronics and Reliability 30 (1990), 2326.
[3] M. Aigner, Combinatorial theory, Springer-Verlag, 1979.
[4] Selim G. Akl, A new algorithm for generating derangements, BIT 20 (1980), 27.
[5]
[6]

, A comparison of combination generation methods, ACM Transactions on


Mathematical Software 7 (1981), 4245.
, The design and analysis of parallel algorithms, Prentice-Hall, 1989.

[7] L. Allison, Generating coset representatives for permutation groups, Journal of Algorithms 2 (1981), 227244.
[8] B. Alspach, J.-C. Bermond, and D. Sotteau, Decomposition into cycles I: Hamilton
decompositions, Cycles and Rays, Kluwer Academic Publishers, 1990, pp. 918.
[9] Brian Alspach, The search for long paths and cycles in vertex-transitive graphs and
digraphs, Combinatorial Mathematics VIII, Lecture Notes in Mathematics #884 (K.L.
McAvency, ed.), Springer, Berlin, 1981, pp. 1422.
[10] F.S. Annexstein and E.A. Kuchko, A ranking algorithm for Hamilton paths in shuffleexchange graphs, Proc. 4th Workshop on Algorithms and Data Structures, Lecture
Notes in Computer Science, vol. 955, Springer-Verlag, 1995, pp. 263269.
[11] Benjamin Arazi, An approach for generating different types of Gray codes, Information
and Control 63 (1984), 110.
[12] D.B. Arnold and M.R. Sleep, Uniform random number generation of n balanced parenthesis strings, ACM Transactions on Programming Languages and Systems 2 (1980),
122128.
[13] M.D. Atkinson and J.R. Sack, Generating binary trees at random, Information Processing Letters 41 (1992), 2123.
[14]

, Uniform generation of combinatorial objects in parallel, Journal of Parallel


and Distributed Computing ?? (1993), to appear.
255

256
[15]

BIBLIOGRAPHY
, Uniform generation of forests of restricted height, Information Processing
Letters 50 (1994), 323327.

[16] P. Auger and T.R. Walsh, Theoretical and experimental comparison of four binary
tree generation algorithms, Congressus Numerantium 93 (1993), 99109.
[17] D. Avis and K. Fukuda, A pivoting algorithm for convex hulls and vertex enumeration
of arrangements and polyhedra, Discrete and Computational Geometry 8 (1992), 295
313.
[18]

, Reverse search for enumeration, Discrete Applied Mathematics 65 (1992),


2146.

[19] M.O. Ball and J.S. Provan, Computing bounds on reachability and connectedness in
stochastic networks, Networks 13 (1983), 253278.
[20] Vinnakota Bapiraju and V.V. Bapeswara Rao, Enumeration of binary trees, Information Processing Letters 51 (1994), 125127.
[21] A.M. Baraev and I.A. Faradjev, Construction and study by computer of regular and
regular bipartite graphs, in: Algorithmic studies in combinatorics (In Russian)
(1978), Moscow.
[22] J.A. Bate and D.M. Miller, Fault detection in CMOS circuits and an algorithm for generating Eulerian cycles in directed hypercubes, Congressus Numerantium 47 (1985),
107117.
[23] B. Bauslaugh and F. Ruskey, Generating alternating permutations lexicographically,
BIT 30 (1990), 1726.
[24] Mounir Belbaraka and Ivan Stojmenovic, On generating b-trees with constant average
delay and in lexicographic order, Information Processing Letters 49 (1994), 2732.
[25] M. Bellmore and P.A. Jensen, An implicit enumeration scheme for proper cut generation, Technometrics 12 (1970), 775788.
[26] Jon Bentley, More programming pearls, Addison-Wesley, 1988.
[27] I. Berger, The enumeration of all trees without duplication, IEEE Transactions on
Circuit Theory CT-14 (1967), 417418.
[28] E.R. Berlekamp, J.H. Conway, and R.K. Guy, Winning ways for your mathematical
plays, Academic Press, New York, 1982.
[29] Jean Berstel and Michel Pocchiola, Average cost of Duvals algorithm for generating
Lyndon words, Theoretical Computer Science 132 (1994), 415425.
[30] T. Beyer and S.M. Hedetniemi, Constant time generation of rooted trees, SIAM J.
Computing 9 (1980), 706712.
[31] G.S. Bhat and C.D. Savage, Balanced Gray codes, Electronic Journal of Combinatorics
3 (1996), #R25, 11pp.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

257

[32] J.R. Bitner, G. Ehrlich, and E.M. Reingold, Efficient generation of the binary reflected
Gray code and its applications, Communications of the ACM 19 (1976), 517521.
[33] J.R. Bitner and E.M. Reingold, Backtrack programming techniques, Communications
of the ACM 18 (1975), 651656.
[34] A. Bjorner and G. Ziegler, Introduction to greedoids, Matroid Applications (Neil
White, ed.), Cambridge, 1992.
[35] J.A. Bondy and U.S.R. Murty, Graph theory with applications, North-Holland, 1976.
[36] A. Bonnin and J.M. Pallo, Sur la generation des arbres binaires bar les B-suites,
Discrete Mathematics 51 (1984), 111117.
[37] J. Boothroyd, Perm (Algorithm 6), Computer Bulletin 3 (1965), 104.
[38]

, Permutation of the elements of a vector (Algorithm 29), The Computer Journal 10 (1967), 310311.

[39]

, Fast permutation of the elements of a vector (Algorithm 30), The Computer


Journal 10 (1970), 311312.

[40] P. Bratley, Permutations with repetitions (Algorithm 306), Communications of the


ACM 10 (1967), 450.
[41] Gunnar Brinkmann, Loosers are easy to find or an easy and efficient method to generate regular graphs and related structures, Tech. Report ???, University of Bielefeld,
1993.
[42] A.Z. Broder, Generating random spanning trees, 30th Annual Symposium on Foundations of Computer Science IEEE Press (1989), 442453.
[43] H. Brunn, Uber verkettung, Sitzungberichte der Bayerischer Akad. Wiss. Math-Phys.
22 (1892), 7799.
[44] M. Buck and D. Wiedemann, Gray codes with restricted density, Discrete Mathematics
48 (1984), 163171.
[45] A. Bultena and F. Ruskey, Transition restricted Gray codes, Electronic Journal of
Combinatorics 3 (1996), #R11 (?? pages).
[46] P. Buneman and L. Levy, The towers of Hanoi problem, Information Processing Letters 10 (1980), 243244.
[47] Y. Cai and M.C. Kong, Generating all maximal cliques and related problems for certain
perfect graphs, Congressus Numerantium 90 (1992), 3355.
[48] R.D. Cameron, C.J. Colbourn, R.C. Read, and N.C. Wormald, Cataloguing the graphs
on 10 vertices, Journal of Graph Theory 9 (1985), 551562.
[49] R. Canfield and S.G. Williamson, A loop-free algorithm for generating the linear extensions of a poset, Order 12 (1995), 5775.
c
uvic01, Frank
Ruskey, 19952001

258

BIBLIOGRAPHY

[50] J.J. Canon, L.A. Dimino, G. Havas, and J.M. Watson, Implemenation and analysis
of the Todd-Coxeter algorithm, Mathematics of Computation 123 (1973), 463490.
[51] M. Carkeet and P. Eades, Performance of subset generating algorithms, Annals of
Discrete Math. 26 (1985), 4958.
[52] 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 Engineering 8 (1982),
235249.
[53] Y.H. Chang, J.S. Wang, and R.C.T. Lee, Generating all maximal independent sets on
trees in lexicographic order, Information Sciences 76 (1994), 279296.
[54] J.P. Char, Generation of trees, two-trees, and storage of master forests, IEEE Transactions on Circuit Theory CT-15 (1968), 228238.
[55] P.J. Chase, Algorithm 383: Permutations of a set with repetitions, Communications
of the ACM 13 (1970), 368369.
[56]

, Combinations of m out of n objects, Communications of the ACM 13 (1970),


368.

[57]

, Combination generation and Graylex ordering, Congressus Numerantium 69


(1989), 215242.

[58] C.C. Chen and N.F. Quimpo, On strongly hamiltonian Abelian groups, Combinatorial
Mathematics VIII (Geelong 1980), Lecture Notes in Mathematics, vol. 884, SpringerVerlag, Berlin, 1981, pp. 2334.
[59] W.K. Chen, On directed trees and directed k-trees of a graph and their generation,
SIAM J. Applied Mathematics 14 (1966), 550560.
[60]

, Applied graph theory, North-Holland, 1976.

[61] B.V. Cherkasskii, A new algorithm for the generation of spanning trees, Kibernetica
1 (1987), 8589, English translation from the Russian: Cybernetics 23 (1987), no.1,
107-113.
[62] N. Chiba and T. Nishizeki, Arboricity and subgraph listing algorithms, SIAM J. Computing 14 (1985), 210223.
[63]

, Planar graphs: Theory and algorithms, North-Holland, 1988, Annals of Discrete Mathematics, vol. 32.

[64] T. Chinburg, C.D. Savage, and H.S. Wilf, Combinatorial families that are exponentially far from being listable in gray code sequence, Trans. Amer. Math. Soc. 351
(1999), 379402.
[65] F.R.K. Chung, P. Diaconis, and R.L. Graham, Universal cycles for combinatorial
structures, Discrete Mathematics 110 (1992), 4359.
[66] Martin Cohn, Walsh functions, sequency, and Gray codes, SIAM J. Applied Mathematics 21(3) (1971), 442447.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

259

[67] C.J. Colbourn and R.C. Read, Orderly algorithms for graph generation, Intern. J.
Computer Math. 7 (1979), 167172.
[68] L. Comtet, Advanced combinatorics, D. Reidel, 1974.
[69] J.H. Conway, N.J.A. Sloane, and A.R. Wilks, Gray codes for reflection groups, Graphs
and Combinatorics 5 (1989), 315325.
[70] Thomas H. Cormen, Charles E. Leiserson, and Thomas H. Cormen, Introduction to
algorithms, McGraw Hill, 1989.
[71] T.M. Cover, Enumerative source encoding, IEEE Trans. Information Theory 19
(1973), 7377.
[72] R.R. Coveyou and J.G. Sullivan, Permutation (algorithm 71), Communications of the
ACM 4 (1961), 497.
[73] J.C. Culberson and G.J.E. Rawlins, New results from an algorithm for counting posets,
Order 7 (1991), 361374.
[74] L.J. Cummings, Gray paths of Lyndon words in the N-cube, Congressus Numerantium
69 (1989), 199206.
[75]

, Gray codes and strongly square-free strings, U. Waterloo, 1993.

[76] L.J. Cummings and M.E. Mays, On the parity of the Witt formula, Congressus Numerantium 80 (1991), 4956.
[77] R.L. Cummins, Hamilton circuits in tree graphs, IEEE Transactions on Circuit Theory
CT-13 (1966), 8290.
[78] S.J. Curran and J.A. Gallian, Hamiltonian cycles and paths in Cayley graphs and
digraphs a survey, Discrete Mathematics ?? (199?), submitted?
[79] E. Damiani, O. DAntona, and G. Naldi, On the connection constants, Studies in
Applied Mathematics 85 (1991), 289302.
[80] N. Dershowitz, A simplified loop-free algorithm for generating permutations, BIT 15
(1975), 158164.
[81] N. Dershowitz and S. Zaks, Enumerations of ordered trees, Discrete Mathematics 31
(1980), 928.
[82] L. Devroye, Non-uniform random variate generation, Springer Verlag, 1986.
[83] A. Dewdney, Ying and yang: recursion and iteration, the Tower of Hanoi and the
Chinese rings, Scientific American 251 (1984), 1928.
[84] P. Diaconis and S. Holmes, Gray codes for randomization procedures, Tech. Report
No. 444, Department of Statistics, Stanford University, 1994.
[85] Dillencourt and Smith, ???, Proc. 6th Canadian Conference on Computational Geometry, ACM Press, 1994, pp. ??????
c
uvic01, Frank
Ruskey, 19952001

260

BIBLIOGRAPHY

[86] E.A. Dinits and M.A. Zaitzev, Algorithm for the generation of nonisomorphic trees,
Avtomat. i Telemekh. (Automat. Remote Control) 4 (38) (1977), 121126 (554558).
[87] J.D. Dixon and Herbert S. Wilf, The random selection of unlabelled graphs, Journal
of Algorithms 4 (1983), 205213.
[88] B. Djokic, M. Miyakawa, S. Sekiguchi, I. Semba, and I. Stojmenovic, A fast iterative
algorithm for generating set partitions, The Computer Journal 32 (1989), 281282.
[89] R.J. Douglas, Bounds on the number of Hamiltonian circuits in the n-cube, Discrete
Mathematics 17 (1977), 143146.
[90] R.J. Duffin, An analysis of the Wang algebra of a network, Trans. American Math.
Society 93 (1959), 114131.
[91] R. Durstenfeld, Random permutation (algorithm 235), Communications of the ACM
7 (1964), 420.
[92] J.-P. Duval, Factorizing words over an ordered alphabet, Journal of Algorithms 4
(1983), 363381.
[93]

, Generation dune section des classes de conjugaison et abre des mots de


Lyndon de longueur bornee, Theoretical Computer Science 60 (1988), 255283.

[94] S. Dvorak, Combinations in lexicographic order, The Computer Journal 33 (1990),


188.
[95] P. Eades, M. Hickey, and R.C. Read, Some Hamilton paths and a minimal change
algorithm, Journal of the ACM 31 (1984), 1929.
[96] P. Eades and Brendan D. McKay, An algorithm for generating subsets of a fixed size
with a strong minimal change property, Information Processing Letters 19 (1984),
131133.
[97] B.C. Eaves, Permute (Algorithm 130), Communications of the ACM 11 (1962), 551.
[98] Anthony Edwards, Pascals arithmetical triangle, Oxford University Press, 1987.
[99] A. Ehrenfeucht, J. Haemer, and D. Haussler, Quasi-monotonic sequences: algorithms
and applications, SIAM Journal on Algebraic and Discrete Methods 8 (1987), 410429.
[100] Gideon Ehrlich, Algorithm 466: Four combinatorial algorithms, Communications of
the ACM 16 (1973), 690691.
[101]

, Loopless algorithms for generating permutations, combinations and other


combinatorial configurations, Journal of the ACM 20 (1973), 500513.

[102] T.C. Enns, Hamiltonian circuits and paths in subset graphs with circular adjacency,
Discrete Mathematics 122 (1993), 153165.
[103] David Eppstein, Arboricity and bipartite subgraph listing algorithms, Information Processing Letters 51 (1994), 207211.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

261

[104] P. Erdos and L.A. Szekely, Application of antilexicographic order I. An enumerative


theory of trees, Advances in Applied Mathematics 10 (1989), 488496.
[105] M.C. Er, A note on generating well-formed parenthesis strings lexicographically, The
Computer Journal 26 (1983), 205207.
[106]

, On generating the N-ary reflected Gray codes, IEEE Transactions on Computers C-33 (1984), 739741.

[107]

, Enumerating ordered trees lexicographically, The Computer Journal 28


(1985), 538542.

[108]

, Efficient generation of binary trees from inorder-postorder sequences, Information Sciences 40 (1986), 175181.

[109]

, Classes of admissible permutations that are generatable by depth-first traversals of ordered trees, The Computer Journal 32 (1988), 76??

[110]

, A fast algorithm for generating set partitions, The Computer Journal 31


(1988), 283284.

[111]

, A simple algorithm for generating non-regular trees in lexicographic order,


The Computer Journal 31 (1988), 6164.

[112]

, Efficient generation of k-ary trees in natural order, The Computer Journal


35 (1992), 306308.

[113] M. Erne and K. Stege, Counting finite posets and topologies, Order 8 (1991), 247265.
[114] O. Egecioglu and J.B. Remmel, Bijections for Cayley trees, spanning trees, and their
q-analogues, Journal of Combinatorial Theory, Series A 42 (1986), 1530.
[115] Shimon Even, Algorithmic combinatorics, MacMillan, New York, 1973.
[116] Da Zhong Fang, Enumeration of all spanning trees of an unndirected graph, J. Tianjin
Univ. 4 (1988), 108116, (In Chinese with English summary).
[117] I.A. Faradjev, Constructive enumeration of combinatorial objects, in: Probl`emes Combinatoires et Theorie des Graphes Colloque. Internat. CNRS 260, Paris (1978),
131135.
[118] R.J. Faudree and R.H. Schelp, The square of a block is strongly path connected, J. of
Combinatorial Theory 20 (1976), 4761.
[119] R. Feldman and P. Mysliwietz, The shuffle exchange network has a Hamilton path,
Proc. 17th Mathematical Foundations of Computer Science Conference, Lecture Notes
in Computer Science, vol. 629, Springer-Verlag, 1992, pp. 246254.
[120] T.I. Fenner and G. Loizou, A binary tree representation and related algorithms for
generating integer partitions, The Computer Journal 23 (1980), 332337.
[121]

, An analysis of two related loop-free algorithms for generating integer partitions, Acta Informatica 16 (1981), 237252.
c
uvic01, Frank
Ruskey, 19952001

262
[122]

BIBLIOGRAPHY
, Tree traversal related algorithms for generating integer partitions, SIAM J.
Computing 12 (1983), 551564.

[123] W. Feussner, Uber


stromverzweigung in netzf
ormigen leitern, Ann. Physik 9 (1902),
13041329.
[124]

, Zur berechnung der stromst


arke in netzf
ormigen leitern, Ann. Physik 15
(1904), 385394.

[125] C.T. Fike, A permutation generation method, The Computer Journal 18 (1975), 21
22.
[126] James Allen Fill and Edward M. Reingold, Solutions manual for combinatorial algorithms: Theory and practice, Prentice-Hall, 1977.
[127] J.P. Fillmore and S.G. Williamson, On backtracking: a combinatorial description of
the algorithm, SIAM J. Computing 3 (1974), 4155.
[128]

, On ranking functions: The symmetries and colorations of the n-cube, SIAM


J. Computing 5 (1975), 297304.

[129] L.L. Fischer and K.C. Krause, Lehrbuch der combinationslehre und der arithmetik,
Dresden, 1812.
[130] P. Flajolet and L. Ramshaw, A note on Gray code and odd-even merge, SIAM J.
Computing 9 (1980), 142158.
[131] P. Flajolet, P. Zimmerman, and B. Van Cutsem, A calculus for the random generation
of combinatorial structures, Theoretical Computer Science 132 (1994), no. 1-2, 135.
[132] H. Fleishner, Eulerian graphs, North-Holland, 19??
[133]

, A way of constructing and a way of counting all Eulerian trails in a connected


Eulerian graph, Tech. Report unpublished, ????, 19??

[134]

, The square of every two-connected graph is Hamiltonian, Journal of Combinatorial Theory, Series B 16 (1974), 2934.

[135] C. Flye-Sainte Marie, Solution to problem number 58, lIntermediare des Mathematiciens 1 (1894), 107110.
[136] S. Foldes, A characterization of hypercubes, Discrete Mathematics 17 (1977), 155159.
[137] A.S. Fraenkel, Systems of numeration, American Mathematical Monthly 92 (1985),
105114.
[138]

, The use and usefulness of numeration systems, Information and Computation


81 (1989), 4661.

[139] H. Fredricksen and I. J. Kessler, An algorithm for generating necklaces of beads in two
colors, Discrete Mathematics 61 (1986), 181188.
[140] H. Fredricksen and J. Maiorana, Necklaces of beads in k colors and k-ary de Bruijn
sequences, Discrete Mathematics 23 (1978), 207210.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

263

[141] H. Fredricksen, A survey of full length non-linear shift register cycle algorithms, SIAM
Review 24 (1982), 195221.
[142] H. Fredricksen and I. J. Kessler, Lexicographic compositions and de Bruijn sequences,
Journal of Combinatorial Theory 22 (1977), 1730.
[143] K. Fukuda and M. Namiki, Finding all common bases in two matroids, Tech. Report
Working paper No. 32, University of Tokyo, Komaba, Dept. of Social and International
Relations, 1993.
[144] Komei Fukuda and Tomomi Matsui, Finding all minimum-cost perfect matchings in
bipartite graphs, Networks 22 (1992), 461468.
[145] G.W. Furnas, The generation of random, binary unordered trees, J. Classification 1
(1984), 187233.
[146] H.N. Gabow, Two algorithms for generating weighted spanning trees in order, SIAM
J. Computing 6 (1977), 139150.
[147] H.N. Gabow and E.W. Myers, Finding all spanning trees of directed and undirected
graphs, SIAM J. Computing 7 (1978), 280287.
[148] Martin Gardner, Knotted doughnuts (and other mathematical entertainments), W.H.
Freeman, 1986.
[149] M.L. Gardner, An algorithm to aid in the design of large scale networks, Large Scale
Systems 8 (1985), 147156.
[150] E.N. Gilbert, Gray codes and paths on the n-cube, Bell Systems Technical Journal 37
(1958), 815826.
[151] L.A. Goldberg, Efficient algorithms for listing unlabeled graphs, J. Algorithms 13
(1992), 128143.
[152]

, Efficient algorithms for listing combinatorial structures, Cambridge University Press, 1993.

[153] S.W. Golomb, Permutations by cutting and shuffling, SIAM Review 3 (1961), 293
297.
[154]
[155]

, Polyominoes, Charles Scribners Sons, New York, 1965.


, Irreducible polynomials, synchronization codes, primitive necklaces, and the
cyclotomic algebra, Combinatorial Mathematics and its Applications (1969), 358370.

[156] S.W. Golomb and L.D. Baumert, Backtrack programming, Journal of the ACM 4
(1965), 516524.
[157] I.P. Goulden and D.M. Jackson, Combinatorial enumeration, Wiley, 19??
[158] H.W. Gould, Research bibliography of two number sequences, Combinatorial Research
Institute, Morgantown, W. Va., June 8, 1976.
c
uvic01, Frank
Ruskey, 19952001

264

BIBLIOGRAPHY

[159] R.L. Graham, D.E. Knuth, and O. Patashnik, Concrete mathematics : A foundation
for computer science, Addison-Wesley, 1989.
[160] F. Gray, Pulse code communication, U.S. Patent 2,632,058, March 17, 1953.
[161] T.A. Gulliver, M. Serra, and V.K. Bhargava, The generation of primitive polynomials
in GF(q) with independent roots and their applications for power residue codes, VLSI
testing, and finite field multipliers using normal basis, Int. J. Electronics 71 (1991),
559576.
[162] U.I. Gupta, D.T. Lee, and C.K. Wong, Ranking and unranking of 2-3 trees, SIAM J.
Computing 11 (1982), 582590.
[163]

, Ranking and unranking of B-trees, Journal of Algorithms 4 (1983), 5160.

[164] Dan Gusfield and Robert W. Irving, The stable marriage problem, The MIT Press,
1989.
[165] Michel Habib, Lhouari Nourine, and George Steiner, Gray codes for the ideals of
interval orders, Journal of Algorithms 25 (1997), 5266.
[166] S.L. Hakimi, On trees of a graph and their generation, Journal of the Franklin Institute
272 (1961), 347359.
[167] S.L. Hakimi and P.G. Green, ???, IEEE Transactions on Circuit Theory 11 (1964),
247255.
[168] M. Hall and D.E. Knuth, Combinatorial analysis and computers, American Mathematics Monthly 2 (1972), 2128.
[169] Kazuaki Harada, Generation of rosary permutations expressed in Hamiltonian circuits, Communications of the ACM 14 (1971), 373379.
[170] F. Harary, J. Hayes, and H.-J. Wu, Survey of hypercubes, Computer and Mathematics
with Applications 15 (1988), 277289.
[171] L.H. Harper, Optimal assignments of numbers to vertices, SIAM J. 12 (1964), 131
135.
[172]

, Optimal numberings and isoperimetric problems on graphs, J. Combinatorial


Theory 1 (1966), 385393.

[173] I. Havel, On Hamiltonian circuits and spanning trees of hypercubes, Casopis


Pest.
Mat. 109 (1984), 135152.
[174] B.R. Heap, Permutations by interchanges, The Computer Journal 6 (1963), 293294.
[175] F.G. Heath, Origins of the binary code, Scientific American 227 (1972), 7683.
[176] G. Hendry and W. Volger, The square of a connected S(K1,3 )-free graph is vertex
pancyclic, J. Graph Theory 9 (1985), 535537.
[177] T. Hickey and J. Cohen, Uniform random generation of strings in a context-free
language, SIAM J. Computing 12 (1983), 645655.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

265

[178] T. Hikita, Listing and counting subtrees of equal size of a binary tree, Information
Processing Letters 17 (1983), 225229.
[179] C.A. Holzmann and F. Harary, On the tree graph of a matroid, SIAM J. Applied
Math. 22 (1972), 187193.
[180] T. Hough and F. Ruskey, An efficient implementation of the Eades, Hickey, Read adjacent interchange combination generation algorithm, J. Combinatorial Mathematics
and Combinatorial Computing 4 (1988), 7986.
[181] J.R. Howell, Generation of permutations by addition, Math. Comp. 16 (1962), 243
244.
[182]

, Permutation generator (algorithm 87), Communications of the ACM 5


(1962), 452453.

[183] S.S. Huang and D. Tamari, Problems of associativity: A simple proof for the lattice property of systems ordered by a semi-associative law, Journal of Combinatorial
Theory, Series A 13 (1972), 713.
[184] Y. Huang, A new algorithm for the generation of binary de Bruijn sequences, Journal
of Algorithms 11 (1990), 4451.
[185] T.C. Hu and B.N. Tien, Generating permutations with non-distinct items, American
Mathematical Monthly 83b (1976), 629631.
[186] G. Hurlbert, On universal cycles for k-subsets of an n-set, SIAM J. Discrete Mathematics 7 (1994), 594604.
[187]

, Multicover ucycles, Discrete Mathematics 137 (1995), 241249.

[188] A. Itai and M. Rodeh, Finding a minimum circuit in a graph, SIAM J. Computing 7
(1978), 413423.
[189] F.M. Ives, Permutation enumeration: Four new permutation algorithms, Communications of the ACM 19 (1976), 6872.
[190] M.E. Jablonski, Theorie des permutations et des arrangements circulaires complets,
Journal de Mathematiques pures et appliquees, fonde par Joseph Liouville 8 (1892),
331349.
[191] B. Jackson, Universal cycles for k-subsets and k-permutations, Discrete Mathematics
117 (1993), 141150.
[192] K.R. James and W. Riha, Algorithm for generating graphs of a given partition, Computing 16 (1976), 153161.
[193] R. Jayakumar, K. Thulasiraman, and M.N.S. Swamy, Complexity of computation of
a spanning tree enumeration algorithm, IEEE Trans. Circuits and Systems 31 (1984),
853860.
[194] T.A. Jenkyns and D. McCarthy, Generating all k-subsets of {1, 2, . . . , n} with minimum changes, ars 40 (1995), 153159.
c
uvic01, Frank
Ruskey, 19952001

266

BIBLIOGRAPHY

[195] M. Jiang and F. Ruskey, Determining the Hamilton-connectedness of certain vertextransitive graphs, Discrete Mathematics 133 (1994), 159169.
[196] B. Johnsen, Generating binary trees with uniform probability, BIT 31 (1991), 1531.
[197] Donald B. Johnson, Finding all the elementary circuits of a directed graph, SIAM J.
Computing 4 (1975), 7784.
[198] S.M. Johnson, Generation of permutations by adjacent transpositions, Math. Computation 17 (1963), 282285.
[199] J.T. Joichi and D.E. White, Gray codes in graphs of subsets, Discrete Mathematics
31 (1980), 2941.
[200] J.T. Joichi, D.E. White, and S.G. Williamson, Combinatorial Gray codes, SIAM J.
Computing 9 (1980), 130141.
[201] A.D. Kalvin and Y.L. Varol, On the generation of all topological sortings, Journal of
Algorithms 4 (1983), 150162.
[202] T. Kamae, The existence of a Hamilton circuit in a tree graph, IEEE Transactions on
Circuit Theory CT-14 (1967), 279283.
[203] Arkady Kanevsky, On the number of minimum size separating vertex sets in a graph
and how to find all of them, Proc. 1st ACM-SIAM Symposium on Discrete Algorithms,
ACM Press, 1990, pp. 411421.
[204] Sampath Kannan, Z. Sweedyk, and Steve Mahaney, Counting and random generation of strings in regular languages, Proc. 6th ACM-SIAM Symposium on Discrete
Algorithms, ACM Press, 1995, pp. 551557.
[205] S. Kapoor and H. Ramesh, Algorithms for generating all spanning trees of undirected,
directed and weighted graphs, Lecture Notes in Computer Science ?? (1992), 461472.
[206] Jerome J. Karaganis, On the cube of a graph, Canadian Mathematical Bulletin 11
(1968), 295296.
[207] Toshinobu Kashiwabara, Sumio Masuda, Kazuo Nakajima, and Toshio Fujisawa, Generation of maximum independent sets of a bipartite graph and maximum cliques of a
circular-arc graph, Journal of Algorithms 13 (1992), 161174.
[208] R.A. Kaye, A Gray code for set partitions, Info. Proc. Lett. 5 (1976), 171173.
[209] Pierre Kelsen, Constant time generation of B-trees, unpublished manuscript, 1987.
[210] P. Kirschenhofer and H. Prodinger, Subblock occurances in positional number systems
and Gray code representation, J. Inform. Optim. Sci. 5 (1984), 2942.
[211] G. Kishi and Y. Kajitani, On Hamiltonian circuits in tree graphs, IEEE Transactions
on Circuit Theory CT-15 (1968), 4250.
[212]

, On the realization of tree graphs, IEEE Transactions on Circuit Theory CT15 (1968), 271273.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

267

[213] M. Kleinert, Die dicke des n-dimensionalen W


urfel-graphen, J. Combinatorial Theory
3 (1967), 1015.
[214] P. Klingsberg, A Gray code for compositions, Journal of Algorithms 3 (1982), 4144.
[215] G. D. Knott, A numbering system for combinations, Communications of the ACM 17
(1974), 4546.
[216]

, A numbering system for permutations of combinations, Communications of


the ACM 19 (1976), 4546.

[217]

, A numbering system for binary trees, Comm. Assoc. Comput. Mach. 20


(1977), 113115.

[218] D.E. Knuth, Fundamental algorithms, Addison-Wesley, 1973.


[219]

, Sorting and searching, Addison-Wesley, 1973.

[220]

, Estimating the efficiency of backtracking programs, Mathematics of Computation 29 (1975), 121136.

[221]

, Lexicographic permutations with restrictions, Discrete Applied Mathematics


1 (1979), 117125.

[222]

, The stanford graphbase, Addison-Wesley, 1994.

[223] D.E. Knuth and J. Szwarcfiter, A structured program to generate all topological sorting
arrangements, Information Processing Letters 2 (1974), 153157.
[224] Y. Koda, Orderly algorithms for generating k-colored graphs, Bulletin of the Institute
of Combinatorics and Its Applications 91 (1993), 223238.
[225]

, The numbers of finite lattices and finite topologies, Congressus Numerantium


10 (1994), 8389.

[226] Y. Koda and F. Ruskey, A Gray code for the ideals of a forest poset, Journal of
Algorithms 15 (1993), 324340.
[227] C.W. Ko and F. Ruskey, Generating permutations of a bag by interchanges, Information Processing Letters 41 (1992), 263269.
[228] V.L. Kompelmakher and V.A. Liskovets, Sequential generation of arrangements by
means of a basis of transpositions, Kibernetica 3 (1975), 1721.
[229] J.F. Korsh, Counting and randomly generating binary trees, Information Processing
Letters 45 (1993), 291294.
[230] J.F. Korsh and S. Lipschutz, Generating multiset permutations in constant time, Journal of Algorithms 25 (1997), 321335.
[231] Bernard Korte, Laszlo Lov
asz , and Rainer Schrader, Greedoids, Springer-Verlag,
1991.
c
uvic01, Frank
Ruskey, 19952001

268

BIBLIOGRAPHY

[232] A. V. Kozina, Coding and generation of nonisomorphic trees, Cybernetics (Kibernetica) 15 (5) (1975 (1979)), 645651 (3843).
[233] D.L. Kreher and D.R. Stinson, Combinatorial algorithms: Generation, enumeration,
and search, CRC Press, 1999.
[234] E. Kubicka and G. Kubicki, Constant time algorithm for generating binary rooted
trees, Congressus Numerantium 90 (1992), 5764.
[235] Ewa Kubicka, An efficient algorithm for examining all trees, unpublished manuscript,
1993.
[236] J. Kurtzberg, Algorithm 94: Combination., Communications of the ACM 5 (1962),
344.
[237] C.W.H. Lam and L.H. Soicher, Three new combination algorithms with the minimal
change property, Communications of the ACM 25 (1982), 555559.
[238] G.G. Langdon, Jr., An algorithm for generating permutations, Communications of the
ACM 10 (1967), 298299.
[239] F.-J. Lapointe and P. Legendre, The generation of random ultrametric matrices representing dendograms, J. Classification 8 (1991), 177200.
[240] S. Latifi and S.Q. Zheng, On Hamiltonian paths between two vertices in hypercube
graphs, Congressus Numerantium 89 (1992), 111117.
[241] R. Laue, Construction of combinatorial objects a tutorial, Bayreuther Math.
Schriften 43 (1993), 5396.
[242] C.C. Lee, D.T. Lee, and C.K. Wong, Generating binary trees of bounded height, Acta
Informatica 23 (1986), 529544.
[243] D.H. Lehmer, Teaching combinatorial tricks to a computer, Proc. Symp. Applied
Mathematics, American Mathematical Society, 1960, pp. 179193.
[244]

, The machine tools of combinatorics, Applied Combinatorial Mathematics,


Wiley, 1964, pp. 531.

[245] J.K. Lenstra and A.H.G. Rinnooy-Kan, A recursive approach to the generation of
combinatorial configurations, Mathematisch Centrum, Amsterdam, 1975.
[246] Harry R. Lewis and Larry Denenberg, Data structures & their algorithms, HarperCollins Publishers, Inc., 1991.
[247] A.L. Liestman and T.C. Shermer, Additive spanners for hypercubes, Parallel Processing Letters 1 (1991), 3542.
[248] G. Li and F. Ruskey, The advantages of forward thinking in generating rooted and
free trees, 10th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA),
SIAM-ACM, 1999, pp. S939940.
[249] Liwu Li, Ranking and unranking of AVL trees, SIAM J. Computing 15 (1986), 1025
1035.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

269

[250] W. Lipski, More on permutation generation methods, Computing 23 (1979), 357365.


[251] C.N. Liu and D.T. Tang, Algorithm 452: Enumerating combinations of m out of n
objects, Communications of the ACM 16 (1973), 485.
[252] Gui Zhen Liu, An algorithm for generating ordered forests lexicographically, Chinese
J. Operations Research 4 (1985), 6566.
[253] M. Lothaire, Combinatorics on words, Addison-Wesley, Reading, MA, 1983.
[254] E. Loukakis, A new backtracking algorithm for generating the family of maximal independent sets of a graph, Computers and Mathematics with Applications 9 (1983),
583589.
[255] J. Lucas, The rotation graph of binary trees is Hamiltonian, Journal of Algorithms 9
(1988), 503535.
[256] J. Lucas, D. Roelants van Baronaigien, and F. Ruskey, On rotations and the generation of binary trees, Journal of Algorithms 15 (1993), 343366.
[257] X.-M. Lu, Towers of Hanoi problem with arbitrary k 3 pegs, International Journal
of Computer Mathematics 24 (1988), 3954.
[258] H. L
uneburg, Gray codes, Abh. Math. Sem. Univ. Hamburg 52 (1982), 208227.
[259]

, On Dedekind numbers, Lecture Notes in Mathematics (Combinatorial Theory) 696 (1982), 251257.

[260] Heinz L
uneburg, Tools and fundamental constructions of combinatorial mathematics,
BI Wissenschaftsverlag, Mannheim/Wein/Zurich, 1989.
[261] E. Makinen, Left distance binary trees representations, BIT 27 (1987), 163169.
[262]

, Efficient generation of rotational-admissible codewords for binary trees, The


Computer Journal 34 (1991), 379.

[263]

, A survey of binary tree codings, The Computer Journal 34 (1991), 438443.

[264] George E. Martin, Polyominoes: A guide to puzzles and problems in tiling, MAA
Spectrum, 1991.
[265] H.W. Martin and B.J. Orr, A random binary tree generator, Computing trends in the
1990s, ACM Seventeenth Computer Science Conference, ACM Press, 1989, Louisville,
Kentucky, pp. 3338.
[266] M.H. Martin, A problem in arrangements, Bulletin of the American Mathematical
Society 40 (1934), 859864.
[267] Dragan Marusic, Hamiltonian circuits in Cayley graphs, Discrete Mathematics 46
(1983), 4954.
[268] D.W. Matula and L.L. Beck, Smallest-last ordering and clustering and graph coloring
algorithms, Journal of the ACM 30 (1983), 417427.
c
uvic01, Frank
Ruskey, 19952001

270

BIBLIOGRAPHY

[269] W. Mayeda and S. Seshu, Generation of trees without duplications, IEEE Transactions
on Circuit Theory CT-12 (1965), 181185.
[270] Brendan D. McKay, Transitive graphs with fewer than 20 vertices, Mathematics of
Computation 33 (1979), 11011121.
[271]

, Isormorph-free exhaustive generation, Journal of Algorithms 26 (1998), 306


324.

[272] Brendan D. McKay and G.F. Royle, Constructing the cubic graphs on up to 20 vertices, Ars Combinatoria 21A (1986), 129140.
[273]

, The transitive graphs with at most 26 vertices, Ars Combinatoria 30 (1990),


161176.

[274] Brendan D. McKay and Nicholas C. Wormald, Uniform generation of random regular
graphs of moderate degree, Journal of Algorithms 11 (1990), 5267.
[275] J.K.S. McKay, Algorithm 262: Number of restricted partitions of n, Communications
of the ACM 8 (1965), 493.
[276]

, Algorithm 263: Partition generator, Communications of the ACM 8 (1965),


493.

[277]

, Algorithm 264: Map of partitions into integers, Communications of the ACM


8 (1965), 493.

[278]

, Algorithm 371: Partitions in natural order, Communications of the ACM 13


(1970), 52.

[279] N. Metropolis and G-C. Rota, Witt vectors and the algebra of necklaces, Advances in
Mathematics 50 (1983), 95125.
[280] C.J. Mifsud, Algorithm 154: Combination in lexicographic order, Communications of
the ACM 6 (1963), 103.
[281] W.H. Mills, Some complete cycles on the n-cube, Proceedings of the AMS 14 (1963),
640643.
[282] S. Minsker, The towers of Antwerpen problem, Information Processing Letters 38
(1991), 107111.
[283] G.J. Minty, A simple algorithm of listing all the trees of a graph, IEEE Transactions
on Circuit Theory CT-12 (1965), 120???
[284] J. Misra, Remark on algorithm 246, ACM Transactions on Mathematical Software 1
(1975), 285.
[285] J.W. Moon, Counting labelled trees, Canadian Mathematical Mongraphs, No. 1, 1970.
[286] T. Mukherjee and P.K. Sarkar, On connectedness and trees of an n-graph, International Journal of Control 4 (1966), 465498.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

271

[287] W. Myrvold and F. Ruskey, Ranking and unranking permutations in linear time,
Information Processing Letters ? (2001), ??????
[288] T.V. Narayana, R.M. Mathsen, and J. Saranji, An algorithm for generating partitions
and its applications, Journal of Combinatorial Theory, Series A 11 (1971), 5461.
[289] A. Nijenhuis and H.S. Wilf, Combinatorial algorithms, 2nd ed., Academic Press, 1978.
[290] S.E. Orcutt, Implementation of permutation functions in Illiac IV-type computers,
IEEE Transactions on Computers C-25 (1976), 929936.
[291] R.J. Ord-Smith, Generation of permutations in pseudo-lexicographic order (algorithm
308), Communications of the ACM 10 (1967), 452.
[292]

, Generation of permutations in lexicographic order (algorithm 323), Communications of the ACM 2 (1968), 117.

[293]

, Generation of permutations in lexicographic order (algorithm 323), Communications of the ACM 11 (1968), 117.

[294]

, Generation of permutation sequences: Part 1, The Computer Journal 13


(1970), 152155.

[295]

, Generation of permutation sequences: Part 2, The Computer Journal 14


(1971), 136139.

[296] E.S. Page and L.B. Wilson, An introduction to computational combinatorics, Cambridge, 1979.
[297] J. Pallo, Enumerating, ranking, and unranking binary trees, The Computer Journal
29 (1986), 171175.
[298]

, Generating trees with n nodes and m leaves, International Journal of Computer Mathematics 16 (1987), 133144.

[299]

, Some properties of the rotation lattice of binary trees, The Computer Journal
31 (1988), 564565.

[300]

, Lexicographic generation of binary unordered trees, Pattern Recognition Letters 10 (1989), 217221.

[301]

, On the listing and random generation of hybrid binary trees, International


Journal of Computer Mathematics 50 (1994), 135145.

[302] J. Pallo and R. Racca, A note on generating binary trees in A-order and B-order,
International Journal of Computer Mathematics 18 (1985), 2739.
[303] C.H. Papadimitriou and M. Yannakakis, The clique problem for planar graphs, Information Processing Letters 13 (1981), 131133.
[304] Ian Parberry, Problems on algorithms, Prentice-Hall, 1995.
[305] J.E.L. Peck and G.F. Schrak, Permute (algorithm 86), Communications of the ACM
5 (1962), 208.
c
uvic01, Frank
Ruskey, 19952001

272

BIBLIOGRAPHY

[306] C. Peter-Orth, All solutions of the Soma cube puzzle, Discrete Mathematics 57 (1985),
105121.
[307] W.W. Peterson and E.J. Weldon, Error-correcting codes, MIT Press, Cambridge, MA,
1972.
[308] L.P. Petrenjuk and A.N. Petrenjuk, On constructive enumeration of 12 vertex cubic
graphs, Combinatorial Analysis No. 3 Moscow (1974), in Russian.
[309] J.E.L. Phillips, Permutation of the elements of a vector in lexicographic order (algorithm 28), The Computer Journal 10 (1967), 310311.
[310] J.C. Picard and M. Queyranne, On the structure of all minimum cuts in a network
and applications, Math. Prog. Study 13 (1980), 816.
[311] L. Pitt, A note on extending Knuths tree estimator to directed acyclic graphs, Information Processing Letters 24 (1987), 203206.
[312] S. Pleszczy
nski, On the generation of permutations, Information Processing Letters 3
(1975), 180183.
[313] David G. Poole, The towers and triangles of Professor Claus (or, Pascal knows
Hanoi), Mathematics Magazine 67 (1994), 323344.
[314] A. Proskurowski, On the generation of binary trees, Journal of the ACM 27 (1980),
12.
[315] A. Proskurowski and E. Laiman, Fast enumeration, ranking, and unranking of binary
trees, Congressus Numerantium 35 (1980), 401413.
[316] A. Proskurowski and F. Ruskey, Binary tree Gray codes, Journal of Algorithms 6
(1985), 225238.
[317] A. Proskurowski, F. Ruskey, and M. Smith, Analysis of algorithms for listing equivalence classes of k-ary strings induced by simple group actions, SIAM J. Discrete
Mathematics 11 (1998), 94109.
[318] J.S. Provan and D.R. Shier, A paradigm for listing (s, t)-cuts in graphs, Tech. Report
UNC/OR TR91-3, Univ. of North Carolina at Chapel Hill, 1991.
[319] G. Pruesse and F. Ruskey, Generating the linear extensions of certain posets by transpositions, SIAM J. Discrete Mathematics 4 (1991), 413422.
[320]

, Gray codes from antimatroids, Order 10 (1993), 239252.

[321]

, Generating linear extensions fast, SIAM J. Computing 23 (1994), 373386.

[322]

, The prism of the acyclic orientation graph is hamiltonian, Electronic Journal


of Combinatorics 2 (1995), no. R5, (6pp).

[323] K. Pruhs, The SPIN-OUT puzzle, SIGCSE Bulletin 25 (1993), 3638.


[324] Paul. W. Purdom, Tree size by partial backtracking, SIAM J. Computing 7 (1978),
481491.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

273

[325] D.F. Rall and P.J. Slater, Generating all permutations by graphical derangements,
unpublished manuscript, < 1990.
[326] A. Ralston, A new memoryless algorithm for de Bruijn sequences, Journal of Algorithms 2 (1981), 5062.
[327] P.V. Ramanan and C.L. Liu, Permutation representation of k-ary trees, Theoretical
Computer Science 38 (1985), 8398.
[328] A. Ramanathan and C.J. Colbourn, Counting almost minimum cutsets with reliability
applications, Math. Prog. 39 (1987), 253261.
[329] Mark Ramras, A new method of generating Hamiltonian cycles on the n-cube, Discrete
Mathematics 85 (1990), 329331.
[330] D. Randall, Efficient generation of random nonsingular matrices, Random Structures
and Algorithms 4 (1993), 111118.
[331] R.A. Rankin, A campanological problem in group theory, Proc. Cambridge Phil. Society 44 (1948), 1725.
[332] E.S. Rapaport, Cayley colour groups and Hamilton lines, Scripta Math. 24 (1959),
5158.
[333] D. Rasmussen, C.D. Savage, and D.B. West, Gray code enumeration of families of
integer partitions, Journal of Combinatorial Theory, Series A 70 (1995), 201229.
[334] R.C. Read, A note on the generation of rosemary permutations, Communications of
the ACM 15 (1972), 775.
[335]

, Every one a winner or how to avoid isomorphism search when cataloguing


combinatorial configurations, Annals of Discrete Math. 2 (1978), 107120.

[336]

, Orderly algorithms for generating restricted classes of graphs, Journal of


Graph Theory 3 (1979), 187195.

[337]

, A survey of graph generation techniques, Combinatorial Mathematics, VIII,


Lecture Notes in Mathematics, vol. 844, Springer-Verlag, 1981, pp. 7789.

[338] R.C. Read and R.E. Tarjan, Bounds on backtracking algorithms for listing cycles,
paths, and spanning trees, Networks 5 (1975), 237252.
[339] D.H. Redelmeier, Counting polyominoes: yet another attack, Discrete Mathematics
36 (1981), 191203.
[340] E.M. Reingold, J. Nievergelt, and N. Deo, Combinatorial algorithms: Theory and
practice, Prentice-Hall, 1977.
[341] J.L. Remy, Un procede iteratif denombrement darbes binaires et son application `
a
leur generation aleatoire, R.A.I.R.O. Informatique Theorique 19 (1985), 179195.
[342] Ch. Reutenauer, Mots circulaires et polynomes irreductibles, Tech. Report 87-39,
CNRS Universite Paris VII, Informatique Theoretique et Programmation, 1987.
c
uvic01, Frank
Ruskey, 19952001

274

BIBLIOGRAPHY

[343] D. Richards, Data compression and Gray-code sorting, Information Processing Letters
22 (1986), 201205.
[344] W. Riha and K.R. James, Efficient algorithms for doubly and multiply restricted partitions, Computing 16 (1976), 163168.

[345] G. Ringel, Uber


drei kombinatorische probleme am n-dimensionalen W
urfel und
W
urfelgitter, Abh. Math. Sem. Univ. Hamburg 20 (1955), 1019.
[346] C.L. Robinson, Permutation (algorithm 317), Communications of the ACM 10 (1967),
729.
[347] Jeffrey Rohl, Recursion via pascal, Cambridge University Press, 1984.
[348] J.S. Rohl, Programming improvements to Fikes algorithm for generating permutations, The Computer Journal 19 (1976), 156.
[349]

, Generating permutations by choosing, The Computer Journal 21 (1978), 302


305.

[350] D. Rotem and Y. Varol, Generation of binary trees from ballot sequences, Journal of
the ACM 25 (1978), 396404.
[351] M.K. Roy, Permutation generation methods, The Computer Journal 21 (1978), 196
301.
[352] Gordon Royle, An orderly algorithm and some applications in finite geometry, Discrete
Mathematics 185 (1999), 105115.
[353] F. Ruskey, Simple combinatorial Gray codes constructed by reversing sublist, Proc.
ISAAC Conference, Lecture Notes in Computer Science, vol. 762, Springer-Verlag,
1993, pp. 201208.
[354] F. Ruskey and T.C. Hu, Generating binary trees lexicographically, SIAM J. Computing
6 (1977), 745758.
[355] F. Ruskey and D.J. Miller, Adjacent interchange generation of combinations and trees,
Tech. Report DCS-44-IR, U. Victoria, Dept. of Computer Science, 1984.
[356] F. Ruskey and A. Proskurowski, Generating binary trees by transpositions, Journal of
Algorithms 11 (1990), 6884.
[357] F. Ruskey, C. Savage, and T.M.Y. Wang, Generating necklaces, Journal of Algorithms
13 (1992), 414430.
[358] F. Ruskey and C.D. Savage, Gray codes for set partitions and restricted growth tails,
Austalasian J. Combinatorics 10 (1994), 8596.
[359] F. Ruskey and J. Sawada, An efficient algorithm for generating necklaces with fixed
density, SIAM J. Computing 29 (1999), 671684.
[360] F. Ruskey and D. Roelants van Baronaigien, Fast recursive algorithms for generating
combinatorial objects, Congressus Numerantium (1984), 5362.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

275

[361] Frank Ruskey, Generating t-ary trees lexicographically, SIAM J. Computing 6 (1977),
424439.
[362]

, Algorithmic solution of two combinatorial problems, Ph.D. thesis, Information


and Computer Science, University of California at San Diego, 1978.

[363]

, Listing and counting subtrees of a tree, SIAM J. Computing 10 (1981), 141


150.

[364]

, Adjacent interchange generation of combinations, Journal of Algorithms 9


(1988), 162180.

[365]

, Transposition generation of alternating permutations, Order 6 (1989), 227


233.

[366]

, Generating linear extensions of posets by transpositions, Journal of Combinatorial Theory, Series B 54 (1992), 77101.

[367] Y. Saad and M.H. Scults, Topological properties of hypercubes, IEEE Transactions on
Computers 22 (1973), 176180.
[368] T.W. Sag, Permutations of a set with repetitions (Algorithm 242), Communications
of the ACM 7 (1964), 585.
[369] C. Savage, Gray code sequences of partitions, Journal of Algorithms 10 (1989), 577
595.
[370]

, Generating permutations with k-differences, SIAM J. Discrete Mathematics


3 (1990), 561573.

[371]

, A survey of combinatorial Gray codes, SIAM Review 39 (1997), no. 4, 605


629.

[372] C.D. Savage and P. Winkler, Monotone Gray codes and the middle levels problem,
Journal of Combinatorial Theory, Series A 70 (1995), 230248.
[373] J. Savage, An efficient algorithm for generating bracelets, SIAM J. Computing ?? (to
appear), ??????
[374] G.F. Schrak and M. Shimrat, Permutation in lexicographic order (algorithm 102),
Communications of the ACM 5 (1962), 346.
[375] F. Schuh, Master book of mathematical recreations, Dover, 1968.
[376] H.I. Scions, Placing trees in lexicographic order, Machine Intelligence 3 (1969), 4360.
[377] R. Sedgewick, Permutation generation methods, Computing Surveys 9 (1977), 137
164.
[378] M. Sekanina, On an ordering of the set of vertices of a connected graph, Publication
of the Faculty of Science, University of Brno 412 (1960), 137142.
[379] I. Semba, Generation of stack sequences in lexicographic order, J. Information Processing 15 (1982), 1720.
c
uvic01, Frank
Ruskey, 19952001

276
[380]

BIBLIOGRAPHY
, An efficient algorithm for generating all partitions of the set {1, 2, . . . , n}, J.
Information Processing 7 (1984), 4142.

[381] M. Serra, Tables of irreducible and primitive polynomials for GF(3), Tech. Report
DCS-??-IR, U. Victoria, Dept. of Computer Science, 1986.
[382] B.D. Sharma and R.K. Khanna, !!!! unknown !!!!, J. Combin. Inform. System Sci. 4
(1979), 227236.
[383]

, On level weight studies of binary and m-ary Gray codes, Information Sciences
21 (1980), 179186.

[384] Bhu Dev Sharma and Ravinder Kumar Khanna, On m-ary Gray codes, Information
Sciences 15 (1978), 3143.
[385] Mok-kong Shen, On the generation of permutations and combinations, BIT 2 (1962),
228231.
[386]

, Generation of permutations in lexicographic order, Communications of the


ACM 6 (1963), 517.

[387] A. Shioura and A. Tamura, Efficiently scanning all spanning trees of an undirected
graph, Tech. Report B-270, Tokyo Institute of Technology, Dept. of Information Sciences, 1993.
[388] Alistair Sinclair, Algorithms for random generation and counting, Birkhauser, 1992.
[389] W. Skarbek, Generating ordered trees, Theoretical Computer Science 57 (1988), 153
159.
[390] Steven Skiena, Implementing discrete mathematics, Addison-Wesley, 1990.
[391] P.J. Slater, Generating all permutations by graphical transpositions, Ars Combinatoria
5 (1978), 219225.
[392] D. Sleator, R.E. Tarjan, and W. Thurston, Rotation distance, triangulations, and
hyperbolic geometry, J. AMS 1 (1988), 647682.
[393] D.H. Smith, Hamiltonian circuits on the n-cube, Canadian Mathematical Bulletin 17
(1975), 759761.
[394] M. Smith, Generating spanning trees, Masters thesis, Dept. of Computer Science,
University of Victoria, 1994.
[395] M. Solomon and R. Finkel, A note on enumerating binary trees, J. Assoc. Comput.
Mach. 27 (1980), 35.
[396] R. Sosic and J. Gu, A polynomial time algorithm for the n-queens problem, SIGART
Bulletin 1 (1990), 711.
[397] M. Squire, C.D. Savage, and D.B. West, A Gray code for the acyclic orientations of
a graph, Dept. of Computer Science, North Carolina State University, 1993.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

277

[398] Matthew B. Squire, The square of the acyclic orientation graph is Hamiltonian, Dept.
of Computer Science, North Carolina State University, 1993.
[399]

, Generating ideals fast, Dept. of Computer Science, North Carolina State


University, 1994.

[400]

, Gray codes for a-free strings, Electronic Journal of Combinatorics 3 (1996),


R17.

[401] G. Stachowiak, Hamilton paths in graphs of linear extensions for unions of posets,
SIAM J. Discrete Mathematics 5 (1992), 199206.
[402] Richard P. Stanley, Enumerative combinatorics, volume 1, Wadsworth, 1986.
[403] Dennis Stanton and Dennis White, Constructive combinatorics, Springer-Verlag, 1986.
[404] G. Steiner, An algorithm to generate the ideals of a partial order, Operations Research
Letters 5 (1986), 317320.
[405] H. Steinhaus, One hundred problems in elementary mathematics, Basic, 1964.
[406] Ian Stewart, Game, set, & math, Basil Blackwell, Inc., 1989.
[407] F. Stockmal, Generation of partitions in part-count form, Communications of the
ACM 5 (1962), 344.
[408] I. Stojmenovic and M. Miyakawa, Applications of a subset-generating algorithm to
base enumeration, knapsack and minimal covering problems, The Computer Journal
31 (1988), 6570.
[409] M.M. Syslo, An efficient cycle vector space algorithm for listing all cycles of a planar
graph, SIAM J. Computing 10 (1981), 797808.
[410] D.T. Tang and C.N. Liu, Distance-2 cycle chaining of constant weight codes, IEEE
Transactions on Computers 22 (1973), 176180.
[411] Maurice Tchuente, Generation of permutations by graphical exchanges, Ars Combinatoria 14 (1982), 115122.
[412] G. Tinhofer, Generating graphs uniformly at random, Computing (Supplement) 7
(1990), 235255.
[413] G. Tinhofer and H. Schreck, Linear time tree codes, Computing 33 (1984), 211225.
[414] J. Todd and H. Coxeter, A practical method for enumerating cosets of a finite abstract
group, Proc. Edinburgh Math. Soc. 5 (1936), 2634.
[415] C. Tompkins, Machine attacks on problems whose variables are permutations, Proc.
Symposium in Applied Mathematics, Numerical Analysis, vol. 6, McGraw-Hill, 1956,
N.Y., pp. 195211.
[416] A.E. Trojanowski, Ranking and listing algorithms for k-ary trees, SIAM J. Computing
7 (1978), 492509.
c
uvic01, Frank
Ruskey, 19952001

278
[417]

BIBLIOGRAPHY
, Ranking and unranking unordered binary trees, Trans. Illinois State Academy
of Science 1 (1979), 4656.

[418] H.F. Trotter, Algorithm 115: Perm, Communications of the ACM 5 (1962), 434435.
[419] W.T. Trotter and P. Erdos, When the Cartesian product of directed cycles is Hamiltonian, Journal of Graph Theory 2 (1978), 137142.
[420] S. Tsukiyama, M. Ide, H. Ariyoshi, and I. Shirakawa, A new algorithm for generating
all the maximial independent sets, SIAM J. Computing 6 (1977), 505517.
[421] S. Tsukiyama, I. Shirakawa, H. Ozaki, and H. Ariyoshi, An algorithm to enumerate
all cutsets of a graph in linear time per cutset, Journal of the ACM 27(4) (1980),
619632.
[422] T. Ueda, Gray codes for necklaces, Discrete Mathematics 219 (2000), 235248.
[423] Vincent Vajnovszki, Generating, ranking, and unranking for binary unordered trees,
Bulletin of EATCS 57 (1995), 221229.
[424]

, Constant time algorithm for generating binary tree gray codes, Studies in
Informatics and Control 5 (1996), 1521.

[425] Vincent Vajnovszki and Jean M. Pallo, Generating binary trees in a-order from codewords defined on four-letter alphabet, Journal of Information and Optimization Science
15 (1994), 345357.
[426] T. van Ardenne-Ehrenfest and N.G. de Bruijn, Circuits and trees in ordered linear
graphs, Simon Stevin 28 (1951), 203217.
[427] D. Roelants van Baronaigien , A loopless algorithm for generating binary tree sequences, Information Processing Letters 39 (1991), 189194.
[428]

, Constant time generation of involutions, Congressus Numerantium 90 (1992),


8796.

[429] D. Roelants van Baronaigien and E.M Neufeld, Loopless generation of subsets with a
given sum, Congressus Numerantium 100 (1994), 147152.
[430] D. Roelants van Baronaigien and F. Ruskey, Generating permutations with given ups
and downs, Discrete Applied Mathematics 36 (1992), 5765.
[431]

, Generating t-ary trees in A-order, Information Processing Letters 27 (1988),


205213.

[432] A.J. van Zanten, Index system and separability of constant weight Gray codes, IEEE
Trans. Information Theory 37 (1991), 12291233.
[433] Y.L. Varol and D. Rotem, An algorithm to generate all topological sorting arrangements, The Computer Journal 24 (1981), 8384.
[434] J. Veerasamy and I. Page, On the towers of Hanoi problem with multiple spare pegs,
International Journal of Computer Mathematics 52 (1994), 1722.
c
uvic01, Frank
Ruskey, 19952001

BIBLIOGRAPHY

279

[435] R.J. Walker, An enumerative technique for a class of combinatorial problems, Proc.
Symposium in Applied Mathematics, Combinatorial Analysis, vol. 10, American
Mathematical Society, 1960, Providence, R.I., p. 91.
[436] T.R. Walsh, Generating nonisomorphic maps without storing them, SIAM J. Algebraic
and Discrete Methods 4 (1983), 161178.
[437] K.T. Wang, On a new method for the analysis of networks, Memoir 2, National Research Institute of Engineering, Academia Sinica, 1934.
[438] T.M. Wang and C.D. Savage, A gray code for necklaces of fixed density, SIAM J.
Discrete Mathematics 9 (1996), 654673.
[439] M.B. Wells, Generation of permutations by transposition, Mathematics of Computation 15 (1961), 192195.
[440]

, Elements of combinatorial computing, Pergamon Press, Elmsford, N.Y., 1971.

[441] A.T. White, Ringing the changes, Math. Proc. Camb. Phil. Soc. 94 (1983), 203215.
[442]

, Ringing the changes II, Ars Combinatoria 20-A (1985), 6575.

[443]

, Ringing the cosets, American Mathematical Monthly 94 (1987), 721746.

[444]

, Ringing the cosets II, Math. Proc. Camb. Phil. Soc. 105 (1989), 5365.

[445] D.E. White and S.G. Williamson, Construction of minimal representative systems,
Linear and Multilinear Algebra 9 (1981), 167180.
[446] J.S. White, Algorithm 373: Number of doubly restricted partitions, Communications
of the ACM 13 (1970), 120.
[447]

, Algorithm 374: Restricted partition generator, Communications of the ACM


13 (1970), 120.

[448] H.S. Wilf, A unified setting for sequencing, ranking and selection algorithms for combinatorial objects, Advances in Math. 24 (1977), 281291.
[449]

, A unified setting for selection algorithms (II), Annals of Discrete Math. 2


(1978), 135148.

[450]

, The uniform selection of free trees, Journal of Algorithms 2 (1981), 204207.

[451]

, Combinatorial algorithms: An update, SIAM, 1989, CBMS 55.

[452] H.S. Wilf and N.A. Yoshimura, Ranking rooted trees, and a graceful application, Perspectives in Computing, Proc. Japan-U.S. Joint Seminar in Discrete Algorithms and
Complexity, Academic Press, 1987, pp. 341350.
[453] S. Gill Williamson, Combinatorics for computer scientists, Computer Science Press,
1985.
[454] S.G. Williamson, Ranking algorithms for lists of partitions, SIAM J. Computing 5
(1976), 602617.
c
uvic01, Frank
Ruskey, 19952001

280
[455]

BIBLIOGRAPHY
, On the ordering, ranking, and random generation of basic combinatorial sets,
Combinatoire et Representation du Groupe Symetrique, Springer-Verlag, 1977, Lecture Notes in Mathematics, No. 579, pp. 311339.

[456] E. Wilmer and M.D. Ernst, Graphs induced by Gray codes, Discrete Mathematics ??
(2001), to appear.
[457] Pawel Winter, An algorithm for the enumeration of spanning trees, BIT 26 (1986),
4462.
[458] David Witte and Joseph A. Gallian, A survey: Hamiltonian cycles in Cayley graphs,
Discrete Mathematics 51 (1984), 293304.
[459] Jacek Wojciechowski, Generation of trees of a graph with the use of decomposition, J.
Franklin Institute 318 (1984), no. 4, 215231.
[460] N.C. Wormald, Generating random regular graphs, Journal of Algorithms 5 (1984),
247280.
[461] R.A. Wright, B. Richmond, A. Odlyzko, and Brendan D. McKay, Constant time
generation of free trees, SIAM J. Computing 15 (1986), 540548.
[462] J. Wu and R. Chen, The towers of Hanoi problem with parallel moves, Information
Processing Letters 44 (1992), 241243.
[463] S. Xie, Note on de Bruin sequences, Discrete Applied Mathematics 16 (1987), 157
177.
[464] H.P. Yap, Point symmetric graphs with p 13 points, Nanta Math. 6 (1973), 820.
[465] Nancy Allison Yoshimura, Ranking and unranking algorithms for trees and other combinatorial objects, Ph.D. thesis, Computer and Information Science, University of
Pennsylvania, 1987.
[466] S. Zaks, Lexicographic generation of ordered trees, Theoretical Computer Science 10
(1980), 6382.
[467] S. Zaks and D. Richards, Generating trees and other combinatorial objects lexicographically, SIAM J. Computing 8 (1979), 7381.
[468] D. Zerling, Generating binary trees using rotations, Journal of the ACM 32 (1985),
694701.

c
uvic01, Frank
Ruskey, 19952001

Chapter 11

Useful Tables
In this section we have included some tables of frequently used numbers, such as the binomial
coefficients

281

282

k\n
0
1
2
3
4
5
6
7
8
9
10
11

CHAPTER 11. USEFUL TABLES

0
1
1
1
1
1
1
1
1
1
1
1
1

1
2
3
4
5
6
7
8
9
10
11

1
3
6
10
15
21
28
36
45
55

1
4
10
20
35
56
84
120
165

1
5
15
35
70
126
210
330

1
6
21
56
126
252
462

1
7
28

1
8

Table 11.1: Binomial coefficients

k\n
1
2
3
4
5
6
7
8

1
1
1
2
5
14
42
132
9

1
2
5
14
42
132

1
3
9
28
90

1
4
14
48

1
5
20

1
6

1
1
1
1
1
1
1
1
1
1

for 0 k 11.

Table 11.2: The numbers T (n, k) =

k\n
1
2
3
4
5
6
7
8
9

2nk
k
2nk
k

1
3
7
15
31
63
127
255

1
6
25
90
301
966
3025

1
10
65
350
1701
7770

1
15
140
1050
6951

1
21
266
2646

1
28
462

1
36

Table 11.3: Stirling numbers of the second kind

for 1 k 9.

n
k

for 1 k 9.

c
uvic01, Frank
Ruskey, 19952001

283

k\n
1
2
3
4
5
6
7
8
9
10
11
12
13

1
1
1
1
1
1
1
1
1
1
1
1
1
1

10

11

12

13

1
1
2
2
3
3
4
4
5
5
6
6

1
1
2
3
4
5
7
8
10
12
14

1
1
2
3
5
6
9
11
15
18

1
1
2
3
5
7
10
13
18

1
1
2
3
5
7
11
14

1
1
2
3
5
7
11

1
1
2
3
5
7

1
1
2
3
5

1
1
2
3

1
1
2

1
1

Table 11.4: The number p(n, k) of numerical partitions of n into k parts for 1 k 11.

k\n
1
2
3
4
5
6
7
8
9

1
1
1
1
1
1
1
1

1
4
11
26
57
120

1
11
66
302
1191

1
26
302
2416

1
57
1191

1
120

Table 11.5: Eulerian numbers for 1 k 9.

c
uvic01, Frank
Ruskey, 19952001

Index
n-queens problem, 41

Campanology, 189, 205


CAT (Constant Amortized Time), 8, 9
Catalan number, 21, 36, 74, 146
Catalan numbers, 21
Cayley graph, 33, 189
over Abelian group, 199
Center, 83
Centroid, 83
Chain, 19
Change-ringing, 189
Chinese Rings, 178
Clique, 28, 246, 247
Closeness relation, 116
Cluster, 125
Co-lexicographic order, 55
Colex (or RL) Superiority Principle, 59
Comb, 134
Combination, 226
Gray code, 185
of a multiset, 71
random, 249
Combinations
Gray code, 125
Combinatorial Gray code, 116
cyclic, 116
Composition, 13, 106, 149
Gray code for, 149153
with restricted parts, 71
Concatenate, 12
Concave, 35
Conjugate, 16, 109
of numerical partition, 37
Constant time initialization, 34
Context-free language, 232, 252
Contingency table, 71, 110
Cover relation, 19
Coxeter graph, 195
Cubic graph, 178, 241

A-order, 107
Alphabet, 11
Alternating permutation, 111
Anchor, 44
Antichain, 19
Antimatroid, 20, 206, 241
basic words, 20
Antipodal chain, 191
Append, 12
Arboricity, 247
AVL tree, 108
B-order, 107
B-tree, 86, 108
generating all, 8689
Backtracking
non-recursive algorithm, 43, 44
recursive algorithm, 43
tree, 43
Bag, 15
Basic word graph, 206
Bell number, 17, 35, 89, 90, 93, 109, 159
BEST (Backtracking Ensuring Success at
Terminals), 8, 9, 47
Bicentral tree, 83
Binary tree, 74, 112
complete, 102
Gray code, 138, 142, 175
rotation, 142
unordered, 112
Binary trees
random generation of, 252
Binomial coefficient, 13
Bitstring, 11
Bracelet, 232
Buckyball, 204
Burnsides Lemma, 32, 215
284

INDEX
Cut, 246
Cutset, 246
Cycle, 246
Cycle lemma, 107, 252
De Bruijn
cycle, 232
graph, 220
sequence, 220
torus, 232
Degree
of a vertex, 26
Degree sequence, 110
Depth-first numbering, 29
Depth-first search, 29, 209
Depth-first-search, 243
Derangement, 14, 35, 106, 181, 252
Digraph
connected, 27
strongly connected, 27
tournament, 28
Direct algorithm, 121
Distributive lattice, 203
Dominating property, 74
Dominoe, 229
Dominoes, 207
Euler pentagonal number theorem, 37
Eulerian
cycle, 208, 210
graph, 208
Eulerian number, 15
Extension, 19
Extension-rotation algorithm, 176178
Extent, 189, 204
Ferrers diagram, 16
Fibonacci number, 33
Free tree, 78, 82, 83, 112
generating all, 8286
random generation of, 252
Function, 12
bijection, 12
injective, 12
surjective, 12
Graph
1-Hamiltonian, 205

285
acyclic, 26
acyclic orientation, 27, 186, 205
adjacency lists, 29
adjacency matrix, 28
arboricity of, 247
automorphism group of, 31
automorphism of, 26
bipartite, 27, 240, 241
Cayley, 53
clique, 28
coloring, 28, 52
complete (Kn ), 27
complete bipartite (Km,n ), 27
connected, 26
connected component, 29
cube of, 27, 204, 206
cubic, 26, 178, 240
directed Cayley, 33
generating non-isomorphic, 236238
grid, 204
Hamilton-connected, 28
Hamilton-laceable, 28
Hamiltonian, 28, 37
hypercube(Qn ), 27
hypo-Hamiltonian, 28
independent set, 28
pancyclic, 28
planar, 247
prism of, 27, 205
product of, 27
random, 252
regular, 26
shuffle-exchange, 206
spanning subgraph of, 28
square of, 27, 204206
subgraph of, 26
undirected Cayley, 33
unicyclic, 24, 241
vertex cover, 28
vertex-transitive, 26, 31
graph, 26
Graphical partitions, 110
Gray code
binary reflected (BRGC), 116, 178, 229
binary tree, 138, 142
composition, 149
for binary trees, 175
c
uvic01, Frank
Ruskey, 19952001

286
for combinations, 125, 185
for multiset permutations, 146
monotone, 190
numerical partition, 153
set partition, 159
uniform, 122
well-formed parenthesis, 139
Greedoid, 241
Group, 26, 30
Abelian, 31, 199
action, 31
cyclic, 30, 232
dihedral, 30, 232
generating set for, 33
hypo-octohedral, 31
order of, 30
order of an element in, 30
subgroup, 30
symmetric, 30, 31
transitive, 31
Hamilton cycle, 28, 53
Hamilton laceable, 179
Hamilton path, 28, 204
Hamiltonian cycle
algorithm for, 176178
Hamming distance, 11, 178
Hasse diagram, 19
Height-balanced (AVL) tree, 113
Hexomino, 51
Hypercube, 114, 202, 203, 210
Hypercube (Qn ), 27, 117
Ideal, 20
generating all, 101103
In-tree, 23, 208
Independent set, 28
maximal, 246
Index
of a permutation, 14
Indirect algorithm, 121
Inversion, 14, 106
string, 68, 106, 111
table, 111
vector, 111
Inversion string, 111
Involution, 15, 106, 111, 230

INDEX
Knights tour, 52
Labelled trees, 78
Latin rectangle, 106, 106
Lattice, 19, 241
distributive, 20, 203
Lexicographic order, 55
Linear algorithm, 9
Linear extension, 19, 27, 52, 110, 113
generating all, 99101
Gray code for, 165174
Linear Feedback Shift Register (LFSR),
228229
List, 11
append, 12
concatenation of, 12
interface between, 12
prepend, 12
Log-concave, 35
Logarithmically concave, 73
Loopless, 5
Loopless algorithm, 121
Lyndon
factorization, 222
word, 185, 214, 216, 217, 220, 227,
230, 231, 233
random, 250
M
obius
function, 215
inversion, 17, 215
Magic square, 110
Matching
perfect, 247
Matrix Tree Theorem, 194
Matroid, 247
listing bases of, 247
maximal independent set, 247
Memoryless, 5, 56, 67
Middle two levels problem, 182
Monotone runs, 106
Multinomial coefficient, 15
Multiplicity representation, 94, 109, 238
Multiset, 15
combinations of, 71
specification of, 68
Multiset permutations, 185
Murasaki diagram, iii
c
uvic01, Frank
Ruskey, 19952001

INDEX
Natural representation, 94, 109
Necklace, 213, 216, 217
factorization, 222
of a string, 222
random, 250
Number, 11
Numerical partition, 93, 113
conjugate of, 37
Gray code, 153
into distinct parts, 109
into odd parts, 109
random, 251
Ordered forest, 107
Ordered tree, 112
degree of a node, 20
Orthogonality relation, 107
Out-tree, 23, 208
Parity difference, 14, 181
Partially ordered set, 19
Partite set, 27
Partition, 16
binary, 109
numerical, 16, 238
conjugate of, 16
part of, 93
set, 17, 225
block of, 17
with given block sizes, 164
Partitions
graphical, 110
Pascals triangle, 13
Pendant vertex, 26, 154
Pentomino, 44
Permutation, 14, 111
k-permutation, 15
alternating
Gray code for, 164
cycle, 106
cycle notation, 14
cycle representation, 34
cycles in, 14
derangement, 14, 35, 106, 181, 252
even, 14
index of, 14
inverse, 34, 35
inverse of, 14

287
inversion, 14, 35, 106
inversion string of, 68, 111
involution, 15, 35, 106
odd, 14
of a multiset
random, 250
one-line notation, 14
parity of, 14
random, 249
rosemary, 111
run, 15
sign, 34
sign of, 14
transposition, 14
up-down, 15, 106
Permutohedron, 19
PET, 61
Peterson graph, 195
Planar graph, 247
Polygon
trianulation of, 182
Polynomial
irreducible, 226, 227, 233
monic, 226
over finite fields, 226
primitive, 226, 227, 233
polynomial
irreducible, 233
Polyomino, 51, 241
order of, 51
Poset, 19
antichain, 19
extension of, 19
fence, 37
forest, 182
generating all, 240
height of element, h(x), 19, 174
ideal of, 20
linear extension of, 19
probability of x preceding y, P (x <
y), 19, 174
Postorder, 20
Pr
ufers correspondence, 36
Pre-necklace, 214
Prefix
property, 12
tree, 12, 103
c
uvic01, Frank
Ruskey, 19952001

288
Preorder, 20
Prepend, 12
Principle subtrees, 20
Prism, 27, 135, 165
Product space, 104, 250
Proof
bijective, 12
combinatorial, 12
Pumping Lemma, 232
Random
permutation of a multiset, 250
ranking, 5
Red-black tree, 108
Relation
anti-reflexive, 12
anti-symmetric, 12
equivalence, 12
partial order, 12
reflexive, 12
symmetric, 12
transitive, 12
Restricted growth (RG) sequences, 159
Restricted growth string, 90
Reverse lexicographic order, 55
Reverse search, 246, 247
Revolving door algorithm, 127
Rodeh, M., 247
Rooted tree, 78, 79, 108, 112
canonic, 80
generating all, 7982
Rosemary permutation, 111
Rotation
graph, 186
Run, 15
Run-length encoding, 105
Ruskey, Frank, 112
Score vector, 109
Self-loop, 27
Set partition, 89, 113
block, 159
Gray code, 159
random, 252
with given block sizes, 164
Shuffle-exchange, 206
Sign, 14
Soma Cube puzzle, 51

INDEX
Spanning subgraph, 28
Spanning tree, 28, 210, 243247
random, 252
ranking, 247
Spin-out puzzle, 123, 178, 185
Square, 206
Stable marriage, 110, 114
Stamp folding, 110
Stirling number
of the second kind, 17
Stirling number (of the second kind), 37,
89
Stirlings approximation, 14
String, 11
periodic, 214
weight of, 230
Suffix
tree, 103
Suffix property, 12
Teeth, 134
Tesseract, 27, 53
The Brain puzzle, 123, 178
Topological sort, 52
Topological sorting, 27
Totient function, 17, 215
Tournament, 28, 239, 241
vortex-free, 231
Towers of Hanoi, 117, 185, 203
Transition sequence, 118
Transposition, 14
tree of, 200
Tree
bicentral, 83
center, 25
center of, 83
centroid of, 83
free, 25, 83
in-tree, 23
ordered, 20
out-tree, 23
prefix, 12, 103
red-black, 108
rooted, 23
canonic, 107
isomorphism of, 107
suffix, 103
c
uvic01, Frank
Ruskey, 19952001

INDEX

289

tree
2-3 tree, 113
B-tree, 113
plane rooted, 113
Tree graph, 244, 247
Triangle, 247
Triangulation
random, 253
Unimodal, 13, 35, 72
Universal cycle, 224226, 233
Unlabelled graph
random, 252
unranking, 5
Vertex
in-degree, 27
out-degree, 27
pendant, 26, 154
Vertex cover, 28
Weight, 230
Well-formed parentheses, 74
random, 252
Well-formed parenthesis
Gray code, 139
Witt formula, 233
Young Tableau
Gray code for, 163
Young tableau, 18

c
uvic01, Frank
Ruskey, 19952001

You might also like