Skip to main content
QUICK REVIEW

[Paper Review] Fast Searching in Packed Strings

Philip Bille|arXiv (Cornell University)|Jul 17, 2009
Algorithms and Data CompressionComputer Science15 references6 citations
TL;DR

This paper presents a fast algorithm for exact string matching on packed strings, leveraging a novel segment automaton and tabulated transition simulation to achieve $ Oigl(\frac{n}{\log_\sigma n} + m + \mathrm{occ}\bigr) $ time complexity on a unit-cost word-RAM. The method exploits word-level parallelism and compact subautomaton encoding to beat the classical Knuth-Morris-Pratt bound for $ m = o(n) $, achieving optimality when $ m = O(n / \log_\sigma n) $.

ABSTRACT

Given strings $P$ and $Q$ the (exact) string matching problem is to find all positions of substrings in $Q$ matching $P$. The classical Knuth-Morris-Pratt algorithm [SIAM J. Comput., 1977] solves the string matching problem in linear time which is optimal if we can only read one character at the time. However, most strings are stored in a computer in a packed representation with several characters in a single word, giving us the opportunity to read multiple characters simultaneously. In this paper we study the worst-case complexity of string matching on strings given in packed representation. Let $m \leq n$ be the lengths $P$ and $Q$, respectively, and let $σ$ denote the size of the alphabet. On a standard unit-cost word-RAM with logarithmic word size we present an algorithm using time $$ O\left(\frac{n}{\log_σn} + m + \occ ight). $$ Here $\occ$ is the number of occurrences of $P$ in $Q$. For $m = o(n)$ this improves the $O(n)$ bound of the Knuth-Morris-Pratt algorithm. Furthermore, if $m = O(n/\log_σn)$ our algorithm is optimal since any algorithm must spend at least $Ω(\frac{(n+m)\log σ}{\log n} + \occ) = Ω(\frac{n}{\log_σn} + \occ)$ time to read the input and report all occurrences. The result is obtained by a novel automaton construction based on the Knuth-Morris-Pratt algorithm combined with a new compact representation of subautomata allowing an optimal tabulation-based simulation.

Motivation & Objective

  • To address the worst-case complexity of string matching on packed strings, where multiple characters are stored per word.
  • To overcome the limitations of classical algorithms like Knuth-Morris-Pratt, which assume one-character-at-a-time access and thus cannot exploit word-level parallelism.
  • To design a data structure and algorithm that achieve near-optimal performance by compactly encoding automaton substates and enabling fast simulation via tabulation.
  • To demonstrate that the algorithm is optimal when $ m = O(n / \log_\sigma n) $, matching the information-theoretic lower bound.
  • To enable efficient multi-search scenarios by precomputing tables independent of the pattern.

Proposed method

  • Introduce the segment automaton $ C(P,r) $, a compact representation of the KMP automaton $ K $, grouping states into overlapping intervals of size $ r = \Theta(\log n) $.
  • Partition the automaton into subautomata based on segment boundaries, enabling efficient simulation using precomputed transition tables.
  • Use tabulation to compute the Next function for each segment, allowing constant-time simulation of up to $ r-1 $ characters per iteration.
  • Apply a modified simulation algorithm that processes light transitions (non-failure, non-accepting) in bulk and handles heavy or accepting transitions individually.
  • Leverage word-level parallelism and bitwise operations to process multiple characters simultaneously, reducing the number of iterations.
  • Optimize space to $ O(n^\varepsilon + m) $ by setting $ t = n^\varepsilon $, balancing time and space complexity.

Experimental results

Research questions

  • RQ1Can we achieve sublinear time per character in packed string matching by exploiting word-level parallelism?
  • RQ2Is it possible to design a compact automaton representation that supports fast simulation and enables optimal time complexity?
  • RQ3How can we efficiently simulate automaton transitions in packed strings without incurring $ O(n) $ time due to frequent state changes?
  • RQ4Can tabulation-based techniques be adapted to the KMP automaton to achieve optimal performance on packed strings?
  • RQ5What is the theoretical limit of performance for packed string matching, and can we match it?

Key findings

  • The proposed algorithm achieves $ O\bigl(\frac{n}{\log_\sigma n} + m + \mathrm{occ}\bigr) $ time complexity on a unit-cost word-RAM with logarithmic word size.
  • For $ m = o(n) $, this improves upon the classical $ O(n) $ bound of the Knuth-Morris-Pratt algorithm.
  • When $ m = O(n / \log_\sigma n) $, the algorithm matches the information-theoretic lower bound of $ \Omega\bigl(\frac{n}{\log_\sigma n} + \mathrm{occ}\bigr) $, making it optimal.
  • The space complexity is $ O(n^\varepsilon + m) $ for any constant $ \varepsilon \in (0,1) $, enabling efficient memory usage.
  • The algorithm uses a novel segment automaton and tabulated transition simulation to enable bulk processing of characters, reducing the number of iterations to $ O(n/r + \mathrm{occ}) $.
  • The method is extensible to multiple searches by precomputing tables independent of the pattern, enabling reuse across queries.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.