Table of Contents
Fetching ...

Practical KMP/BM Style Pattern-Matching on Indeterminate Strings

Hossein Dehghani, Neerja Mhaskar, W. F. Smyth

TL;DR

This work tackles pattern matching on indeterminate strings over small alphabets by introducing a prime-number encoding that maps to regular strings, enabling adapted KMP and Boyer–Moore algorithms. The two proposed methods, $KMP\_Indet$ and $BM\_Indet$, operate with only $Θ(m)$ extra space while handling indeterminate letters through on-the-fly construction of $q'$ and prefix arrays, achieving worst-case $O(nm\sqrt{m})$ time in the indeterminate regime. Empirical results show that $BM\_Indet$ often outperforms $KMP\_Indet$ and the DBWT baseline on small patterns and binary alphabets, while $KMP\_Indet$ and BF tend to be more robust as $m$ grows or the alphabet enlarges. The work demonstrates a practical, low-memory approach to indeterminate pattern matching and suggests potential extensions to other Boyer–Moore variants.

Abstract

In this paper we describe two simple, fast, space-efficient algorithms for finding all matches of an indeterminate pattern $p = p[1..m]$ in an indeterminate string $x = x[1..n]$, where both $p$ and $x$ are defined on a "small" ordered alphabet $Σ$ $-$ say, $σ= |Σ| \le 9$. Both algorithms depend on a preprocessing phase that replaces $Σ$ by an integer alphabet $Σ_I$ of size $σ_I = σ$ which (reversibly, in time linear in string length) maps both $x$ and $p$ into equivalent regular strings $y$ and $q$, respectively, on $Σ_I$, whose maximum (indeterminate) letter can be expressed in a 32-bit word (for $σ\le 4$, thus for DNA sequences, an 8-bit representation suffices). We first describe an efficient version KMP Indet of the venerable Knuth-Morris-Pratt algorithm to find all occurrences of $q$ in $y$ (that is, of $p$ in $x$), but, whenever necessary, using the prefix array, rather than the border array, to control shifts of the transformed pattern $q$ along the transformed string $y$. We go on to describe a similar efficient version BM Indet of the Boyer- Moore algorithm that turns out to execute significantly faster than KMP Indet over a wide range of test cases. A noteworthy feature is that both algorithms require very little additional space: $Θ(m)$ words. We conjecture that a similar approach may yield practical and efficient indeterminate equivalents to other well-known pattern-matching algorithms, in particular the several variants of Boyer-Moore.

Practical KMP/BM Style Pattern-Matching on Indeterminate Strings

TL;DR

This work tackles pattern matching on indeterminate strings over small alphabets by introducing a prime-number encoding that maps to regular strings, enabling adapted KMP and Boyer–Moore algorithms. The two proposed methods, and , operate with only extra space while handling indeterminate letters through on-the-fly construction of and prefix arrays, achieving worst-case time in the indeterminate regime. Empirical results show that often outperforms and the DBWT baseline on small patterns and binary alphabets, while and BF tend to be more robust as grows or the alphabet enlarges. The work demonstrates a practical, low-memory approach to indeterminate pattern matching and suggests potential extensions to other Boyer–Moore variants.

Abstract

In this paper we describe two simple, fast, space-efficient algorithms for finding all matches of an indeterminate pattern in an indeterminate string , where both and are defined on a "small" ordered alphabet say, . Both algorithms depend on a preprocessing phase that replaces by an integer alphabet of size which (reversibly, in time linear in string length) maps both and into equivalent regular strings and , respectively, on , whose maximum (indeterminate) letter can be expressed in a 32-bit word (for , thus for DNA sequences, an 8-bit representation suffices). We first describe an efficient version KMP Indet of the venerable Knuth-Morris-Pratt algorithm to find all occurrences of in (that is, of in ), but, whenever necessary, using the prefix array, rather than the border array, to control shifts of the transformed pattern along the transformed string . We go on to describe a similar efficient version BM Indet of the Boyer- Moore algorithm that turns out to execute significantly faster than KMP Indet over a wide range of test cases. A noteworthy feature is that both algorithms require very little additional space: words. We conjecture that a similar approach may yield practical and efficient indeterminate equivalents to other well-known pattern-matching algorithms, in particular the several variants of Boyer-Moore.
Paper Structure (10 sections, 7 theorems, 3 equations, 11 figures, 6 algorithms)

This paper contains 10 sections, 7 theorems, 3 equations, 11 figures, 6 algorithms.

Key Result

Lemma 1

The border and prefix arrays of a regular string of length $n$ can each be computed in $\mathcal{O}(n)$ time.

Figures (11)

  • Figure 1: Border array $\beta_{\hbox{\boldmath $x$}}$ and prefix array $\pi_{\hbox{\boldmath $x$}}$ for string $\hbox{\boldmath $x$}=aabaabaa\{a,b\}baa\{a, c\}$.
  • Figure 2: Illustration of the KMP_Indet algorithm when a prefix of the pattern $\hbox{\boldmath $q$}[1..j]$ matches the text substring $\hbox{\boldmath $y$}[i-j+1..i]$. If either $\hbox{\boldmath $q$}[1..j-1]$ or $\hbox{\boldmath $y$}[i-j+2..i]$ contains an indeterminate letter, then we construct $\hbox{\boldmath $q'$} = \hbox{\boldmath $q$}[1..j-1]\hbox{\boldmath $y$}[i-j+2..i]$ to compute the shift by constructing the prefix table for $\hbox{\boldmath $q'$}$.
  • Figure 3: The figure simulates the execution of KMP_Indet on the text $\hbox{\boldmath $x$}=aabaabaa\{a,b\}baa\{a,c\}$ and pattern $\hbox{\boldmath $p$}=aabaa$. After execution, KMP_Indet returns the list of positions $\{1, 4, 8\}$ at which $\hbox{\boldmath $p$}$ occurs in $\hbox{\boldmath $x$}$. 'x' in the third alignment identifies a mismatch.
  • Figure 4: Illustration of the BM_indet algorithm when a suffix of the pattern of length $j \!-\! 1$$\hbox{\boldmath $q$}[j-1..m]$ matches the text substring $\hbox{\boldmath $y$}[i+j-2..i+m-1]= \hbox{\boldmath $t$}'$. If either $\hbox{\boldmath $q$}[1..m-1]$ or $\hbox{\boldmath $t$}'$ contains an indeterminate letter, then we construct $\hbox{\boldmath $q'$} = \hbox{\boldmath $t'$}^R\hbox{\boldmath $q$}[1..m-1]^R$ to compute the shift.
  • Figure 5: The figure simulates the execution of BM_Indet on the text $\hbox{\boldmath $x$}=aabaabaa\{a,b\}baa\{a,c\}$ and pattern $\hbox{\boldmath $p$}=aabaa$. After execution, BM_Indet returns the list of positions $\{1, 4, 8\}$ at which $\hbox{\boldmath $p$}$ occurs in $\hbox{\boldmath $x$}$. 'x' in the third alignment identifies a mismatch.
  • ...and 6 more figures

Theorems & Definitions (10)

  • Lemma 1: AHU74SW08
  • Lemma 2: S03SW08
  • Lemma 3: IR16
  • Lemma 4
  • Lemma 5
  • proof
  • Theorem 10
  • Remark 11
  • Remark 12
  • Theorem 13