The Fine-Grained Complexity of Episode Matching

Philip Bille, Inge Li Gørtz, Shay Mozes, Teresa Anna Steiner, Oren Weimann · DOI

Abstract

Given two strings $S$ and $P$, the Episode Matching problem is to find the shortest substring of $S$ that contains $P$ as a subsequence. The best known upper bound for this problem is $\tilde O(nm)$ by Das et al. (1997), where $n,m$ are the lengths of $S$ and $P$, respectively. Although the problem is well studied and has many applications in data mining, this bound has never been improved. In this paper we show why this is the case by proving that no $O((nm)^{1-\epsilon})$ algorithm (even for binary strings) exists, unless the Strong Exponential Time Hypothesis (SETH) is false. We then consider the indexing version of the problem, where $S$ is preprocessed into a data structure for answering episode matching queries $P$. We show that for any $\tau$, there is a data structure using $O(n+\left(\frac{n}\tau\right)^k)$ space that answers episode matching queries for any $P$ of length $k$ in $O(k\cdot \tau\cdot \log \log n )$ time. We complement this upper bound with an almost matching lower bound, showing that any data structure that answers episode matching queries for patterns of length $k$ in time $O(n^\delta)$, must use $\Omega(n^{k-k\delta-o(1)})$ space, unless the Strong $k$-Set Disjointness Conjecture is false. Finally, for the special case of $k=2$, we present a faster construction of the data structure using fast min-plus multiplication of bounded integer matrices.

Introduction

Episode Matching is a cornerstone problem in data mining and sequential pattern matching. Given a long text string $S$ of length $n$ and a pattern string $P$ of length $m$, the objective is to identify the shortest contiguous substring of $S$ that contains $P$ as a subsequence.

To distinguish these terms: a substring is a contiguous block of characters (e.g., "apple" is a substring of "snapple"), whereas a subsequence is a sequence derived by deleting zero or more characters without altering the order of the remaining ones (e.g., "ape" is a subsequence of "apple"). Since its formalization in 1997, the algorithmic standard for this problem has remained at $O(nm)$. Despite its widespread utility in discovering frequent episodes in event streams, the "quadratic" barrier has persisted for over 25 years. This research provides a definitive theoretical explanation for this stagnation using the framework of fine-grained complexity.

The $O(nm)$ Barrier and SETH

The primary contribution of this work is the proof that the existing $O(nm)$ algorithm is likely optimal. The researchers demonstrate that Episode Matching is "SETH-hard," meaning that any algorithm significantly faster than $O(nm)$—specifically $O((nm)^{1-\epsilon})$—would imply a breakthrough in solving the Satisfiability (SAT) problem, effectively disproving the Strong Exponential Time Hypothesis (SETH).

To establish this lower bound, the authors utilize a reduction from the Orthogonal Vectors (OV) problem. They developed a sophisticated "gadget" construction—including coordinate and separator gadgets—to encode vector information into strings. Notably, they prove that this hardness holds even for simple binary alphabets (strings consisting only of 0s and 1s). This suggests that Episode Matching is fundamentally difficult, potentially even harder than the Longest Common Subsequence (LCS) problem in scenarios where the pattern is significantly shorter than the text.

Indexing and Space-Time Trade-offs

In many real-world applications, the text $S$ is static while multiple queries $P$ are performed. This motivates the "indexing" version of the problem, where $S$ is preprocessed to accelerate future queries. The paper explores the limits of this approach, revealing a strict "no free lunch" rule regarding memory and speed.

The Upper Bound

The researchers propose a thresholding strategy based on a parameter $\tau$. By categorizing character combinations as "frequent" or "non-frequent":

  1. Frequent combinations are precomputed and stored in a lookup table.
  2. Non-frequent combinations are processed at query time using efficient predecessor data structures, such as y-fast tries.

For a pattern of fixed length $k$, this results in a data structure using $O(n + (n/\tau)^k)$ space with a query time of $O(k \cdot \tau \cdot \log \log n)$.

The Lower Bound

To determine if this trade-off is optimal, the authors link the indexing problem to the Strong $k$-Set Disjointness Conjecture. They prove that any data structure achieving a query time of $O(n^\delta)$ must necessarily occupy $\Omega(n^{k-k\delta-o(1)})$ space. This almost perfectly matches their proposed upper bound, suggesting that the discovered space-time trade-offs are near-optimal.

Optimization for Short Patterns ($k=2$)

For the specific case where the pattern length is $k=2$, the paper introduces an optimized construction method. By applying fast min-plus matrix multiplication for bounded integer matrices, the authors achieve a construction time that surpasses the naive $O(n\sigma)$ approach (where $\sigma$ is the alphabet size). While min-plus multiplication often carries high constant factors in practice, its theoretical application here provides a faster asymptotic bound for building the index.

Conclusion and Future Directions

This research provides a comprehensive map of the complexity landscape for Episode Matching. It confirms that the $O(nm)$ barrier is a fundamental limit of the problem rather than a lack of algorithmic ingenuity.

However, several avenues for future work remain. The current lower bounds are conditional on conjectures like SETH and $k$-Set Disjointness. Furthermore, the indexing results are optimized for a fixed pattern length $k$; developing a general data structure that handles variable $k$ efficiently without prohibitive space requirements remains an open challenge. Finally, while the results are theoretically robust, empirical benchmarking of the $k=2$ matrix multiplication strategy could determine its viability in practical data mining implementations.

Summary updated

Abstract

Given two strings $S$ and $P$, the Episode Matching problem is to find the shortest substring of $S$ that contains $P$ as a subsequence. The best known upper bound for this problem is $\tilde O(nm)$ by Das et al. (1997) , where $n,m$ are the lengths of $S$ and $P$, respectively. Although the problem is well studied and has many applications in data mining, this bound has never been improved. In this paper we show why this is the case by proving that no $O((nm)^{1-ε})$ algorithm (even for binary strings) exists, unless the Strong Exponential Time Hypothesis (SETH) is false. We then consider the indexing version of the problem, where $S$ is preprocessed into a data structure for answering episode matching queries $P$. We show that for any $τ$, there is a data structure using $O(n+\left(\frac{n}τ\right)^k)$ space that answers episode matching queries for any $P$ of length $k$ in $O(k\cdot τ\cdot \log \log n )$ time. We complement this upper bound with an almost matching lower bound, showing that any data structure that answers episode matching queries for patterns of length $k$ in time $O(n^δ)$, must use $Ω(n^{k-kδ-o(1)})$ space, unless the Strong $k$-Set Disjointness Conjecture is false. Finally, for the special case of $k=2$, we present a faster construction of the data structure using fast min-plus multiplication of bounded integer matrices.
BibTeX
@inproceedings{bille2022fine,
  author = {Philip Bille and Inge Li Gørtz and Shay Mozes and Teresa Anna Steiner and Oren Weimann},
  title = {{The Fine-Grained Complexity of Episode Matching}},
  booktitle = {CPM},
  year = {2022},
  doi = {10.4230/lipics.cpm.2022.4},
}
Figure