Suffix-Array LCP Construction and Pattern Search on the Word RAM #
This module implements Kasai et al.'s linear-time LCP array construction
([KasaiLeeArimuraArikawaPark2001]) and Manber and Myers's O(|p| log |sa|) suffix-array pattern
search ([ManberMyers1993]) as uniform 20-register Basis.standard Word-RAM programs over word
arrays, charging every memory read and write, rank-array scatter step, character comparison,
arithmetic operation, and loop branch.
Memory layouts and cost bounds #
Kasai LCP array construction (
kasaiCode,kasaiProgram):- Input (
State.init I.encode):KasaiInput.encode I = |text| :: |sa| :: (text ++ sa)places|text|atmem[1],|sa|atmem[2],textat[3, 3 + |text|), andsaat[3 + |text|, 3 + |text| + |sa|). - Workspace and output: zero-initializes the rank array
rkat[3 + |text| + |sa|, 3 + 2 * |text| + |sa|)viaCslib.WordRAM.zeroFill, scatters ranks fromsa, and writeskasai I.text I.sa(= lcpArray I.text I.sawhenIsSuffixArray I.text I.sa) as a length-prefixed array aty.regs Reg.out = 3 + 2 * |text| + |sa|, preservingtextandsa. - Amortized analysis and time: using the potential
$$\Phi(i, h) = 47 \cdot (|text| - i) - 14 \cdot \min(h, |text| - i),$$
each matched character in
kasaiExtBody(14instructions) increaseshup to at most|text| - i, whilehdrops by at most1between text positions. On every input(text, sa)and word widthwwithFits w (3 * |text| + |sa| + 4),kasaiCodeexecutes within55 * |text| + 8 * |sa| + 25instructions (55 * |text| + 8 * |sa| + 26forkasaiProgram, and63 * |text| + 26whenIsSuffixArray I.text I.sa).
- Input (
Suffix-array rank-interval and occurrence search (
rangeSearchCode,rangeSearchProgram,occurrencesCode,occurrencesProgram):- Input (
State.init I.encode):SearchInput.encode I = |text| :: |sa| :: |pat| :: (text ++ sa ++ pat)places|text|,|sa|,|pat|atmem[1..3]andtext,sa,patcontiguously frommem[4]. - Execution and time: runs
Cslib.Algorithms.WordRAM.BinarySearch.bisectCodetwice over[0, |sa|)using an overflow-safeO(|pat|)suffix-comparison probe (16 * |pat| + 24instructions per probe).rangeSearchProgramoutputs[(search I.text I.sa I.pat).1, (search I.text I.sa I.pat).2]aty.regs Reg.out = 4 + |text| + |sa| + |pat|within(32 * |pat| + 63) * (Nat.log 2 |sa| + 1) + 26instructions (O(|pat| log |sa|)independent of occurrence count) wheneverFits w (|text| + |sa| + |pat| + 6).occurrencesProgramoutputsoccurrences I.text I.sa I.patwithin(32 * |pat| + 63) * (Nat.log 2 |sa| + 1) + 9 * |occurrences I.text I.sa I.pat| + 27instructions wheneverFits w (|text| + 2 * |sa| + |pat| + 6).
- Input (
Main definitions #
Cslib.Algorithms.WordRAM.SuffixArray.KasaiInput,KasaiInput.encode: input record and word encoding for Kasai LCP construction.Cslib.Algorithms.WordRAM.SuffixArray.SearchInput,SearchInput.encode: input record and word encoding for suffix-array pattern search.Cslib.Algorithms.WordRAM.SuffixArray.Reg.out: output base register (19).Cslib.Algorithms.WordRAM.SuffixArray.kasaiCode,kasaiProgram: Word-RAM Kasai LCP construction.Cslib.Algorithms.WordRAM.SuffixArray.rangeSearchCode,rangeSearchProgram: Word-RAMO(|pat| log |sa|)suffix-array rank-interval search.Cslib.Algorithms.WordRAM.SuffixArray.occurrencesCode,occurrencesProgram: Word-RAMO(|pat| log |sa| + occ)suffix-array occurrence-list search.
Main results #
Cslib.Algorithms.WordRAM.SuffixArray.kasaiCode_exec,kasaiProgram_computesListInTime, andkasaiProgram_computesLcpArrayInTime: linear-time Word-RAM Kasai LCP bounds.Cslib.Algorithms.WordRAM.SuffixArray.rangeSearchCode_execandrangeSearchProgram_computesListInTime:O(|pat| log |sa|)Word-RAM rank-interval search bounds.Cslib.Algorithms.WordRAM.SuffixArray.occurrencesCode_exec,occurrencesProgram_computesListInTime, andoccurrencesProgram_computesListInTime_worstCase:O(|pat| log |sa| + occ)andO(|pat| log |sa| + |sa|)Word-RAM occurrence-reporting bounds.
References #
Input encodings and registers #
Output base register (19) for all programs in Cslib.Algorithms.WordRAM.SuffixArray.
Shared memory helpers #
Part 1: Kasai et al.'s O(n) LCP array construction #
Uniform Basis.standard Word-RAM program for Kasai et al.'s LCP array construction.
kasaiCode executes within 55 * |I.text| + 8 * |I.sa| + 25 instructions from
Data.init I.encode whenever Fits w (3 * |I.text| + |I.sa| + 4), writing kasai I.text I.sa
as a length-prefixed array at y.regs Reg.out = 3 + 2 * |I.text| + |I.sa| and preserving both
I.text and I.sa.
kasaiProgram computes kasai I.text I.sa in 55 * |I.text| + 8 * |I.sa| + 26 instructions
on every KasaiInput at every word width w with Fits w (3 * |I.text| + |I.sa| + 4).
Linear-time Word-RAM Kasai LCP theorem. On valid suffix-array inputs (IsSuffixArray),
kasaiProgram computes lcpArray I.text I.sa within 63 * |I.text| + 26 instructions at every
word width w with Fits w (4 * |I.text| + 4).
Part 2: O(|p| log |sa|) Suffix-Array Pattern Search #
Uniform Basis.standard Word-RAM program for O(|pat| log |sa|) suffix-array rank-interval
search.
Uniform Basis.standard Word-RAM program for suffix-array occurrence search.
rangeSearchCode executes within (32 * |I.pat| + 63) * (Nat.log 2 |I.sa| + 1) + 25
instructions from Data.init I.encode whenever
Fits w (|I.text| + |I.sa| + |I.pat| + 6), writing
[(search I.text I.sa I.pat).1, (search I.text I.sa I.pat).2] at
y.regs Reg.out = 4 + |I.text| + |I.sa| + |I.pat|.
rangeSearchProgram computes
[(search I.text I.sa I.pat).1, (search I.text I.sa I.pat).2] in
(32 * |I.pat| + 63) * (Nat.log 2 |I.sa| + 1) + 26 instructions (O(|I.pat| log |I.sa|)) at every
word width w with Fits w (|I.text| + |I.sa| + |I.pat| + 6).
occurrencesCode executes within
(32 * |I.pat| + 63) * (Nat.log 2 |I.sa| + 1) + 9 * |occurrences I.text I.sa I.pat| + 26
instructions from Data.init I.encode whenever
Fits w (|I.text| + 2 * |I.sa| + |I.pat| + 6), writing occurrences I.text I.sa I.pat at
y.regs Reg.out = 4 + |I.text| + |I.sa| + |I.pat|.
occurrencesProgram computes occurrences I.text I.sa I.pat in
(32 * |I.pat| + 63) * (Nat.log 2 |I.sa| + 1) + 9 * |occurrences I.text I.sa I.pat| + 27
instructions at every word width w with
Fits w (|I.text| + 2 * |I.sa| + |I.pat| + 6).
Worst-case input-size bound for occurrencesProgram: since
|occurrences I.text I.sa I.pat| ≤ |I.sa|, occurrencesProgram runs within
(32 * |I.pat| + 63) * (Nat.log 2 |I.sa| + 1) + 9 * |I.sa| + 27 instructions.