Abstract
Internal Pattern Matching (IPM) queries find all occurrences of a fragment $X$ within another fragment $Y$ of a text $T$, typically under the constraint $|Y| < 2|X|$. While static texts support $O(1)$ queries with linear space, compressed and dynamic representations have historically faced a performance gap. This paper introduces an $O(\log n)$-time algorithm for IPM queries on balanced run-length straight-line programs (RLSLPs). By operating directly on the grammar without extra preprocessing, this method optimizes the PILLAR model for approximate pattern matching in compressed and dynamic environments.
Introduction: The IPM Problem
Internal Pattern Matching (IPM) is a fundamental string operation. Given two fragments $X$ and $Y$ of a text $T$, the goal is to locate all instances of $X$ inside $Y$. In the context of the "PILLAR model"—a framework designed to unify approximate pattern matching across various settings—IPM queries were a significant bottleneck. While operations like Longest Common Extension (LCE) queries had been optimized to $O(\log n)$ time for compressed strings, IPM queries lagged at $O(\log^3 n)$ or worse.
This paper addresses this gap by providing a logarithmic-time solution that works for texts represented as run-length straight-line programs (RLSLPs). These structures are essential for handling massive datasets that are either highly repetitive (compressed) or subject to frequent edits (dynamic).
Recompression and RLSLPs
The core of the proposed approach lies in the structure of the RLSLP, which is built using a technique called recompression. Recompression transforms a text into a grammar by alternating between two types of compression steps:
- Run-Length Encoding (rle): Compressing blocks of identical characters (e.g.,
aaaabecomesa^4). - Pair Compression (pair): Compressing adjacent pairs of distinct characters into a single new symbol.
As shown in the figure above, this process creates an "uncompacted parse tree" where each level represents a round of compression. The algorithm navigates this hierarchy to find matches without ever fully decompressing the string.
The $O(\log n)$ Query Algorithm
The algorithm achieves its efficiency through three primary stages:
- Proxy Level Selection: The algorithm identifies a "proxy level" $\ell$ in the RLSLP where the fragments $X$ and $Y$ are represented by compressed "proxies" $\bar{X}_\ell$ and $Y'_\ell$. These proxies are small enough to be processed in time proportional to the number of grammar levels, which is $O(\log n)$.
- Pattern Matching on Compressed Strings: It performs pattern matching on the run-length encoded proxy strings. Because the query is restricted to $|Y| < 2|X|$, the starting positions of any occurrences of $X$ in $Y$ must form an arithmetic progression.
- Verification via LCE: Instead of checking every possible match, the algorithm uses a constant number of LCE queries to verify the entire arithmetic progression at once. This "bulk verification" is key to maintaining logarithmic complexity.
A major advantage of this method is that it requires zero preprocessing of the RLSLP. It can be implemented directly on top of existing dynamic string data structures, providing IPM functionality as a "drop-in" upgrade.
Experimental Validation
The authors implemented the algorithm in Rust and tested it on synthetic datasets, specifically prefixes of the infinite 3-Fibonacci string. These strings are highly periodic and compressible, making them ideal for stress-testing grammar-based algorithms.
The experiments confirmed the theoretical $O(\log n)$ scaling for string lengths up to $2^{32}$ (approximately 4 billion characters). Furthermore, the memory footprint remained minimal; for a string of length $2^{20}$, the data structure occupied less than 6KB.
Conclusion and Future Work
This work completes the optimization of the PILLAR model for compressed and dynamic texts, bringing IPM queries to the same $O(\log n)$ efficiency as LCE queries. While the current implementation serves as a robust proof-of-concept, future work will focus on heuristic optimizations for real-world (non-synthetic) data and developing methods to construct RLSLPs directly from other compressed formats like LZ77.
Summary updated