Girish Mahajan (Editor)

De Bruijn sequence

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
De Bruijn sequence

In combinatorial mathematics, a De Bruijn sequence of order n on a size-k alphabet A is a cyclic sequence in which every possible length-n string on A occurs exactly once as a substring (i.e., as a contiguous subsequence). Such a sequence is denoted by B(k, n) and has length kn, which is also the number of distinct substrings of length n on A; de Bruijn sequences are therefore optimally short. There are ( k ! ) k n 1 k n distinct De Bruijn sequences B(k, n).

Contents

The sequences are named after the Dutch mathematician Nicolaas Govert de Bruijn. According to him, the existence of De Bruijn sequences for each order together with the above properties were first proved, for the case of alphabets with two elements, by Camille Flye Sainte-Marie in 1894, whereas the generalization to larger alphabets is originally due to Tanja van Aardenne-Ehrenfest and himself.

In most applications, A = {0,1}.

History

The earliest known example of a De Bruijn sequence comes from Sanskrit prosody where, since the work of Pingala, each possible three-syllable pattern of long and short syllables is given a name, such as 'y' for short–long–long and 'm' for long–long–long. To remember these names, the mnemonic yamātārājabhānasalagām is used, in which each three-syllable pattern occurs starting at its name: 'yamātā' has a short–long–long pattern, 'mātārā' has a long–long–long pattern, and so on, until 'salagām' which has a short–short–long pattern. This mnemonic, equivalent to a De Bruijn sequence on binary 3-tuples, is of unknown antiquity, but is at least as old as C. P. Brown's 1869 book on Sanskrit prosody that mentions it and considers it "an ancient line, written by Pāṇini".

In 1894, A. de Rivière raised the question in an issue of the French problem journal L'Intermédiaire des Mathématiciens, of the existence of a circular arrangement of length 2 n which contains all 2 n binary sequences of length n . The problem was solved, along with the count 2 2 n 1 n , by C. Flye Sainte-Marie in the same year. This was largely forgotten, and Martin (1934) proved the existence of such cycles for general alphabet size in place of 2, with an algorithm for constructing them. Finally, when in 1944 Kees Posthumus conjectured the count 2 2 n 1 n for binary sequences, de Bruijn proved the conjecture in 1946, through which the problem became well-known.

Karl Popper independently describes these objects in his The Logic of Scientific Discovery (1934), calling them "shortest random-like sequences".

Examples

  • Taking A = {0, 1}, there are two distinct B(2, 3): 00010111 and 11101000, one being the reverse or negation of the other.
  • Two of the 2048 possible B(2, 5) in the same alphabet are 00000100011001010011101011011111 and 00000101001000111110111001101011.
  • Construction

    The De Bruijn sequences can be constructed by taking a Hamiltonian path of an n-dimensional De Bruijn graph over k symbols (or equivalently, a Eulerian cycle of a (n − 1)-dimensional De Bruijn graph).

    An alternative construction involves concatenating together, in lexicographic order, all the Lyndon words whose length divides n.

    An inverse Burrows-Wheeler transform can be used to generate the required Lyndon words in lexicographic order.

    De Bruijn sequences can also be constructed using shift registers or via finite fields.

    Example using De Bruijn graph

    Goal: to construct a B(2, 4) De Bruijn sequence of length 24 = 16 using Eulerian (n − 1 = 4 − 1 = 3) 3-D De Bruijn graph cycle.

    Each edge in this 3-dimensional De Bruijn graph corresponds to a sequence of four digits: the three digits that label the vertex that the edge is leaving followed by the one that labels the edge. If one traverses the edge labeled 1 from 000, one arrives at 001, thereby indicating the presence of the subsequence 0001 in the De Bruijn sequence. To traverse each edge exactly once is to use each of the 16 four-digit sequences exactly once.

    For example, suppose we follow the following Eulerian path through these nodes:

    000, 000, 001, 011, 111, 111, 110, 101, 011,

    These are the output sequences of length k:

    0 0 0 0 _ 0 0 0 1 _ _ 0 0 1 1

    This corresponds to the following De Bruijn sequence:

    0 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1

    The eight vertices appear in the sequence in the following way:

    {0 0 0} 0 1 1 1 1 0 1 1 0 0 1 0 1 0 {0 0 0} 1 1 1 1 0 1 1 0 0 1 0 1 0 0 {0 0 1} 1 1 1 0 1 1 0 0 1 0 1 0 0 0 {0 1 1} 1 1 0 1 1 0 0 1 0 1 0 0 0 0 {1 1 1} 1 0 1 1 0 0 1 0 1 0 0 0 0 1 {1 1 1} 0 1 1 0 0 1 0 1 0 0 0 0 1 1 {1 1 0} 1 1 0 0 1 0 1 0 0 0 0 1 1 1 {1 0 1} 1 0 0 1 0 1 0 0 0 0 1 1 1 1 {0 1 1} 0 0 1 0 1 0 0 0 0 1 1 1 1 0 {1 1 0} 0 1 0 1 0 0 0 0 1 1 1 1 0 1 {1 0 0} 1 0 1 0 0 0 0 1 1 1 1 0 1 1 {0 0 1} 0 1 0 0 0 0 1 1 1 1 0 1 1 0 {0 1 0} 1 0 0 0 0 1 1 1 1 0 1 1 0 0 {1 0 1} ... 0} 0 0 0 1 1 1 1 0 1 1 0 0 1 {0 1 ... ... 0 0} 0 0 1 1 1 1 0 1 1 0 0 1 0 {1 ...

    ...and then we return to the starting point. Each of the eight 3-digit sequences (corresponding to the eight vertices) appears exactly twice, and each of the sixteen 4-digit sequences (corresponding to the 16 edges) appears exactly once.

    Example using inverse Burrows-Wheeler transform

    Mathematically, an inverse Burrows-Wheeler transform on a word w generates a multi-set of equivalence classes consisting of strings and their rotations. These equivalence classes of strings each contain a Lyndon word as a unique minimum element, so the inverse Burrows-Wheeler transform can be considered to generate a set of Lyndon words. It can be shown that if we perform the inverse Burrows-Wheeler transform on a word w consisting of the size-k alphabet repeated kn-1 times (so that it will produce a word the same length as the desired De Bruijn sequence), then the result will be the set of all Lyndon words whose length divides n. It follows that arranging these Lyndon words in lexicographic order will yield a De Bruijn sequence B(k,n), and that this will be the first De Bruijn sequence in lexicographic order. The following method can be used to perform the inverse Burrows-Wheeler transform, using its standard permutation:

    1. Sort the characters in the string w, yielding a new string w'
    2. Position the string w' above the string w, and map each letter's position in w' to its position in w while preserving order. This process defines the standard permutation.
    3. Write this permutation in cycle notation with the smallest position in each cycle first, and the cycles sorted in increasing order.
    4. For each cycle, replace each number with the corresponding letter from string w' in that position.
    5. Each cycle has now become a Lyndon word, and they are arranged in lexicographic order, so dropping the parentheses yields the first De Bruijn sequence.

    For example, to construct the smallest B(2,4) De Bruijn sequence of length 24 = 16, repeat the alphabet (ab) 8 times yielding w=abababababababab. Sort the characters in w, yielding w'=aaaaaaaabbbbbbbb. Position w' above w as shown, and map each element in w' to the corresponding element in w by drawing a line. Number the columns as shown so we can read the cycles of the permutation:

    Starting from the left, the cycles are: (1) (2 3 5 9) (4 7 13 10) (6 11) (8 15 14 12) (16).

    Then, replacing each number by the corresponding letter in w' from that column yields: (a)(aaab)(aabb)(ab)(abbb)(b).

    These are all of the Lyndon words whose length divides 4, in lexicographic order, so dropping the parentheses gives B(2,4) = aaaabaabbababbbb.

    Algorithm

    The following Python code calculates a De Bruijn sequence, given k and n, based on an algorithm from Frank Ruskey's Combinatorial Generation.

    which prints

    00010111 aabacadbbcbdccdd

    Note that these sequences are understood to "wrap around" in a cycle. For example, the first sequence contains 110 and 100 in this fashion.

    Uses

    The sequence can be used to shorten a brute-force attack on a PIN-like code lock that does not have an "enter" key and accepts the last n digits entered. For example, a digital door lock with a 4-digit code would have B(10, 4) solutions, with length 7004100000000000000♠10000. Therefore, only at most 7004100000000000000♠10000 + 3 = 7004100030000000000♠10003 (as the solutions are cyclic) presses are needed to open the lock. Trying all codes separately would require 4 × 7004100000000000000♠10000 = 7004400000000000000♠40000 presses.

    The symbols of a De Bruijn sequence written around a circular object (such as a wheel of a robot) can be used to identify its angle by examining the n consecutive symbols facing a fixed point. This angle-encoding problem is known as the "rotating drum problem". Gray codes can be used as similar rotary positional encoding mechanisms.

    De Bruijn cycles are of general use in neuroscience and psychology experiments that examine the effect of stimulus order upon neural systems, and can be specially crafted for use with functional magnetic resonance imaging.

    A De Bruijn sequence can be used to quickly find the index of the least significant set bit ("right-most 1") or the most significant set bit ("left-most 1") in a word using bitwise operations. An example of returning the index of the least significant bit from a 32 bit unsigned integer is given below using bit manipulation and multiplication.

    The index of the LSB in v is stored in r and if v has no set bits the operation returns 0. The constant, 0x077CB531U, in the expression is a De Bruijn sequence.

    f-fold De Bruijn sequences

    f-fold n-ary De Bruijn sequence' is an extension of the notion n-ary De Bruijn sequence, such that the sequence of the length f k n contains every possible subsequence of the length n exactly f times. For example, for n = 2 the cyclic sequences 11100010 and 11101000 are two-fold 2-ary binary De Bruijn sequences. The number of two-fold De Bruijn sequences, N n for n = 1 is N 1 = 2 , the other known numbers are N 2 = 5 , N 3 = 72 , and N 4 = 43768 .

    De Bruijn torus

    A De Bruijn torus is a toroidal array with the property that every k-ary m-by-n matrix occurs exactly once.

    Such a pattern can be used for two-dimensional positional encoding in a fashion analogous to that described above for rotary encoding. Position can be determined by examining the m-by-n matrix directly adjacent to the sensor, and calculating its position on the De Bruijn torus.

    De Bruijn decoding

    Computing the position of a particular unique tuple or matrix in a De Bruijn sequence or torus is known as the De Bruijn Decoding Problem. Efficient O(n log n) decoding algorithms exists for special, recursively constructed sequences and extend to the two dimensional case. De Bruijn decoding is of interest, e.g., in cases where large sequences or tori are used for positional encoding.

    References

    De Bruijn sequence Wikipedia