Tutorials › Graph Theory (Undergraduate) › Constructing de Bruijn Sequences From Euler Circuits

Eulerian Trails and Circuits · Tutorial 81 of 799

Constructing de Bruijn Sequences From Euler Circuits

Use the overlaps in an Euler circuit to construct and verify cyclic de Bruijn sequences.

Intermediate 11 min read

What You'll Learn

  • Define the order-k de Bruijn digraph for a finite alphabet.
  • Verify degree balance and strong connectivity in that digraph.
  • Explain why its Euler circuits exist.
  • Prove that successive edge labels give the required cyclic windows.
  • Construct and check binary and ternary de Bruijn sequences.
  • Distinguish a cyclic sequence from its linear spelling.

From Overlapping Words to a Cyclic Sequence

A de Bruijn digraph turns overlapping words into a route problem. Its vertices represent words one symbol shorter than the words we want to list; each directed edge represents one word and joins its prefix to its suffix. An Euler circuit uses every edge exactly once. The overlap at each step then lets us read the edge labels as one cyclic sequence containing every target word once.

Let \(A\) be a finite alphabet with \(q\ge 1\) symbols, and let \(k\ge 1\). A word of length \(k\) is an ordered string of \(k\) symbols from \(A\). The prefix of length \(k-1\) is obtained by deleting its last symbol, and the suffix of length \(k-1\) by deleting its first. When \(k=1\), both are the empty word.

Definition: The order-\(k\) de Bruijn digraph on \(A\) has as vertices all words of length \(k-1\). For each word \(a_1a_2\cdots a_k\) of length \(k\), it has a directed edge, labelled by that word, from \(a_1a_2\cdots a_{k-1}\) to \(a_2a_3\cdots a_k\). A directed Euler circuit is a closed directed trail that uses every edge exactly once.

Edges are identified by their labels. For \(k=1\), the graph has one vertex and one loop for each alphabet symbol, so it is a directed multigraph if the loops are regarded as edges with a common endpoint. For \(k\ge2\), each word determines its directed edge, including when that edge is a loop. We will use the circuit to order all length-\(k\) words and then read a symbol from each edge.

Why an Euler Circuit Exists

For any vertex \(a_1\cdots a_{k-1}\), there are \(q\) outgoing edges: append any one symbol of \(A\). There are also \(q\) incoming edges: prepend any one symbol of \(A\). Thus every vertex has equal in-degree and out-degree. When \(k=1\), the unique vertex has \(q\) incoming and \(q\) outgoing loops, counted with multiplicity.

The digraph is strongly connected: from any vertex, append in order the \(k-1\) symbols of a chosen destination word. After these \(k-1\) shifts, the current vertex is that destination. For \(k=1\), there is only one vertex, so strong connectivity holds as well.

Standard result (directed Euler circuit characterization): A finite directed graph has an Euler circuit if its vertices of nonzero degree lie in one strongly connected component and every vertex has equal in-degree and out-degree. In particular, a strongly connected directed graph with these degree equalities has an Euler circuit. This characterization is part of the earlier material on directed Euler circuits.

Every vertex in the order-\(k\) de Bruijn digraph has nonzero degree when \(q\ge1\). The degree calculation and strong-connectivity argument therefore verify the characterization's hypotheses. In particular, an Euler circuit exists. The number of edges is \(q^k\), since each of the \(k\) positions in an edge label has \(q\) choices.

The Euler-Circuit Construction

Theorem: For every finite alphabet \(A\) of size \(q\ge1\) and every \(k\ge1\), an Euler circuit in the order-\(k\) de Bruijn digraph gives a cyclic sequence of length \(q^k\) in which every length-\(k\) word over \(A\) occurs exactly once as a consecutive cyclic block.

Proof. List the edges in the order in which an Euler circuit traverses them as \(e_0,e_1,\ldots,e_{N-1}\), where \(N=q^k\). Let \(x_i\) be the first symbol of the label of \(e_i\). Read the indices cyclically, so that \(e_N=e_0\) and \(x_N=x_0\). This defines a cyclic sequence \(x_0x_1\cdots x_{N-1}\).

We show that the length-\(k\) block starting at \(x_i\) is exactly the label of \(e_i\). If \(k=1\), the block consists just of \(x_i\), which is the label of the loop \(e_i\), as required. Now suppose \(k\ge2\), and write the label of \(e_i\) as \(a_1a_2\cdots a_k\). Because \(e_i\) and \(e_{i+1}\) meet at a vertex, the suffix of length \(k-1\) of the first label equals the prefix of length \(k-1\) of the second. In symbols, the symbol in position \(t+1\) of the label of \(e_i\) equals the symbol in position \(t\) of the label of \(e_{i+1}\), for \(1\le t\le k-1\). Applying this overlap successively shows that the first symbol of the label of \(e_{i+j}\) is \(a_{j+1}\) for \(0\le j\le k-1\). Hence \(x_ix_{i+1}\cdots x_{i+k-1}=a_1a_2\cdots a_k\), with indices read cyclically.

An Euler circuit uses every edge exactly once, and the edges are in one-to-one correspondence with the length-\(k\) words. Thus, as \(i\) ranges over the \(N\) edge positions, the cyclic blocks starting at \(x_i\) are precisely all length-\(k\) words, each once. This proves the result. \(\square\)

The cyclic qualification matters: a block may start near the end of the written sequence and continue from its beginning. If a linear string is preferred, write the first \(k-1\) symbols again after the cyclic sequence. The resulting string has length \(q^k+k-1\); its \(q^k\) length-\(k\) blocks starting at the original positions are exactly the cyclic blocks.

Worked Constructions

Worked Example: A Binary Sequence of Order 2

Take \(A=\{0,1\}\) and \(k=2\). The vertices are \(V=\{0,1\}\), and the directed edges, shown as label and direction, are \(00:0\to0\), \(01:0\to1\), \(10:1\to0\), and \(11:1\to1\). Consider the circuit with edge labels \(00,01,11,10\). Its consecutive endpoints match: \(00\) ends at \(0\), \(01\) ends at \(1\), \(11\) ends at \(1\), and \(10\) returns to \(0\).

Take the first symbol of each edge label in circuit order. This gives \(0,0,1,1\), hence the cyclic sequence \(0011\). Its length is \(2^2=4\). The cyclic length-2 blocks are \(00\), \(01\), \(11\), and \(10\), in that order. These are the four binary words of length 2, with none repeated.

Check: the edge labels in the circuit are exactly \(00,01,11,10\), each once. The sequence's four cyclic blocks reproduce those labels, including \(10\), which starts at the last symbol and wraps to the first.

Worked Example: A Binary Sequence of Order 3

Take \(A=\{0,1\}\) and \(k=3\). The vertices are \(V=\{00,01,10,11\}\). The directed edges are \(000:00\to00\), \(001:00\to01\), \(010:01\to10\), \(011:01\to11\), \(100:10\to00\), \(101:10\to01\), \(110:11\to10\), and \(111:11\to11\).

One Euler circuit has edge labels \(000,001,010,101,011,111,110,100\). The successive vertex transitions are \(00\to00\to01\to10\to01\to11\to11\to10\to00\), so the route closes. All eight edges occur once. Reading the first symbol of each label gives \(0,0,0,1,0,1,1,1\), the cyclic sequence \(00010111\).

Its length is \(2^3=8\). To check every cyclic block, start at each position in turn: the blocks are \(000\), \(001\), \(010\), \(101\), \(011\), \(111\), \(110\), and \(100\). These are all eight binary words of length 3. In particular, the last two blocks wrap around the end of the written string.

Check: the displayed blocks equal the edge labels in circuit order, and the list contains eight distinct words. This independently confirms that no length-3 word is missing or repeated.

Worked Example: A Ternary Sequence of Order 2

Let \(A=\{0,1,2\}\) and \(k=2\). The vertices are \(V=\{0,1,2\}\). The directed edges are \(00:0\to0\), \(01:0\to1\), \(02:0\to2\), \(10:1\to0\), \(11:1\to1\), \(12:1\to2\), \(20:2\to0\), \(21:2\to1\), and \(22:2\to2\).

The edge-label order \(00,01,10,02,21,11,12,22,20\) gives the vertex route \(0\to0\to1\to0\to2\to1\to1\to2\to2\to0\). It is closed and uses each of the nine edges exactly once. Taking the first symbol from each label gives the cyclic sequence \(001021122\), of length \(3^2=9\).

Its length-2 cyclic blocks, starting at the nine positions, are \(00\), \(01\), \(10\), \(02\), \(21\), \(11\), \(12\), \(22\), and \(20\). These are the nine ordered pairs from \(\{0,1,2\}\), each once.

Check: the circuit list includes three edges leaving each vertex: the labels beginning with \(0\) are \(00,01,02\), those beginning with \(1\) are \(10,11,12\), and those beginning with \(2\) are \(20,21,22\). The nine blocks listed are distinct and match the nine edge labels.

Reading and Checking the Construction

The construction depends on two different facts. The Euler circuit guarantees that every edge label appears exactly once; the overlap at consecutive edges guarantees that these labels are consecutive windows in a single cyclic sequence. Neither fact alone is enough. An arbitrary ordering of the words would use every word once but might not give matching overlaps, while a closed walk with matching overlaps might fail to use every edge.

A useful check for a proposed sequence is to list its cyclic windows, not only the windows that fit inside its written form. There must be \(q^k\) starting positions, and the corresponding blocks must be all distinct. Since there are exactly \(q^k\) possible words, \(q^k\) distinct blocks certify that every word occurs.

Common Pitfall: Forgetting the Wraparound

For the binary order-3 sequence \(00010111\), reading only windows that fit in the linear string produces six blocks: \(000,001,010,101,011,111\). This count is \(8-3+1=6\), not eight, so it cannot verify the cyclic construction. The missing cyclic windows start at the last two positions: \(110\) starts at the second-last symbol and continues from the beginning, while \(100\) starts at the last symbol and continues from the beginning. Including them gives all eight required blocks.

A related mistake is to concatenate complete edge labels and treat the result as the sequence. The labels overlap in \(k-1\) symbols, so concatenating them repeats those shared symbols. The construction instead takes one symbol from each edge, while the overlaps prove that each length-\(k\) window recovers the full label.

Check Your Understanding

Use the edge-label and cyclic-window definitions to answer these questions.

  1. For an alphabet of size \(q\) and order \(k\), how many vertices and edges does the de Bruijn digraph have?
  2. Explain why every vertex has equal in-degree and out-degree, and why the digraph is strongly connected.
  3. For \(A=\{0,1\}\) and \(k=2\), verify the cyclic blocks of \(0110\). Does it contain every binary word of length 2 exactly once?
  4. In the order-3 binary example, which two blocks are missed if the written string is treated as linear rather than cyclic?
  5. Why does using every edge once not, by itself, prove that an arbitrary list of edge labels forms consecutive windows of one sequence?