---
title: Exact Algorithm for CDTW
url: https://www.emergentmind.com/topics/exact-algorithm-for-cdtw
type: topic
---

# Exact Algorithm for CDTW

Continuous Dynamic Time Warping (CDTW) generalizes classical Dynamic Time Warping (DTW) to operate directly on the continuous parameterizations of polygonal curves. Unlike standard DTW—which matches sample points—CDTW matches subsegments flexibly, producing a similarity measure robust to sampling rate and outliers. Despite theoretical and practical advantages, the CDTW problem has required careful algorithmic development to realize exact polynomial-time solutions, particularly for 1D and certain 2D settings.

## 1. Formal Definition of CDTW

Given two 1D polygonal curves $P=p_1,\ldots,p_n$ and $Q=q_1,\ldots,q_m$ (with respective arc-length parameter ranges $[0,p]$, $[0,q]$), the CDTW similarity is defined via monotone parameterizations. The cost of matching is
\[
d_{CDTW}(P,Q) = \inf_{\gamma} \int_{0}^{p+q} \|P(\gamma_1(z)) - Q(\gamma_2(z))\|\,dz
\]
where $\gamma: [0,p+q] \to [0,p]\times[0,q]$ is a monotone path from $(0,0)$ to $(p,q)$ with $L_1$ or $L_2$ norm in $\mathbb{R}$ (or $\mathbb{R}^d$ for multi-dimensional cases) [2203.04531].

## 2. Dynamic Programming Framework for 1D Curves

The key challenge for CDTW is to propagate cost functions on cell boundaries in the continuous parameter space. The algorithm by Buchin et al. [2203.04531] divides the domain $[0,p]\times[0,q]$ into a grid, managing cost as piecewise-quadratic functions on cell boundaries.

- On boundary $y=y_j$, for $x\in[x_i,x_{i+1}]$, the maintained cost is
  \[
  f(x) = a_kx^2 + b_kx + c_k, \quad x\in[\alpha_k,\alpha_{k+1}]
  \]
- Propagation through cells considers all input points $s$ to an output $t$, combining existing cost $f(s)$ and the path integral $\int_s^t h\,dz$.

Each cell induces three cases for the optimal path structure, resulting in only $O(1)$ new quadratic pieces per input piece, computable in $O(1)$ time (Lemma~3.3, 4.3).

## 3. Polynomial-Time Algorithm and Complexity Bound

Combinatorial arguments show that the total number of quadratic pieces of all cost functions over the DP is $O((n+m)^5)$ across the grid. The main steps are:

- Partition the set of anti-diagonal boundaries into $O(k^2)$ subsegments (Lemma~5.1).
- On each subsegment $A_{k,\ell}$ of the $k$-th anti-diagonal, there are at most $O(k^2)$ quadratic pieces (Lemma~5.2).
- Construct lower envelopes of these quadratics via stack-based scanning in $O(M)$ time per cell, where $M$ is the number of input pieces.

This yields an $O(n^5)$-time and $O(n^4)$-space exact algorithm for one-dimensional curves [2203.04531].

## 4. Function Representation and Recurrence Propagation

Boundary cost functions on cell segments are maintained as lists of quadratic pieces $(a,b,c)$ over intervals. Propagation involves mapping each input quadratic to new output quadratics (types A, B, or C by optimal path type):

```pseudo
for each quadratic (a, b, c) in input:
    compute O(1) output quadratics via path structure analysis
    add to output list
envelope := build_lower_envelope(output_list)
```

The lower envelope maintenance is facilitated by the no-crossing property of optimal paths (Lemma 4.1), which ensures dominance can be resolved efficiently.

## 5. Extensions to 2D and General Norms

Exact computation under the Euclidean norm ($L_2$) for 2D curves is proven impossible in the algebraic computation model—certain CDTW values are transcendental and demand non-elementary operations (Baker's theorem; [2511.20420]). However, for polygonal norms (e.g., the gauge norm induced by a regular $k$-gon), an exact polynomial-time CDTW algorithm exists.

- Parameter-space is partitioned into $n\times m$ cells.
- Boundary cost functions are maintained as stacks of piecewise-quadratic segments.
- Propagation in each cell exploits geometric properties (valley lines/direct crossings), and candidates for cost are derived as bivariate quadratics.
- Overlay and lower envelope construction on border intervals scales as $O(k^2)$ per cell, with global complexity $O(N\cdot k^2\log k\, \alpha(k))$, where $N$ is the total number of pieces [2511.20420].

For the Euclidean setting, a $(1+\epsilon)$-approximation is achievable by substituting the Euclidean norm with a polygonal norm $R_k$, with $k=O(\epsilon^{-1/2})$ capturing the desired precision.

## 6. Critical Properties and Algorithmic Guarantees

- The 1D exact algorithm is the first to guarantee polynomial (quintic) bound on cost function complexity for CDTW [2203.04531].
- The method avoids any grid discretization, maintaining continuous DP propagation throughout.
- For 2D polygonal norms, exact computability and polynomial-time guarantee hold; for $L_2$, only approximation is possible in algebraic models.
- The dynamic programming propagation exploits invariants: local complexity growth is strictly bounded, cumulative minimality is stack-preserving, and envelope update does not explode the number of pieces.

## 7. Practical Considerations and Limitations

- While $O(n^5)$ is polynomial, the exponent is high; heuristics and pruning strategies are needed for practical application.
- Implementation demands robust representation and management of piecewise-quadratic functions and lower envelopes.
- Extending to higher dimensions or different norms (beyond polygonal) may introduce further complexity or intractability.
- Real-world clustering experiments demonstrate practical CDTW advantages over DTW and Fréchet measures [2203.04531].

---

References:
- Buchin et al. "Computing Continuous Dynamic Time Warping of Time Series in Polynomial Time" [2203.04531]
- "Fundamentals of Computing Continuous Dynamic Time Warping in 2D under Different Norms" [2511.20420]

Source: https://www.emergentmind.com/topics/exact-algorithm-for-cdtw