Algorithm Search⌘K

Proofs: LZ-index

These are the human proofs of LZ-index, at full length.

The LZ-index reports every occurrence once, by the number of phrases it touches full

This proof shows: correctness.

We use the pseudocode, the notation and the invariant (Z1) to (Z3) of the page. Let T′=T$T' = T\$, and for a position qq of T′T', let ph(q)ph(q) be the phrase that contains it. An occurrence of PP at ss occupies T′[s…s+m−1]T'[s \ldots s + m - 1]; since PP has no $\$, it lies inside TT. It touches the phrases ph(s),…,ph(s+m−1)ph(s), \ldots, ph(s + m - 1). We show that the three parts of SEARCH report exactly the occurrences that touch one, two, and at least three phrases, each once, and nothing else.

Lemma 1 (prefix closure). Every prefix of a phrase is a phrase, and the LZTrie node of BkB_k is an ancestor of the node of BjB_j exactly when BkB_k is a prefix of BjB_j.

Proof. By (Z1) each phrase is an earlier phrase plus one symbol, so by induction its prefixes are phrases. In a trie, the node of uu is an ancestor of the node of ww exactly when uu is a prefix of ww. □\square

One phrase (line L3). Soundness. Line L3 takes a phrase BkB_k ending with PP, which is what the RevTrie subtree of PrP^r holds by (Z2), and a phrase BjB_j with BkB_k as a prefix, by Lemma 1. Then Bj[∣Bk∣−m…∣Bk∣−1]=PB_j[\lvert B_k \rvert - m \ldots \lvert B_k \rvert - 1] = P, so PP occurs at start(j)+∣Bk∣−mstart(j) + \lvert B_k \rvert - m by (Z3), inside BjB_j. Completeness. If PP occurs inside BjB_j, ending at offset ee, then Bj[0…e]B_j[0 \ldots e] is a phrase BkB_k by Lemma 1, it ends with PP, and it is a prefix of BjB_j, so line L3 reports the occurrence with ∣Bk∣=e+1\lvert B_k \rvert = e + 1. Uniqueness. For a fixed jj, different kk are different prefixes of BjB_j, of different lengths, and give different positions.

Two phrases (lines L4 and L5). Soundness. For a cut ii, a reported pair has BjB_j ending with P[0…i−1]P[0 \ldots i - 1] and Bj+1B_{j+1} starting with P[i…]P[i \ldots]. Since BjB_j ends at start(j+1)−1start(j + 1) - 1, the occurrence at start(j+1)−istart(j + 1) - i touches exactly BjB_j and Bj+1B_{j+1}. Completeness. An occurrence at ss that touches exactly BjB_j and Bj+1B_{j+1} has i=start(j+1)−si = start(j + 1) - s symbols in BjB_j, with 1≤i≤m−11 \le i \le m - 1; then BjB_j ends with P[0…i−1]P[0 \ldots i - 1] and Bj+1B_{j+1} starts with P[i…]P[i \ldots], so the pair is reported for this ii. Uniqueness. The cut ii is the distance from ss to the phrase boundary inside the occurrence, so it is determined by the occurrence.

At least three phrases (lines L6 and L7). Let an occurrence at ss touch Ba,…,BbB_a, \ldots, B_b with b≥a+2b \ge a + 2. Then Ba+1B_{a+1} lies wholly inside it, starting at i=start(a+1)−s≥1i = start(a + 1) - s \ge 1 and ending before the end of the occurrence, so Ba+1=P[i…d−1]B_{a+1} = P[i \ldots d - 1] with d<md < m, and i≤m−2i \le m - 2. Completeness. For this ii, the walk of line L6 along P[i…]P[i \ldots] meets the node of Ba+1B_{a+1} at depth d−id - i, since by Lemma 1 all its prefixes are nodes; with k=a+1k = a + 1, Bk−1B_{k-1} ends with P[0…i−1]P[0 \ldots i - 1] and the following phrases spell a string that starts with P[d…]P[d \ldots], so line L7 reports start(k)−i=sstart(k) - i = s. Soundness. If line L7 reports start(k)−istart(k) - i, then P[0…i−1]P[0 \ldots i - 1] is a suffix of Bk−1B_{k-1}, P[i…d−1]=BkP[i \ldots d - 1] = B_k, and P[d…]P[d \ldots] is a prefix of Bk+1Bk+2⋯B_{k+1} B_{k+2} \cdots, so PP occurs there; it starts inside Bk−1B_{k-1} and ends after BkB_k, so it touches at least three phrases. Uniqueness. For a given occurrence, BkB_{k} must be the first phrase that starts inside it after its first position, so k=a+1k = a + 1 and ii are determined.

The three parts cover the occurrences that touch one, two, and at least three phrases, which are all the occurrences, and these classes are disjoint. So SEARCH reports each occurrence exactly once. ■\blacksquare

Back to LZ-index