Towards Efficient Interactive Computation of
Dynamic Time Warping Distance
Abstract
The dynamic time warping (DTW) is a widely-used method that allows us to efficiently compare two time series that can vary in speed. Given two strings and of respective lengths and , there is a fundamental dynamic programming algorithm that computes the DTW distance for and together with an optimal alignment in time and space. In this paper, we tackle the problem of interactive computation of the DTW distance for dynamic strings, denoted , where character-wise edit operation (insertion, deletion, substitution) can be performed at an arbitrary position of the strings. Let and be the sizes of the run-length encoding (RLE) of and , respectively. We present an algorithm for that occupies space and uses time to update a compact differential representation of the DP table per edit operation, where denotes the number of cells in whose values change after the edit operation. Our method is at least as efficient as the algorithm recently proposed by Froese et al. running in time, and is faster when is smaller than which, as our preliminary experiments suggest, is likely to be the case in the majority of instances.
1 Introduction
The dynamic time warping (DTW) is a classical and widely-used method that allows us to efficiently compare two temporal sequences or time series that can vary in speed. A fundamental dynamic programming algorithm computes the DTW distance for two strings and together with an optimal alignment in time and space [12], where and . This algorithm allows one to update the DP table for in time (resp. time) when a new character is appended to (resp. to ).
In this paper, we introduce the “dynamic” DTW problem, denoted , where character-wise edit operation (insertion, deletion, substitution) can be performed at an arbitrary position of the strings. More formally, we wish to maintain a (space-efficient) representation of that can dynamically be modified according to a given operation. This representation should be able to quickly answer the value of upon query, together with an optimal alignment achieving . This kind of interactive computation for (a representation of) can be of practical merits, e.g. when simulating stock charts, or editing musical sequences. Another example of applications of is a sliding window version of DTW which computes between and every substring of of arbitrarily fixed length .
Incremental/decremental computation of a DP table is a restricted version of the aforementioned interactive computation, which allows for prepending a new character to , and/or deleting the leftmost character from . A number of incremental/decremental computation algorithms have been proposed for the unit-cost edit distance and weighted edit distance: Kim and Park [9] showed an incremental/decremental algorithm for the unit-cost edit distance that occupies space and runs in time per operation. Hyyrö et al. [7] proposed an algorithm for the edit distance with integer weights which uses space and runs in time per operation, where is the maximum weight in the cost function. This translates into time under constant weights. Schmidt [13] gave an algorithm that uses space and runs in time per operation for a general weighted edit distance. Hyyrö and Inenaga [5] presented a space efficient alternative to incremental/decremental unit-cost edit distance computation which runs in time per operation but uses only space, where and are the sizes of run-length encoding (RLE) of and , respectively. Since and always hold, the terms can be much smaller than the term for strings that contain many long character runs. Later, Hyyrö and Inenaga [6] presented a space-efficient alternative for edit distance with integer weights, which runs in time per operation and requires space.
Fully-dynamic interactive computation for the (weighted) edit distance was also considered: Let be the position in where the modification has been performed. For the unit cost edit distance, Hyyrö et al. [8] presented a representation of the DP table which uses space and can be updated in time per operation, where and is the maximum weight. They also showed that there exist instances that require time to update their data structure per operation. Very recently, Charalampopoulos et al. [3] showed how to maintain an optimal (weighted) alignment of two fully-dynamic strings in time per operation, where .
While computing longest common subsequence (LCS) and weighted edit distance of strings of length can both be reduced to computing DTW of strings of length [1, 10], a reduction to the other direction is not known. It thus seems difficult to directly apply any of the aforementioned algorithms to our problem. Also, a conditional lower bound suggests that strongly sub-quadratic DTW algorithms are unlikely to exist [1, 2]. Thus, any method that recomputes the nav̈e DP table from scratch should take almost quadratic time per update.
Our contribution. This paper takes the first step towards an efficient solution to . Namely, we present an algorithm for that occupies space and uses time to update a compact differential representation for the DP table per edit operation, where denotes the number of cells in whose values change after the edit operation. Since always holds, our method is always at least as efficient as the naïve method that recomputes the full DP table in time, or the algorithm of Froese et al. [4] that recomputes another sparse representation of in time. While there exist worst-case instances that give , our preliminary experiments suggest that, in many cases, can be much smaller than the size of which is .
2 Preliminaries
We consider sequences (strings) of characters from an alphabet of real numbers. Let be a string consisting of characters from . The run-length encoding of string is a compact representation of such that each maximal run of the same characters in is represented by a pair of the character and the length of the run. More formally, let denote the set of positive integers. For any non-empty string , , where and for any , and for any . Each in is called a (character) run, and is called the exponent of this run. The size of is the number of runs in . E.g., for string of length 16, and its size is 5.
Dynamic time warping (DTW) is a commonly used method to compare two temporal sequences that may vary in speed. Consider two strings and . To formally define the DTW for and , we consider an grid graph such that each vertex has (at most) three directed edges; one to the lower neighbor (if it exists), one to the right neighbor (if it exists), and one to the lower-right neighbor (if it exists). A path in that starts from vertex and ends at vertex is called a warping path, and is denoted by a sequence of adjacent vertices. Let be the set of all warping paths in . Note that each warping path in corresponds to an alignment of and . The DTW for strings and , denoted , is defined by .
The fundamental -time and space solution for computing , given in [12], fills an dynamic programming table such that for and . Therefore, after all the cells of are filled, the desired result can be obtained by . The value for each cell is computed by the following well-known recurrence:
(1) |
In the rest of this paper, we will consider the problem of maintaining a representation for , each time one of the strings, , is dynamically modified by an edit operation (i.e. single character insertion, deletion, or substitution) on an arbitrary position in . We call this kind of interactive computation of as the dynamic DTW computation, denoted by .
Let denote the string after an edit operation is performed on , and denote the dynamic programming table after it has been updated to correspond to . In a special case where the edit operation is performed at the right end of , where we have (insertion), (deletion) or (substitution) with a character , then can easily be updated to in time by simply computing a single column at index or using recurrence (1).

As in Figure 1, in the worst case, the values of cells of the DP table for can change after an edit on . The following lemma gives a stronger statement that updating to in our scenario cannot be amortized:
Lemma 1
There are strings , and a sequence of edits on such that cells in have different values in the corresponding cells in .
3 Our Algorithm based on RLE
We first explain the data structures which are used in our algorithm.
Differential representation of . The first idea of our algorithm is to use a differential representation of : Each cell of contains two fields that respectively store the horizontal difference and the vertical difference, namely, and . We let for any and for any . The diagonal difference can easily be computed from and and thus is not explicitly stored in .
In our algorithm we make heavy use of the following lemma:
Lemma 2
For any ,
and for any ,
where .
RLE-based sparse differential representation . The second key idea of our algorithm is to divide the dynamic programming table into “boxes” that are defined by intersections of maximal runs of and . Note that contains such boxes. Let and be the RLEs of and . Let , , , and . We define a sparse table for that consists only of the rows and columns on the borders of the maximal runs in and . Namely, is a sparse table that only stores the rows and the columns , of (see Figure 3).
![]() ![]() |
Each row and column of is implemented by a linked list as follows: each cell has four links to the upper, lower, left, and right neighbors in (if these neighbors exist), plus a diagonal link to the right-lower direction. This diagonal link from points to the first cell that is reached by following the right-lower diagonal path from , namely, is the smallest integer such that or . Clearly occupies space. can answer in time by tracing cells of from to .
For each and , we consider the region of that is surrounded by the borders of the th and th runs of , and the th and th runs of . This region is called a box for , and is denoted by . For ease of description, we will sometimes refer to a box also in and .
3.1 Updating after an edit operation
Suppose that an edit operation has been performed at position of string and let denote the edited string. Let denote the dynamic programming table for , and the difference representation for . As Figure 4 shows, the number of changed cells in can be much smaller than that of changed cells in (see also Figure 1).

Let denote the sparse table for . Since consists only of the boundary cells, the number of changed cells in can even be much smaller. In what follows, we show how to efficiently update to .
Because the prefix remains unchanged after the edit operation, for any we have by Lemma 2 and recurrence (1). Hence, we can restrict ourselves to the indices . We define as a correcting offset of string indices before and after the update: if a character has been inserted at position of , if a character has been deleted from position of , and otherwise. Now, for any , and column in corresponds to column in .
Let be any box on . For the the top row of , we use a linked list that stores the column indices () such that , in increasing order. We also compute, in each element of the list, the value for of the corresponding column index . We use similar lists , , and for the bottom row, left column, and right column of , respectively. We compute these lists when an edit operation is performed to string , and use them to update to efficiently.
Let denote the number of cells in our sparse representation such that . In the sequel, we prove:
Theorem 1
Our algorithm updates to in time.
Initial step. Suppose that is in the th run of string . Let be any of the boxes of that contain column , where . Due to Lemma 2, is the only cell in the first row where we may have . can be easily computed in time by Lemma 2. Then, can be computed in time by tracing the first row and using for increasing . The list only contains (coupled with ) if , and it is empty otherwise.
Editing string at position incurs some structural changes to : (a) gets wider by one (insertion of the same character to a run), (b) gets narrower by one (deletion of a character), (c) is divided into or boxes (insertion of a different character to a run, or character substitution).
In cases (a) and (b), the diagonal links of need to be updated. A crucial observation is that the total number of such diagonal links to update is bounded by for all the boxes , …, , since the destinations of such diagonal links are within the same column of ( in case (a), and in case (b)). For each box , if (i.e. is a square or a horizontal rectangle), then we scan the top row from right to left and fix the diagonal links until encountering the first cell in whose diagonal link needs no updates (see Figure 6). The case with (i.e. is a vertical rectangle) can be treated similarly. By the above observation, these costs for all boxes that contain the edit position sum up to .
Figure 5: Case (a) of the initial step.
The dashed arcs are the old diagonal links in ,
and the sold arcs are the modified diagonal links in .
The gray cells depict cells and .
Figure 6: Case (c) of the initial step,
where character substitution has been performed at position .
The dashed arcs are the old diagonal links in
from row up to ,
and the sold arcs are the modified diagonal links
from new column in .
In case (a), we shift the right column of to the right by one position, and reuse it as the right column of . This incurs two new cells and in (the gray cells in Figure 6). We can compute in time using Lemma 2. Now consider to compute for the new right column. Since this right column initially stores for the old , using Lemma 2, we can compute in increasing order of , from top to bottom, in time each. We can compute in time by simply scanning the first row. Then, we can compute for increasing , using , and construct . This takes a total of time. Finally, is computed from and in time. Case (b) can be treated similarly.
For case (c), we consider a sub-case where a character substitution was performed completely inside a run of , at position . This divides an existing box into three boxes , , and . Thus, there appear three new columns , , and in . Then, the diagonal links for these new columns can be computed in time each, by scanning row from , from right to left (see Figure 6). The values for the cells in these new columns, as well as the values for column , can also be computed in similar ways to cases (a) and (b). The other sub-cases of (c) can be treated similarly.
Updating cells on row and column . In what follows, suppose that we are given a box to the right of the edit position , in which some boundary cell values may have to be updated. For ease of exposition, we will discuss the simplest case with substitution where the column indices do not change between and . The cases with insertion/deletion can be treated similarly by considering the offset value appropriately.
Now our task is to quickly detect the boundary cells of such that , and to update them. We assume that the boundary cell values of the preceding boxes and have already been computed.
We consider how to detect the cells on the top boundary row and the cells on the left boundary column of box that need to be updated, and how to update them. For this sake, we use the following lemma on the values of , which is immediate from Lemma 2:
Lemma 3
Let and . Suppose that for any cell with or , the value of has already been computed. If , then or .
Intuitively, Lemma 3 states that the cell such that must be propagated from its left neighbor or its top neighbor. We use this lemma for updating the boundaries of each box stored in . Recall that the values on the preceding row and on the preceding column have already been updated. Then, the cells on and of box with can be found in constant time each, from the lists and maintained for the preceding row and preceding column , respectively.
We process column indices in increasing order, and suppose that we are currently processing column index in the bottom row of the preceding box . According to the above arguments, this indicates that the cells in the top row of that need to be updated (i.e., ). We assume that, for any with , the value of has already been computed. Also, we have maintained a partial list for where the last element of this partial list stores the largest such that and , together with the value of . Now it follows from Lemma 2 that both and can be respectively computed in constant time from and , and thus we can check whether in constant time as well. In case , we append to the partial list for . By the definition of , we have . Since is stored with the current column index in the list , can also be computed in constant time.
Suppose we have processed cell . We perform the same procedure as above for the right-neighbor cells with and increasing , until encountering the first cell such that (1) , (2) , or (3) . In cases (1) and (2), we move on to the next element of in , and perform the same procedure as above. We are done when we encounter case (3) or becomes empty. The total number of cells for all boxes in is bounded by .
In a similar way, we process row indices in increasing order, update the cells on the left column , and maintain another partial list for .
Updating cells on row and column . Let us consider how to detect the cells on the bottom row and the cells on the right column of box that need to be updated, and how to update them.
The next lemma shows monotonicity on the values of inside each .
Lemma 4 ([4])
For any with and , . For any with and , .
The next corollary is immediate from Lemma 4.
Corollary 1
For any cell with and , . Also, for any cell with and , .
Now we obtain the next lemma, which is a key to our algorithm.
Lemma 5
For any cell with and , .
-
Proof. By Corollary 1, and for and . Thus clearly . Therefore, for the value of in Lemma 2, we have , which leads to
(2) (3) By applying equation (3) to the term of equation (2), we get
Recall that , since we are considering cells in the same box . Thus . By applying equation (2) to the term of equation (3), we similarly obtain .
For any and , let be the smallest positive integer that satisfies or . By Lemma 5, for any cell on the bottom row or on the right column , we have and . This means that iff . Thus, finding cells with on the bottom row or on the right column reduces to finding cells with on the row or on the column . See Figure 8.
Figure 7: Diagonal propagation of inside box .
Figure 8: Illustration for the case where in Lemma 6.
We have shown how to compute for the top row and for the left column . We here explain how to use (we can use in a symmetric manner). We process column indices in in increasing order, and suppose that we are currently processing column index in the top row of the current box . We check whether . For this sake, we need to know the values of and . Recall that, by Lemma 5, is equal to () on the bottom row (if ) or on the right column (if ), where . Since the cell can be retrieved in constant time by the diagonal link from the cell on the top row , we can compute in constant time, applying Lemma 5 to the upper-left direction.
Computing is more involved. By Lemma 2, we can compute from and . Since is on the top row , has already been computed. Consider to compute . Since , it suffices to compute and . By definition, . Since , we can retrieve the value of from the current element of the list , in time. Since , we can compute in time.
What remains is how to compute . We use the next lemma.
Lemma 6
For any cell with and , let and . Then,
Let . Since , . Since , , and , we get . Thus it follows from Lemma 6 that
Since the value is already computed and stored in the corresponding element of , we can compute, in time, by
Thus, we can determine in time whether , and hence whether .
Suppose . Then we need to compute . This can be computed in constant time using Lemma 6, by , where . We add the column index to list if , and/or add the row index to list if , together with the value of .

The above process of computing is illustrated in Figure 9. Suppose we have processed cell . We perform the same procedure as above for the right-neighbor cells with and increasing , until encountering the first cell such that (1) , (2) , or (3) . In cases (1) and (2), we remove from and move to the next element of in . We are done when we encounter case (3) or becomes empty. By Lemma 5, the total number of cells for all boxes in is .
Batched updates. Our algorithm can efficiently support batched updates for insertion, deletion, substitution of a run of characters.
Theorem 2
Let be the string after a run-wise edit operation on , and let . can be updated to in time where denotes the number of cells where the values differ between and .
Since is the length of the string after modification, in Theorem 2 is bounded by . Thus, we can perform a batched run-wise update on our sparse table in worst-case time. Let be the total number of characters that are involved in a run-wise batched edit operation from to (namely, a run of characters is inserted, a run of characters is deleted, or a run of characters is substituted for a run of characters with ). Then a naïve -time applications of Theorem 1 to the run-wise batched edit operation requires time. Since , the batched update of Theorem 2 is faster than the naïve method by a factor of whenever . We also remark that our batched update algorithm is at least as efficient as building the sparse DP table of Froese et al.’s algorithm [4] from scratch using time and space.
3.2 Evaluation of
As was proven previously, our algorithm works in time per edit operation on one of the strings. In this subsection, we analyze how large the would be in theory and practice. Although in the worst case for some strings (Theorem 3), our preliminary experiments shown below suggest that can be much smaller than in many cases.
Theorem 3
Consider strings and of RLE sizes and , respectively, where and . We assume lexicographical orders of characters as for , for , and . If we delete from , then .
We have also conducted preliminary experiments to estimate practical values of , using randomly generated strings. For simplicity, we set and for all experiments. We fixed the alphabet size throughout our experiments.


In the first experiment, we fixed the RLE size , randomly generated two strings and of varying lengths from to , and compared the values of and the sizes of . For each , we randomly generated 50 pairs of strings and of length each, and took the average values for and the sizes of when was deleted from . In the second experiment, we fixed the string length and randomly generated two strings and of varying RLE sizes from to . For each , we randomly generated 50 pairs of strings and of RLE size , and took the average values for and the sizes of when was deleted from . The results are shown in Figure 10. In both experiments, is much smaller than the size of . It is noteworthy that even when the values of () and () are close, the value of stayed very small. This suggests that our algorithm can be fast also on strings that are not RLE-compressible.
Acknowledgments
This work was supported by JSPS KAKENHI Grant Numbers JP18K18002 (YN), JP17H01697 (SI), JP20H04141 (HB), JP18H04098 (MT), and JST PRESTO Grant Number JPMJPR1922 (SI).
References
- [1] A. Abboud, A. Backurs, and V. V. Williams. Tight hardness results for LCS and other sequence similarity measures. In FOCS 2015, pages 59–78, 2015.
- [2] K. Bringmann and M. Künnemann. Quadratic conditional lower bounds for string problems and dynamic time warping. In FOCS 2015, pages 79–97, 2015.
- [3] P. Charalampopoulos, T. Kociumaka, and S. Mozes. Dynamic string alignment. In CPM 2020, pages 9:1–9:13, 2020.
- [4] V. Froese, B. J. Jain, M. Rymar, and M. Weller. Fast exact dynamic time warping on run-length encoded time series. CoRR, abs/1903.03003, 2020.
- [5] H. Hyyrö and S. Inenaga. Compacting a dynamic edit distance table by RLE compression. In SOFSEM 2016, pages 302–313, 2016.
- [6] H. Hyyrö and S. Inenaga. Dynamic RLE-compressed edit distance tables under general weighted cost functions. Int. J. Found. Comput. Sci., 29(4):623–645, 2018.
- [7] H. Hyyrö, K. Narisawa, and S. Inenaga. Dynamic edit distance table under a general weighted cost function. In SOFSEM 2010, pages 515–527, 2010.
- [8] H. Hyyrö, K. Narisawa, and S. Inenaga. Dynamic edit distance table under a general weighted cost function. J. Disc. Algo., 34:2–17, 2015.
- [9] S.-R. Kim and K. Park. A dynamic edit distance table. J. Disc. Algo., 2:302–312, 2004.
- [10] W. Kuszmaul. Dynamic time warping in strongly subquadratic time: Algorithms for the low-distance regime and approximate evaluation. In ICALP 2019, pages 80:1–80:15, 2019.
- [11] A. Nishi, Y. Nakashima, S. Inenaga, H. Bannai, and M. Takeda. Towards efficient interactive computation of dynamic time warping distance. CoRR, abs/2005.08190, 2020.
- [12] H. Sakoe and S. Chiba. Dynamic programming algorithm optimization for spoken word recognition. IEEE Transactions on Acoustics, Speech, and Signal Processing, 26(1):43–49, 1978.
- [13] J. P. Schmidt. All highest scoring paths in weighted grid graphs and their application in finding all approximate repeats in strings. SIAM J. Comp., 27(4):972–992, 1998.
Appendix A Appendix: Omitted proofs
A.1 Proof for Theorem 3
To prove Theorem 3, we establish the following lemma.
Lemma 7
Let be the dynamic programming table for the above strings and . Then,
-
Proof. By recurrence (1), the argument holds for any cells and , where and . For any cell with or , suppose that the argument holds for any with and . We consider the five following cases:
-
1.
Case : For the cell , it follows from the inductive hypothesis and recurrence (1) that . Since , . Similarly, for the cells and , and . Since , . Because , it holds that . By the inductive hypothesis, . Thus we have , which implies .
- 2.
-
3.
Case : By symmetric arguments to Case 1.
-
4.
Case : By symmetric arguments to Case 2.
-
5.
Case : For the cells and , by the inductive hypothesis and . Thus and .
-
1.
We are ready to prove Theorem 3.
-
Proof. For simplicity, we assume that the column indices of begin with 0. In the grid graph over , we assign a weight to each in-coming edge of cell . We also consider the grid graph over obtained by removing from .
Consider a cell with that is on the top row of some box , namely for some . By Lemma 7, is the minimum weight path from to . Similarly, is the minimum weight path from to .
For the cell that is the upper neighbor of and is on the bottom row of box , by analogous arguments to the above, is the minimum weight path from to , and is the minimum weight path from to .
Let be the sub-path of and ending at , the sub-path of and ending at , the sub-path of and from to , and the sub-path of and from to . Let be the edge from to , be the edge from to , be the edge from to , and be the edge from to . See Figure 11 that depicts these paths and edges.
Figure 11: Minimum weight paths to and over and .
For any path in , let denote the total weights of edges in . Now we have and . By the definition of DTW, stores the cost of the minimum weight path from to , and stores the cost of the minimum weight path from to . Thus and . Similarly, and .
Now , and . This leads to . Recall that . Thus . Also, because , , Consequently, .
Therefore, for any cell with that lies on the top row of any character run in , . Since each top row contains such cells, and since , there are such cells for top rows . Symmetric arguments show that there are cells with for left rows . Thus, .
A.2 Proof for Lemma 1
-
Proof. The lemma can be shown in a similar manner to the proof for Theorem 3 above. In so doing, we set and in the strings and of Section 3.2, and consider strings and such that for and for , and . Since deleting the leftmost character of is symmetric to appending a new character to such that , we get lower bound for the number of cells where per appended character. If we repeat this by recursively appending new characters such that for , we get lower bound for the number of cells where for each . Hence there are a total of cells in that differ from the corresponding cells in , for edit operations on .