Skip to main content
Code Review

Return to Revisions

2 of 8
timing results shouldn't include `bwt`
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210

1. Introduction

The only actual question you asked is about how to make the inverse Burrows–Wheeler transform go faster. But as you'll see below, that's plenty for one question here on Code Review. If you have anything to ask about your LZ77 and LZ78 implementations, I suggest you start a new question.

2. A bug

First, the output of your function ibwt doesn't match the example output in your question:

>>> from pprint import pprint
>>> pprint(ibwt('ssyxxiit'))
['ixsixtys',
 'ixtysixs',
 'sixsixty',
 'sixtysix',
 'tysixsix',
 'xsixtysi',
 'xtysixsi',
 'ysixsixt']

(You should always prepare your question by copying and pasting input and output from your source files and from an interactive session, rather than typing in what you think it should be. You can easily introduce or cover up mistakes if you do the latter.)

So it looks as if you are missing a transpose:

>>> pprint(transpose(ibwt('ssyxxiit')))
['iisstxxy',
 'xxiiysts',
 'stxxsiyi',
 'iystixsx',
 'xsiyxtis',
 'tixssyxi',
 'yxtiissx',
 'ssyxxiit']

3. Avoiding transpositions

With that bug fixed, let's see how long your function takes on a medium-sized amount of data:

>>> import random, string
>>> from timeit import timeit
>>> s = ''.join(random.choice(string.printable) for _ in xrange(1024))
>>> t = bwt(s)
>>> timeit(lambda:ibwt(t[1]), number=1)
49.83987808227539

Not so good! What can we change? Well, an obvious idea is to cut out all those calls to transpose. At the moment you transpose your matrix in order to sort it, and then transpose it back again each time round the loop. By keeping the matrix in transposed form throughout, you can avoid having to call transpose at all:

def ibwt2(s):
 n = len(s)
 m = [''] * n
 for _ in xrange(n):
 m = sorted(s[i] + m[i] for i in xrange(n))
 return m

That simple change results in a big improvement:

>>> s = ''.join(random.choice(string.printable) for _ in xrange(1024))
>>> t = bwt(s)
>>> timeit(lambda:ibwt2(t[1]), number=1)
0.9414288997650146

4. Aside on programming style

Something that's perhaps worth mentioning at this point is that you seem quite keen on transforming your code so as to use a functional style (for example in this question, where you wanted to express a loop using reduce). Transforming code into pure functional form is an excellent intellectual exercise, but you have to be aware of two problems that often arise:

  1. Pure functional programs can easily end up doing a lot of allocation and copying. For example, it is often tempting to transform your data into a format suitable for passing in to a function (rather than transforming your function so that it operates on the data you have). Thus in your ibwt function it is convenient to call sorted on the data in row-major order, but convenient to append a new instance of the string s in column-major order. In order to express both of these operations in the most convenient way, you have to transpose the matrix back and forth, and since you do this functionally, each transposition has to copy the whole matrix.

  2. Pursuit of pure functional style often results in tacit or point-free programming where variable names are avoided and data is routed from one function to another using combinators. This can seem very elegant, but variable names have two roles in a program: they don't just move data around, they also act as mnemonics to remind programmers of what the data means. Point-free programs can end up being totally mysterious because you lose track of exactly what is being operated on.

Anyway, aside over. Can we make any more progress on the inverse Burrows–Wheeler transform?

5. An efficient implementation

If you look at what the inverse Burrows–Wheeler transform does, you'll see that it repeatedly permutes an array of strings (by sorting them). The permutation is exactly the same at each step (exercise: prove this!). For example, if we are given the string SSYXXIIT, then at each step we apply the permutation SSYXXIITIISSTXXY, which (taking note of where repeated letters go) is the permutation 0123456756017342.

This observation allows us to reconstruct a row of the matrix without having to compute the whole matrix. In this example we want to reconstruct row number 3. Well, we know that the final matrix looks like this:

I.......
I.......
S.......
S*......
T.......
X.......
X.......
Y.......

so the first element of the row is S. What's the next letter of this row (marked with *)? Where did this come from? Well, we know that the last step of the transform must have looked like this:

S0...... I5......
S1...... I6......
Y2...... -> S0......
X3...... -> S1......
X4...... -> T7......
I5...... -> X3......
I6...... X4......
T7...... Y2......

So the * must have come from row 1 (which is the image of 3 under the permutation), and so it must be I (which is element number 1 of the sorted string IISSTXXY).

We can follow this approach all the way along the row. Here's an implementation in Python:

def ibwt3(k, s):
 """
 Return the `k`th row of the inverse Burrows-Wheeler transform
 matrix for the string `s`.
 >>> ibwt3(3, 'SSYXXIIT')
 'SIXTYSIX'
 """
 def row():
 permutation = sorted((t, i) for i, t in enumerate(s))
 p = permutation[k]
 for _ in range(len(s)):
 yield p[0]
 p = permutation[p[1]]
 return ''.join(row())

Let's check that works:

>>> ibwt3(3, 'SSYXXIIT')
'SIXTYSIX'
>>> s = ''.join(random.choice(string.printable) for _ in xrange(1024))
>>> t = bwt(s)
>>> ibwt3(*t) == s
True

and is acceptably fast:

>>> timeit(lambda:ibwt3(*t), number=1)
0.0013821125030517578

(If you compare my ibwt3 to the sample Python implementation on Wikipedia you'll see that mine is not only much simpler, but also copes with arbitrary characters, not just bytes in the range 0–255. So either I've missed something important, or the Wikipedia article could be improved.)

Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210
default

AltStyle によって変換されたページ (->オリジナル) /