Skip to content

LZ76 factorization

A symbol histogram counts how often each symbol occurs. Lempel–Ziv 76 factorization also looks at where symbols occur together. Repeated substrings let a later part of the sequence be described using material that appeared earlier.

The resulting factor count C reflects how often the parser encounters new material. At a matched input length and encoding, sequences with more repeated patterns usually need fewer factors.

How the parse advances

Start at the first byte. At the next position, find the longest substring beginning there that also starts at an earlier position. Try to extend that match by one byte to obtain a new phrase, then continue after it. Earlier occurrences can overlap the position being parsed, allowing a repeated pattern to extend through the current phrase.

The algorithm follows Lempel and Ziv (1976), with the final-phrase counting convention explained below.

A worked example: banana

import lzcomplexity as lz

count, boundaries = lz.factorization("banana")
assert count == 3
assert boundaries == [0, 1, 2, 3, 7]

The first three bytes introduce b, a and n. At position 3, the remaining ana already starts at position 1. The parse attempts to extend that match by one byte, producing the final boundary 7, beyond the six-byte input.

Starting position Bytes visible in the input What the parser finds
0 b First symbol
1 a A new symbol
2 n A new symbol
3 ana A previous match reaches the end; the attempted extension lies outside the input

The library reports the three completed phrases. It keeps the attempted final boundary in the boundary vector. Consequently, count need not equal len(boundaries) - 1.

To display the material covered by each interval, clamp its end to the input length:

sequence = "banana"
count, boundaries = lz.factorization(sequence)
visible = [
    sequence[start:min(end, len(sequence))]
    for start, end in zip(boundaries, boundaries[1:])
]
assert visible == ["b", "a", "n", "ana"]

Those four visible pieces are useful for inspection. They do not change the reported count of three. For non-ASCII text, boundaries index UTF-8 bytes, so slice an encoded byte string rather than Python Unicode characters; see input types.

Boundary and count conventions

For an ordinary input containing at least two distinct bytes:

  1. The boundary vector starts with 0, 1.
  2. A step advances by longest_previous_match + 1.
  3. If the final boundary is within the input, every interval contributes to C.
  4. If it exceeds the input length, that unfinished final interval is excluded from C but remains in the vector.

The distinction between completed phrases and a final copied tail is especially visible in short sequences.

Empty and constant inputs

Inputs with fewer than two distinct bytes take a special path:

Input Returned C Boundaries
"" 1 [0, 1, 0]
"a" 1 [0, 1, 1]
"aaaa" 1 [0, 1, 4]

For empty input, [0, 1, 0] marks the special empty case. Exclude missing sequences from an analysis before interpreting their counts. Constant input has count one independently of its length.

What changes the count?

Ordering, sequence length and symbolization can change the parse. An exact one-to-one relabeling of individual byte symbols preserves equality relations and therefore the factor count. A many-to-one mapping can create repeats. Expanding a symbol into multiple bytes changes both length and the set of substrings.

Counts commonly grow with recording length. Use entropy-density normalization to study new information per symbol, and compare counts at matched lengths and encodings.

Under the hood

The Rust core constructs a suffix array, its longest-common-prefix array, and a longest-previous-factor table. The parser can then advance using previous-match lengths instead of repeatedly scanning all earlier text. Implementation follows those stages and describes parallel work and memory use.

LZ76 is a parsing algorithm. LZ77 and LZ78 codecs, including compressors such as gzip, use different conventions and produce different quantities.

Next: calculate your first result, understand h, or read the full Python reference.