r/dailyprogrammer • u/jnazario 2 0 • Jul 06 '16
[2016-07-06] Challenge #274 [Intermediate] Calculating De Bruijn sequences
Description
In combinatorial mathematics, a k-ary De Bruijn sequence B(k, n) of order n, named after the Dutch mathematician Nicolaas Govert de Bruijn, is a cyclic sequence of a given alphabet A with size k for which every possible subsequence of length n in A appears as a sequence of consecutive characters exactly once. At the terminus, you "wrap" the end of the sequence around to the beginning to get any remaining subsequences.
Each B(k, n) has length k^n.
A De Bruijn sequence B(2, 3) (with alphabet 0 and 1) is therefore:
00010111Similarly, B("abcd", 2) (with alphabet "a", "b", "c", and "d") is therefore:
aabacadbbcbdccddFor those sequences of length, every trigram (for the former case) or bigram (for the latter case) is represented in the result.
De Bruijn sequences have various applications, including in PIN pad testing and rotor angle calculation.
Input Description
You'll be given two inputs k and n, the first is an integer or a a string of unique characters, the second is the length of the subsequences to ensure are encoded.
Output Description
Your program should emit a string that encodes the De Bruijn sequence.
Input
5 3
2 4
abcde 4Output
The outputs expected for those (in order) are:
00010020030040110120130140210220230240310320330340410420430441112113114122123124132133134142143144222322423323424324433343444
0000100110101111
aaaabaaacaaadaaaeaabbaabcaabdaabeaacbaaccaacdaaceaadbaadcaaddaadeaaebaaecaaedaaeeababacabadabaeabbbabbcabbdabbeabcbabccabcdabceabdbabdcabddabdeabebabecabedabeeacacadacaeacbbacbcacbdacbeaccbacccaccdacceacdbacdcacddacdeacebacecacedaceeadadaeadbbadbcadbdadbeadcbadccadcdadceaddbaddcadddaddeadebadecadedadeeaeaebbaebcaebdaebeaecbaeccaecdaeceaedbaedcaeddaedeaeebaeecaeedaeeebbbbcbbbdbbbebbccbbcdbbcebbdcbbddbbdebbecbbedbbeebcbcbdbcbebcccbccdbccebcdcbcddbcdebcecbcedbceebdbdbebdccbdcdbdcebddcbdddbddebdecbdedbdeebebeccbecdbecebedcbeddbedebeecbeedbeeeccccdccceccddccdeccedcceecdcdcecdddcddecdedcdeececeddcedeceedceeeddddeddeededeeee
4
u/JakDrako Jul 06 '16
C#
Output
3
u/Godspiral 3 3 Jul 06 '16
in J, by Henry Rich (commented lyndon word approach)
2
u/skeeto -9 8 Jul 06 '16
C, generating the sequence by concatenating Lyndon words.
2
u/gabyjunior 1 2 Jul 06 '16
C, concatenating Lyndon words of length dividing De Bruijn sequence order.
Takes custom digits/order as program arguments.
Output
2
u/mbdomecq Jul 07 '16
C++. I don't know what Lyndon words are so I just used a depth-first search.
2
u/augus7 Jul 07 '16
Man, that was time-consuming for me. Read about De Bruijn sequences and Lyndon words first before coding...
I used Duval's algorithm to generate Lyndon words then concatenated them.
Python:
Output
2
u/skratz17 Jul 09 '16 edited Jul 10 '16
Java
Probably not the best way, but it works.
EDIT: Improved my generation of all sequences greatly by "incrementing" sequences (using the given alphabet's ordering). Also remove sequences already present in the DeBruijn string from all sequences list more efficiently (by sorting sequences list and using binary search). This solution is still waaaay worse than several others here, but is a very obvious way to solve this problem (albeit waaaay too complex, particularly in terms of memory concerns), and I didn't see this approach in any other submission (...and probably for good reason).
1
u/thorwing Jul 06 '16
JAVA 8
Not the same Sequence as you, but a "De Bruijn" sequence nonetheless.
OUTPUT
1
u/mikeciavprog Jul 06 '16 edited Jul 06 '16
PHP, using the Lyndon word concatenation method
Edit to include output:
1
u/Say_What1 Jul 07 '16 edited Jul 07 '16
Python 2.7
Output:
1
u/wcastello Jul 07 '16 edited Sep 04 '25
A moment is captured in silence.
1
u/Scroph 0 0 Jul 07 '16
D (dlang) solution. Generates Lyndon words and concatenates those whose length divides N.
1
u/[deleted] Jul 09 '16 edited Jun 11 '17
deleted 0.7012 What is ^^^this?
1
u/SirCinnamon Jul 19 '16
Java, using Duval. Feedback is welcome!
https://github.com/sircinnamon/DeBruijn/blob/master/Bruijn.java
1
u/jeaton Jul 27 '16
JavaScript ES6: