Lempel–Ziv sliding-window parsing (LZ77) and the exhaustive history (LZ76) #
A phrase u is reproducible from the text h already written, within window W, if it can be
written by copying symbols one at a time from d positions back, for some 1 ≤ d ≤ W with
d ≤ |h|. The copy may run into u itself: aaaa is reproducible from a with d = 1. A
history of x is a factorization x = u₁ ⋯ u_c into nonempty phrases each of which is
reproducible from u₁ ⋯ u_{i-1} except possibly for its last symbol ([LZ76], where the
window is unbounded; [ZL77] with a finite window).
The greedy (exhaustive) parse takes as each phrase the longest reproducible prefix of the rest
of the input followed by one more symbol (or the whole rest, if it is reproducible). For example,
with an unbounded window, ABBABBABBBAABABAA parses as A, B, BA, BBABBB, AA, BABA, A, and
aaaaaaaa as a, aaaaaaa (an overlapping copy). Each phrase is encoded as a Phrase codeword
⟨d, ℓ, a⟩: copy ℓ symbols from distance d, then write a.
Scope: the window bounds only how far back a copy may start. As in [LZ76], a copy may be arbitrarily long; the lookahead buffer of [ZL77], which also bounds the phrase length, and its fixed-width codewords are not modelled. The functions are list-based reference implementations, and no running time is claimed.
Main definitions #
Cslib.Algorithms.Lean.LempelZiv.LZ77.Phrase: an LZ77 codeword(offset, length, next).Cslib.Algorithms.Lean.LempelZiv.LZ77.Reproducible:uis a copy from the texth, within the windowW, possibly overlapping itself.Cslib.Algorithms.Lean.LempelZiv.LZ77.IsHistory: a sequence of nonempty phrases, each reproducible except for its last symbol.Cslib.Algorithms.Lean.LempelZiv.LZ77.IsExhaustive: no phrase but the last is reproducible.Cslib.Algorithms.Lean.LempelZiv.LZ77.parse: the greedy parse.Cslib.Algorithms.Lean.LempelZiv.LZ77.encode,Cslib.Algorithms.Lean.LempelZiv.LZ77.decode: the(offset, length, next)code. The decoder copies symbol by symbol and does not depend on the window.Cslib.Algorithms.Lean.LempelZiv.LZ77.complexity: the Lempel–Ziv complexity, the number of phrases of the greedy parse with an unbounded window.
Main results #
Cslib.Algorithms.Lean.LempelZiv.LZ77.flatten_parse,Cslib.Algorithms.Lean.LempelZiv.LZ77.isHistory_parse,Cslib.Algorithms.Lean.LempelZiv.LZ77.isExhaustive_parse: the greedy parse is an exhaustive history of its input.Cslib.Algorithms.Lean.LempelZiv.LZ77.decode_encode: the code is lossless.Cslib.Algorithms.Lean.LempelZiv.LZ77.length_parse_le_of_isHistory: the greedy parse has the fewest phrases among all histories with the same window ([LZ76]).Cslib.Algorithms.Lean.LempelZiv.LZ77.parse_eq_of_isExhaustive: the exhaustive history is unique.
References #
The decoder #
An LZ77 codeword (offset, length, next): copy length symbols starting offset positions
before the end of the text written so far (0 when length = 0), then append next.
- offset : ℕ
Distance backward in the window where the match starts (
0whenlength = 0). - length : ℕ
Number of symbols copied before the trailing symbol
next. - next : α
Trailing symbol appended after the copied prefix.
Instances For
Reproduction and histories #
u is reproducible from the text h within window W: it is empty, or it is a copy that
starts d symbols back, for some 1 ≤ d ≤ W with d ≤ |h|; the copy may run into u itself.
This is reproduction h → h u of [LZ76] with the copy's start restricted to the last
W positions of h.
Instances For
The empty phrase is reproducible.
Reproducibility is closed under prefixes.
Moving the first k symbols of a reproducible phrase into the text keeps the rest
reproducible.
Every infix of the text is reproducible when the window covers the whole text.
The greedy parse #
The length of the longest reproducible prefix of r.
The greedy parse of r after the text h: the next phrase is the longest reproducible
prefix of r followed by one more symbol (all of r if r is reproducible).
The greedy (exhaustive) parse of x with window W.
The Lempel–Ziv complexity of x ([LZ76]): the number of phrases of its exhaustive
history with an unbounded window. Copies never start before the beginning of the text, so the
window |x| is unbounded for x.
The longest reproducible prefix of r is no longer than r.
The prefix of length copyLen is reproducible.
Every reproducible prefix of r is at most copyLen long.
The phrases of the greedy parse concatenate to the input.
The greedy parse is a history of its input.
The greedy parse is exhaustive: no phrase but the last is reproducible.
Optimality #
One step of the exchange argument: a phrase u of a history that starts no later than the
greedy phrase (after h) also ends no later.
Optimality of the exhaustive history ([LZ76]): the greedy parse of x has no
more phrases than any history of x with the same window.
Uniqueness of the exhaustive history ([LZ76]): an exhaustive history of x is
its greedy parse.
The (distance, length, symbol) code #
The smallest distance from which u is a copy, within window W; 0 if u is empty or
there is no such distance.
If u is a copy from some distance within the window, then bestDist is such a distance
(or u is empty).
Encode each nonempty phrase u after the text h as
⟨bestDist W h u.dropLast, |u| - 1, a⟩, where a is the last symbol of u. Empty phrases are
skipped.
The LZ77 code of x with window W.
The LZ77 code is lossless. The decoder does not need the window.