LListing Words in Free Groups
COLIN RAMSAY ∗ Abstract.
Lists of equivalence classes of words under rotation or rotation plus reversal (i.e.,necklaces and bracelets) have many uses, and efficient algorithms for generating these lists exist. Incombinatorial group theory elements of a group are typically written as words in the generators andtheir inverses, and necklaces and bracelets correspond to conjugacy classes and relators respectively.We present algorithms to generate lists of freely and cyclically reduced necklaces and bracelets infree groups. Experimental evidence suggests that these algorithms are CAT – that is, they run inconstant amortized time.
Key words. necklace, bracelet, CAT algorithm, free group, reduced word, conjugacy class
AMS subject classifications.
1. Introduction.
Given an ordered alphabet of size k , a necklace of length n is the lexicographically least element of an equivalence class of k -ary strings oflength n under rotation. A word is called a prenecklace if it is the prefix of somenecklace. An aperiodic necklace is called a Lyndon word . A bracelet of length n isthe lexicographically least element of an equivalence class of k -ary strings of length n under string rotation and string reversal.For a fixed k the number of necklaces (also Lydon words, prenecklaces andbracelets) grows exponentially with the length. See, for example, [3, 13] for exactcounts and bounds. So generating a complete list of the length n necklaces takes ex-ponential time, and our goal is an algorithm where the computation (the total amountof change to the data structures, not including any processing of the generated neck-laces) is proportional to the number of necklaces generated. Such an algorithm is aconstant amortized time, or CAT, algorithm.In group theory, elements of a group can be represented by strings (or words )in the group’s generators and their inverses. Symbolic algebra systems such as GAP and
Magma [1, 2] make sophisticated testing of large numbers of examples straight-forward, and efficient algorithms for generating complete lists of words, up to someequivalence, are an important part of this. The extant enumeration algorithms do nottake into account the group structure, and we demonstrate how they can be recast toaddress this. For necklaces this process is trivial, while for bracelets we need modifythe reversal checking code materially.The remainder of this paper is organized as follows. Section 2 gives some back-ground material on necklaces and bracelets and on free groups and group presenta-tions, and discusses the analogues of necklaces and bracelets in groups. Sections 3and 4, respectively, describe our necklace and bracelet listing algorithms, and we dis-cuss our results in Section 5. Appendix A describes the tests we performed on therunning times of our algorithms.
2. Background.
The first algorithm for generating necklaces, the FKM algo-rithm (due to Fredericksen, Kessler and Maiorana [6, 7]), was proved to be CATin [11]. A simple recursive CAT algorithm to generate prenecklaces, necklaces andLyndon words was given in [3], and it is this algorithm which forms the basis of ourwork. ∗ School of Information Technology and Electrical Engineering, The University of Queensland,Australia ([email protected]). 1 a r X i v : . [ m a t h . C O ] J un C. RAMSAY
Duval’s algorithm for factoring a string, of length n , into Lyndon words [5] yieldsan algorithm for generating the necklace of the string in O ( n ) time. Thus a straight-forward approach to generating bracelets is to generate the necklaces and to rejectthose where the necklace of the reversal is less than the necklace. However, this doesnot yield a CAT algorithm. The algorithm given in [13] is based on the recursive algo-rithm of [3] and maintains auxiliary data regarding the current prenecklace, using thisto guide testing against its reversal and control the computation. The total amountof extra work, amortized over all bracelets, is constant, so this bracelet generatingalgorithm is CAT.Given a set S of g symbols define the set S (cid:48) = S ∪ { s − : s ∈ S } . The set of allwords on S (cid:48) is F g , the free group of rank g . The group operation is concatenation, s − is read as the inverse of s , and the empty word is the identity element. Words withno substrings of the form ss − or s − s are called freely reduced . Two words representthe same element of F g if and only if they are identical after being freely reduced.(See [8, 9] for more details on combinatorial group theory.)Let w = s · · · s (cid:96) ∈ F g . If s and s (cid:96) are not inverses of each other, then w is cyclically reduced . Given an r ∈ S (cid:48) then the word r − wr is the conjugate of w by r ,denoted w r . If r = s (resp., s − (cid:96) ) and the substring r − s (resp., s (cid:96) r ) is canceled from w r then the resulting word is a rotation of w by one position. If r = s = s − (cid:96) thencanceling r − s and s (cid:96) r performs a cyclic reduction step and reduces the length of w by two. Repeated conjugation of a word may render it cyclically reduced or freelyreduced, rotate it arbitrarily, or increase its length arbitrarily.Conjugation partitions the words in F g into conjugacy classes . Given an orderon S (cid:48) , we take as class representatives the lexicographically least element of the freelyand cyclically reduced words in the class. So, in the context of F g , listing the freelyand cyclically reduced necklaces of length (cid:96) is equivalent to listing the conjugacyclasses whose shortest words have length (cid:96) . A word which is both freely reduced andcyclically reduced will be called simply reduced .Reversing a word w is not meaningful in F g . However reversing w and thenreplacing each of its elements by its inverse generates w − (i.e., freely reducing ww − or w − w results in the empty word). Given a set of words R in F g , the normal closure N of R in F g is the set of all words which are concatenations of conjugates of thewords in R and their inverses. Groups are often described as quotient groups of freegroups and N is a normal subgroup of F g , so the quotient F g /N describes some group G . (Formally, there is a homomorphism from F g onto G with kernal N , see [8, 9].)The pair ( S, R ) is a presentation for G , written as G = (cid:104) S : R (cid:105) . The elements of S are the generators of G . The words in R are equal to the identity in G and are called relators . So, in the context of F g , listing the reduced bracelets of length (cid:96) is equivalentto listing equivalence classes of possible relators of length (cid:96) in a presentation.Enumerating reduced necklaces and bracelets in groups is equivalent to enumerat-ing general necklaces and bracelets with forbidden substrings. An efficient algorithmexists to enumerate necklaces with a forbidden substring [12], however the analysistherein assumes that the substring has length at least three. For substrings of lengthone or two, [12] notes that “trivial algorithms can be developed”. In our case, wesimply test each potential addition to the current prenecklace, and skip those whichcannot yield a reduced necklace.In the remainder of this paper, unless explicitly stated otherwise, we are alwaysworking in the free group F g . The number of group generators will be denoted by g and the word length by (cid:96) (both assumed positive), with the set of possible symbols inour words having size k = 2 g . From [10, Theorems 1.1 & 14.2] we have the following ISTING WORDS IN FREE GROUPS
Theorem
The number of reduced words of length (cid:96) in F g is equal to C ( g, (cid:96) ) = (cid:40) (2 g − (cid:96) + 1 , if (cid:96) is odd; (2 g − (cid:96) + 2 g − , if (cid:96) is even.Let φ denote the Euler totient function. Then the number of reduced necklaces oflength (cid:96) in F g is equal to CC ( g, (cid:96) ) = 1 (cid:96) (cid:88) d | (cid:96) φ ( d ) C ( g, (cid:96)/d ) . In the general case (i.e, not in F g ) it is possible for a necklace and its reversal tobe equal, up to rotation – consider the necklace ababb and its reversal bbaba . However,in F g a reduced necklace cannot be equal to its inverse or any of its inverse’s rotations.More generally, we have the following result. Lemma
Let w be a freely reduced word of length (cid:96) > in F g . Then noconjugate of w − equals w .Proof. Let w = s · · · s (cid:96) , and write 1 for the empty word and ¯ x for x − .(i) We first prove that w (cid:54) = ¯ w . If w = ¯ w then w is its own inverse and so ww = 1.Now put w = ¯ uvu , where u, v ∈ F g are freely reduced and u has maximal length.Since w is freely reduced, v is non-empty and, by u ’s maximality, there is no freereduction in vv . Thus ww = ¯ uvu ¯ uvu = ¯ uvvu is a freely reduced non-empty word,contradicting ww = 1.(ii) Now assume that w r = ¯ s k · · · ¯ s ¯ s (cid:96) · · · ¯ s k +1 , 1 (cid:54) k (cid:54) (cid:96) −
1, is a proper rotationof ¯ w which equals w . If ¯ s ¯ s (cid:96) = 1 then free reduction of w r yields a word of length n < (cid:96) , so w (cid:54) = w r . Thus w r must be freely reduced, and w = w r implies that w = ¯ s k · · · ¯ s = s · · · s k = ¯ w . However this is impossible (part (i), with w = w ),so ¯ w is not a proper rotation of w .(iii) Now consider arbitrary conjugation of ¯ w , followed by free reduction. If thisyields a word of length n (cid:54) = (cid:96) , then w (cid:54) = ¯ w . If not, then part (i) or (ii) applies.Thus the set of reduced necklaces in F g of length (cid:96) can be partitioned into pairs,where the words in a pair are inverses (up to rotation) and are not equal underrotation. So precisely one member of each pair is a bracelet, and the number ofreduced bracelets of length (cid:96) in F g is CC ( g, (cid:96) ) / prime ) words,and it is trivial to modify our algorithms to generate these (see Section 5). From [4,Equation (2.2)] we have the following result. Theorem
Let µ denote the M¨obius function. Then the number of reducedprime words of length (cid:96) in F g is equal to τ ( g, (cid:96) ) = (cid:88) d | (cid:96) µ (cid:18) (cid:96)d (cid:19) C ( g, d ) . Thus, in F g there are τ ( g, (cid:96) ) /(cid:96) reduced necklaces and τ ( g, (cid:96) ) / (cid:96) reduced braceletsof length (cid:96) which are not proper powers.For F , with generator z , there are only two reduced necklaces ( z (cid:96) and ( z − ) (cid:96) ) andone reduced bracelet ( z (cid:96) ) for all (cid:96) >
0, so we ignore this case and assume throughout
C. RAMSAY
Algorithm 1
The areInv() function function areInv ( x, y : integer ) returns boolean; begin if x mod 2 = 0 then begin if x + 1 = y then return true ; end ; else begin if x = y + 1 then return true ; end ; return false ; end ; Algorithm 2
The getInv() function function getInv ( x : integer ) returns integer; begin if x mod 2 = 0 then return x + 1; return x − end ;that g >
1. Obviously, for g = 1 and (cid:96) > g = 1 but they are not CAT, since it takes O ( (cid:96) ) timeto set the word to z · · · z or to z − · · · z − .
3. Listing Necklaces.
We first need to decide on the conventions we adopt forrepresenting the group generators and their inverses, and how to order these k symbols.Using the integers ± . . . , ± g is straightforward and is convenient for generating andchecking inverses. However, it is awkward for running through the symbols in orderand checking symbol ordering, since we require a generator to precede its inverse andfor ± . . . , k − j denote the generators and odd integers j + 1 their inverses.To handle inverses we introduce the two utility functions areInv() and getInv() of Algorithms 1 and 2. These, respectively, check whether or not two symbols areinverses and return the inverse of a symbol. These functions are common to boththe necklace and bracelet algorithms and run in constant time. We separate outthese functions for simplicity – in practice they can be compiled as inline functionsor replaced by macros.Our recursive necklace generation procedure genNeck() in Algorithm 3 is now asimple modification of [3, Algorithm 2.1], with the if statements at Lines 9 and 13ensuring that the prenecklaces remain freely reduced and that the final necklaces arecyclically reduced. For clarity we do not use the guard value a of [3] and instead usethe wrapper code of Algorithm 4. This explicitly sets a (recording its inverse in aoi to facilitate cyclic reduction checking) and ensures that the prenecklaces at entry to genNeck() are non-empty.
4. Listing Bracelets.
Our bracelet algorithm is inspired by that in [13], withreversal of a prenecklace being replaced by word inversion (i.e., reversal and element-to-inverse mapping). The recursive bracelet generation procedure genBrace() and itswrapper code are given in Algorithms 5 and 6, while Algorithm 7 is the checkInv() function for comparing a prenecklace with its inverse. The genBrace() procedure is anaugmented version of genNeck() , with the additional code checking each prenecklace
ISTING WORDS IN FREE GROUPS Algorithm 3
The genNeck() procedure procedure genNeck ( t, p : integer ); local j : integer; begin if t > (cid:96) then begin if (cid:96) mod p = 0 then process necklace a · · · a (cid:96) ; return ; end ; j := a t − p ; if not areInv ( a t − , j ) and ( t < (cid:96) or j (cid:54) = aoi ) then begin a t := j ; genNeck ( t + 1 , p ); end ; for j from a t − p + 1 to k − do begin if not areInv ( a t − , j ) and ( t < (cid:96) or j (cid:54) = aoi ) then begin a t := j ; genNeck ( t + 1 , t ); end ; end ; return ; end ; Algorithm 4
The necklace code wrapper global (cid:96), g, k, aoi : integer; a : integer array; local j : integer; g := number of generators; (cid:96) := word length; k := 2 g ; for j from to k − do begin a := j ; aoi := getInv ( j ); genNeck ( 2,1 ); end ;(i.e., the putative “prebracelets”) against its inverse and rejecting those that cannotyield bracelets. Thus each rotation of the inverse of the final words is tested, andnecklaces which are not bracelets are not generated.The use of inverses as opposed to reversals actually results in a somewhat simpleralgorithm compared with that in [13]. Firstly, note that our order is chosen so thata generator immediately precedes its inverse. This implies that bracelets cannotstart with a generator inverse and so these can be skipped in the wrapper code.Secondly, although the checkInv() function can return “equal” as well as “less than”or “greater than” (as does the CheckRev() function in [13]), by Lemma 2.2 it neverdoes so since our preneckaces are always freely reduced. This simplifies the code inthe genBrace() procedure, which needs only four parameters compared with the sixof
GenBracelets() in [13].The t and p arguments of genBrace() are, respectively, the index of the nextposition in the array a and the length of the longest prefix of a that is a Lyndonword. The u and v arguments are, respectively, the number of copies of a at thestart of a and the number of copies of a − = aoi at the end of a , with the initial valueof v saved in the local variable vv . The code at Lines 9 to 14 and Lines 22 and 25 C. RAMSAY
Algorithm 5
The genBrace() procedure procedure genBrace ( t, p, u, v : integer ); local j, vv := v : integer; begin if t > (cid:96) then begin if (cid:96) mod p = 0 then process bracelet a · · · a (cid:96) ; return ; end ; j := a t − p ; if j = a then begin v := 0; if u = t − then u := u + 1; end ; else if j = aoi then v := v + 1; else v := 0; if not areInv ( a t − , j ) and ( t < (cid:96) or j (cid:54) = aoi ) then begin a t := j ; if u = v then begin if checkInv ( t, u + 1 ) < then genBrace ( t + 1 , p, u, v ); end ; else if u > v then genBrace ( t + 1 , p, u, v ); end ; if u = t then u := u − for j from a t − p + 1 to k − do begin if not areInv ( a t − , j ) and ( t < (cid:96) or j (cid:54) = aoi ) then begin if j = aoi then v := vv + 1; else v := 0; a t := j ; if u = v then begin if checkInv ( t, u + 1 ) < then genBrace ( t + 1 , t, u, v ); end ; else if u > v then genBrace ( t + 1 , t, u, v ); end ; end ; return ; end ;adjusts u and v as necessary for the next call to genBrace() (if any), using the currentvalue of j (i.e., the next potential value for a t ) and the current word length t − genBrace() are inside the if -statements of Lines 17 to 20and Lines 27 to 30. If u > v then the current prenecklace is less than its inverse,so we can immediately call genBrace() . If u < v then the inverse is less than theprenecklace, the prenecklace cannot yield a bracelet, so we do nothing. If u = v thenwe need to call the checkInv() function to compare the prenecklace with its inverse.If the prenecklace is less we call genBrace() , otherwise we do nothing.When the checkInv() function is called, the current prenecklace starts with u copies of a and ends with v = u copies of aoi . The remainder, γ , is non-empty, doesnot start with a or end with aoi , and is freely reduced. The arguments t and i arethe current prenecklace length and the index of the start of γ . The for -loop compares ISTING WORDS IN FREE GROUPS Algorithm 6
The bracelet code wrapper global (cid:96), g, k, aoi : integer; a : integer array; local j : integer; g := number of generators; (cid:96) := word length; k := 2 g ; for j from to k − step do begin a := j ; aoi := getInv ( j ); genBrace ( 2,1,1,0 ); end ; Algorithm 7
The checkInv() function function checkInv ( t, i : integer ) returns integer; local j : integer; begin for j from i to t do begin if a j < getInv ( a t − j +1 ) then return − if a j > getInv ( a t − j +1 ) then return +1; end ; return end ; γ with its inverse, returning − γ (and thus the prenecklace) precedes its inverseand +1 if γ − precedes γ . The upper limit on the for -loop is simply a convenientplaceholder – the loop is guaranteed to return − i (cid:54) (cid:98) ( t + 1) / (cid:99) .
5. Concluding Remarks.
We have implemented our algorithms in the C and Magma languages and incorporated them into programs for generating and testinglists of conjugacy classes and presentations. The generation of word lists has provedvery fast, with the programs’ running times being dominated by the times to processthe necklaces and bracelets in the lists. Empirical evidence (see Appendix A) suggeststhat our algorithms are CAT, but we have no proof of this.The genNeck() and genBrace() procedures as given process all reduced necklacesand bracelets. If the “ (cid:96) mod p = 0” tests are replaced by “ (cid:96) = p ” then only the reducedaperiodic necklaces (Lyndon words) and bracelets are processed by these procedures.The recursive nature of our necklace and bracelet algorithms induces a tree struc-ture on their search spaces. These trees can easily be split into subtrees, allowingan enumeration to be parallelised [3] or distributed across a set of heterogeneousmachines. Appendix A. Complexity Tests.
Implementations of our necklace and braceletlisting algorithms for reduced words in groups have proved very effective. Howeverwe have no proof that they run in constant amortized time. Accordingly, in a similarmanner to [14, § § a · · · a (cid:96) ” actions in genNeck() and genBrace() were replaced bycode to accumulate the total number of necklaces and of bracelets. These counts werechecked against the expected counts from Section 2 and used to calculate the workper necklace and per bracelet data. C. RAMSAY � ��� � � � ��� � � � ��� � � � ��� � � � ��� � � � � � �� � �� � �� � �� � ����������������� Fig. 1 . Work per Necklace versus Word Length. � ��� � � � ��� � � � ��� � � � ��� � � � ��� � � � � � �� � �� � �� � �� � ����������������� Fig. 2 . Work per Bracelet versus Word Length.
The areInv() and getInv() functions are used by both algorithms and are con-stant time. Each algorithm starts with a for -loop, each iteration of which makes acall to genNeck() or to genBrace() . For necklaces we count the total number ofcalls, both direct and recursive, to genNeck() . Apart from its embedded for -loop,each call to genNeck() is constant time. So our measure of the amount of work doneis the total number of calls to genNeck() plus the total number of iterations of theembedded for -loop across all calls to genNeck() . Figure 1 plots, for various values
ISTING WORDS IN FREE GROUPS g , the word length against the ratio of total workto number of necklaces.For bracelets we count the total number of calls to genBrace() and the totalnumber of iterations of its embedded for -loop. We also need to account for the checkInv() function, which is not constant time. This function is only called if u and v (in genBrace() ) are equal, so at least one iteration of checkInv() ’s embedded for -loop is guaranteed for each call. Thus, we count the total number of iterations ofthis loop across all calls to checkInv() and add this to our total. Figure 2 plots, forvarious values of the number of group generators g , the word length against the ratioof total work to number of bracelets.For both necklaces and bracelets, for all 2 (cid:54) g (cid:54)
6, the ratio of the total amountof work done to the number of reduced words generated is decreasing (after an initialpeak) as the word length increases. This strongly suggests that the algorithms areCAT.
REFERENCES[1]
GAP – Groups, Algorithms, and Programming, Version 4.8.7
W. Bosma, J. Cannon, and C. Playoust , The Magma algebra system, I: The user language ,J. Symbolic Comput., 24 (1997), pp. 235–265.[3]
K. Cattell, F. Ruskey, J. Sawada, M. Serra, and C. R. Miers , Fast algorithms to generatenecklaces, unlabeled necklaces, and irreducible polynomials over GF(2) , J. Algorithms, 37(2000), pp. 267–282.[4]
M. Coornaert , Asymptotic growth of conjugacy classes in finitely-generated free groups , In-ternat. J. Algebra Comput., 15 (2005), pp. 887–892.[5]
J. P. Duval , Factorizing words over an ordered alphabet , J. Algorithms, 4 (1983), pp. 363–381.[6]
H. Fredericksen and I. J. Kessler , An algorithm for generating necklaces of beads in twocolors , Discrete Math., 61 (1986), pp. 181–188.[7]
H. Fredericksen and J. Maiorana , Necklaces of beads in k colors and k -ary de Bruijnsequences , Discrete Math., 23 (1978), pp. 207–210.[8] R. C. Lyndon and P. E. Schup , Combinatorial Group Theory , Springer-Verlag, 1977.[9]
W. Magnus, A. Karrass, and D. Solitar , Combinatorial Group Theory , John Wiley & Sons,1966.[10]
I. Rivin , Growth in free groups (and other stories) , 1999, https://arxiv.org/abs/math/9911076.[11]
F. Ruskey, C. Savage, and T. M. Wang , Generating necklaces , J. Algorithms, 13 (1992),pp. 414–430.[12]
F. Ruskey and J. Sawada , Generating necklaces and strings with forbidden substrings , in Com-puting and Combinatorics, D.-Z. Du, P. Eades, V. Estivill-Castro, X. Lin, and A. Sharma,eds., vol. 1858 of Lecture Notes in Comput. Sci., Springer-Verlag, New York, 2000, pp. 330–339.[13]
J. Sawada , Generating bracelets in constant amortized time , SIAM J. Comput., 31 (2001),pp. 259–268.[14]