Suffix arrays and LCP arrays #
The suffix array of a word w over a linearly ordered alphabet lists the starting positions
0, …, |w| - 1 of its nonempty suffixes w.drop i in increasing lexicographic order
([ManberMyers1993]). Its LCP array records the length of the longest common prefix of every
two suffixes adjacent in that order, measured by List.lcpLen
(CslibFrontier.Foundations.Data.List.CommonPrefix). This module gives the specifications, the
uniqueness of suffix arrays, a reference construction by sorting, and the combinatorial lemma
behind Kasai et al.'s linear-time LCP construction ([KasaiLeeArimuraArikawaPark2001]).
Source Conventions #
- Indexing: positions and ranks are
0-based. The suffix of rankrstarts atsa[r], andlcp[r]compares the suffixes of ranksr - 1andr;lcp[0] = 0by convention. - Alphabet: any
[LinearOrder α], with Mathlib's lexicographic order onList α, in which a proper prefix is smaller than the word it extends. No end-of-word sentinel is assumed, and the empty suffix (position|w|) is not listed.
Main definitions #
Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray:sais a permutation ofList.range w.lengthwhose suffixes are strictly increasing.Cslib.Algorithms.Lean.SuffixArray.suffixArray: the positions sorted by their suffixes.Cslib.Algorithms.Lean.SuffixArray.lcpArray: the LCP array of a list of positions.
Main results #
Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.unique: suffix arrays are unique.Cslib.Algorithms.Lean.SuffixArray.isSuffixArray_suffixArray: the sorting construction is the suffix array.Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.lt_iff_drop_lt: rank order is suffix order.Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.getElem_lcpArray_sub_one_le: the height lemmalcp[rank[i + 1]] ≥ lcp[rank[i]] - 1of[KasaiLeeArimuraArikawaPark2001], stated for ranksr,r'withsa[r] + 1 = sa[r'].
References #
Suffix arrays #
sa is the suffix array of w: a permutation of the positions 0, …, |w| - 1 listing the
suffixes w.drop i in strictly increasing lexicographic order.
salists every position ofwexactly once.The suffixes at the listed positions strictly increase.
Instances For
Suffix arrays are unique.
The reference construction #
The suffix array computed by sorting the positions 0, …, |w| - 1 by their suffixes.
Sorting the positions by their suffixes yields the suffix array.
A list is a suffix array of w iff it is suffixArray w.
LCP arrays #
The LCP array of w with respect to the positions sa: entry 0 is 0 and entry r > 0
is the length of the longest common prefix of the suffixes at sa[r - 1] and sa[r].
Height lemma ([KasaiLeeArimuraArikawaPark2001]): if the suffix of rank r' starts one
position after the suffix of rank r, then lcp[r'] ≥ lcp[r] - 1. In terms of the rank array,
lcp[rank[i + 1]] ≥ lcp[rank[i]] - 1.