arXiv:2609.40249cs.DSSep 2026Cited by 2 papers on PaperVantage

Manuscript Offprint

arXiv:2609.40249

Dynamic Time Warping in the Low-Distance Regime

Itai Boneh, Shay Golan, Tomasz Kociumaka

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Dynamic Time Warping (DTW) is a classical similarity measure for strings and time series, designed to compare sequences up to local stretching. It is particularly natural when two sequences represent similar objects sampled or traversed at varying speeds. Given non-empty strings SS and TT over an alphabet Σ\Sigma and a mismatch-cost function δ:Σ2→R≥0\delta : \Sigma^2 \to \mathbb{R}_{\geq 0}, the dynamic time warping DTWδ(S,T)\text{DTW}_\delta(S, T) is the minimum value of ∑iδ(S′[i],T′[i])\sum_i \delta(S'[i], T'[i]) over all pairs of equal-length strings S′S' and T′T' obtained by duplicating characters in SS and TT, respectively. For strings of length at most nn, the value DTWδ(S,T)\text{DTW}_\delta(S, T) is computable in O(n2)O(n^2) time via dynamic programming [Sakoe and Chiba; 1978], and the running time cannot be reduced beyond n2−o(1)n^{2-o(1)} under the Orthogonal Vectors Hypothesis (OVH), and hence also under the Strong Exponential Time Hypothesis [Abboud, Backurs, and Vassilevska Williams; 2015] [Bringmann and Künnemann; 2015].

In many settings, however, one is interested not in arbitrary pairs of sequences but in highly similar ones when the optimal expansions incur only a few mismatches. This leads to the low-distance regime, where the running time is expressed in terms of nn and an integer budget k≥1k \geq 1 upper-bounding DTWδ(S,T)\text{DTW}_\delta(S, T). Under this parameterization, the standard assumption is that δ(a,a)=0\delta(a, a) = 0 and δ(a,b)≥1\delta(a, b) \geq 1 for a≠ba \neq b; otherwise, the parameter kk can be made meaningless by rescaling δ\delta. For several classical similarity measures, this low-distance regime admits much faster algorithms: a sequence of results beginning with unweighted edit distance [Landau and Vishkin; 1988] and recently extending to weighted edit distance and tree edit distance [Das, Gilbert, Hajiaghayi, Kociumaka, and Saha; 2023] yields running times of O(n+poly(k))O(n + \text{poly}(k)). For DTW with metric costs, the best known bound is O(nk)O(nk) [Kuszmaul; 2019], leaving open whether the dependence on nn and kk can be improved when kk is low.

We resolve this question negatively. Assuming the Orthogonal Vectors Hypothesis, we show that computing DTW requires n1−o(1)⋅kn^{1-o(1)} \cdot k time even for the discrete mismatch-cost function δ\delta that assigns cost 1 to every mismatch. Our lower bound applies to the whole spectrum of thresholds kk between constant and linear in nn.

The hardness proof is based on a new reduction from Orthogonal Vectors in which vector coordinates are encoded in the lengths of equal-character runs. The resulting instances are very structured: collapsing runs to single characters reveals long substrings with short periods. We complement the lower bound with an algorithm that runs in O~(n+poly(k))\tilde{O}(n + \text{poly}(k)) time whenever, after collapsing runs in the inputs, every substring with period O(k)O(k) has length poly(k)\text{poly}(k).

Finally, we extend this lower bound to DTW pattern matching, which asks whether any non-empty substring of a length-n text has DTW distance at most k from a length-m pattern. We prove that the classic O(nm)O(nm)-time dynamic-programming algorithm is near-optimal under OVH, even when the threshold k is only logarithmic in n.

Funding Itai Boneh: Partially supported by Israel Science Foundation grant 810/21. Partially supported by the Polish National Science Centre grant number 2023/51/B/ST6/01505; Shay Golan: Partially supported by Israel Science Foundation grant 810/21.

1 Introduction

Dynamic Time Warping (DTW) is a classical similarity measure for sequences, particularly well suited to time series recorded at varying speeds. It was introduced in the context of speech recognition [Ita75, SC78] and has since found applications in a variety of areas, including data mining [RCM +^{+}12, WMD +^{+}13], bioinformatics [AC01], chemometrics [TvdBA04], music processing [Mül07], motion analysis [SMA11], handwriting recognition [DNO +^{+}08], and signature verification [MP99].

Dynamic Time Warping is defined for strings over an alphabet Σ\Sigma equipped with a mismatch-cost function δ:Σ2→R≥0\delta : \Sigma^2 \to \mathbb{R}_{\geq 0}. For two strings X,Y∈ΣnX, Y \in \Sigma^n of the same length nn, the weighted Hamming distance is HDδ(X,Y):=∑i=1nδ(X[i],Y[i])\text{HD}_\delta(X, Y) := \sum_{i=1}^n \delta(X[i], Y[i]). For non-empty strings S,T∈Σ+S, T \in \Sigma^+, the DTW distance is defined as the minimum weighted Hamming distance between same-length expansions (time warps) of SS and TT, obtained by arbitrarily duplicating characters in SS and TT, respectively:

DTWδ(S,T):=min⁡S′,T′:same−lengthexpansionsofSandTHDδ(S′,T′).\mathrm{DTW}_{\delta}(S,T):=\min_{S^{\prime},T^{\prime}:same-length expansions of S and T}\mathrm{HD}_{\delta}(S^{\prime},T^{\prime}).

For the discrete metric (unit mismatch-cost function), we omit the subscript and write DTW(S, T); for example, DTW(aab, aba) = HD(aabb, aaba) = 1.

Problem Dynamic Time Warping (DECISION VERSION).

Input. Non-empty strings S,T∈Σ≤nS, T \in \Sigma^{\leq n}, a threshold k∈R≥0k \in \mathbb{R}_{\geq 0}, and oracle access to a mismatch-cost function δ:Σ2→R≥0\delta : \Sigma^2 \to \mathbb{R}_{\geq 0}.

Task. Decide whether DTWδ(S,T)≤k\text{DTW}_\delta(S, T) \leq k.

More generally, one may ask to compute DTWδ(S,T)\text{DTW}_\delta(S, T) and output optimal expansions of SS and TT; without loss of generality, their common length is at most ∣S∣+∣T∣|S| + |T|. When S,T∈Σ≤nS, T \in \Sigma^{\leq n} and δ\delta can be evaluated in constant time (as we assume throughout), this can be done via the standard O(n2)O(n^2)-time dynamic program already presented in [SC78]. More recently, Abboud, Backurs, and Vassilevska Williams [ABW15] as well as Bringmann and Künnemann [BK15] showed that, under the Strong Exponential Time Hypothesis (SETH), DTW does not admit an n2−Ω(1)n^{2-\Omega(1)}-time algorithm. Their lower bounds already hold under the Orthogonal Vectors Hypothesis (OVH), which asserts quadratic hardness for finding an orthogonal pair across two sets of binary vectors (see Hypothesis 3.13). They hold even for simple costs: the discrete metric over a constant-size alphabet [ABW15] and the absolute-difference metric over constantly many integers [BK15].

Only a few settings are known in which these lower bounds can be circumvented. These include binary strings [ABW15, Kus21], strings that are compressible with respect to run-length encoding [FJRW23, BGMW24], and the low-distance regime [Kus19], which is the main focus of this work, where the running time may depend on kk and is optimized for k≪nk \ll n. Kuszmaul [Kus19] gave an O(n+nk)O(n + nk)-time algorithm for metric costs normalized so that δ(a,a)=0\delta(a, a) = 0 and δ(a,b)≥1\delta(a, b) \geq 1 for a≠ba \neq b, and used it to approximate DTW.

This O(n+nk)O(n + nk) bound has not been improved even for the discrete metric despite the fact that the closely related edit distance [Lev65] has admitted a faster O(n+k2)O(n + k^2)-time algorithm for more than 35 years [LV88]. More recently, the O(n+nk)O(n + nk) bound for edit distance [Ukk85] has been improved for weighted edit distance under normalized cost functions, first to O(n+k5)O(n + k^5) [DGH +^{+}23], then to O~(n+nk3)\tilde{O}(n + \sqrt{nk^3}) [CKW23], 1^1 and, for integer weights bounded by a constant, to O~(n+k2)\tilde{O}(n + k^2) [GK25]. These results raised hopes for analogous running times for DTW. Our first main result rules out this possibility.

1^{1} The O~(⋅)\tilde{O}(\cdot) notation hides polylogarithmic factors in the input size, which is n in the case of DTW and edit distance.

Dynamic Time Warping in the Low-Distance Regime

Main Theorem 1. Assuming the Orthogonal Vectors Hypothesis (Hypothesis 3.13), the following holds for every constant 0<ε<10 < \varepsilon < 1. Let (kn)n=1∞(k_n)_{n=1}^\infty be an integer sequence whose entries knk_n are computable in O(n)O(n) time and satisfy 1≤kn≤12n1 \leq k_n \leq \frac{1}{2}n for all sufficiently large nn. No deterministic algorithm for the DTW problem can solve all instances with ∣S∣=∣T∣=n|S| = |T| = n and k=knk = k_n in time O(n1−ε⋅kn)O(n^{1-\varepsilon} \cdot k_n).

The lower bound already holds for the unit mismatch-cost function δ\delta over an alphabet of constant size. Under randomized OVH, the same statement holds for bounded-error randomized algorithms.

In short, we show that O(n+nk)O(n + n k) time is near-optimal even for the discrete (unit-cost) metric. Our formal statement is stronger than what is usually proved in multivariate fine-grained complexity (see [BK18]): it rules out significant improvements for every threshold kk given as an efficiently computable function of nn, rather than only for thresholds of the form k∈Θ(nκ)k \in \Theta(n^\kappa) with a fixed exponent 0≤κ≤10 \leq \kappa \leq 1.

Pattern Matching for Dynamic Time Warping Another setting where the low-distance regime is easier for edit distance is the pattern matching version of the problem. Here, the task is to decide whether a pattern of length m is similar to any non-empty substring of a text of length n. More generally, one may ask to locate all such substrings. Dynamic Time Warping has been studied in this setting both in theoretical [SI22, GDPS22] and applied contexts [KKS05, PKCP01, WWP +^{+}19], under various names, including subsequence matching, even though only substrings (contiguous subsequences) are sought.

Problem Pattern Matching for Dynamic Time Warping (DECISION VERSION).

Input. A non-empty pattern P∈ΣmP \in \Sigma^m, a non-empty text T∈ΣnT \in \Sigma^n, a threshold k∈R≥0k \in \mathbb{R}_{\geq 0}, and oracle access to a mismatch-cost function δ:Σ2→R≥0\delta : \Sigma^2 \to \mathbb{R}_{\geq 0}.

Task. Decide whether there exists a non-empty substring T[t…t′]T[t \ldots t'] such that DTWδ(P,T[t…t′])≤k\text{DTW}_\delta(P, T[t \ldots t']) \leq k.

The PMDTW problem can be solved in O(nm)O(nm) time by a straightforward adaptation of the textbook dynamic-programming solution to the DTW problem, and the results of [ABW15, BK15] can be extended to show that no significantly faster algorithms exist under OVH. Under the discrete metric, the zero-distance case is solvable in O(n+m)O(n + m) time, and the same bound is known for k=1k = 1 [GDPS22].

In contrast, the analogous problem for unit-cost edit distance admits the classical O(n+nk)O(n + nk)-time algorithm [LV89] and can even be solved in O(n+⌈n/m⌉k4)O(n + \lceil n/m \rceil k^4) [CH02] or O~(n+⌈n/m⌉k3.5)\tilde{O}(n + \lceil n/m \rceil k^{3.5}) [CKW22] time. Recently, the first two running times have been recovered up to polylogarithmic factors for normalized weights; the third running time is recovered for integer metric weights bounded by a constant [CKW25].

Our second main result shows that no analogous improvement is possible for DTW: the textbook O(nm)O(nm) running time remains essentially optimal even when k is logarithmic in the text length.

Main Theorem 2. Assuming the Orthogonal Vectors Hypothesis (Hypothesis 3.13), for every 0<ε<10 < \varepsilon < 1, there exists cε>0c_\varepsilon > 0 such that the following holds. Let (mn)n=1∞(m_n)_{n=1}^\infty and (kn)n=1∞(k_n)_{n=1}^\infty be integer sequences computable in O(n)O(n) time and satisfying cεlog⁡n≤kn≤12mn≤12nc_\varepsilon \log n \leq k_n \leq \frac{1}{2} m_n \leq \frac{1}{2} n for all sufficiently large nn. No deterministic algorithm for the PMDTW problem can solve all instances with ∣P∣=mn|P| = m_n, ∣T∣=n|T| = n, and k=knk = k_n in time O(n1−ε⋅mn)O(n^{1-\varepsilon} \cdot m_n).

The lower bound already holds for the unit-cost function δ\delta over an alphabet of constant size. Under randomized OVH, the same statement holds for bounded-error randomized algorithms.

What Makes Dynamic Time Warping Hard? The instances we construct to prove Main Theorems 1 and 2 not only establish hardness but also reveal a concrete structural obstruction for computing DTW fast.

To describe this obstruction, we use the shrunk version shrink(X) of a string X∈Σ∗X \in \Sigma^*, obtained by collapsing each maximal run of consecutive equal characters into a single copy of that character; for example, shrink(aaaabba) = aba. This notion is relevant for DTW because, under the discrete metric, DTW(X, Y) = 0 holds for strings X,Y∈Σ+X, Y \in \Sigma^+ if and only if shrink(X) = shrink(Y). We also use the notion of the period per(X) of a string X∈Σ+X \in \Sigma^+, defined as the smallest integer p∈[1…∣X∣]p \in [1 \ldots |X|] such that X[i]=X[i+p]X[i] = X[i + p] for all i∈[1…∣X∣−p]i \in [1 \ldots |X| - p]; for example, per(abaabaabaa) = 3 because abaabaabaa = aba · aba · aba · a.

An interesting feature of the strings constructed to prove Main Theorems 1 and 2 is that their shrunk versions contain very long substrings with very small periods. Concretely, each of the constructed length-n strings contains, after shrinking, a substring of length Ω(n/log⁡n)\Omega(n/\log n) and period O(log⁡n)O(\log n).

Motivated by this phenomenon, for an integer p≥1p \geq 1, we define Lp(X)L_p(X) to be the length of the longest substring of shrink(X) with period at most pp, and set L0(X):=0L_0(X) := 0. We investigate how the complexity of the DTW problem depends on this measure. For a real threshold k≥0k \geq 0, set p:=24⌈k⌉p := 24 \lceil k \rceil. Our algorithm solves the DTW problem in O~(n+Lp(S)⋅k2)\widetilde{O}(n + L_p(S) \cdot k^2) time for every normalized mismatch-cost function δ\delta.

Main Theorem 3. There is an algorithm that, given an instance of the DTW problem with a normalized mismatch-cost function and a real threshold k≥0k \geq 0, solves it in time O~(n+Lp(S)⋅k2)\widetilde{O}(n + L_p(S) \cdot k^2), where p:=24⌈k⌉p := 24 \lceil k \rceil. In case of a YES answer, the algorithm outputs an optimal alignment (that is, optimal expansions of S and T).

In particular, inputs with Lp(S)=poly(k)L_p(S) = \text{poly}(k) admit an O~(n+poly(k))\widetilde{O}(n + \text{poly}(k))-time algorithm.

Computational Model We use the word RAM with w=Θ(log⁡n)w = \Theta(\log n)-bit words, storing each input character in a separate word and assuming constant-time oracle access to the mismatch-cost function. Our algorithm supports O(w)O(w)-bit integer weights and extends to real weights in the real RAM model [EvdHM24]; precise conventions appear in Section 3. Our deterministic lower bounds assume OVH, and their bounded-error randomized counterparts assume randomized OVH, defined alongside Hypothesis 3.13.

References (59)