Documentation

CslibFrontier.Algorithms.Lean.SuffixArray.Kasai

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 #

Main definitions #

Main results #

References #

Kasai et al.: the algorithm #

Inverse of a list of positions: rankArray n sa has size n and stores r at position sa[r].

Scattering one additional suffix-array entry appends a single Array.set! at rank sa.length.

theorem Cslib.Algorithms.Lean.SuffixArray.getElem!_rankArray {n : ℕ} {sa : List ℕ} (hnd : sa.Nodup) (hlt : ∀ i ∈ sa, i < n) {r : ℕ} (hr : r < sa.length) :
(rankArray n sa)[sa[r]]! = r

rankArray n sa inverts sa when sa lists distinct positions below n.

@[irreducible]
def Cslib.Algorithms.Lean.SuffixArray.kasaiLoop {α : Type u_1} [DecidableEq α] (w : Array α) (sa rk : Array ℕ) (i h : ℕ) (lcp : Array ℕ) :

The main loop of Kasai et al.'s construction at text position i, with current common-prefix length h.

theorem Cslib.Algorithms.Lean.SuffixArray.kasaiLoop_of_le {α : Type u_1} [DecidableEq α] (w : Array α) (sa rk : Array ℕ) {i : ℕ} (hi : w.size ≤ i) (h : ℕ) (lcp : Array ℕ) :
kasaiLoop w sa rk i h lcp = lcp

Once i reaches w.size, kasaiLoop returns the accumulated lcp array.

theorem Cslib.Algorithms.Lean.SuffixArray.kasaiLoop_of_rk_zero {α : Type u_1} [DecidableEq α] (w : Array α) (sa rk : Array ℕ) {i : ℕ} (hi : i < w.size) (hr : rk[i]! = 0) (h : ℕ) (lcp : Array ℕ) :
kasaiLoop w sa rk i h lcp = kasaiLoop w sa rk (i + 1) h lcp

At rank 0, kasaiLoop advances i without modifying h or lcp.

theorem Cslib.Algorithms.Lean.SuffixArray.kasaiLoop_of_rk_ne_zero {α : Type u_1} [DecidableEq α] (w : Array α) (sa rk : Array ℕ) {i : ℕ} (hi : i < w.size) (hr : rk[i]! ≠ 0) (h : ℕ) (lcp : Array ℕ) :
kasaiLoop w sa rk i h lcp = have h' := h + (List.drop (sa[rk[i]! - 1]! + h) w.toList).lcpLen (List.drop (i + h) w.toList); kasaiLoop w sa rk (i + 1) (h' - 1) (lcp.set! rk[i]! h')

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.

@[simp]
theorem Cslib.Algorithms.Lean.SuffixArray.size_kasaiLoop {α : Type u_1} [DecidableEq α] (w : Array α) (sa rk : Array ℕ) (i h : ℕ) (lcp : Array ℕ) :
(kasaiLoop w sa rk i h lcp).size = lcp.size

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.

@[simp]

Correctness #

Correctness of Kasai et al.'s algorithm: on a suffix array, kasaiArray returns the LCP array.

theorem Cslib.Algorithms.Lean.SuffixArray.kasai_eq_lcpArray {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (hsa : IsSuffixArray w sa) :
kasai w sa = lcpArray w sa

Correctness of Kasai et al.'s algorithm on lists.