Kasai et al.'s linear-time LCP array construction #
Given a word w and its suffix array sa, the algorithm of Kasai, Lee, Arimura, Arikawa and Park
([KasaiLeeArimuraArikawaPark2001]) computes the LCP array by visiting the suffixes in text order
i = 0, 1, …, |w| - 1. It compares the suffix at i with its predecessor in sa starting from
the previous common-prefix length minus one, which the height lemma
(IsSuffixArray.getElem_lcpArray_sub_one_le) shows is already a common prefix. The common-prefix
length drops by at most one per step and never exceeds the remaining length.
Machine-level O(|w|) running-time bounds on word arrays are proved in
CslibFrontier.Algorithms.WordRAM.SuffixArray.
Source Conventions #
- Indexing:
0-based positions and ranks, as inCslib.Algorithms.Lean.SuffixArray.Basic; the result haslcp[0] = 0. - Rank array:
rankArray w.size sa.toListstoresrat positionsa[r], the inverse permutation ofsa.
Main definitions #
Cslib.Algorithms.Lean.SuffixArray.rankArray: inverse of a list of distinct positions.Cslib.Algorithms.Lean.SuffixArray.kasaiLoop,kasaiArray,kasai: pure Kasai LCP construction on arrays and its list wrapper.
Main results #
Cslib.Algorithms.Lean.SuffixArray.getElem!_rankArray:rankArray n sainvertssa.Cslib.Algorithms.Lean.SuffixArray.rankArray_append_singleton: incremental scatter step ofrankArray.Cslib.Algorithms.Lean.SuffixArray.kasaiArray_toListandkasai_eq_lcpArray: on a suffix array the algorithm returns the LCP array.
References #
Kasai et al.: the algorithm #
At a nonzero rank rk[i]!, kasaiLoop extends h by the longest common prefix from
sa[rk[i]! - 1]! + h and i + h, records h' at rk[i]!, and continues from h' - 1.
Kasai et al.'s LCP construction on arrays: visit the positions i = 0, 1, … in text order,
extend the common prefix of the suffix at i and its predecessor in sa from the previous length
minus one, and record it at rank rank[i].
Kasai et al.'s LCP construction on lists.
Correctness #
Correctness of Kasai et al.'s algorithm: on a suffix array, kasaiArray returns the LCP
array.
Correctness of Kasai et al.'s algorithm on lists.