De Bruijn Sequence

Back to Tools

Overview

The initial application for the de Bruijn sequance for me was a solution posted as comment to an Youtube video from Robin (8-Bit Show And Tell channel). At that time I wasn't aware of the de Bruijn sequence at all. I just developed the idea to efficently store all possible sequences in a compact way. My first attempt with length 3 was easy to generate, just by trail-and-error. I saw there was space for a length 4 sequence too and even there I created the sequance manually.
After the post someone commented that this is a well-known sequence called the de Bruijn sequence.

The application and comment posting

Another boost:

0 a=4:a$="...MNMNNNNMMMMNNMNMMNM":forb=16to.step.:printmid$(a$,a+b*rnd(.),a);next

(M is the shift+m graphic symbol), N is the shift+n graphic symbol)

The M is the graphic backslash, N the graphic slash (shifted M, N on the keyboard). The se quence gives all 16 possible variations (2^4) of 2 symbols on 4 places by indexing from 4 to 19 (16 + 3 symbols). This index is randomized, selecting one of the 16 different 4 char acter sequences.

The dots are a filler to be able to use variable A (primarily needed as string length) als o as some offset, because MID$ needs something > 0. A is defined earlier for faster access , because it is referenced twice, A$ and B only once.
This takes on my PAL C64 171 jiffies or 2.85 seconds.

This seems the maximum possible if keyed in via screen editor (with abbreviations). If we dismiss the line limit of the screen editor this game could played further as far as we keep A$ limited to the maximum of 255 characters (the line length is unlimited, but a re-chaining of the program must not happen).
If an appropriate A$ can be composed, the a possible for A and B would be 7 and 128, givin g a length of 140 for A$.
This task is left to the readers (or Robin), to push it to the extremes ... ;)

Just for comparison and analysis: Saw @csbruce's MID$ approach later skimming through the comments, but it is limited to a 3 character sequence. The above one accomplishes a 4 char acter sequence by using a overlapping structure of all needed 4 char sequences which have 1 index distance (saving a multiplication too).


Implementation of de Bruijn

The BASIC V2 implementation is not recursive as the high-level implementions on geeksforgeeks.org. To come over recursions, I replaced it with stacks organized as an array. The recursion cannot be replace with a simple loop because it's not a tail-end-recursion.

The first implementation is string-based but it has severe limits because of the maximum length with 255 characters The numerical variant (de-bruijn-a.bas) uses integer arrays, to get an element range limited only by the free Basic memory, which enhances the possible solutions depending on length and base.

See Wikipedia: de Bruijn sequance (Wikipedia: De-Bruijn-Folge - german)

Download

Disk images

  1. de-bruijn.d64
    D64 image with all BASIC V2 programs generating the the de Bruijn sequence (from below)

Sources

  1. de-bruijn-a.bas
    General base (from 2 to 36) implementation of de Bruijn sequence in BASIC V2
    Numeric-based, for base 2 it is limited to length 12, for base 16 length 3 and 36 may not exceed length 1.
    Setup the length in line 100 and the base in line 102.
    Remove line 330 and 351 to prevent the tracing output to make the execution faster.
    Timing: (PAL)
    Base 16 length 2: 03'03"
    Base 2 length 7: 00'39"
    Base 2 length 12: 30'04"
  2. de-bruijn-g.bas
    General base (from 2 to 36) implementation of de Bruijn sequence in BASIC V2
    String-based, for base 2 it is limited to length 7, for base 16 or larger, up to 36 only length 1 is possible.
    Setup the length in line 100 and the base in line 102.
  3. de-bruijn-2.bas
    Base 2 optimized implementation (string based) of de Bruijn sequence in BASIC V2
    String-based, but limited to length 7 (variable L in line 100).
    Timing: (PAL)
    Base 2 length 7: 00'39"
  4. de-bruijn-exp.bas
    Experimental implementation (string based) of de Bruijn sequence in BASIC V2
Autor: Johann E. Klasek
Copyright notice: The programs and files could be freely used, copied and distributed as long the author's name is preserved.

 


Best viewed with any browser zurück zur Startseite