<<

AGray Co de for Combinations of a

y

Frank Ruskey Carla Savage

Department of Computer Science Department of Computer Science

University of Victoria North Carolina State University

Victoria B VW P Canada Raleigh North Carolina

Abstract

Let Ck n n n denote the of all k elementcombinations of the

t

multiset consisting of n o ccurrences of i for i tEach combination

i

is itself a multiset For example C ff g f g f g f gg

Weshowthatmultiset combinations can b e listed so that successive combina

ultaneously generalize tions dier by one element Multiset combinations sim

for which minimal change listings called Gray co des are known

comp ositions and combinations

Research supp orted by NSERC grantA

y

Research supp orted by National Science Foundation GrantNo CCR and National

Security Agency Grant No MDAH

Intro duction

By Ck n n n we denote the set of all ordered t tuples x x x

t t

satisfying

x x x k where x n for i t

t i i

We assume throughout the pap er that n is a p ositiveinteger for i t

i

Solutions to are referred to as combinations of a multiset b ecause they can b e

regarded as k of the multiset consisting of n copies of i for i t

i

Solution x x x corresp onds to the k consisting of x copies of i for

t i

i t For example C corresp onds to ff g f g f g f gg

When each n is the set Ck n n n is simply the set of all combinations of

i t

t elements chosen k at a time

Alternatively the elements of Ck n n n can b e regarded as placements

t

of k identical balls into t labeledboxes where the ith b ox can hold at most n

i

balls ie comp ositions of k into t parts in whichtheith part is at most n Under

i

this in terpretation C f gWhen

each n is at least k Ck n n n is the set of all comp ositions of k into t

i t

parts

These are frequently o ccurring combinatorial ob jects and it is natural to consider

ecient for generating them In particular we will fo cus on listing multiset

combinations in a minimal change order Minimal change listings of combinatorial

ob jects are called Gray codes after Frank Gray who patentedascheme for listing

n strings so that successive strings dier in just one bit

We adopt the convention that lower case b old letters representt tuples eg

a a a a Two elements x y of Ck nareadjacent if there are indices p

t

and q suchthat x y for i p q where in addition x y and x y

i i p p q p

This concept of adjacency seems to b e the most natural one for solutions in

to an equation of the form x x x k p ossibly sub ject to some other side

t

constraints It has b een applied to combinations eg Bitner Ehrlich and Reingold

or Eades and McKay comp ositions eg as attributed to Knuth in Wilf

and to partitions eg Savage Rasmussen Savage and West For

each of these classes it was shown that there is an exhaustive listing of the elements

in which successive elements on the list are adjacent under this criterion

In this pap er we seek a listing of the elements of Ck ninwhich successive ele

ments are adjacent Such a listing will b e referred to as a for combinations

of a multiset

The problem of eciently generating the combinations of a multiset in any order

was also given in Reingold Nievergelt and Deo pg exercise but the

asso ciated solutions manual by Fill and Reingold contains no solution A lo opfree

called COMPOMAX was develop ed by Ehrlich In Ehrlichs al

gorithm successive solutions dier in two adjacent p ositions but in those p ositions

the elements may dier by more than one from their previous values Ro elants van

Baronaigien Ruskey and Engels provide a simpler constant amortized time gen

eration algorithm which however is not a Gray co de it generates the combinations

in lexicographic order

The Construction

Dene a multiset combination x to b e left extreme if x implies either i or

i

x n and right extreme if x implies i n or x n Equivalently

i i i i i

x is left right extreme if it is maximum minimumin Ck n in the lexicographic

ordering of t tuples For example in C is left extreme and

is right extreme Let n n n n Left and right extreme

t

multiset combinations are unique although a multiset combination is b oth left and

right extreme if k or k n since in these cases Ck n has only one element

We call a Gray co de for combinations of a multiset extreme if it starts with the right

extreme combination and ends with the left extreme combination

Lemma If both Ck n and Ck n are nonempty then their right left extreme

elements dier in only one position and in that position by one

Pro of If x x x isright extreme for Ck n there is an index j j t

t

such that x for i j and x n for t ij Then the right extreme

i i i

elementof Ck n is obtained byadding to x if x n and otherwise by

j j j

adding to x The left extreme case is similar

j

Our Gray co de construction is recursive with the basis cases covered in Lemmas

and and the general case in Theorem If x Ck n then x must satisfy

t

maxk n n x minn k

t t t

Thus when t Ck n can b e partitioned as the disjoint union

minn k

t

Ck n Ck x n n

t t

x maxk nn

t t

Let Gk n b e the graph whose vertex set is Ck n and where edges exist

between vertices that are adjacent AGraycodeforCk n is a Hamilton path

in Gk n

k Lemma Thereisanextreme Gray code listing of Ck n when t and

n

Pro of Let p maxk n and q maxk n In this case Gk n n isa

path and there is only one p ossible list as shown b elow

p k p p k p k q q

Lemma ThereisaGray code listing of Ck n when t which is extreme

unless n n and k n

Pro of Here it is helpful to consider the graph Gk n n n one instance of which

is shown in Figure The solid squares represent the extreme vertices Some variation

of this gure can o ccur dep ending up on the lo cation of the lines x x k and

x x k n

Figure shows a construction that yields a Hamilton path b etween the two ex

treme vertices The path is constructed a column at a time from lefttoright with the

p ossible exception of the rightmost two columns where a zigzag path from b ottom

totop may b e necessary This construction works so long as n If n then

it is p ossible to zigzag from one extreme to the next unless n and n k

In this case Ck n has four elements which can b e listed in Gray co de order

as k k k k

If there is an extreme Gray code for Ck n then there is an extreme Lemma

Gray code for Cn k n

s s s s s

n





s s s s s s





decreasing x  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 s s s s s s s s

x x k s s s s s s s s

s s s s s s s

s s s s s s

n

x x k n

Figure The graph Gk n n n

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 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 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 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 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 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 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 s

s s s s s s s s s

s s s s s s s

Figure Extreme Gray co des a n even b n odd

B C B C

B C B C

A B C B B C

A A

C C B B

C C B B

C C D B C B

A A

Figure Path matices arising in the pro of of Theorem

Pro of Given x x x x inCk n dene x y where y n x

t i i i

for i tNow observethat is a graph isomorphism b etween Gk nand

Gn k n It is also clear that a left extreme vertex in Gk n is right extreme in

Gk n and viceversa

The following technical lemma will b e used in Theorem b elowtoshowthe

existence of a GraycodeinCk n Let  denote the by Bo olean with

all entries

Lemma Let A B C and D be the four Boolean matrices in FigureLet M be

the Boolean product M M M M where k and M fA B C D g for

k i

i k Then M  unless M M M

k

Pro of It can b e checked that the pro duct of anytwo distict matrices from fA B C D

g is Thus M  if at least two dierent matrices are factors in the pro duct

M M M

k

Note that in constructing a GraycodeforCk n it suces to consider the cases

where n is nondecreasing n n n For if is a p ermutation of ftg

t

and n n n n aGraycodefor Ck n can b e obtained from a

t

Gray co de for Ck nby p ermuting the co ordinates by However this p ermutation

do es not preserve extreme elements Theorem b elow guarantees an extreme Gray

co de for all Ck n with nondecreasing n and therefore a Gray co de not necessarily

extreme for all multiset combinations

there is an extreme Gray code for Theorem For al l k and nondecreasing n

Ck n

L

Extreme Gray

co de for

C

C

i i

i w z C M M

i i i

z w

Cz Extreme Gray Extreme Gray

i

Cw co de for co de for

i

Cz Cw

i i

C C

C C

C C

C C

C C

Figure Example Gray co de construction in Theorem for C

Pro of The case t is trivial or empty and the cases t and t are covered by

Lemmas and So we assume that t and pro ceed by induction on t By Lemma

wemay assume also that k n Let x be right extreme Since x has at least four

co ordinates arranged in nondecreasing order x x Now consider the problem

of listing the solutions to z w k where z n n and w n n

t

By Lemma there is an extreme Gray co de for this set namely

L z w z w z w

m m

k i and w maxk n n For i m where z w i

m i i

let C b e the subset of Ck n dened by

i

C Cz n n Cw n n n

i i i t

and let G b e the subgraph of Gk n induced by C Then

i i

m

Ck n C

i

i

See Figure for an example

Toprove the existence of a GraycodeforCk n we showthateach C has a

i

Gray co de L satisfying

i

i For im the last elementof L is adjacent to the rst elementof L

i i

ii the rst elementof L is right extreme and

iii the last elementof L is left extreme

m

Then L L L is an extreme GraycodeforCk n

m

By induction there exist extreme Gray co des

i i i

M for Cz n n fromr to l and

i

z z z

i i i

M for Cw n n from r to l

i t

w w w

See example in Figure Thus G has as a spanning subgraph a grid graph denoted

i

H inwhichx x is adjacenttoy y if either

i t t

i

and x y for i or x x is the successor of y y on M

j j

z

i

x x is the successor of y y on M and x y for i

t t i i

w

See Figure for the grids in the example of Figure The four corners of H corre

i

i i i i i i

r sp ond to the four dierent pairs of extreme elements r r l l l

w z z w z w

i i

and l r we refer to those corners as rr rl ll lr resp ectively If has only

z w

one element then rr lr and rl ll if has only one element then rr rl and

lr ll Note that by Lemma for im corresp onding corners of H and

i

H are adjacent

i

Our strategy is to connect hamilton paths through successive grid graphs at their

corners in sucha way that we end and start at the extreme elements of Ck n

Dep ending up on the dimensions of a grid graph H only certain corners of H

i i

may b e connected by Hamilton paths in H The four p ossibilities are summarized

i

by the path matrices shown in Figure b elow Both the rows and columns of these

matrices are indexed rr rl ll lr A entry indicates that there is a Hamilton path

between the corners and a indicates that there is no such Hamilton path Matrix

r by c grid graph with r and c b oth o dd matrix B to r odd c A corresp onds to an

even matrix C to r even c o dd matrix D to r even c even If r c matrix

represents the p ossible hamilton paths Otherwise if r use matrix B or if c

use matrix C

Let P b e the path matrix for H For i mwewant a hamilton path q

i i i

in H which starts and ends at a corner of H such that q starts in corner rr right

i i

extreme q ends in corner ll left extreme and for i m q ends in the corner

m i

b egins This is p ossible if and of H corresp onding to the corner of H in which q

i i i

only if the pro duct P P P P has entry P rrll equal to By Lemma it

m

suces to show that either m and P rrll or that the sequence P P P

m

contains at least two distinct elements

The rst entry of L is always z w k Thus C C n n

Ck n n The rst factor contains only the element so H has only

t

one row and therefore P f B gIfm the result follows Otherwise m

z w k and C C n n Ck n n The rst factor

t

of C contains exactly twoelements and so that H has tworows and

therefore P fB g Then by Lemma the pro duct P P P  since P

m

and P are distinct

Final Remarks

An op en problem is to nd Gray co des where the x that change are adjacent In that

i

case the underlying graph is bipartite and wemay compute the dierence in the sizes

H

v typ e B v

H



typ e C v v v

v v v

H v v v



typ e A

v v v

P

P

P

P v v v

P

P

P

H v v v P



typ e C

P

P

P

v v v P

P

P

P

P

H v v



typ e B

Figure The grid graphs for the example in Figure GraycodeinC

can b e obtained by using indicated adjacency links b etween grids

of the partite sets If the dierence is greater than one then there is no Graycode

Given a solution x to dene the weight w x of x as

X

w x x x x x

t i

i even

Observethatmoving a ball from one b ox to an adjacentboxchanges the parityof

the weight Dene the parity dierence dk ntobe

X

w x

dk n

xCk n

The parity dierence satises the following recurrence relation

and k even if t

if t and k odd

P

minn k

dk n

t

dk i n n if todd

t

imaxk nn

t

P

minn k

t

i

dk i n n if teven

t

imaxk nn

t

This recurrence can b e solved if all n See for example Buck and Wiedemann

i

Eades Hickey and Read Ko and Ruskey Then

if t and k are o dd

dk

btc

otherwise

bkc

By similar reasoning it can also b e solved when all n

i

if t and k are o dd

dk

btk c

otherwise

bkc

We close bymentioning three op en problems

Determine exact conditions under which the parity dierence is zero

Develop an algorithm for generating the Hamilton path in Gk n that runs in

time prop ortional to jCk nj

This pap er shows the existence of a Hamilton path in Gk n We b elieve that

there is a Hamilton cycle in Gk n whenever t

References

J Bitner G Ehrlich and E Reingold Ecient Generation of the Binary Re

ectedGray Code and its ApplicationsCommunications of the ACM

M Buck and D Wiedemann Gray Codes with Restricted Density Discrete Math

ematics

P Eades M Hickey and R Read Some Hamilton Paths and a Minimal Change

Algorithm Journal of the ACM

P Eades and B McKay AnAlgorithm for Generating Subsets of Fixed Size with

aStrong Minimal Change Property Information Pro cessing Letters

G Ehrlich Algorithm Four combinatorial algorithms Communications of

the ACM

G Ehrlich Loopless Algorithms for Generating Combinations and

Other Combinatorial Congurations Journal of the ACM

W Engels D Ro elants van Baronaigien and F Ruskey Generating Combina

tions of a Multiset manuscript

JA Fill and EM Reingold Solutions Manual for Combinatorial Algorithms

Theory and PracticePrenticeHall

F Gray Pulse code communicationUSPatent

CW Ko and F Ruskey Solution of Some Multidimensional Lattice Path Parity

DierenceRecurrenceRelations

D J Rasmussen CD Savage and DB West Gray Code of Fami

lies of Integer Partitions Journal of Combinatorial Theory Series A to app ear

EM Reingold J Nievergelt and N Deo Combinatorial Algorithms Theory

and Practice PrenticeHall

CD Savage Gray Codes Sequences of Partitions J Algorithms

HS Wilf Combinatorial Algorithms AnUpdate SIAM CBMS