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 #
- Result:
search w sa p = (lo, hi)is the half-open rank interval[lo, hi); the occurrences ofpare the positionssa[lo], …, sa[hi - 1], listed in suffix order rather than text order. - Empty pattern:
p = []returns(0, |w|), every positioni < |w|. UnlikeCslib.Algorithms.Lean.KMP.kmpMatchesList, the position|w|of the empty suffix is not reported, since the suffix array does not list it.
Main definitions #
Cslib.Algorithms.Lean.SuffixArray.cmpSuffix: comparepwith a suffix ofw, up to|p|characters.Cslib.Algorithms.Lean.SuffixArray.probeBelow,probeAtMost: threshold predicates on ranks for the lower and upper endpoints.Cslib.Algorithms.Lean.SuffixArray.searchArray,search: the rank interval of the suffixes starting withp.Cslib.Algorithms.Lean.SuffixArray.occurrences: the positions of the occurrences ofp.
Main results #
Cslib.Algorithms.Lean.SuffixArray.cmpSuffix_eq:cmpSuffix w p icomparespwith(w.drop i).take |p|.Cslib.Algorithms.Lean.SuffixArray.searchArray_specandsearch_spec: on a suffix array, the suffix of rankrstarts withpifflo ≤ r < hi.Cslib.Algorithms.Lean.SuffixArray.mem_occurrences:i ∈ occurrences w sa piffi < |w|andpoccurs inwat positioni.
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 .eq iff p is a prefix of the suffix.
Searching a suffix array #
Binary-search probe at rank r: is the |p|-window of the suffix of rank r below p?
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.
The starting positions of the occurrences of p in w, listed in suffix-array order.
Specification of the probe used to find the lower end of the rank interval.
Specification of the probe used to find the upper end of the rank interval.
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.
Correctness of suffix-array search on lists.
occurrences w sa p lists exactly the positions where p occurs in w (as a prefix of a
nonempty suffix).