Counting and constructing balanced de Bruijn necklaces at every length

A balanced de Bruijn necklace of length \(L\) distributes the \(L\) cyclic windows of each width as evenly as possible among all words of that width. More precisely, for every \(1\le j\le L\), each word of length \(j\) over the labelled alphabet \(A=\{0,\ldots,K-1\}\) must occur either \(\lfloor L/K^j\rfloor\) or \(\lceil L/K^j\rceil\) times, counting windows that wrap around the end. Rotations represent the same necklace, while orientation and alphabet labels are retained. For every \(K\ge2\) and \(L\ge1\), one system of binary margins gives an exact count, a linear-time cycle-joining construction and a low-space rule for generating a single necklace window by window. Let \(D=\max(1,\lceil\log_K L\rceil)\) be the critical width. At width \(D\), every used word occurs once, so each longer window is distinguished by its width-\(D\) prefix. If \(L\le K\), any cyclic ordering of \(L\) distinct letters is balanced, giving \(\binom KL(L-1)!\) necklaces. For \(L>K\), put \(q_j=\lfloor L/K^j\rfloor\), \(r_j=L-K^j q_j\) and \(d_j=q_{j-1}-Kq_j\) for \(j\ge1\), with \(q_0=L\). Write the proposed count of a word \(w\) of length \(j\) as \(n_j(w)=q_j+h_j(w)\), where \(h_j(w)\in\{0,1\}\) and \(h_0(\epsilon)=0\) for the empty word \(\epsilon\). The margins are \[\sum_{b\in A}h_j(wb)=d_j+h_{j-1}(w)=\sum_{a\in A}h_j(aw),\qquad w\in A^{j-1},\quad 1\le j\le D. \] They require that summing counts over either one-letter extension gives the count of the shorter word. Choose \(r_1\) letters with flag one. At each subsequent width \(j\), for each middle word \(v\in A^{j-2}\), fill the separate binary matrix \(H_v(a,b)=h_j(avb)\) with row sums \(d_j+h_{j-1}(av)\) and column sums \(d_j+h_{j-1}(vb)\). Each level has exactly \(r_j\) flags equal to one, including zero when the floor and ceiling coincide. Summing over extensions recovers all shorter counts from the terminal profile. Nellore and Ward's first question asks how many necklaces meet these simultaneous frequency requirements. For each terminal profile \(h=h_D\) obtained from the margins, form a directed graph on \(A^{D-1}\). Include the arc labelled \(w\in A^D\) from its prefix to its suffix exactly when \(q_D+h(w)=1\). Each vertex \(u\) has equal positive in-degree and out-degree, both \(n_{D-1}(u)\). Let \(T_h\) be the determinant obtained by deleting the row and column for any fixed root from \(\operatorname{diag}(\deg^+)-\operatorname{Adj}\), where \(\operatorname{Adj}_{uv}\) counts arcs from \(u\) to \(v\). Loops count in both terms and cancel in the Laplacian. With \(V=K^{D-1}\), \(b=q_{D-1}\) and \(r=r_{D-1}\), the answer for \(L>K\) is \[C_K(L)=((b-1)!)^{V-r}(b!)^r\sum_{\text{terminal profiles }h}T_h. \] The directed matrix-tree theorem gives zero weight to a disconnected profile. For a connected profile, the BEST theorem counts its Euler circuits. Fixing one first arc at the root gives exactly one representative per rotation class, since every arc label is distinct. Thus the sum counts all balanced necklaces, and for \(K=2\) the profile enumeration and determinants take \(O(\operatorname{poly}(L)2^{3L/4})\) integer arithmetic operations. Nellore and Ward, §3 question 1, p. 9:14 There is a smaller calculation within \(r\le K\) deletions of a full length \(K^m\), for any integer \(m\ge2\) and \(0\le r\le K\). The allowable omissions are specified by choosing \(r\) letters and permuting them. For each chosen letter, delete the length-\(m\) arc word formed by starting at that letter and following its permutation cycle, wrapping around as needed. A cycle of length \(\ell\) contributes the weight \[\delta_\ell(K,m)=\det\!\left[I_\ell-\frac{1}{K}(I_\ell-S_\ell)\sum_{t=0}^{m-2}(S_\ell/K)^t\right], \] where \(I_\ell\) is the identity and \(S_\ell\) has a one in each position \((i,i+1\bmod\ell)\) and zeros elsewhere. Writing \(B_K(m)=(K!)^{K^{m-1}}/K^m\) for the classical full de Bruijn count gives \[C_K(K^m-r)=\frac{B_K(m)}{(K-1)^r}\binom Kr\,r!\,[z^r]\exp\!\left(\sum_{\ell=1}^{r}\frac{\delta_\ell(K,m)}{\ell}z^\ell\right). \] Here \([z^r]\) extracts the coefficient of \(z^r\) in the formal power series. At \(r=0\) the coefficient is one. For \(r>0\) all determinants have size at most \(r\). Since \(\delta_1=1\), a single deletion gives \(K B_K(m)/(K-1)\) necklaces. Nellore and Ward also asked for a construction beyond recursive Lempel lifting, and Cameron, Gündogan and Sawada asked specifically for efficient cycle joining under the same all-width balance condition. The construction holds \(L\) fixed. Choose the first \(r_1\) letters at width one. In each later matrix, order the columns by decreasing required sum, breaking ties in alphabet order. Cycle through this list from its first entry, giving each row in alphabet order as many consecutive columns as its required sum and resuming from that position for the next row. Mark those entries one and the rest zero. Every row takes at most \(K\) columns, and the prescribed column sums differ by at most one, so this fills both margins exactly. Nellore and Ward, §3 question 3, p. 9:14 · Cameron, Gündogan and Sawada, §6 item 2, PDF p. 18 Use the resulting width-\(D\) arcs to find the initial Euler components. Form an undirected graph whose vertices are these components. For each \(v\in A^{D-2}\), link the component of row \(0v\) to that of every other row \(av\) and retain the designated row pair with each link. This graph is connected. The links group all rows of a block together, and every column \(vb\) has a present incoming arc from one of those rows. The groups therefore contain both endpoints of every possible arc of the full de Bruijn graph, which is connected. Take a spanning tree of the component graph. For each tree edge, select one current outgoing arc at each designated row and exchange their targets, replacing \(av\to vb\) and \(cv\to vd\) by \(av\to vd\) and \(cv\to vb\). The two rows are still in different components at that step, so the cross-arcs were absent. Each exchange joins the two components and preserves all degrees, hence every profile below width \(D\). Read the last letter of each arc of an Euler circuit of the final graph. This gives the \(L\) letters of the necklace. For \(L\le K\), use \(0,\ldots,L-1\) directly. The construction uses \(O(L)\) word-RAM operations, including preparation and output, and \(O(L)\) words of \(O(\log(K+L))\) bits. Lempel lifting already gave linear total time. The new construction obtains that bound by filling the margins directly and using one spanning tree of the initial component graph. The remaining question asks for a successor rule whose only sequence state is the current critical-width window. For \(L>K\), the same deterministic margin fill can be evaluated one entry at a time. Start with \(h_1(a)=[a<r_1]\). For \(2\le j\le D\), \(v\in A^{j-2}\) and \(a,b\in A\), use \[\begin{aligned} s_v(a)&=\left(a d_j+\sum_{c\in A,\,c<a}h_{j-1}(cv)\right)\bmod K,\\ p_v(b)&=\sum_{c\in A}[(-h_{j-1}(vc),c)<(-h_{j-1}(vb),b)],\\ h_j(avb)&=[(p_v(b)-s_v(a))\bmod K<d_j+h_{j-1}(av)]. \end{aligned} \] Brackets denote indicators, pairs are compared lexicographically and residues lie in \(\{0,\ldots,K-1\}\). The quantities \(s_v(a)\) and \(p_v(b)\) are the row's starting position and the column's rank in the repeated list, indexed from zero. This recurrence supplies the initial arc test on demand. Nellore and Ward, §3 question 2, p. 9:14 Two ordered sets of links turn this arc test into a successor. In each set, keep a link exactly when its endpoints are disconnected in the undirected base graph augmented with all earlier candidate links. This selects the Kruskal forest on the base components. To read an entry after the corresponding swaps, trace its slot backwards through the kept links in reverse order, moving to the other endpoint whenever a link touches the slot, then read the initial value at the resulting slot. First take the unswitched support as the base graph and order the row links \(0v\leftrightarrow av\), \(a\ne0\), lexicographically by \((v,a)\). At each row use its least initial outgoing letter as the slot to swap. Replace that letter by the one obtained from the backwards scan and keep every other letter in the row. This gives an arc test for the connected support. In that connected graph, define \(\sigma(e)\) for an arc \(e\) ending at \(u\) by pairing its rank among the lexicographically ordered incoming arcs of \(u\) with the same rank among the outgoing arcs. This permutation initially gives a collection of cycles. Use the arcs as vertices of the second base graph, with undirected edges \(e\leftrightarrow\sigma(e)\). At each head \(u\), link its least incoming arc to each other incoming arc, ordering the candidates by the head and then the other arc. For every kept link, swap the successors of its two endpoints. Each swap joins two current cycles and preserves overlap, since their outgoing successors start at the same head. The result is one orbit of length \(L\). Initialise with the least present width-\(D\) word. Given a present window, trace its successor slot backwards through the second set of swaps, apply \(\sigma\) there and append the last letter of the resulting arc while dropping the first letter of the current window. An absent window is rejected. For \(L\le K\), initialise at zero and advance through the singleton windows \(0,\ldots,L-1\) cyclically. The two forest tests use Reingold's undirected connectivity algorithm with adjacency recomputed on demand. For \(L>K\), the profile recursion has \(O(D)\) levels with \(O(\log L)\) bits per level, and the two forest tests have fixed nesting depth. The rule uses \(O(\log^2 L)\) working bits and polynomial time in \(L\) per shift, with polynomial-time initialisation. In the singleton case padded alphabet labels use \(O(\log(K+L))\) working bits. The rule retains the current window and recomputes the necessary entries from \(K,L\) and that window, without storing output history, profiles, graphs or a successor

Authors

Institutions

Publication Details

Journal
Zenodo (CERN European Organization for Nuclear Research)
Published
2026-12-03
DOI
https://doi.org/10.5281/zenodo.24794419
Primary Topic
semigroups and automata theory
Type
preprint
Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
preprint

Counting and constructing balanced de Bruijn necklaces at every length

Zixuan He
Zenodo (CERN European Organization for Nuclear Research)
semigroups and automata theory
preprint

Counting and constructing balanced de Bruijn necklaces at every length

Zixuan He
preprint en

Abstract

A balanced de Bruijn necklace of length \(L\) distributes the \(L\) cyclic windows of each width as evenly as possible among all words of that width. More precisely, for every \(1\le j\le L\), each word of length \(j\) over the labelled alphabet \(A=\{0,\ldots,K-1\}\) must occur either \(\lfloor L/K^j\rfloor\) or \(\lceil L/K^j\rceil\) times, counting windows that wrap around the end. Rotations represent the same necklace, while orientation and alphabet labels are retained. For every \(K\ge2\) and \(L\ge1\), one system of binary margins gives an exact count, a linear-time cycle-joining construction and a low-space rule for generating a single necklace window by window. Let \(D=\max(1,\lceil\log_K L\rceil)\) be the critical width. At width \(D\), every used word occurs once, so each longer window is distinguished by its width-\(D\) prefix. If \(L\le K\), any cyclic ordering of \(L\) distinct letters is balanced, giving \(\binom KL(L-1)!\) necklaces. For \(L>K\), put \(q_j=\lfloor L/K^j\rfloor\), \(r_j=L-K^j q_j\) and \(d_j=q_{j-1}-Kq_j\) for \(j\ge1\), with \(q_0=L\). Write the proposed count of a word \(w\) of length \(j\) as \(n_j(w)=q_j+h_j(w)\), where \(h_j(w)\in\{0,1\}\) and \(h_0(\epsilon)=0\) for the empty word \(\epsilon\). The margins are \[\sum_{b\in A}h_j(wb)=d_j+h_{j-1}(w)=\sum_{a\in A}h_j(aw),\qquad w\in A^{j-1},\quad 1\le j\le D. \] They require that summing counts over either one-letter extension gives the count of the shorter word. Choose \(r_1\) letters with flag one. At each subsequent width \(j\), for each middle word \(v\in A^{j-2}\), fill the separate binary matrix \(H_v(a,b)=h_j(avb)\) with row sums \(d_j+h_{j-1}(av)\) and column sums \(d_j+h_{j-1}(vb)\). Each level has exactly \(r_j\) flags equal to one, including zero when the floor and ceiling coincide. Summing over extensions recovers all shorter counts from the terminal profile. Nellore and Ward's first question asks how many necklaces meet these simultaneous frequency requirements. For each terminal profile \(h=h_D\) obtained from the margins, form a directed graph on \(A^{D-1}\). Include the arc labelled \(w\in A^D\) from its prefix to its suffix exactly when \(q_D+h(w)=1\). Each vertex \(u\) has equal positive in-degree and out-degree, both \(n_{D-1}(u)\). Let \(T_h\) be the determinant obtained by deleting the row and column for any fixed root from \(\operatorname{diag}(\deg^+)-\operatorname{Adj}\), where \(\operatorname{Adj}_{uv}\) counts arcs from \(u\) to \(v\). Loops count in both terms and cancel in the Laplacian. With \(V=K^{D-1}\), \(b=q_{D-1}\) and \(r=r_{D-1}\), the answer for \(L>K\) is \[C_K(L)=((b-1)!)^{V-r}(b!)^r\sum_{\text{terminal profiles }h}T_h. \] The directed matrix-tree theorem gives zero weight to a disconnected profile. For a connected profile, the BEST theorem counts its Euler circuits. Fixing one first arc at the root gives exactly one representative per rotation class, since every arc label is distinct. Thus the sum counts all balanced necklaces, and for \(K=2\) the profile enumeration and determinants take \(O(\operatorname{poly}(L)2^{3L/4})\) integer arithmetic operations. Nellore and Ward, §3 question 1, p. 9:14 There is a smaller calculation within \(r\le K\) deletions of a full length \(K^m\), for any integer \(m\ge2\) and \(0\le r\le K\). The allowable omissions are specified by choosing \(r\) letters and permuting them. For each chosen letter, delete the length-\(m\) arc word formed by starting at that letter and following its permutation cycle, wrapping around as needed. A cycle of length \(\ell\) contributes the weight \[\delta_\ell(K,m)=\det\!\left[I_\ell-\frac{1}{K}(I_\ell-S_\ell)\sum_{t=0}^{m-2}(S_\ell/K)^t\right], \] where \(I_\ell\) is the identity and \(S_\ell\) has a one in each position \((i,i+1\bmod\ell)\) and zeros elsewhere. Writing \(B_K(m)=(K!)^{K^{m-1}}/K^m\) for the classical full de Bruijn count gives \[C_K(K^m-r)=\frac{B_K(m)}{(K-1)^r}\binom Kr\,r!\,[z^r]\exp\!\left(\sum_{\ell=1}^{r}\frac{\delta_\ell(K,m)}{\ell}z^\ell\right). \] Here \([z^r]\) extracts the coefficient of \(z^r\) in the formal power series. At \(r=0\) the coefficient is one. For \(r>0\) all determinants have size at most \(r\). Since \(\delta_1=1\), a single deletion gives \(K B_K(m)/(K-1)\) necklaces. Nellore and Ward also asked for a construction beyond recursive Lempel lifting, and Cameron, Gündogan and Sawada asked specifically for efficient cycle joining under the same all-width balance condition. The construction holds \(L\) fixed. Choose the first \(r_1\) letters at width one. In each later matrix, order the columns by decreasing required sum, breaking ties in alphabet order. Cycle through this list from its first entry, giving each row in alphabet order as many consecutive columns as its required sum and resuming from that position for the next row. Mark those entries one and the rest zero. Every row takes at most \(K\) columns, and the prescribed column sums differ by at most one, so this fills both margins exactly. Nellore and Ward, §3 question 3, p. 9:14 · Cameron, Gündogan and Sawada, §6 item 2, PDF p. 18 Use the resulting width-\(D\) arcs to find the initial Euler components. Form an undirected graph whose vertices are these components. For each \(v\in A^{D-2}\), link the component of row \(0v\) to that of every other row \(av\) and retain the designated row pair with each link. This graph is connected. The links group all rows of a block together, and every column \(vb\) has a present incoming arc from one of those rows. The groups therefore contain both endpoints of every possible arc of the full de Bruijn graph, which is connected. Take a spanning tree of the component graph. For each tree edge, select one current outgoing arc at each designated row and exchange their targets, replacing \(av\to vb\) and \(cv\to vd\) by \(av\to vd\) and \(cv\to vb\). The two rows are still in different components at that step, so the cross-arcs were absent. Each exchange joins the two components and preserves all degrees, hence every profile below width \(D\). Read the last letter of each arc of an Euler circuit of the final graph. This gives the \(L\) letters of the necklace. For \(L\le K\), use \(0,\ldots,L-1\) directly. The construction uses \(O(L)\) word-RAM operations, including preparation and output, and \(O(L)\) words of \(O(\log(K+L))\) bits. Lempel lifting already gave linear total time. The new construction obtains that bound by filling the margins directly and using one spanning tree of the initial component graph. The remaining question asks for a successor rule whose only sequence state is the current critical-width window. For \(L>K\), the same deterministic margin fill can be evaluated one entry at a time. Start with \(h_1(a)=[a<r_1]\). For \(2\le j\le D\), \(v\in A^{j-2}\) and \(a,b\in A\), use \[\begin{aligned} s_v(a)&=\left(a d_j+\sum_{c\in A,\,c<a}h_{j-1}(cv)\right)\bmod K,\\ p_v(b)&=\sum_{c\in A}[(-h_{j-1}(vc),c)<(-h_{j-1}(vb),b)],\\ h_j(avb)&=[(p_v(b)-s_v(a))\bmod K<d_j+h_{j-1}(av)]. \end{aligned} \] Brackets denote indicators, pairs are compared lexicographically and residues lie in \(\{0,\ldots,K-1\}\). The quantities \(s_v(a)\) and \(p_v(b)\) are the row's starting position and the column's rank in the repeated list, indexed from zero. This recurrence supplies the initial arc test on demand. Nellore and Ward, §3 question 2, p. 9:14 Two ordered sets of links turn this arc test into a successor. In each set, keep a link exactly when its endpoints are disconnected in the undirected base graph augmented with all earlier candidate links. This selects the Kruskal forest on the base components. To read an entry after the corresponding swaps, trace its slot backwards through the kept links in reverse order, moving to the other endpoint whenever a link touches the slot, then read the initial value at the resulting slot. First take the unswitched support as the base graph and order the row links \(0v\leftrightarrow av\), \(a\ne0\), lexicographically by \((v,a)\). At each row use its least initial outgoing letter as the slot to swap. Replace that letter by the one obtained from the backwards scan and keep every other letter in the row. This gives an arc test for the connected support. In that connected graph, define \(\sigma(e)\) for an arc \(e\) ending at \(u\) by pairing its rank among the lexicographically ordered incoming arcs of \(u\) with the same rank among the outgoing arcs. This permutation initially gives a collection of cycles. Use the arcs as vertices of the second base graph, with undirected edges \(e\leftrightarrow\sigma(e)\). At each head \(u\), link its least incoming arc to each other incoming arc, ordering the candidates by the head and then the other arc. For every kept link, swap the successors of its two endpoints. Each swap joins two current cycles and preserves overlap, since their outgoing successors start at the same head. The result is one orbit of length \(L\). Initialise with the least present width-\(D\) word. Given a present window, trace its successor slot backwards through the second set of swaps, apply \(\sigma\) there and append the last letter of the resulting arc while dropping the first letter of the current window. An absent window is rejected. For \(L\le K\), initialise at zero and advance through the singleton windows \(0,\ldots,L-1\) cyclically. The two forest tests use Reingold's undirected connectivity algorithm with adjacency recomputed on demand. For \(L>K\), the profile recursion has \(O(D)\) levels with \(O(\log L)\) bits per level, and the two forest tests have fixed nesting depth. The rule uses \(O(\log^2 L)\) working bits and polynomial time in \(L\) per shift, with polynomial-time initialisation. In the singleton case padded alphabet labels use \(O(\log(K+L))\) working bits. The rule retains the current window and recomputes the necessary entries from \(K,L\) and that window, without storing output history, profiles, graphs or a successor

Zenodo (CERN European Organization for Nuclear Research)
University of Glasgow (GB)
semigroups and automata theory
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.

Laika access coming soon