I'm trying to compute de Bruijn sequences for alphabets which have a number of characters which is not a power of two.
For alphabets with a 2^k characters, calculating de Bruijn sequences is easy: There are several simple rules, such as "Prefer Ones" and "Prefer Opposites" which work for generating B(2,n). B(2^k,n) is exactly the same as B(2,kn), if you read the 1s and 0s as binary codes for the actual characters in your alphabet. E.g., you can interpret B(2,8n) as being over n-length sequences of bytes.
Prefer Ones is quite simple: Write n zeros. Then, always write a one unless it would cause the repetition of an n-length string; otherwise, write a zero.
Presently, I don't see how to generalize such rules to non-power-of-two-sized alphabets.
There's a general method for calculating de Bruijn sequences via graphs: Let each n-length sequence generated by your alphabet be a node; put an edge from A to B iff the rightmost n-1 characters of A are the same as the leftmost n-1 characters of B. Label each edge with the last character of the string in the head vertex. Any Eulerian path through this graph will generate a de Bruijn sequence, and the peculiar construction we used guarantees that there will be at least one such path. We can use Fleury's Algorithm to (nondeterministically) construct an Eulerian path:
The resulting string will be a de Bruijn sequence.
This algorithm is somewhat more complex to implement than Prefer Ones. The simplicity of Prefer Ones is that one needs only to consult the output already generated to determine what to do. Is there a straightforward way to generalize Prefer Ones (or, possibly Prefer Opposites) to alphabets of non-power-of-two sizes?
Theorem 5 Binary De Bruijn sequences of order n exist for all n, and there are exactly 22n−1 of them.
A de Bruijn sequence of order is a cyclic sequence of length where each substring of length is a unique binary string. As an example, the cyclic sequence 0000100110101111 of length 16 is a de Bruijn sequence for .
This is my C++ implementation of the algorithm in Figure 1 from a paper by Sawada and Ruskey:
void debruijn(unsigned int t,
unsigned int p,
const unsigned int k,
const unsigned int n,
unsigned int* a,
boost::function<void (unsigned int*,unsigned int*)> callback)
{
if (t > n) {
// we want only necklaces, not pre-necklaces or Lyndon words
if (n % p == 0) {
callback(a+1, a+p+1);
}
}
else {
a[t] = a[t-p];
debruijn(t+1, p, k, n, a, callback);
for (unsigned int j = a[t-p]+1; j < k; ++j) {
a[t] = j;
debruijn(t+1, t, k, n, a, callback);
}
}
}
struct seq_printer {
const std::vector<char>& _alpha;
seq_printer(const std::vector<char>& alpha) : _alpha(alpha) {}
void operator() (unsigned int* a, unsigned int* a_end) const {
for (unsigned int* i = a; i < a_end; ++i) {
std::cout << _alpha[*i];
}
}
};
...
std::vector<char> alpha;
alpha.push_back('a');
alpha.push_back('b');
alpha.push_back('c');
unsigned int* a = new unsigned int[N+1];
a[0] = 0;
debruijn(1, 1, alpha.size(), N, a, seq_printer(alpha));
if (N > 1) std::cout << alpha[0];
std::cout << std::endl;
delete[] a;
The full reference for the paper is: Joe Sawada and Frank Ruskey, "An Efficient Algorithm for Generating Necklaces with Fixed Density", SIAM Journal of Computing 29:671-684, 1999.
According to this web page at the combinatorial group of the CS department at UVic, there's a result due to Fredericksen that you can generate a de Bruijn sequence (in fact, the lexicographically smallest one) by concatenating "the lexicographic sequence of Lyndon words of lengths divisible by n". There's even source code to build the sequence that you can request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With