A Gray Code for Combinations of a Multiset
Total Page:16
File Type:pdf, Size:1020Kb
AGray Co de for Combinations of a Multiset y Frank Ruskey Carla Savage Department of Computer Science Department of Computer Science University of Victoria North Carolina State University Victoria BC VW P Canada Raleigh North Carolina Abstract Let Ck n n n denote the set 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 subsets of the multiset consisting of n copies of i for i t i Solution x x x corresp onds to the k subset 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 algorithms 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 nbit 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 integers 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 integer 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 Gray code 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 algorithm 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 matrix 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