Documentation

CslibFrontier.Algorithms.WordRAM.SuffixArray

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 #

  1. Kasai LCP array construction (kasaiCode, kasaiProgram):

    • Input (State.init I.encode): KasaiInput.encode I = |text| :: |sa| :: (text ++ sa) places |text| at mem[1], |sa| at mem[2], text at [3, 3 + |text|), and sa at [3 + |text|, 3 + |text| + |sa|).
    • Workspace and output: zero-initializes the rank array rk at [3 + |text| + |sa|, 3 + 2 * |text| + |sa|) via Cslib.WordRAM.zeroFill, scatters ranks from sa, and writes kasai I.text I.sa (= lcpArray I.text I.sa when IsSuffixArray I.text I.sa) as a length-prefixed array at y.regs Reg.out = 3 + 2 * |text| + |sa|, preserving text and sa.
    • 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 (14 instructions) increases h up to at most |text| - i, while h drops by at most 1 between text positions. On every input (text, sa) and word width w with Fits w (3 * |text| + |sa| + 4), kasaiCode executes within 55 * |text| + 8 * |sa| + 25 instructions (55 * |text| + 8 * |sa| + 26 for kasaiProgram, and 63 * |text| + 26 when IsSuffixArray I.text I.sa).
  2. 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| at mem[1..3] and text, sa, pat contiguously from mem[4].
    • Execution and time: runs Cslib.Algorithms.WordRAM.BinarySearch.bisectCode twice over [0, |sa|) using an overflow-safe O(|pat|) suffix-comparison probe (16 * |pat| + 24 instructions per probe).
      • rangeSearchProgram outputs [(search I.text I.sa I.pat).1, (search I.text I.sa I.pat).2] at y.regs Reg.out = 4 + |text| + |sa| + |pat| within (32 * |pat| + 63) * (Nat.log 2 |sa| + 1) + 26 instructions (O(|pat| log |sa|) independent of occurrence count) whenever Fits w (|text| + |sa| + |pat| + 6).
      • occurrencesProgram outputs occurrences I.text I.sa I.pat within (32 * |pat| + 63) * (Nat.log 2 |sa| + 1) + 9 * |occurrences I.text I.sa I.pat| + 27 instructions whenever Fits w (|text| + 2 * |sa| + |pat| + 6).

Main definitions #

Main results #

References #

Input encodings and registers #

Input to Word-RAM Kasai LCP array construction: a word text and a candidate suffix array sa.

  • text : List ℕ

    Input text over word alphabet ℕ.

  • sa : List ℕ

    Candidate suffix array (list of starting positions).

Instances For

    Flat word-list encoding [|text|, |sa|] ++ text ++ sa: State.init I.encode places |text| at mem[1], |sa| at mem[2], text at [3, 3 + |text|), and sa at [3 + |text|, 3 + |text| + |sa|).

    Input to Word-RAM suffix-array pattern search: text text, suffix array sa, and pattern pat.

    • text : List ℕ

      Input text over word alphabet ℕ.

    • sa : List ℕ

      Suffix array of text (list of starting positions).

    • pat : List ℕ

      Pattern to search for in text.

    Instances For

      Flat word-list encoding [|text|, |sa|, |pat|] ++ text ++ sa ++ pat: State.init I.encode places |text|, |sa|, |pat| at mem[1..3], text at [4, 4 + |text|), sa at [4 + |text|, 4 + |text| + |sa|), and pat at [4 + |text| + |sa|, 4 + |text| + |sa| + |pat|).

      @[reducible, inline]

      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 #

      Structured 20-register Word-RAM program computing kasai I.text I.sa at Reg.out.

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

      Structured 20-register Word-RAM program computing the rank interval [(search I.text I.sa I.pat).1, (search I.text I.sa I.pat).2] at Reg.out.

      Uniform Basis.standard Word-RAM program for O(|pat| log |sa|) suffix-array rank-interval search.

      Structured 20-register Word-RAM program computing occurrences I.text I.sa I.pat at Reg.out.

      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.