Multi de Bruijn Sequences

# Multi de Bruijn Sequences

Glenn Tesler* Department of Mathematics
University of California, San Diego
La Jolla, CA 92093-0112
###### Abstract.

We generalize the notion of a de Bruijn sequence to a “multi de Bruijn sequence”: a cyclic or linear sequence that contains every -mer over an alphabet of size exactly times. For example, over the binary alphabet , the cyclic sequence and the linear sequence each contain two instances of each 2-mer . We derive formulas for the number of such sequences. The formulas and derivation generalize classical de Bruijn sequences (the case ). We also determine the number of multisets of aperiodic cyclic sequences containing every -mer exactly times; for example, the pair of cyclic sequences contains two instances of each -mer listed above. This uses an extension of the Burrows-Wheeler Transform due to Mantaci et al, and generalizes a result by Higgins for the case .

*This work was supported in part by National Science Foundation grant CCF-1115206.

## 1. Introduction

We consider sequences over a totally ordered alphabet of size . A linear sequence is an ordinary sequence of elements of , denoted in string notation as . Define the cyclic shift of a linear sequence by . In a cyclic sequence, we treat all rotations of a given linear sequence as equivalent:

 (a1…an)={ρi(a1…an)=ai+1…ana1…ai−1:i=0,…,n−1}.

Each rotation is called a linearization of the cycle .

A -mer is a sequence of length over . The set of all -mers over is . A cyclic de Bruijn sequence is a cyclic sequence over alphabet (of size ) in which all -mers occur exactly once. The length of such a sequence is , because each of the -mers accounts for one starting position.

In 1894, the problem of counting cyclic de Bruijn sequences over a binary alphabet (the case ) was proposed by de Rivière [7] and solved by Sainte-Marie [16]. In 1946, the same problem was solved by de Bruijn [4], unaware of Sainte-Marie’s work. In 1951, the solution was extended to any size alphabet () by van Aardenne-Ehrenfest and de Bruijn [19, p. 203]:

 (1) \# cyclic de Bruijn sequences=q!qk−1/qk.

The sequences were subsequently named de Bruijn sequences, and work continued on them for decades before the 1894 publication by Sainte-Marie was rediscovered in 1975 [5]. Table 1 summarizes the cases considered and notation used in these and other papers.

We introduce a cyclic multi de Bruijn sequence: a cyclic sequence over alphabet (of size ) in which all -mers occur exactly times, with . Let denote the set of all such sequences, The length of such a sequence is , because each of the -mers accounts for starting positions. For , , , , one such sequence is . The length is . Each -mer 000, 001, 010, 011, 100, 101, 110, 111 occurs twice, including overlapping occurrences and occurrences that wrap around the end.

Let denote the set of linearizations of cyclic sequences in . These will be called linearized or linearized cyclic multi de Bruijn sequences. Let denote the set of linearizations that start with -mer . In the example above, the two linearizations starting with are and .

For a sequence and , let denote concatenating copies of .

A cyclic sequence (or a linearization ) of length has a th order rotation for some (positive integer divisor of ) iff iff for some sequence of length . The order of cycle (or linearization ) is the largest such that . For example, has order , while if all rotations of are distinct, then has order . Since a cyclic multi de Bruijn sequence has exactly copies of each -mer, the order must divide into . Sets , , and denote multi de Bruijn sequences of order that are cyclic, linearized cyclic, and linearized cyclic starting with .

A linear multi de Bruijn sequence is a linear sequence of length in which all -mers occur exactly times. Let denote the set of such sequences and denote those starting with -mer . A linear sequence does not wrap around and is not the same as a linearization of a cyclic sequence. For , , , an example is 111101100010100011, with length .

For , we will compute the number of linearized cyclic (Sec. 2), cyclic (Sec. 3), and linear (Sec. 4) multi de Bruijn sequences. We also compute the number of cyclic and linearized cyclic sequences with a th order rotational symmetry. We give examples in Sec. 5. In Sec. 6, we use brute force to generate all multi de Bruijn sequences for small values of , confirming our formulas in certain cases. In Sec. 7, we show how to select a uniform random linear, linearized cyclic, or cyclic multi de Bruijn sequence. A summary of our notation and formulas is given in Table 2.

We also consider another generalization: a multicyclic de Bruijn sequence is a multiset of aperiodic cyclic sequences such that every -mer occurs exactly times among all the cycles. For example, has two occurrences of each -mer . This generalizes results of Higgins [10] for the case . In Sec. 8, we develop this generalization using the “Extended Burrows-Wheeler Transform” of Mantaci et al [13, 14]. In Sec. 9, we give another method to count multicyclic de Bruijn sequences by counting the number of ways to partition a balanced graph into aperiodic cycles with prescribed edge multiplicities.

We implemented these formulas and algorithms in software available at
http://math.ucsd.edu/gptesler/multidebruijn .

Related work. The methods van Aardenne-Ehrenfest and de Bruijn [19] developed in 1951 to generalize de Bruijn sequences to alphabets of any size potentially could have been adapted to ; see Sec. 3. In 1957, Dawson and Good [3] counted “circular arrays” in which each -mer occurs times (in our notation). Their formula corresponds to an intermediate step in our solution, but overcounts cyclic multi de Bruijn sequences; see Sec. 2.3. Very recently, in 2016, Osipov [15] introduced the problem of counting “-ary -fold de Bruijn sequences.” However, Osipov only gives a partial solution, which appears to be incorrect; see Sec. 6.

## 2. Linearizations of cyclic multi de Bruijn sequences

### 2.1. Multi de Bruijn graph

We will compute the number of cyclic multi de Bruijn sequences by counting Eulerian cycles in a graph and adjusting for multiple Eulerian cycles corresponding to each cyclic multi de Bruijn sequence. For now, we assume that ; we will separately consider in Sec. 2.4. We will also clarify details for the case in Sec. 2.5.

Define a multigraph whose vertices are the -mers over alphabet of size . There are vertices. For each -mer , add directed edges , each labelled by . Every vertex has outdegree and indegree . Further, we will show in Sec. 2.2 that the graph is strongly connected, so is Eulerian.

Consider a walk through the graph:

 c1c2⋯ck−1→c2c3⋯ck→c3c4⋯ck+1→⋯→crcr+1⋯cr+k−2

This walk corresponds to a linear sequence , which can be determined either from vertex labels (when ) or edge labels (when ). If the walk is a cycle (first vertex equals last vertex), then it also corresponds to a linearization of cyclic sequence . Starting at another location in the cycle will result in a different linearization of the same cyclic sequence.

Any Eulerian cycle in this graph gives a cyclic sequence in . Conversely, each sequence in corresponds to at least one Eulerian cycle.

Cyclic multi de Bruijn sequences may also be obtained through a generalization of Hamiltonian cycles. Consider cycles in , with repeated edges and vertices allowed, in which every vertex (-mer) is visited exactly times (not double-counting the initial vertex when it is used to close the cycle). Each such graph cycle starting at vertex corresponds to the linearization it spells in , and vice-versa. However, we will use the Eulerian cycle approach because it leads to an enumeration formula.

### 2.2. Matrices

Let and form the adjacency matrix of directed graph . When ,

 (2) Ac1…ck−1,d1…dk−1={mif c2…ck−1=d1…dk−20otherwise.

For every pair of -mers and , the walks of length from to have this form:

 c1…ck−1→c2…ck−1d1→⋯→ck−1d1…dk−2→d1…dk−1

For each of the arrows, there are parallel edges to choose from. Thus, there are walks of length from to , so the graph is strongly connected and for all pairs of vertices . This gives , where is the matrix of all 1’s.

has an eigenvalue of multiplicity and an eigenvalue of multiplicity . So has an eigenvalue of multiplicity and an eigenvalue of multiplicity .

Thus, has one eigenvalue of the form , where is a th root of unity, and eigenvalues equal to . In fact, the first eigenvalue of is , because the all 1’s column vector is a right eigenvector with eigenvalue (that is, ): for every vertex , we have

 (A→1)w=∑v∈Ωk−1(Avw)(→1)w=∑v∈Ωk−1Avw⋅1=\textscIndeg(w)=mq.

Similarly, the all 1’s row vector is a left eigenvector with eigenvalue .

The degree matrix of an Eulerian graph is an diagonal matrix with on the diagonal and for . All vertices in have indegree and outdegree , so . The Laplacian matrix of is . It has one eigenvalue equal to and eigenvalues equal to .

### 2.3. Number of Eulerian cycles in the graph

Choose any edge in . Let be the -mer represented by . The number of spanning trees of with root (with a directed path from each vertex to root ) is given by Tutte’s Matrix-Tree Theorem for directed graphs [18, Theorem 3.6] (also see [19, Theorem 7] and [17, Theorem 5.6.4]). For a directed Eulerian graph, the formula can be expressed as follows [17, Cor. 5.6.6]: the number of spanning trees of rooted at is

 (3) 1n⋅(product of the n−1 nonzero eigenvalues of L)=(mq)qk−1−1qk−1.

By the BEST Theorem [19, Theorem 5b] (also see [17, pp. 56, 68] and [8]), the number of Eulerian cycles with initial edge is

 (4) (# spanning ~{}trees rooted at v)⋅∏x∈V(\textscOutdeg(x)−1)! (5) =1qk−1⋅(mq)qk−1−1⋅(mq−1)!qk−1=1m⋅qk⋅(mq)!qk−1.

Each Eulerian cycle spells out a linearized multi de Bruijn sequence that starts with -mer . However, when , there are multiple cycles generating each such sequence. Let be an Eulerian cycle spelling out linearization . For -mer , the first edge of the cycle, , was given; the other edges labelled by are parallel to and may be permuted in in any of ways. For the other -mers, the edges representing each -mer can be permuted in in ways. Thus, each linearization starting in is generated by Eulerian cycles that start with edge . Divide Eq. (5) by this factor to obtain the number of linearized multi de Bruijn sequences starting with -mer :

 (6) |LCy(m,q,k)| =1m⋅qk⋅(mq)!qk−1m!qk/m=1qk⋅(mq)!qk−1m!qk=W(m,q,k)qk

where we set

 (7) W(m,q,k) =(mq)!qk−1m!qk=((mq)!m!q)qk−1=(mqm,…,mq of these)qk−1.

Related work. Dawson and Good [3, p. 955] computed the number of “circular arrays” containing each -mer exactly times (in our notation; theirs is different), and obtained an answer equivalent to (5). They counted graph cycles in starting on a specific edge, but when , this does not equal ; their count includes each cyclic multi de Bruijn sequence multiple times. This is because they use the convention that the multiple occurrences of each symbol are distinguishable [3, p. 947]. We do additional steps to count each cyclic multi de Bruijn sequence just once: we adjust for multiple graph cycles corresponding to each linearization (6) and for multiple linearizations of each cyclic sequence (Sec. 3).

### 2.4. Special case k=1

When , the number of sequences of length with each symbol in occurring exactly times is . Divide by to obtain the number of linearized de Bruijn sequences starting with each -mer :

 |LCy(m,q,1)|=1q⋅(mq)!m!q.

This agrees with plugging into (6), so (6) holds for as well.

The derivation of (6) in Secs. 2.12.3 requires , due to technicalities. The above derivation uses a different method, but we can also adapt the first derivation as follows.

has a single vertex ‘’ (a -mer) and loops on , because for each , there are loops . The sequence spelling out a walk can be determined from edge labels but not vertex labels, since they’re null.

The adjacency matrix is rather than Eq. (2). The Laplacian matrix has one eigenvalue: . In (3), the product of nonzero eigenvalues is vacuous; on plugging in , the formula correctly gives that there is spanning tree (it is the vertex without any edges). The remaining steps work for as-is.

### 2.5. Special case q=1

For , the only cyclic sequence of length is . We regard cycle and linearization as having an occurrence of starting at each position, even if , in which case some positions of are used multiple times to form . With this convention, there are exactly occurrences of in . Thus, for and ,

 LC(m,1,k) ={0m}, LC0k(m,1,k) ={0m}, C(m,1,k) ={(0m)}, L(m,1,k) ={0m+k−1}.

Using positions of a cycle multiple times to form a -mer will also arise in multicyclic de Bruijn sequences in Sec. 8.

## 3. Cyclic multi de Bruijn sequences

In a classical de Bruijn sequence (), each -mer occurs exactly once, so the number of cyclic de Bruijn sequences equals the number of linearizations that begin with any fixed -mer :

 |C(1,q,k)|=|LCy(1,q,k)|=(q!)qk−1/qk.

This agrees with (1). But a multi de Bruijn sequence has cyclic shifts starting with , and the number of these that are distinct varies by sequence. Let be the order of the cyclic symmetry of the sequence; then there are distinct linearizations starting with , so

 (8) |LC(d)y(m,q,k)|=md⋅|C(d)(m,q,k)|.

Further, we have the following:

###### Lemma 3.1.

For and :

(a)

(b) For each -mer , .

(c)

###### Proof.

At , all sets in (a)–(c) have size . Below, we consider .

(a) Let . Since has order , it splits into equal parts, . The occurrences of each -mer in reduce to occurrences of each -mer in , and the order of is , so .

Conversely, if then has occurrences of each -mer and has order , so .

(b) Continuing with (a), the length of is at least (since for and ), so the initial -mer of and must be the same.

(c) The map in (a) induces a bijection as follows. Let . For any linearization of , let and set . This is well-defined: the distinct linearizations of are where . Rotating by also rotates by and gives the same cycle in , so all linearizations of give the same result.

Conversely, given , the unique inverse is . As above, this is well-defined. ∎

Partitioning the cyclic multi de Bruijn sequences by order gives the following, where the sums run over positive integers that divide into :

 (9) |C(m,q,k)|=∑d|m|C(d)(m,q,k)|=∑d|m|C(1)(m/d,q,k)|,

Each cyclic sequence of order has distinct linearizations starting with , so

 |LCy(m,q,k)| =∑d|m|LC(d)y(m,q,k)|=∑d|mmd⋅|C(d)(m,q,k)| =∑d|mmd⋅|C(1)(m/d,q,k)|=∑d|md⋅|C(1)(d,q,k)|.

By Möbius Inversion,

 m⋅|C(1)(m,q,k)|=∑d|mμ(d)⋅|LCy(m/d,q,k)|.

Solve this for :

 (10) |C(1)(m,q,k)| =1m∑d|mμ(d)⋅|LCy(m/d,q,k)|.

Combining this with (6) and Lemma 3.1(c) gives

 (11) |C(d)(m,q,k)|=1(m/d)qk∑r|(m/d)μ(r)W(m/(rd),q,k).

Plug (10) into (9) to obtain

 |C(m,q,k)| =∑d|m|C(1)(m/d,q,k)| =∑d|m1m/d∑d′|(m/d)μ(d′)⋅|LCy(m/(dd′),q,k)|. Change variables: set r=dd′ =∑r|m∑d|r1m/d⋅μ(r/d)⋅|LCy(m/r,q,k)| =1m∑r|m⎛⎝∑d|rd⋅μ(r/d)⎞⎠⋅|LCy(m/r,q,k)| =1m∑r|mϕ(r)⋅|LCy(m/r,q,k)|=1m∑r|mϕ(m/r)⋅|LCy(r,q,k)|,

where is the Euler totient function. Thus,

 (12) |C(m,q,k)|=1mqk∑r|mϕ(m/r)⋅W(r,q,k).

Related work. Van Aardenne-Ehrenfest and de Bruijn [19, Sec. 5] take a directed Eulerian multigraph and replace each edge by a “bundle” of parallel edges to obtain a multigraph . They compute the number of Eulerian cycles in in terms of the number of Eulerian cycles in , and obtain a formula related to (12). Their bundle method could have been applied to count cyclic multi de Bruijn sequences in terms of ordinary cyclic de Bruijn sequences by taking , , and considering , but they did not do this. Instead, they set and found a correspondence between Eulerian cycles in vs.  with , yielding a recursion in for . They derived (1) by this recursion rather than the BEST Theorem, which was also introduced in that paper. Dawson and Good [3, p. 955] subsequently derived (1) by the BEST Theorem.

## 4. Linear multi de Bruijn sequences

Now we consider , the set of linear sequences in which every -mer over occurs exactly times. A linear sequence is not the same as a linearized representation of a circular sequence. Recall that a linearized sequence has length . A linear sequence has length , because there are positions at which -mers start, and an additional positions at the end to complete the final -mers. Below, assume .

###### Lemma 4.1.

For , for each sequence in , the first and last -mer are the same.

###### Proof.

Count the number of times each -mer occurs in as follows:

Each -mer occurs times in among starting positions . Since is the first letters of different -mers, there are occurrences of starting in this range. There is also a -mer with an additional occurrence at starting position (the last positions of ), so occurs times in while all other -mers occur times.

By a similar argument, the -mer at the start of has a total of occurrences in , and all other -mers occur exactly times.

So and each occur times in . But we showed that there is only one -mer occurring times, so . ∎

For , this leads to a bijection . Given , drop the last characters to obtain sequence . To invert, given , form by repeating the first characters of at the end.

For , the same -mer occurs linearly at position of and circularly at position of , so every -mer occurs the same number of times in both linear sequence and cycle . Thus, maps to , and does the reverse.

This also gives . Multiply (6) by choices of initial -mer to obtain

 (13) |L(m,q,k)|=|LC(m,q,k)|=qk⋅|LCy(m,q,k)|=W(m,q,k).

Above, we assumed . Eq. (13) also holds for , since all four parts equal (see Sec. 2.5).

## 5. Examples

The case , arbitrary , is the classical de Bruijn sequence. Eq. (12) has just one term, , and agrees with (1):

 |C(1,q,k)|=1qk⋅ϕ(1)⋅(q1,…,1)qk−1=1qk⋅1⋅q!qk−1=q!qk−1qk.

When is prime, the sum in (12) for the number of cyclic multi de Bruijn sequences has two terms (divisors ):

 |C(p,q,k)| =1pqk(ϕ(1)⋅W(p,q,k)+ϕ(p)⋅W(1,q,k)) (14) =1pqk⎛⎜⎝((pq)!p!q)qk−1+(p−1)⋅(q!)qk−1⎞⎟⎠.

For , Table 3 lists all multi de Bruijn sequences of each type. Table 3(A) shows linearizations beginning with . By (6),

 |LC00(2,2,2)|=122(2⋅22,2)22−1=14(42,2)2=14⋅62=9.

Some of these are equivalent upon cyclic rotations, yielding 5 distinct cyclic multi de Bruijn sequences, shown in Table 3(B). By (12) or (14),

 |C(2,2,2)| =12⋅22∑r|2ϕ(2/r)(2rr,r)21=18(ϕ(1)(2⋅22,2)2+ϕ(2)(2⋅11,1)2) =18(1⋅(42,2)2+1⋅(21,1)2)=18(62+22)=408=5.

There are 9 linearizations starting with each -mer 00, 01, 10, 11. Converting these to linear multi de Bruijn sequences yields linear multi de Bruijn sequences in total, shown in Table 3(C). By (13),

 |L(2,2,2)|=(2⋅22,2)21=(42,2)2=62=36.

Table 3(D) shows the 36 multicyclic de Bruijn sequences; see Sec. 8.

## 6. Generating multi de Bruijn sequences by brute force

Osipov [15] recently defined a “-ary -fold de Bruijn sequence”; this is the same as a cyclic multi de Bruijn sequence but in different terminology and notation. The description below is in terms of our notation , which respectively correspond to Osipov’s (see Table 2).

Osipov developed a method [15, Prop. 3] to compute the number of cyclic multi de Bruijn sequences for multiplicity , alphabet size , and word size . This method requires performing a detailed construction for each . The paper states the two sequences for , which agrees with our of size 2, and then carries out this construction to evaluate cases . It determines complete answers for those specific cases only [15, pp. 158–161]. For , Osipov’s answer agrees with ours, . But for and respectively, Osipov’s method gives 72 and 43768, whereas by (12) or (14), we obtain and .

For these parameters, it is feasible to find all cyclic multi de Bruijn sequences by a brute force search in (with ). This agrees with our formula (12), while disagreeing with Osipov’s results for and . Table 4 lists the 82 sequences in . The 52496 sequences for and an implementation of the algorithm below are on the website listed in the introduction. We also used brute force to confirm (12) for all combinations of parameters with at most a 32-bit search space ():

 |C(2,2,2)| =5 |C(6,2,2)| =35594 |C(2,2,3)| =82 |C(3,2,2)| =34 |C(7,2,2)| =420666 |C(3,2,3)| =6668 |C(4,2,2)| =309 |C(8,2,2)| =5176309 |C(4,2,3)| =750354 |C(5,2,2)| =3176 |C(2,3,2)| =40512 |C(2,2,4)| =52496.

Represent by -digit base integers (range ):

 N=ℓ−1∑j=0ajqj(0≤aj

Since we represent each element of by a linearization that starts with -mer , we restrict to . To generate :

• for from to (pruning as described later):

• let be the -digit base expansion of

• if (cycle is a cyclic multi de Bruijn sequence

• if (and is smallest among its cyclic shifts)

• then output cycle

To generate , skip (B4) and output linearizations in (B5).

To determine if is a cyclic multi de Bruijn sequence, examine its -mers (subscripts taken modulo ) in the order , counting how many times each -mer is encountered. Terminate the test early (and potentially prune as described below) if any -mer count exceeds . If the test does not terminate early, then all -mer counts must equal , and it is a multi de Bruijn sequence.

If when -mer counting terminates, then some -mer count exceeded without examining . Advance to instead of , skipping all remaining sequences starting with the same characters. This is equivalent to incrementing the digit prefix as a base number and then concatenating ’s to the end. However, if , then all characters have been examined, so advance to .

We may do additional pruning by counting -mers for . Each -mer is a prefix of -mers and thus should occur times in . If a -mer has more than occurrences in with , advance as described above.

We may also restrict the search space to length sequences with exactly occurrences of each of the characters. We did not need to implement this for the parameters considered.

## 7. Generating a uniform random multi de Bruijn sequence

We present algorithms to select a uniform random multi de Bruijn sequence in the linear, linearized, and cyclic cases.

Kandel et al [11] present two algorithms to generate random sequences with a specified number of occurrences of each -mer: (i) a “shuffling” algorithm that permutes the characters of a sequence in a manner that preserves the number of times each -mer occurs, and (ii) a method to choose a uniform random Eulerian cycle in an Eulerian graph and spell out a cyclic sequence from the cycle. Both methods may be applied to select linear or linearized multi de Bruijn sequences uniformly. However, additional steps are required to choose a uniform random cyclic de Bruijn sequence; see Sec. 7.2.

###### Example 7.1.

For , Table 3(A) lists the nine linearizations starting with 00. The random Eulerian cycle algorithm selects each with probability . Table 3(B) lists the five cyclic de Bruijn sequences. The four cyclic sequences of order 1 are each selected with probability since they have two linearizations starting with , while (00110011) has one such linearization and thus is selected with probability . Thus, the cyclic sequences are not chosen uniformly (which is probability for each).

Kandel et al’s shuffling algorithm performs a large number of random swaps of intervals in a sequence and approximates the probabilities listed above. Below, we focus on the random Eulerian cycle algorithm.

### 7.1. Random linear(ized) multi de Bruijn sequences

Kandel et al [11] present a general algorithm for finding random cycles in a directed Eulerian graph, and apply it to the de Bruijn graph of a string: the vertices are the -mers of the string and the edges are the -mers repeated with their multiplicities in the string. As a special case, the de Bruijn graph of any cyclic sequence in is the same as our . We explain their algorithm and how it specializes to .

Let be a directed Eulerian multigraph and be an edge in . The proof of the BEST Theorem [19, Theorem 5b] gives a bijection between

1. Directed Eulerian cycles of whose first edge is , and

2. ordered pairs