Table of Contents
Fetching ...

Approximate Cartesian Tree Matching with One Difference

Bastien Auvray, Julien David, Samah Ghazawi, Richard Groult, Gad M. Landau, Thierry Lecroq

TL;DR

This work advances approximate Cartesian tree pattern matching with a single difference by introducing a flexible MetaAlgorithm that uses two linear representations (forward/reverse parent-distance and Skipped-number) to test CT-equivalence under one swap, mismatch, insertion, or deletion, achieving $O(nm)$ worst-case time with favorable average-case behavior. It complements this with an automaton-based method (inspired by PALP19) that builds a bounded-size automaton to recognize swap-affected representations, yielding $O((m^2+n)\log m)$ worst-case time and $O(m^2)$ space, scalable to more than one difference. The paper also provides deep structural analyses of how the PD and SN representations change under a swap, introduces a swap graph over Cartesian trees, and demonstrates practical performance through experiments supporting constant average-case behavior under random models. Together, these advances enable practical, theory-grounded approximate CTM with one difference and lay groundwork for extensions to broader error models and partial orders.

Abstract

Cartesian tree pattern matching consists of finding all the factors of a text that have the same Cartesian tree than a given pattern. There already exist theoretical and practical solutions for the exact case. In this paper, we propose the first algorithms for solving approximate Cartesian tree pattern matching with one difference given a pattern of length m and a text of length n. We present a generic algorithm that find all the factors of the text that have the same Cartesian tree of the pattern with one difference, using different notions of differences. We show that this algorithm has a O(nM) worst-case complexity and that, for several random models, the algorithm has a linear average-case complexity. We also present an automaton based algorithm, adapting [PALP19], that can be generalized to deal with more than one difference.

Approximate Cartesian Tree Matching with One Difference

TL;DR

This work advances approximate Cartesian tree pattern matching with a single difference by introducing a flexible MetaAlgorithm that uses two linear representations (forward/reverse parent-distance and Skipped-number) to test CT-equivalence under one swap, mismatch, insertion, or deletion, achieving worst-case time with favorable average-case behavior. It complements this with an automaton-based method (inspired by PALP19) that builds a bounded-size automaton to recognize swap-affected representations, yielding worst-case time and space, scalable to more than one difference. The paper also provides deep structural analyses of how the PD and SN representations change under a swap, introduces a swap graph over Cartesian trees, and demonstrates practical performance through experiments supporting constant average-case behavior under random models. Together, these advances enable practical, theory-grounded approximate CTM with one difference and lay groundwork for extensions to broader error models and partial orders.

Abstract

Cartesian tree pattern matching consists of finding all the factors of a text that have the same Cartesian tree than a given pattern. There already exist theoretical and practical solutions for the exact case. In this paper, we propose the first algorithms for solving approximate Cartesian tree pattern matching with one difference given a pattern of length m and a text of length n. We present a generic algorithm that find all the factors of the text that have the same Cartesian tree of the pattern with one difference, using different notions of differences. We show that this algorithm has a O(nM) worst-case complexity and that, for several random models, the algorithm has a linear average-case complexity. We also present an automaton based algorithm, adapting [PALP19], that can be generalized to deal with more than one difference.
Paper Structure (31 sections, 27 theorems, 39 equations, 15 figures, 8 algorithms)

This paper contains 31 sections, 27 theorems, 39 equations, 15 figures, 8 algorithms.

Key Result

Lemma 2

Let us consider an $\textsc{equivalenceTest}$ function between linear representations of two sequences $x$ and $p$ of length $m$. Assuming we have a probabilistic model that guarantees that there exists a constant $\kappa \in (0,1)$ such that for all position $1\le i \le m-1$ we have: then the average-case complexity of $\textsc{equivalenceTest}$ is $\Theta(1)$.

Figures (15)

  • Figure 1: A sequence $x = (3, 5, 6, 2, 1, 7, 8, 4, 9)$, its Cartesian tree $C(x)$ and its corresponding parent-distance table $\overrightarrow{PD}_x$ and $\overleftarrow{PD}_x$. We have $\operatorname{rb}(C(x)) = (5,8,9)$.
  • Figure 2: The construction of $C(x[1\ldots h])$ from $C(x[1\ldots h-1])$. Node $h$ is inserted at the end of the rightmost path of $C(x[1\ldots h-1])$.
  • Figure 3: The Cartesian trees of $x=(5,7,4,1,6,8,9,2,3)$ and its associated tables.
  • Figure 4: Let $x=(2,3,4,1,5,7,8,6,9,)$ and $y=(4,5,6,3,1,7,8,2,9)$. The sequence $x$$CT_\tau$ matches $y$ (see Definition \ref{['ct-tau']} with $x'=(4,5,6,1,2,7,8,3,9)$ and $y'=(4,5,6,2,1,7,8,3,9)$). A swap at position $4$ moves the red node from the right subtree of the root to the left one. In general, a swap at position $i$ consists either in moving the leftmost descendant of the right subtree to a rightmost position in the left subtree (that is if $x[i] < x[i+1]$), or the opposite, in moving the rightmost descendant of the left subtree to a leftmost position of the right subtree of its parent. Note that we also have $x \overset{\tau}{\approx}_{CT} y'$, $x' \overset{\tau}{\approx}_{CT} y$ and of course $x' \overset{\tau}{\approx}_{CT} y'$.
  • Figure 5: Examples of approximate Cartesian tree matching when the error respectively comes, from left to right, from a mismatch, an insertion or a deletion. The sequence at the top does not change, but the equivalent one at the bottom does.
  • ...and 10 more figures

Theorems & Definitions (74)

  • definition 1: Cartesian tree $C(x)$, $C_h(x)$, $\mathcal{C}$, $\mathcal{C}_m$
  • definition 2: left and right subtrees and branches
  • definition 3: Cartesian tree matching (CTM)
  • definition 4: Parent-distance representation $\overrightarrow{PD}_x$
  • definition 5: Reverse parent-distance representation $\overleftarrow{PD}_x$
  • definition 6: Skipped-nodes representation $\operatorname{rbs}_x$
  • definition 7: Skipped-number representation $SN_x$
  • definition 8: Referent $\operatorname{ref}_{x}$
  • definition 9: Swap $\tau(x,i)$
  • definition 10: $CT_\tau$ matching
  • ...and 64 more