Documentation

CslibFrontier.Algorithms.Lean.SuffixArray.Search

Pattern search in a suffix array #

The suffixes of w that start with a pattern p occupy a contiguous range of ranks in the suffix array, because the words with prefix p form an interval of the lexicographic order. Two binary searches over sa find the endpoints of that range (the plain search of [ManberMyers1993], without LCP information), comparing p with at most |p| characters of a suffix per probe.

Query-complexity bounds for bisection are proved in CslibFrontier.Algorithms.Lean.Query.BinarySearch, and machine-level O(|p| log |w|) running-time bounds on word arrays are proved in CslibFrontier.Algorithms.WordRAM.SuffixArray.

Source Conventions #

Main definitions #

Main results #

References #

Comparing a pattern with a suffix #

Compare the pattern p with the first |p| characters of the suffix of w at position i.

cmpSuffix w p i compares p with the window (w.drop i).take |p| of the same length as p (shorter if the suffix is shorter).

cmpSuffix reports .gt iff the window of the suffix is below p.

cmpSuffix reports .eq or .gt iff the window of the suffix is at most p.

cmpSuffix reports .eq iff p is a prefix of the suffix.

Searching a suffix array #

def Cslib.Algorithms.Lean.SuffixArray.probeBelow {α : Type u_1} [LinearOrder α] (w : Array α) (sa : Array ℕ) (p : List α) (r : ℕ) :

Binary-search probe at rank r: is the |p|-window of the suffix of rank r below p?

def Cslib.Algorithms.Lean.SuffixArray.probeAtMost {α : Type u_1} [LinearOrder α] (w : Array α) (sa : Array ℕ) (p : List α) (r : ℕ) :

Binary-search probe at rank r: is the |p|-window of the suffix of rank r at most p?

Find the range of ranks whose suffixes start with p, by two binary searches over sa: lo is the first rank whose suffix's |p|-window is not below p, and hi is the first rank whose window is above p.

def Cslib.Algorithms.Lean.SuffixArray.search {α : Type u_1} [LinearOrder α] (w : List α) (sa : List ℕ) (p : List α) :

The rank interval of suffixes of w starting with p.

The starting positions of the occurrences of p in w, listed in suffix-array order.

theorem Cslib.Algorithms.Lean.SuffixArray.probeBelow_eq_true_iff {α : Type u_1} [LinearOrder α] (w : Array α) (sa : Array ℕ) (p : List α) {r : ℕ} (hr : r < sa.size) :

Specification of the probe used to find the lower end of the rank interval.

theorem Cslib.Algorithms.Lean.SuffixArray.probeAtMost_eq_true_iff {α : Type u_1} [LinearOrder α] (w : Array α) (sa : Array ℕ) (p : List α) {r : ℕ} (hr : r < sa.size) :

Specification of the probe used to find the upper end of the rank interval.

theorem Cslib.Algorithms.Lean.SuffixArray.searchArray_spec {α : Type u_1} [LinearOrder α] {w : Array α} {sa : Array ℕ} (hsa : IsSuffixArray w.toList sa.toList) (p : List α) :
(searchArray w sa p).1 ≤ (searchArray w sa p).2 ∧ (searchArray w sa p).2 ≤ sa.size ∧ ∀ (r : ℕ) (hr : r < sa.size), p <+: List.drop sa[r] w.toList ↔ (searchArray w sa p).1 ≤ r ∧ r < (searchArray w sa p).2

Correctness of suffix-array search: on a suffix array, searchArray returns ranks lo ≤ hi ≤ |sa| such that the suffix of rank r starts with p exactly when lo ≤ r < hi.

theorem Cslib.Algorithms.Lean.SuffixArray.search_spec {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (hsa : IsSuffixArray w sa) (p : List α) :
(search w sa p).1 ≤ (search w sa p).2 ∧ (search w sa p).2 ≤ sa.length ∧ ∀ (r : ℕ) (hr : r < sa.length), p <+: List.drop sa[r] w ↔ (search w sa p).1 ≤ r ∧ r < (search w sa p).2

Correctness of suffix-array search on lists.

theorem Cslib.Algorithms.Lean.SuffixArray.mem_occurrences {α : Type u_1} [LinearOrder α] {w : List α} {sa : List ℕ} (hsa : IsSuffixArray w sa) (p : List α) {i : ℕ} :
i ∈ occurrences w sa p ↔ i < w.length ∧ p <+: List.drop i w

occurrences w sa p lists exactly the positions where p occurs in w (as a prefix of a nonempty suffix).