Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Forked from rgov/debruijn.py
Created April 20, 2022 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancejpollard/614275d509a113823f098567c14251fb to your computer and use it in GitHub Desktop.
Save lancejpollard/614275d509a113823f098567c14251fb to your computer and use it in GitHub Desktop.
de Bruijn sequence generator
def deBruijn(n, k):
'''
An implementation of the FKM algorithm for generating the de Bruijn
sequence containing all k-ary strings of length n, as described in
"Combinatorial Generation" by Frank Ruskey.
'''
a = [ 0 ] * (n + 1)
def gen(t, p):
if t > n:
for v in a[1:p + 1]:
yield v
else:
a[t] = a[t - p]
for v in gen(t + 1, p):
yield v
for j in xrange(a[t - p] + 1, k):
a[t] = j
for v in gen(t + 1, t):
yield v
return gen(1, 1)
if __name__ == '__main__':
print ''.join([ chr(ord('A') + x) for x in deBruijn(3, 26) ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment