arXiv:2611.01874cs.DSNov 2026

Manuscript Offprint

arXiv:2611.01874

Efficient Lempel-Ziv Prediction with the Run-Length BWT

Ohad Avnery

Abstract

Lempel-Ziv Plurality (LZP) is an algorithm of Kosoy that predicts the next symbol of a string by using its Lempel-Ziv factorization, and makes at most O(zlog⁡n)O(z \log n) mistakes on every string of length nn with zz LZ77 phrases. We give efficient online implementations of LZP and of a new variant of it. Our first implementation maintains a run-length encoded Burrows-Wheeler transform (RLBWT) of the reversed history. It implements LZP, uses O(zlog⁡zlog⁡2n)O(z \log z \log^2 n) bits, and has worst-case update and prediction times O(log⁡z+log⁡log⁡n)O(\log z + \log \log n) and O(σ(log⁡z+log⁡log⁡n))O(\sigma(\log z + \log \log n)), respectively, where σ\sigma is the size of the alphabet. This is an exponential improvement over Kosoy's implementation, whose update time is O(zlog⁡7n)O(z \log^7 n). We then introduce the Lempel-Ziv Majority (LZM) predictor, which has the same mistake bound and update time, and predicts in O(1)O(1) time independently of σ\sigma. These two predictors accept new symbols at any time, without knowing the alphabet in advance. For an alphabet known in advance, a different data structure gives O(σ(1+log⁡z/log⁡log⁡n))O(\sigma(1 + \log z / \log \log n)) time per symbol, which is constant when σ=O(1)\sigma = O(1) and z=polylog(n)z = \text{polylog}(n).

Keywords: online learning, sequence prediction, mistake bounds, Lempel-Ziv factorization, Burrows-Wheeler transform, dynamic data structures

1. Introduction

Online sequence prediction is a classical problem in machine learning and algorithms. A predictor reads a sequence one symbol at a time, each time guessing the next symbol, then reading the true symbol and updating its state. What one would like is a single algorithm that is at once computationally efficient, general-purpose (able to learn a wide range of patterns from many domains), and equipped with a formal mistake bound that guarantees a small number of prediction mistakes for the sequences we care about. Unfortunately, we do not know of any predictor that satisfies all three properties.

The theoretical gold standard for sequence prediction is Solomonoff induction (Hutter et al., 2024), which gives higher weight to sequences with small Kolmogorov complexity: those generated by short computer programs. This predictor satisfies the second and third requirements, with guaranteed mistake bounds on all computable data; however, Solomonoff induction is itself uncomputable, as is the notion of Kolmogorov complexity it is based on. In order to build efficient predictors, we should relax the notion of string complexity to simpler patterns that are easier to recognize efficiently.

A natural repetitiveness measure is the size of the LZ77 parse: for a string x,z(x)x, z(x) is defined to be the number of phrases in its LZ77 factorization. This measure captures highly repetitive strings, which can be built by reusing and copying previous terms. The broader Lempel-Ziv approach was in fact first proposed as a computationally tractable stand-in for Kolmogorov complexity (Lempel and Ziv, 1976), and it has considerable expressive power: many rich families of deterministic sequences studied in combinatorics on words (Allouche and Shallit, 2003), such as automatic sequences, Sturmian words and many morphic sequences, have LZ77 parse size only logarithmic in their length

(Kosoy, 2026a; Navarro, 2022), and are therefore well-predicted by any algorithm whose error scales with zz. Predicting strings with small z(x)z(x) well can thus serve as a computable toy analogue of the Solomonoff ideal.

Kosoy's Lempel-Ziv Plurality predictor (LZP) does exactly this (Kosoy, 2026a). At each step it takes the current LZ77 phrase, finds the earlier places in the history where that phrase has occurred, and predicts the symbol that most often followed them. LZP makes at most O(zlog⁡n)O(z \log n) mistakes on any length-nn sequence, so a highly repetitive sequence is predicted with correspondingly few errors. However, the given implementation of LZP reconstructs an auxiliary compressed index on each symbol's update and prediction, giving O(zlog⁡7n)O(z \log^7 n) time for each operation when the alphabet is constant (Kosoy, 2026a, Section D.2.3).

Our first contribution removes this cost. Rather than storing the LZ77 factorization explicitly, we maintain a run-length encoded Burrows-Wheeler transform (RLBWT) of the reversed history. In this representation, all occurrences of a given phrase form a single contiguous interval, and the symbols that follow them can be counted efficiently using rank queries. Extending the current phrase by one symbol is a single incremental update. This implements LZP, while reducing the time of a single update to O(log⁡z+log⁡log⁡n)O(\log z + \log \log n).

Our second contribution changes the prediction rule itself to make prediction faster. LZP prediction calculates the exact plurality in a range by iterating over all distinct symbols, costing an extra factor of the alphabet size σ\sigma. We define a weaker prediction rule that is only guaranteed to predict a symbol if it has over half of the votes. The resulting Lempel-Ziv Majority predictor (LZM) inherits the same mistake bound as LZP. It calculates the vote majority with a single query, using Boyer-Moore cancellation. This allows prediction to run as fast as updates, and precalculating prediction makes prediction time O(1)O(1).

Our third contribution gives a faster implementation for small, fixed alphabets. Our predictors only need a small interface of a dynamic run-length encoded string that the dynamic RLBWT is implemented over. Each RLBWT update only requires local changes to the runs. This allows us to use the dynamic partial sums structure of Bille et al. (2018). The resulting LZP implementation does updates and predictions in O(σ(1+log⁡z/log⁡log⁡n))O(\sigma(1 + \log z / \log \log n)) worst-case time. In particular, we achieve worst-case constant time when σ=O(1)\sigma = O(1) and z=polylog(n)z = \text{polylog}(n).

None of these implementations is fastest for all parameters (Table 1). In Section 5 we ask for a single dynamic run-length encoded string with range-majority queries whose operations all take O(1+log⁡r/log⁡w)O(1 + \log r / \log w) worst-case time, where rr is the number of runs and ww is the word size. Such a structure would make LZM run in constant time per symbol whenever z=polylog(n)z = \text{polylog}(n), for any alphabet. We believe that such a structure exists, and leave a construction for future work.