in reply to Generic De Bruijn Sequence

Other replies mentioned the "prefer ones" algorithm. If you think, instead, of it as a "prefer last" algorithm, it appears to work for your stated problem. Start with T of the first character. Then try to add the last character to the answer, moving from last character to second last character to third last character, etc., as long as a repetition exists.

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1188292 use strict; use warnings; my $n = shift // 3; # alphabet size my $t = shift // $n; # size of tuples my @alphabet = ('A'..'Z', '0'..'9', 'a'..'z'); my %previous; @previous{@alphabet[1..$#alphabet]} = @alphabet; # last to 2nd last, e +tc. my $need = $n ** $t + $t - 1; # length of the answer $_ = $alphabet[0] x $t; # start string with first chars printf "%77s\n", $_ while s/^ (?=(.{$t})) (?=.+\1) . /$previous{$&}/x # prev char if repeat or $need > length && s/^/$alphabet[$n - 1]/; # or add last char print; # the answer my $chars = join '', @alphabet[0..$n-1]; # test if valid /^[$chars]+$/ or die "invalid character"; my %all; $all{$1}++ while /(?=(.{$t}))/g; print "\nwant @{[ $n ** $t ]} unique tuples, have @{[ scalar keys %all + ]}"; print "solution passes tests";

I haven't found a case yet where backtracking is required.

Replies are listed 'Best First'.
Re^2: Generic De Bruijn Sequence
by LanX (Saint) on Apr 20, 2017 at 16:44 UTC
    Is this a guess or do you have a formal proof?

    Took me a while to understand why prefer "ones succeeds", and AFAIK only if the alphabet is a power of two.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      It's a guess.

      Does "prefer ones" succeed regardless of the alphabet size?

      My solution is, I think, a "prefer last" solution. Testing a few basic, low valued, parameters appears to give the correct answer.

      Perhaps if someone has a good head for this, they could suggest some parameter values that break under "prefer last" (or indeed, any other method). I don't have the time to tinker, and besides, I'm not that good at poking around the edges.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        I don't know what this "prefer last" is exactly supposed to mean. But there is such a thing as lexicographically minimal de Bruijn sequence. It becomes lexicographically maximal when you reverse the alphabet. So I'm wondering if you might obtain the desired output by simply remapping the symbols of MinimalDeBruijn($alphabet_size).