On this page
The eight three-bit binary words, 000 to 111, take 24 digits to write out. Write just eight, 00010111, round a circle, and every three neighbours read clockwise give a different word, all eight of them. That loop is a de Bruijn sequence, and its shared digits are what make it useful for keypad codes and card magic.
What is a de Bruijn sequence?
A de Bruijn sequence B(k, n) is a cyclic sequence on an alphabet of k symbols in which every possible string of length n occurs exactly once as a block of consecutive symbols (Wikipedia: De Bruijn sequence). Since there are k^n such strings and each new symbol finishes a new one, the sequence has length k^n (MathWorld). For binary words of length 3 there are exactly two: 00010111 and 11101000 (Wikipedia).
Check the first one, wrapping round from the end: 000, 001, 010, 101, 011, 111, 110, 100. Eight windows, eight different words.
The name comes from N. G. de Bruijn's 1946 paper *A combinatorial problem* (KNAW Digital Library). The idea is older: Camille Flye Sainte-Marie proved the two-symbol case in 1894, and the earliest known example comes from Sanskrit prosody linked to Pingala (Wikipedia).
How do you build a de Bruijn sequence from a graph?
In the de Bruijn graph, each arrow shifts every symbol one place left and adds a new symbol at the end (Wikipedia: De Bruijn graph). Every de Bruijn sequence corresponds to an Eulerian cycle on a de Bruijn graph, a route that uses every arrow exactly once (MathWorld).
Worked example for n = 3 (the diagram above):
Draw four vertices: 00, 01, 10, 11.
From each vertex draw two arrows, one for adding 0 and one for adding 1, labelled with the three-bit word they make. Arrow 101 leaves 10 and lands on 01: drop the first digit, keep the last two.
Every vertex has two arrows in and two out, and every vertex can reach every other, so a closed route through all eight arrows exists.
Start at 00 and walk: 000, 001, 010, 101, 011, 111, 110, 100, back to 00.
Write 00, then the last digit of the first six arrows: 00010111. The last two arrows add 0 and 0, the digits you started with, so the loop closes.
Our post on Hamiltonian cycles and Dirac's test sets the two kinds of route side by side. To revise graphs and Eulerian circuits from the ground up, the Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson.
How many de Bruijn sequences are there?
For binary words of length n, the number of distinct de Bruijn sequences is 2^(2^(n−1)−n), counting rotations of the same loop as one (OEIS A016031). The OEIS entry lists 1, 2, 16 and 2,048 for n = 2, 3, 4 and 5, and 67,108,864 for n = 6. Flye Sainte-Marie found this count in 1894, but his result was largely forgotten; Posthumus conjectured it again in 1944 and de Bruijn proved it in 1946 (Wikipedia).
Sanity check: n = 3 gives 2^(4−3) = 2, the pair above; n = 2 gives 1, the loop 0011.
How does a de Bruijn sequence crack a keypad code?
Picture a keypad lock with digits 0 to 9 that opens as soon as the last four keys pressed match the code. Typing every code separately takes 4 × 10,000 = 40,000 presses, but a de Bruijn sequence B(10, 4) covers all 10,000 codes in at most 10,003 presses (Wikipedia). After three presses, every key finishes a new code.
For a three-digit code the same reasoning gives 1,002 presses against 3,000. The design lesson: a lock that needs Enter after each attempt breaks the overlap. For why longer codes matter, see how to calculate password strength.
How does the de Bruijn card trick work?
The Aperiodical works out a card trick based on one by Persi Diaconis and Ron Graham, from their book *Magical Mathematics* (The Aperiodical). Each card gets a six-digit binary string, and the deck is stacked in the order of a de Bruijn sequence of order 6. A spectator cuts, draws six cards and calls out only their colours. Because every six-colour window appears once, the colours pin down the cards. With 64 strings but 52 cards, the write-up had to swap eight cards to make the code fit.
Try it yourself: a 20-minute practice plan
Write 0011 round a circle and check that 00, 01, 11 and 10 each appear once.
Draw the graph for n = 4: eight vertices from 000 to 111, sixteen arrows. Find one Eulerian circuit, read off a 16-digit loop, and tick off its windows as 0 to 15 using binary place values.
Find a second loop. The formula says there are 16.
Check that aacbbccab, in a circle, holds all nine two-letter words on a, b, c (MathWorld's example).
If a step stalls, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
Start with step 1: four digits, one circle, your first de Bruijn sequence.



