Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64624
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; '"""': 0.07; 'string': 0.09; '[0,': 0.09; '[0]': 0.09; 'append': 0.09; 'ascii': 0.09; 'integers': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sequences.': 0.09; 'subject:string': 0.09; 'python': 0.11; 'def': 0.12; "'0'": 0.16; '1):': 0.16; '50)': 0.16; 'alphabet': 0.16; 'better?': 0.16; 'outputs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subsequences': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'algorithms.': 0.24; 'fine': 0.24; 'header:X-Complaints-To:1': 0.27; "skip:' 10": 0.31; 'url:wiki': 0.31; 'accomplished': 0.31; 'factor': 0.31; 'sep': 0.31; 'url:wikipedia': 0.31; 'this.': 0.32; 'option': 0.32; 'could': 0.34; 'convert': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'add': 0.35; 'sequence': 0.36; 'entry': 0.36; 'method': 0.36; 'url:org': 0.36; 'changing': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'generating': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'length': 0.61; 'more': 0.64; 'limit': 0.70; 'below.': 0.71; 'computation.': 0.84; 'n):': 0.84; 'questions;': 0.84; 'ugly,': 0.84; 'time)': 0.91; 'wanting': 0.93; '2013': 0.98 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: generate De Bruijn sequence memory and string vs lists |
| Date | Thu, 23 Jan 2014 18:31:31 +0100 |
| Organization | None |
| References | <CALyJZZVU+j64Jn7fqqzLmwW+KcM=43UMKjo=HW7umnq8MSP2Uw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508487ec.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5901.1390498260.18130.python-list@python.org> (permalink) |
| Lines | 102 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1390498260 news.xs4all.nl 2895 [2001:888:2000:d::a6]:53995 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64624 |
Show key headers only | View raw
Vincent Davis wrote:
> For reference, Wikipedia entry for De Bruijn sequence
> http://en.wikipedia.org/wiki/De_Bruijn_sequence
>
> At the above link is a python algorithm for generating De Brujin
> sequences. It works fine but outputs a list of integers [0, 0, 0, 1, 0, 1,
> 1, 1] and I would prefer a string '00010111'. This can be accomplished by
> changing the last line from;
> return sequence
> to
> return ''.join([str(i) for i in sequence])
> See de_bruijn_1 Below.
>
> The other option would be to manipulate strings directly (kind of).
> I butchered the original algorithm to do this. See de_bruijn_2 below. But
> it is much slower and ungly.
>
> I am wanting to make a few large De Bruijin sequences. hopefully on the
> order of de_bruijn(4, 50) to de_bruijn(4, 100) (wishful thinking?). I
> don't know the limits (memory or time) for the current algorithms. I think
> I am will hit the memory mazsize limit at about 4^31. The system I will be
> using has 64GB RAM.
> The size of a De Brujin sequence is k^n
>
> My questions;
> 1, de_bruijn_2 is ugly, any suggestions to do it better?
> 2, de_bruijn_2 is significantly slower than de_bruijn_1. Speedups?
> 3, Any thought on which is more memory efficient during computation.
>
> #### 1 ####
> def de_bruijn_1(k, n):
> """
> De Bruijn sequence for alphabet size k (0,1,2...k-1)
> and subsequences of length n.
> From wikipedia Sep 22 2013
> """
> a = [0] * k * n
> sequence = []
> def db(t, p,):
> if t > n:
> if n % p == 0:
> for j in range(1, p + 1):
> sequence.append(a[j])
> else:
> a[t] = a[t - p]
> db(t + 1, p)
> for j in range(int(a[t - p]) + 1, k):
> a[t] = j
> db(t + 1, t)
> db(1, 1)
> #return sequence #original
> return ''.join([str(i) for i in sequence])
>
> d1 = de_bruijn_1(4, 8)
>
> #### 2 ####
> def de_bruijn_2(k, n):
> global sequence
> a = '0' * k * n
> sequence = ''
> def db(t, p):
> global sequence
> global a
> if t > n:
> if n % p == 0:
> for j in range(1, p + 1):
> sequence = sequence + a[j]
> else:
> a = a[:t] + a[t - p] + a[t+1:]
> db(t + 1, p)
> for j in range(int(a[t - p]) + 1, k):
> a = a[:t] + str(j) + a[t+1:]
> db(t + 1, t)
> return sequence
> db(1, 1)
> return sequence
>
> d2 = de_bruijn_2(4, 8)
You could change de_bruijn_1() to use `bytearray`s instead of `list`s:
# Python 2
def debruijn(k, n):
a = k * n * bytearray([0])
sequence = bytearray()
append = sequence.append # factor out method lookup
def db(t, p,):
if t > n:
if n % p == 0:
for j in xrange(1, p + 1):
append(a[j]+48) # add 48 to convert to ascii
else:
a[t] = a[t - p]
db(t + 1, p)
for j in xrange(a[t - p] + 1, k):
a[t] = j
db(t + 1, t)
db(1, 1)
return sequence
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: generate De Bruijn sequence memory and string vs lists Peter Otten <__peter__@web.de> - 2014-01-23 18:31 +0100
csiph-web