NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Contextual Pattern Mining and Counting

Ling Li, Daniel Gibney, Sharma V. Thankachan, Solon P. Pissis, Grigorios LoukidesarXiv 2025

TL;DR

This paper introduces two new string-processing problems focused on analyzing the context (the surrounding characters) of a pattern: Contextual Pattern Mining (CPM) and Contextual Pattern Counting (CPC). The authors develop linear-work algorithms and optimized data structures that scale efficiently to billion-letter datasets, outperforming existing reporting-based baselines by up to three orders of magnitude in query time while drastically reducing memory usage.

What Problem Does This Paper Address?

Standard string algorithms excel at finding where a pattern $P$ appears or how often it occurs. However, in fields like bioinformatics and text mining, the environment immediately surrounding a pattern is often just as critical as the pattern itself. For example, in genomics, "flanking sequences" to the left and right of a specific DNA pattern help researchers understand gene regulation and DNA shape fluctuations. In text mining, the words surrounding a ambiguous word like "bank" (e.g., "river" vs. "savings") clarify its meaning.

The authors formalize this surrounding environment as the context $(L, R)$ of a pattern $P$, where $L$ and $R$ are strings of length $l$ and $r$ directly to its left and right. They address two distinct challenges:

  1. Contextual Pattern Mining (CPM): Finding all patterns of a fixed length that appear in at least $\tau$ distinct contexts across a text.
  2. Contextual Pattern Counting (CPC): Preprocessing a text so that a user can instantly query a pattern and get the total count of its unique surrounding contexts.

Prior approaches are highly impractical for massive datasets because they rely on reporting every single occurrence of a pattern before deduplicating the contexts. If a pattern appears millions of times, these reporting-based methods suffer from severe performance bottlenecks.

Methods

The authors decouple counting from reporting by introducing algorithmic frameworks tailored for both internal and external memory.

  • For CPM: They design a linear-work framework utilizing Suffix Arrays (SA) and Longest Common Prefix (LCP) arrays. It features an Internal Memory (IM) version running in $O(n)$ time and space, alongside an External Memory (EM) version. The EM version optimizes disk I/O operations ($O(\text{sort}(n))$ I/Os), allowing researchers to specify a maximum RAM limit and offload massive datasets to disk storage.
  • For CPC: A naive approach requires quadratic space, which is unusable for large texts. To solve this, the authors combine Heavy-Path Decomposition on suffix trees with Orthogonal Range Counting data structures, mapping the string problem into a geometric coordinate system.
  • LZ77 Optimization: To make the CPC index truly practical, they leverage Lempel-Ziv (LZ77) factorization and a user-defined maximum query length bound $B$. Since every substring has a "primary occurrence" intersecting an LZ77 phrase boundary, they can safely truncate the suffix and prefix trees. This bounds the index space by the number of compression phrases rather than the raw text length $n$.

The relationship between the main suffix tree and these auxiliary prefix trees used for range counting is illustrated below:

Figure 2: (Left) The suffix tree for the string banana$. (Right) The prefix trees PT(u1) and PT(u2). The string depths in all trees are indicated in boxes and each leaf is labeled with the corresponding suffix array value.

Datasets

The algorithms were evaluated using five large-scale, real-world datasets spanning multiple domains:

  • WIKI: A collection of Wikipedia article revisions about Albert Einstein ($\approx 474$ million letters).
  • BST: A repository containing all commits from the Boost GitHub repository ($\approx 3.1$ billion letters).
  • SDSL: A repository containing all commits from the SDSL GitHub repository ($\approx 5.5$ billion letters).
  • SARS: A genomic dataset consisting of SARS-CoV-2 sequences ($10$ billion letters).
  • CHR: Chromosome 19 sequences from 1,000 human haplotypes ($16$ billion letters).

Key Insights and Ideas

  • Geometric Range Counting Avoids the Reporting Trap: Traditional text indexes scale linearly with the number of pattern occurrences. By translating context counting into multi-dimensional range queries, the authors ensure that query time depends on the length of the query and a small polylogarithmic factor, completely independent of how frequently the pattern appears in the text.
  • Exploiting Redundancy via Phrase Boundaries: The integration of LZ77 compression with a query length bound allows the index to ignore massive stretches of repetitive text. Because distinct contexts must manifest around the boundaries of compression phrases, the index can be aggressively truncated without sacrificing accuracy.

Key Results

  • Massive Scalability for Mining: The external memory version of the CPM algorithm successfully processed the 16-billion-letter human genome dataset (CHR) in 5.4 hours while restricted to a strict 32 GB RAM limit. In contrast, standard internal memory approaches crashed, requiring more than 512 GB of RAM.
  • Index Size Reduction: On an 8-billion-letter genomic prefix, the optimized CPC index was constructed 52% faster, used 75% less memory during construction, and resulted in an index 74% smaller than the state-of-the-art reporting baseline (49 GB vs. 425 GB).
  • Orders-of-Magnitude Speedups: The optimized CPC index achieved query speeds 2 to 3 orders of magnitude faster than competing methods, maintaining flat, sub-millisecond response times even as context lengths scaled up.
Figure 1: Average query time vs. l = r for all length-6 substrings.

Limitations and Future Work

  • Query Length Constraints: The highly optimized version of the CPC index requires an upfront parameter $B$ that bounds the maximum query length ($l + m + r \le B$). If a user needs to query a context longer than $B$, the index must either fall back to a slower unoptimized mode or be entirely reconstructed.
  • Implementation Trade-offs: While the paper utilizes advanced theoretical range-counting structures to prove its time bounds, the actual implementation relies on practical spatial data structures like $R^*$-trees. The authors suggest that further engineering of specialized geometric data structures could yield even higher performance gains.

Summary updated

BibTeX
@misc{li2025contextual,
  author = {Ling Li and Daniel Gibney and Sharma V. Thankachan and Solon P. Pissis and Grigorios Loukides},
  title = {{Contextual Pattern Mining and Counting}},
  year = {2025},
  eprint = {2506.17613},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.17613},
}
Figure