Fast Algorithms to Generate Necklaces, Unlabeled Necklaces, and Irreducible Polynomials Over GF(2)
Total Page:16
File Type:pdf, Size:1020Kb
Journal of Algorithms 37, 267᎐282Ž. 2000 doi:10.1006rjagm.2000.1108, available online at http:rrwww.idealibrary.com on Fast Algorithms to Generate Necklaces, Unlabeled Necklaces, and Irreducible Polynomials over GFŽ. 2 1 Kevin Cattell HP Research Labs, Santa Rosa, California E-mail: kevin᎐ [email protected] Frank Ruskey, Joe Sawada, and Micaela Serra Department of Computer Science, Uni¨ersity of Victoria, Victoria, British Columbia, Canada E-mail: [email protected]; [email protected], [email protected] and C. Robert Miers Department of Mathematics and Statistics, Uni¨ersity of Victoria, Victoria, British Columbia, Canada E-mail: [email protected] Received August 22, 1998 Many applications call for exhaustive lists of strings subject to various con- straints, such as inequivalence under group actions. A k-ary necklace is an equivalence class of k-ary strings under rotationŽ. the cyclic group . A k-ary unlabeled necklace is an equivalence class of k-ary strings under rotation and permutation of alphabet symbols. We present new, fast, simple, recursive algo- rithms for generatingŽ. i.e., listing all necklaces and binary unlabeled necklaces. These algorithms have optimal running times in the sense that their running times are proportional to the number of necklaces produced. The algorithm for generat- ing necklaces can be used as the basis for efficiently generating many other equivalence classes of strings under rotation and has been applied to generating bracelets, fixed density necklaces, and chord diagrams. As another application, we describe the implementation of a fast algorithm for listing all degree n irreducible and primitive polynomials over GFŽ. 2 . ᮊ 2000 Academic Press 1 Research supported by NSERC. 267 0196-6774r00 $35.00 Copyright ᮊ 2000 by Academic Press All rights of reproduction in any form reserved. 268 CATTELL ET AL. 1. INTRODUCTION Four of the most natural group actions on strings over a fixed alphabet are:Ž. a leaving the string unchanged, Ž. b reversing the string, Ž. c rotating the string, andŽ. d permuting the symbols of the string by a permutation of the alphabet symbols. The four groups giving rise to these actions areŽ. a ޚ ޚ ރ 12,bŽ.acting on the indices Ž reversal .Ž. , c the cyclic n acting on the ޓ indices, andŽ. d the symmetric group k acting on the alphabet, assuming the alphabet consists of k symbols. Each group action, or composition of group actions, partitions the set of k-ary strings into equivalence classes, namely the orbits of the action. Many applications call for exhaustive lists of representatives of these equivalence classes. To generate such equivalence classes, it is natural to choose as representative the lexicographically smallest string. With this representation, efficient algorithms are known for generating the equiva- lence classes of each of the actionsŽ.Ž.Ž. a , b , c , and Ž. d . By ‘‘efficient’’ we mean that the amount of computation used in generating the objects is proportional to the number of objects generated. Such algorithms are said to be CAT, for constant amortized time. ForŽ. a we are simply counting in base k which is known to be efficient for k G 2. ForŽ. b efficient algorithms are easily developed. In case Ž. c the equivalence classes are usually called necklaces. Efficient algorithms for generating necklaces were developed by Fredricksen and Kesslerwx 5 and Fredricksen and Maioranawx 6 ; these algorithms were proven to be efficient by Ruskey et al wx20 . Closely related algorithms for generating Lyndon wordsŽ. aperiodic necklaces were developed by Duvalwx 3 and shown to be efficient by Berstel and Pocchiolawx 1 . In caseŽ. d the representative strings are usually called restricted growth functions and efficient algorithms for generating them have been developed by Erwx 4 , Kaye w 12 x , and others. In addition to these four natural group actions considered in isolation, interesting equivalence classes also emerge by composing two or more of the group actions. For example, composingŽ. b and Ž. c results in the dihedral groups, with the resulting equivalence classes often called bracelets. Only recentlyŽ. using the framework outlined in this paper Sawadawx 23 developed an algorithm to generate k-ary bracelets in constant amortized time. Proskurowski et al.wx 17 show that the orbits of the composition ofŽ. b and Ž. d can be generated in amortized Ok Ž' .time, which is CAT if k is fixed. It remains an interesting challenge to develop efficient algorithms for the other compositions. In this paper we present a new recursive framework for necklace generation. We then use this framework to develop a CAT algorithm to generate equivalence classes of the complemented cycling registerŽ which GENERATING UNLABELED NECKLACES 269 are in one-to-one correspondence with vortex-free tournaments. We also use this framework to develop a CAT algorithm for generating unlabeled binary necklacesŽ. and Lyndon words , i.e., the composition of Ž.Ž. c and d for k s 2. This algorithm was used in the implementation of a fast algorithm for listing all degree n irreducible polynomials over GFŽ. 2 . The framework has also been used to efficiently generate other classes of strings as summarized below. Undoubtedly, other applications of the framework will also be discovered. ⅷ Necklaces: Done in this paper, CAT algorithm. ⅷ Vortex-free tournaments: Done in this paper, CAT algorithm. ⅷ Binary unlabeled necklaces: Done in this paper, CAT algorithm. ⅷ Irreducible polynomials over GFŽ. 2 : Done in this paper. ⅷ Fixed density necklaces: Done in Ruskey and Sawadawx 21 , CAT algorithm. ⅷ Bracelets: Done in Sawadawx 23 , a CAT algorithm. ⅷ Chord diagramsŽ i.e., 2n points on an oriented circle embedded in the plane joined pairwise by chords. : to appear. ⅷ Necklaces with forbidden substrings: Done in Ruskey and Sawada wx22 , CAT algorithm if the forbidden substring is a Lyndon word. ⅷ A basis for the nth homogeneous component of the free Lie algebra: to appear. The combinatorial objects which we are listing are fundamental and there is considerable interest in having efficient algorithms to generate them. This interest comes from mathematicians, computer scientists, electrical engineers, and scientists in other disciplines. Our algorithms have all been implemented and are used in the Combinatorial Object ServerŽ. COS at http:rr www.theory.csc.uvic.car˜cos in the sections on Necklaces and Irre- ducible Polynomials over GFŽ. 2 . 1.1. Background and Definitions def ⌺ s y ⌺n Defining the alphabet kkÄ40, 1, . , k 1 , the set consists of all ; ⌺U k-ary strings of length n. Define an equivalence relation on k by ␣ ;  ¨ g ⌺q ␣ s ¨  s ¨ if and only if there exist u, k such that u and u. The equivalence classes of ; are called necklaces and we identify each necklace with the lexicographically least representative in its equivalence class. The set of all necklaces of length n over a k-ary alphabet is denoted NkŽ.n . def s ␣ g ⌺n < ␣ F  ␣ ;  NkkŽ.n Ä4for all Ž.1 270 CATTELL ET AL. s For example N2Ž.4 Ä40000, 0001, 0011, 0101, 0111, 1111 . The cardinality ␣ s иии ␣ of NkkŽ.n is denoted Nn Ž.. For a string aa12 an, let Ž .denote a ␣ иии s иии left shift of by one position: Ž.aa12 an a2 aan 1. In an unlabeled necklace we do not care about which color a bead has, but only about whether two beads have different colors. Unlabeled neck- laces 0001 and 0111 are identical since one can be transformed into the other by interchanging 0 and 1 at every position of the string. Formally, the set NkŽ.n of all k-ary length n unlabeled necklaces is defined as follows: def s ␣ g ¬ ␣ F ␣i ᭙ g ޓ - - Nˆ kkŽ.n Ä4N Ž.n Ž. Ž . kand 0 i n .2 Ž. s For example, Nˆˆ2Ž.4 Ä40000, 0001, 0011, 0101 . As before,, we denote NnkŽ. s < < Nˆ kŽ.n . A string ␣ is periodic if ␣ s  k where  is nonempty and k ) 0. An aperiodic necklace is called a Lyndon word. The set of all k-ary Lyndon words of length n is denoted L kŽ.n . defs ␣ g ¬ ␣ L kkŽ.n Ä4N Ž.n is aperiodic s For example, L 2Ž.4 Ä40001, 0011, 0111 . The cardinality of L kŽ.n is de- noted LnkŽ.. An unlabeled Lyndon word is an aperiodic unlabeled necklace. The set of all k-ary length n unlabeled Lyndon words is denoted ˆL kŽ.n with s cardinality LnˆˆkŽ.. For example, L 2 Ž.4 Ä40001, 0011 . A word ␣ is called a prenecklace if it is the prefix of some necklace. The set of all k-ary prenecklaces of length n is denoted PkŽ.n . def s ␣ g ⌺nm¬ ␣ g q G  g ⌺ PkkkŽ.n Ä4N Žn m .for some m 0 and k s j For example P22Ž.4 N Ž.4 Ä40010, 0110 . The cardinality of PkŽ.n is denoted PnkkŽ.. Let Wn Ž.denote the number of prenecklaces of length at most n. These numbers will prove useful in analyzing the algorithms developed below. Formally, n defs q WnkkŽ.1 Ý Pi Ž..3 Ž. is1 We denote unlabeled prenecklaces PˆˆkkŽ.n with cardinality Pn Ž.. def s ␣ g ⌺nm¬ ␣ g q G  g ⌺ PˆˆkkkŽ.n Ä4N Žn m .for some m 0 and k GENERATING UNLABELED NECKLACES 271 THEOREM 1.1. The following formulae are ¨alid for all n G 1, k G 1: 11 s n r dnˆ s r d Lnk Ž.ÝÝ Ž.dk , Ln2 Ž. Ž.d 2, Ž. 4 n dn<<2n odd dn 11 s n r dnˆ s y r d Nnk Ž.ÝÝ Ž.dk , Nn22 Ž.Nn Ž. Ž.d 2,5 Ž. n dn<<2n odd dn nn s ˆˆs PnkkŽ .ÝÝLi Ž., Pn22 Ž .Li Ž..6 Ž.