[Paper Review] A Fast Generic Sequence Matching Algorithm
This paper presents a fast, generic sequence matching algorithm that combines the worst-case efficiency of the Knuth-Morris-Pratt (KMP) algorithm with the average-case performance of the Boyer-Moore algorithm by integrating a hash-coded skip loop. The algorithm achieves linear worst-case time complexity with at most 2n comparisons and sublinear average-case behavior, outperforming traditional Boyer-Moore variants in diverse scenarios including DNA and Unicode sequences, while remaining suitable for generic C++ libraries like the STL.
A string matching -- and more generally, sequence matching -- algorithm is presented that has a linear worst-case computing time bound, a low worst-case bound on the number of comparisons (2n), and sublinear average-case behavior that is better than that of the fastest versions of the Boyer-Moore algorithm. The algorithm retains its efficiency advantages in a wide variety of sequence matching problems of practical interest, including traditional string matching; large-alphabet problems (as in Unicode strings); and small-alphabet, long-pattern problems (as in DNA searches). Since it is expressed as a generic algorithm for searching in sequences over an arbitrary type T, it is well suited for use in generic software libraries such as the C++ Standard Template Library. The algorithm was obtained by adding to the Knuth-Morris-Pratt algorithm one of the pattern-shifting techniques from the Boyer-Moore algorithm, with provision for use of hashing in this technique. In situations in which a hash function or random access to the sequences is not available, the algorithm falls back to an optimized version of the Knuth-Morris-Pratt algorithm.
Motivation & Objective
- To develop a sequence matching algorithm with optimal worst-case time complexity and improved average-case performance.
- To address the limitations of existing algorithms in small-alphabet, long-pattern problems (e.g., DNA) and large-alphabet scenarios (e.g., Unicode).
- To design a generic algorithm suitable for integration into C++ standard libraries like the STL, supporting arbitrary sequence types.
- To balance efficiency across diverse input types by combining KMP's robustness with a novel, hash-optimized skip mechanism.
- To provide a fallback to optimized KMP when random access or hashing is unavailable, ensuring consistent performance.
Proposed method
- The algorithm extends the Knuth-Morris-Pratt (KMP) algorithm by incorporating a skip loop inspired by the Accelerated Boyer-Moore (ABM) algorithm.
- It uses a hash-coded skip technique to efficiently determine how far to shift the pattern after a mismatch, reducing comparisons in average cases.
- The method includes a fallback to a standard KMP variant when hashing or random access is not available, preserving worst-case guarantees.
- The algorithm is implemented as a generic template for arbitrary sequence types T, enabling use in C++ Standard Template Library (STL) and similar generic libraries.
- It employs a next table (similar to KMP) for mismatch recovery and a skip table computed using character frequency and hashing for fast pattern shifts.
- The design supports multiple variants: one with hashing for fast skips, one without hashing, and a pure KMP fallback, depending on available operations.
Experimental results
Research questions
- RQ1Can a generic sequence matching algorithm be designed that maintains O(n) worst-case time complexity while achieving sublinear average-case performance?
- RQ2How can the efficiency of Boyer-Moore-style skip loops be preserved in small-alphabet, long-pattern problems such as DNA sequence matching?
- RQ3Can a hash-coded skip mechanism improve performance in large-alphabet scenarios like Unicode text processing?
- RQ4What is the trade-off between initialization cost and average-case speed when integrating skip loops into KMP-based algorithms?
- RQ5How can such an algorithm be implemented generically to support arbitrary sequence types in C++ templates?
Key findings
- The algorithm achieves a worst-case bound of 2n comparisons, matching KMP, ensuring predictable performance.
- In average-case English text searches, the algorithm performs better than the fastest known Boyer-Moore variants, including TBM and LC, due to optimized skip logic.
- The hash-coded skip loop enables efficient performance in large-alphabet scenarios (e.g., Unicode), where traditional Boyer-Moore skip tables become memory-intensive.
- For long-pattern, small-alphabet problems such as DNA matching, the algorithm avoids the performance degradation seen in standard Boyer-Moore variants.
- The algorithm degrades gracefully to an optimized KMP variant when hashing or random access is unavailable, maintaining worst-case efficiency.
- Benchmarking confirms the algorithm's strong performance across diverse workloads, including word searches, large texts, and sequence matching in generic C++ contexts.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.