Implementation of algorithm W by  Martin Grabmüller