<s>
In	O
combinatorial	O
mathematics	O
,	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
of	O
order	O
n	O
on	O
a	O
size-k	O
alphabet	O
A	O
is	O
a	O
cyclic	O
sequence	O
in	O
which	O
every	O
possible	O
length-n	O
string	O
on	O
A	O
occurs	O
exactly	O
once	O
as	O
a	O
substring	O
(	O
i.e.	O
,	O
as	O
a	O
contiguous	O
subsequence	O
)	O
.	O
</s>
<s>
And	O
since	O
has	O
exactly	O
symbols	O
,	O
De	B-Language
Bruijn	I-Language
sequences	I-Language
are	O
optimally	O
short	O
with	O
respect	O
to	O
the	O
property	O
of	O
containing	O
every	O
string	O
of	O
length	O
n	O
at	O
least	O
once	O
.	O
</s>
<s>
As	O
he	O
later	O
wrote	O
,	O
the	O
existence	O
of	O
de	B-Language
Bruijn	I-Language
sequences	I-Language
for	O
each	O
order	O
together	O
with	O
the	O
above	O
properties	O
were	O
first	O
proved	O
,	O
for	O
the	O
case	O
of	O
alphabets	O
with	O
two	O
elements	O
,	O
by	O
.	O
</s>
<s>
Automata	B-Application
for	O
recognizing	O
these	O
sequences	O
are	O
denoted	O
as	O
de	O
Bruijn	O
automata	B-Application
.	O
</s>
<s>
The	O
earliest	O
known	O
example	O
of	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
comes	O
from	O
Sanskrit	O
prosody	O
where	O
,	O
since	O
the	O
work	O
of	O
Pingala	O
,	O
each	O
possible	O
three-syllable	O
pattern	O
of	O
long	O
and	O
short	O
syllables	O
is	O
given	O
a	O
name	O
,	O
such	O
as	O
'	O
y	O
 '	O
for	O
short	O
–	O
long	O
–	O
long	O
and	O
'	O
m	O
 '	O
for	O
long	O
–	O
long	O
–	O
long	O
.	O
</s>
<s>
This	O
mnemonic	O
,	O
equivalent	O
to	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
on	O
binary	O
3-tuples	O
,	O
is	O
of	O
unknown	O
antiquity	O
,	O
but	O
is	O
at	O
least	O
as	O
old	O
as	O
Charles	O
Philip	O
Brown	O
's	O
1869	O
book	O
on	O
Sanskrit	O
prosody	O
that	O
mentions	O
it	O
and	O
considers	O
it	O
"	O
an	O
ancient	O
line	O
,	O
written	O
by	O
Pāṇini	O
"	O
.	O
</s>
<s>
Karl	O
Popper	O
independently	O
describes	O
these	O
objects	O
in	O
his	O
The	O
Logic	O
of	O
Scientific	O
Discovery	O
(	O
1934	O
)	O
,	O
calling	O
them	O
"	O
shortest	B-Language
random-like	I-Language
sequences	I-Language
"	O
.	O
</s>
<s>
The	O
de	B-Language
Bruijn	I-Language
sequences	I-Language
can	O
be	O
constructed	O
by	O
taking	O
a	O
Hamiltonian	O
path	O
of	O
an	O
n-dimensional	O
de	O
Bruijn	O
graph	O
over	O
k	O
symbols	O
(	O
or	O
equivalently	O
,	O
an	O
Eulerian	O
cycle	O
of	O
an	O
(	O
n1	O
)	O
-dimensional	O
de	O
Bruijn	O
graph	O
)	O
.	O
</s>
<s>
An	O
inverse	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
can	O
be	O
used	O
to	O
generate	O
the	O
required	O
Lyndon	O
words	O
in	O
lexicographic	O
order	O
.	O
</s>
<s>
De	B-Language
Bruijn	I-Language
sequences	I-Language
can	O
also	O
be	O
constructed	O
using	O
shift	B-General_Concept
registers	I-General_Concept
or	O
via	O
finite	O
fields	O
.	O
</s>
<s>
Goal	O
:	O
to	O
construct	O
a	O
B(2,4 )	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
of	O
length	O
24	O
=	O
16	O
using	O
Eulerian	O
(	O
n−1	O
=	O
4−1	O
=	O
3	O
)	O
3-D	O
de	O
Bruijn	O
graph	O
cycle	O
.	O
</s>
<s>
If	O
one	O
traverses	O
the	O
edge	O
labeled	O
1	O
from	O
000	O
,	O
one	O
arrives	O
at	O
001	O
,	O
thereby	O
indicating	O
the	O
presence	O
of	O
the	O
subsequence	O
0001	O
in	O
the	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
.	O
</s>
<s>
This	O
corresponds	O
to	O
the	O
following	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
:	O
</s>
<s>
Mathematically	O
,	O
an	O
inverse	O
Burrows	B-Algorithm
—	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
on	O
a	O
word	O
generates	O
a	O
multi-set	O
of	O
equivalence	O
classes	O
consisting	O
of	O
strings	O
and	O
their	O
rotations	O
.	O
</s>
<s>
These	O
equivalence	O
classes	O
of	O
strings	O
each	O
contain	O
a	O
Lyndon	O
word	O
as	O
a	O
unique	O
minimum	O
element	O
,	O
so	O
the	O
inverse	O
Burrows	B-Algorithm
—	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
can	O
be	O
considered	O
to	O
generate	O
a	O
set	O
of	O
Lyndon	O
words	O
.	O
</s>
<s>
It	O
can	O
be	O
shown	O
that	O
if	O
we	O
perform	O
the	O
inverse	O
Burrows	B-Algorithm
—	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
on	O
a	O
word	O
consisting	O
of	O
the	O
size-k	O
alphabet	O
repeated	O
kn−1	O
times	O
(	O
so	O
that	O
it	O
will	O
produce	O
a	O
word	O
the	O
same	O
length	O
as	O
the	O
desired	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
)	O
,	O
then	O
the	O
result	O
will	O
be	O
the	O
set	O
of	O
all	O
Lyndon	O
words	O
whose	O
length	O
divides	O
n	O
.	O
It	O
follows	O
that	O
arranging	O
these	O
Lyndon	O
words	O
in	O
lexicographic	O
order	O
will	O
yield	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
B(k,n )	O
,	O
and	O
that	O
this	O
will	O
be	O
the	O
first	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
in	O
lexicographic	O
order	O
.	O
</s>
<s>
The	O
following	O
method	O
can	O
be	O
used	O
to	O
perform	O
the	O
inverse	O
Burrows	B-Algorithm
—	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
,	O
using	O
its	O
standard	O
permutation	B-Algorithm
:	O
</s>
<s>
Write	O
this	O
permutation	B-Algorithm
in	O
cycle	B-Algorithm
notation	I-Algorithm
with	O
the	O
smallest	O
position	O
in	O
each	O
cycle	O
first	O
,	O
and	O
the	O
cycles	O
sorted	O
in	O
increasing	O
order	O
.	O
</s>
<s>
Each	O
cycle	O
has	O
now	O
become	O
a	O
Lyndon	O
word	O
,	O
and	O
they	O
are	O
arranged	O
in	O
lexicographic	O
order	O
,	O
so	O
dropping	O
the	O
parentheses	O
yields	O
the	O
first	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
.	O
</s>
<s>
For	O
example	O
,	O
to	O
construct	O
the	O
smallest	O
B(2,4 )	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
of	O
length	O
24	O
=	O
16	O
,	O
repeat	O
the	O
alphabet	O
(	O
ab	O
)	O
8	O
times	O
yielding	O
.	O
</s>
<s>
Number	O
the	O
columns	O
as	O
shown	O
so	O
we	O
can	O
read	O
the	O
cycles	O
of	O
the	O
permutation	B-Algorithm
:	O
</s>
<s>
Starting	O
from	O
the	O
left	O
,	O
the	O
Standard	O
Permutation	B-Algorithm
notation	O
cycles	O
are	O
:	O
.	O
</s>
<s>
The	O
following	O
Python	B-Language
code	I-Language
calculates	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
,	O
given	O
k	O
and	O
n	O
,	O
based	O
on	O
an	O
algorithm	O
from	O
Frank	O
Ruskey	O
's	O
Combinatorial	O
Generation	O
.	O
</s>
<s>
De	B-Language
Bruijn	I-Language
cycles	I-Language
are	O
of	O
general	O
use	O
in	O
neuroscience	O
and	O
psychology	O
experiments	O
that	O
examine	O
the	O
effect	O
of	O
stimulus	O
order	O
upon	O
neural	O
systems	O
,	O
and	O
can	O
be	O
specially	O
crafted	O
for	O
use	O
with	O
functional	B-Algorithm
magnetic	I-Algorithm
resonance	I-Algorithm
imaging	I-Algorithm
.	O
</s>
<s>
The	O
symbols	O
of	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
written	O
around	O
a	O
circular	O
object	O
(	O
such	O
as	O
a	O
wheel	O
of	O
a	O
robot	B-Algorithm
)	O
can	O
be	O
used	O
to	O
identify	O
its	O
angle	O
by	O
examining	O
the	O
n	O
consecutive	O
symbols	O
facing	O
a	O
fixed	O
point	O
.	O
</s>
<s>
Gray	B-Device
codes	I-Device
can	O
be	O
used	O
as	O
similar	O
rotary	O
positional	O
encoding	O
mechanisms	O
,	O
a	O
method	O
commonly	O
found	O
in	O
rotary	B-Algorithm
encoders	I-Algorithm
.	O
</s>
<s>
A	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
can	O
be	O
used	O
to	O
quickly	O
find	O
the	O
index	O
of	O
the	O
least	O
significant	O
set	O
bit	O
(	O
"	O
right-most	O
1	O
"	O
)	O
or	O
the	O
most	O
significant	O
set	O
bit	O
(	O
"	O
left-most	O
1	O
"	O
)	O
in	O
a	O
word	O
using	O
bitwise	O
operations	O
and	O
multiplication	O
.	O
</s>
<s>
The	O
following	O
example	O
uses	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
to	O
determine	O
the	O
index	O
of	O
the	O
least	O
significant	O
set	O
bit	O
(	O
equivalent	O
to	O
counting	O
the	O
number	O
of	O
trailing	O
'	O
0	O
 '	O
bits	O
)	O
in	O
a	O
32	O
bit	O
unsigned	O
integer	O
:	O
</s>
<s>
This	O
power	O
of	O
2	O
is	O
multiplied	O
(	O
arithmetic	O
modulo	O
232	O
)	O
by	O
the	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
,	O
thus	O
producing	O
a	O
32-bit	O
product	O
in	O
which	O
the	O
bit	O
sequence	O
of	O
the	O
5	O
MSBs	O
is	O
unique	O
for	O
each	O
power	O
of	O
2	O
.	O
</s>
<s>
The	O
5	O
MSBs	O
are	O
shifted	O
into	O
the	O
LSB	O
positions	O
to	O
produce	O
a	O
hash	O
code	O
in	O
the	O
range	O
[	O
0	O
,	O
31 ]	O
,	O
which	O
is	O
then	O
used	O
as	O
an	O
index	O
into	O
hash	B-Algorithm
table	I-Algorithm
BitPositionLookup	O
.	O
</s>
<s>
The	O
selected	O
hash	B-Algorithm
table	I-Algorithm
value	O
is	O
the	O
bit	O
index	O
of	O
the	O
least	O
significant	O
set	O
bit	O
in	O
v	O
.	O
</s>
<s>
In	O
the	O
above	O
example	O
an	O
alternative	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
(	O
0x06EB14F9U	O
)	O
is	O
used	O
,	O
with	O
corresponding	O
reordering	O
of	O
array	O
values	O
.	O
</s>
<s>
The	O
choice	O
of	O
this	O
particular	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
is	O
arbitrary	O
,	O
but	O
the	O
hash	B-Algorithm
table	I-Algorithm
values	O
must	O
be	O
ordered	O
to	O
match	O
the	O
chosen	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
.	O
</s>
<s>
A	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
can	O
be	O
used	O
to	O
shorten	O
a	O
brute-force	O
attack	O
on	O
a	O
PIN-like	O
code	O
lock	O
that	O
does	O
not	O
have	O
an	O
"	O
enter	O
"	O
key	O
and	O
accepts	O
the	O
last	O
n	O
digits	O
entered	O
.	O
</s>
<s>
f-fold	O
n-ary	O
de	O
Bruijn	O
sequence'''	O
is	O
an	O
extension	O
of	O
the	O
notion	O
n-ary	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
,	O
such	O
that	O
the	O
sequence	O
of	O
the	O
length	O
contains	O
every	O
possible	O
subsequence	O
of	O
the	O
length	O
n	O
exactly	O
f	O
times	O
.	O
</s>
<s>
For	O
example	O
,	O
for	O
the	O
cyclic	O
sequences	O
11100010	O
and	O
11101000	O
are	O
two-fold	O
binary	O
de	B-Language
Bruijn	I-Language
sequences	I-Language
.	O
</s>
<s>
The	O
number	O
of	O
two-fold	O
de	B-Language
Bruijn	I-Language
sequences	I-Language
,	O
for	O
is	O
,	O
the	O
other	O
known	O
numbers	O
are	O
,	O
,	O
and	O
.	O
</s>
<s>
Computing	O
the	O
position	O
of	O
a	O
particular	O
unique	O
tuple	O
or	O
matrix	O
in	O
a	O
de	B-Language
Bruijn	I-Language
sequence	I-Language
or	O
torus	O
is	O
known	O
as	O
the	O
de	O
Bruijn	O
Decoding	O
Problem	O
.	O
</s>
