Documentation

CslibFrontier.Algorithms.Lean.SuffixArray.Basic

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 #

Main definitions #

Main results #

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.

Instances For
    @[instance_reducible]
    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.mem_iff {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {i : ℕ} :
    i ∈ sa ↔ i < w.length
    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.getElem_lt {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {r : ℕ} (hr : r < sa.length) :
    sa[r] < w.length
    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.getElem_eq_getElem_iff {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {r r' : ℕ} (hr : r < sa.length) (hr' : r' < sa.length) :
    sa[r] = sa[r'] ↔ r = r'
    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.drop_lt_drop {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {r r' : ℕ} (hr : r < r') (hr' : r' < sa.length) :

    Ranks order the suffixes strictly.

    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.lt_iff_drop_lt {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {r r' : ℕ} (hr : r < sa.length) (hr' : r' < sa.length) :
    r < r' ↔ List.drop sa[r] w < List.drop sa[r'] w

    Rank order is suffix order.

    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.unique {α : Type u_1} [LinearOrder α] {w : List α} {sa sa' : List ℕ} (h : IsSuffixArray w sa) (h' : IsSuffixArray w sa') :
    sa = sa'

    Suffix arrays are unique.

    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.exists_getElem_eq {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {i : ℕ} (hi : i < w.length) :
    ∃ (r : ℕ) (hr : r < sa.length), sa[r] = i

    Every position has a rank.

    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].

    theorem Cslib.Algorithms.Lean.SuffixArray.getElem_lcpArray_zero {α : Type u_1} [DecidableEq α] (w : List α) (sa : List ℕ) (h : 0 < (lcpArray w sa).length) :
    (lcpArray w sa)[0] = 0
    theorem Cslib.Algorithms.Lean.SuffixArray.getElem_lcpArray_of_pos {α : Type u_1} [DecidableEq α] (w : List α) (sa : List ℕ) {r : ℕ} (hr : r < (lcpArray w sa).length) (hr0 : 0 < r) :
    (lcpArray w sa)[r] = (List.drop sa[r - 1] w).lcpLen (List.drop sa[r] w)
    theorem Cslib.Algorithms.Lean.SuffixArray.IsSuffixArray.getElem_lcpArray_sub_one_le {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (h : IsSuffixArray w sa) {r r' : ℕ} (hr : r < sa.length) (hr' : r' < sa.length) (hsucc : sa[r] + 1 = sa[r']) :
    (lcpArray w sa)[r] - 1 ≤ (lcpArray w 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.