Asymptotic Hausdorff and Language Similarity
Abstract
We introduce the Asymptotic Hausdorff lifting, denoted , a general method for lifting an element-level metric to a (pseudo-) metric on sets, that captures asymptotic similarity in infinite domains equipped with a notion of size. The construction is designed to be insensitive to finite deviations and to avoid the limitations of classical Hausdorff-based approaches, which are often overly sensitive to outliers and fail to reflect asymptotic behavior.
Formal languages provide a central motivating instance of this framework, where elements are words and sets are languages. When applied to normalized edit distances, the Asymptotic Hausdorff lifting yields metric-valued distances between languages that reflect asymptotic edit behavior while preserving metric structure. We study the equivalence classes of regular languages induced by for normalized edit distances , and characterize their asymptotic essence. Focusing in particular on the normalized edit distance of Marzal and Vidal, ned, we investigate the computation of for regular languages and for bounded context-free languages.
Keywords and phrases:
Automata theory, formal Languages, Metric Spaces, Language similarity, Edit Distance, asymptotic AnalysisCategory:
Track B: Automata, Logic, Semantics, and Theory of ProgrammingFunding:
Gal Meirom: Supported by ISF grant 2507/21 and Frankel Center for Computer Science, BGU.Copyright and License:
2012 ACM Subject Classification:
Theory of computation Formal languages and automata theory ; Theory of computation Design and analysis of algorithmsAcknowledgements:
We thank Dror Fried, Guy Ofek, Omer Shachar and Gera Weiss for helpful comments on an early draft of this paper.Editors:
Sayan Bhattacharya, Danupon Nanongkai, Michael Benedikt, and Gabriele PuppisSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Various applications in formal methods call for a notion of similarity between languages. Such a need arises, for example, in applications of program repair, robustness quantification, and grammatical inference. In all these settings, similarity between two languages and is grounded in a notion of edit operations required to transform words into words . More generally, edit distance itself has been extensively studied in areas such as error-correcting codes, parsing theory, speech recognition, and molecular biology, highlighting its broad relevance.
In the context of robustness, Filliot et al. [9] study the computation of , where is a cost function for editing into , implemented by a given weighted transducer. Similarity functions of this form are also studied by Mohri [19], Samanta et al. [23], and Henzinger et al. [14]. The latter also considers the dual quantity .
In repair applications, Benedikt et al. [2, 4] consider the standard edit operations of insertion, substitution, and deletion (with uniform costs) required to transform a string in into a string in . In [2], they ask whether one can transform any word in into a word in using a bounded number of edits. For example, if and , then at most one edit operation (inserting a ) is required to transform any string in into a string in . In contrast, if and , then there is no bound on the number of edits required to transform a word in into a word in . They note that requiring a uniform bound on the number of edits is a strong restriction, and in subsequent work [4] they therefore study the percentage of letters that need to be edited. In the latter example, the expected value is , since words in are of the form and such a word requires edit operations to transform into a closest word in .
In general, one expects such similarity notions to induce a metric or a pseudo-metric. Some applications, such as repair, are inherently asymmetric; in such cases, one may only require adherence to the triangle inequality . The triangle inequality is nevertheless central, as it ensures alignment with user intuitions, enables compositional reasoning (inferring distances between and from distances between and ), and is computationally beneficial in optimization and learning tasks by enabling pruning, efficient indexing, and incremental updates.
It turns out that none of the similarity notions used in the works mentioned above constitute a metric. In this work, we ask whether it is possible to obtain a metric or a pseudo-metric between languages when the underlying similarity between words is based on the rationale of [4] and captures the percentage of edit operations required to transform words in to words in in the limit. Since we are interested in percentages, we restrict attention to functions returning values in . That is, we seek a function that is a metric (or pseudo-metric) and captures the intuitions underlying [4].
We next present several examples that formalize the desired intuitions and requirements emerging from [4].
Requirement 1 (Percentage).
As discussed above, one may expect to be . Rather than insisting on a specific numerical value, we require a monotonicity property:
Intuitively, this reflects that as the percentage of symbols requiring editing increases, so does the induced distance.
Requirement 2 (Outlier-insensitive, finite-subset indifferent).
Consider repairing to . Since any word is of the form and such a word requires at least edits (i.e. one edit per character), we expect . Consider now repairing to . The percentage of edit operations for words of the form is zero, while the percentage for the word is . Consequently, the supremum of the edit-percentage from words in to is . This is undesired as it gives the impression that is as farthest as possible from , while we expect a value reflecting they are quite similar. Similarly, we expect repairing to to return and not , since for all but finitely many words, the percentage is . That is, we expect to be outlier-insensitive, which we formulate as follows: for all infinite languages and finite languages . Note that this requirement necessarily violates identity of indiscernibles on the full powerset, as it entails . Thus, we seek for a pseudo-metric rather than a metric.111The identity of indiscernibles prescribes that iff and a pseudo-metric relaxes this condition to require only .
Requirement 3 (Bounded-edits insensitivity).
Consider now repairing to . In contrast to the previous example, every word requires at least one edit operation to reach a closest word in (in fact, exactly one). However, to capture the percentage nature, we note that the percentage of the number of edits required to transform to diminishes as or grows. Note that this is true also if we consider repairing to . While now every word requires edits, still as or grows to infinity, the edits required are negligible compared to the length of the word. Thus, the following formal requirement emerges from [4]: If there exists a bound such that every can be transformed into a word in with at most edits and vice versa, then .
Remark 1.1.
One may argue that it is desirable to distinguish the language from or from . Indeed, there exist language similarity measures that make such distinctions, in particular [1]. In applications where such sensitivity is required, one may combine such a measure with the distance developed here (for example, via a product construction). In this work, however, we deliberately impose invariance under finite subsets of words and a finite number of edits, as these capture the asymptotic notion of similarity that we and [4] aim to capture.
To summarize, we seek a language similarity function with a bounded codomain, specifically , that is a pseudo-metric and satisfies the following requirements:
-
1.
Percentage nature: for all
-
2.
Outlier insensitivity: for all infinite and finite .
-
3.
Bounded-edits insensitivity: If there exists a bound such that every can be transformed into a word in with at most edits and vice versa, then .
Since languages are sets of words, and we seek a similarity notion induced by edit distance between words, a natural question is whether there exists a general method to lift a metric d on a universe to a metric on subsets of that meets these requirements. A classical lifting scheme is given by the Hausdorff distance. Let us first recall the common way to define a distance between an element and a set . This measure, is defined as , i.e. it measures the distance of to the closest element in .222Its use in the context of formal languages goes back to [25] where d is the Levenshtein Edit Distance [17]. Next, the directional (asymmetric) distance from set to set is defined as , namely it is the distance of the farthest element in to . Finally, given , the Hausdorff distance with respect to d is the function defined by
i.e. it takes the maximum of the directional distance from to and in the other direction.
While the Hausdorff lifting yields a metric and can be used to lift edit distances between words to distances between languages, applying it to the standard Levenshtein edit distance ed [17] fails to produce a similarity measure with a percentage nature, and the resulting values are unbounded. For example, and .
Replacing ed by a normalized edit distance such as ned [18], ged [26], or ced [8] restores the percentage nature, but does not address a more fundamental limitation: the Hausdorff distance is inherently sensitive to outliers.333Formal definitions of ed, ned, ged, and ced appear in Subsection 4.1. At a high level, ed denotes the minimum number of edit operations, whereas ned, ged, and ced correspond to different normalized variants. Indeed, while , we have . This shortcoming is not specific to edit distance, but arises from the supremum-based definition of the Hausdorff construction itself.
The Hausdorff distance is well suited to lifting distances on finite universes or universes where elements are conceived as having the same size. In contrast, it is size-oblivious and ill suited to infinite universes whose elements admit an unbounded notion of size, as is the case in formal languages, where languages of interest are necessarily infinite and contain words of unbounded length. Similar limitations of Hausdorff-type constructions have been observed in prior work on language similarity measures, leading to the proposal of various alternative notions of language similarity; we review these in Section 2.
Our main contribution is a new lifting scheme , which we term Asymptotic Hausdorff. This construction lifts an element-level (pseudo) metric d to a set-level pseudo-metric while remaining insensitive to finite outliers and respecting unbounded growth in element size. Unlike the classical Hausdorff distance, it is specifically suited to infinite universes equipped with a natural size notion and yields metric-valued distances whose behavior is asymptotically aligned with the underlying element-level distance.
The lifting is defined for element-level metrics satisfying a property we call the asymptotic separation property. We show that normalized edit distances such as ned, ged, and ced satisfy this property, and that the resulting language distance meets all of our stated requirements. We further show that this property is satisfied by other common distance functions such as the Euclidean distance, the metric, and in fact every norm-induced metric. This generality suggests applications beyond the formal-languages setting, particularly in domains that employ finite representations or generators of infinitely many objects, equipped with a natural notion of size.
One such application arises in the study of graph spanner constructions. A spanner is a subgraph of a given weighted graph and thus shares its vertex set with the input. Spanner constructions are thus naturally viewed as set of objects indexed by graph size. Any metric dist comparing spanners over the same graph – for example, based on stretch, distortion, or sparsity – can be lifted via to obtain an asymptotic comparison between spanner constructions, focusing on large-scale behavior while abstracting away finite-size effects.
Another example comes from procedural texture generators in computer graphics. Such generators are commonly modeled as functions , for , producing a continuous scalar field that is mapped to concrete values such as colors, materials, or block types. Well-known instances include Perlin noise, simplex noise, Worley noise, and their fractal variants. Similarity between generators is typically assessed by comparing the finite structures they induce over bounded spatial regions (often called patches), independently of spatial location. By equipping such patches with an appropriate distance measure that captures their similarity, and using their spatial extent as the size parameter, the lifting naturally captures asymptotic similarity between texture generators.
After presenting the abstract framework of the Asymptotic Hausdorff lifting, we return to edit-operation-based language similarity measures, study their properties, and establish complexity results for computing our primary instance, . For regular languages, we prove Pspace-hardness and give an approximation algorithm in coNExp. For bounded context-free languages (BCFLs), we present an exact algorithm running in Exp.
Missing proofs are available in the full version.
2 Language Similarity Notions in the Literature
A variety of notions for measuring similarity between formal languages have been explored in the literature, arising from different motivations. In the following, we survey these measures through the lens of the requirements identified in the introduction, and highlight limitations that motivate our Asymptotic Hausdorff metric.
Complexity-based similarity measures
Since our focus is on formal languages, where (possibly infinite) languages are finitely represented by some computational model, one natural approach is to define a distance measure between languages via a distance between their representations. To ensure that such a measure is insensitive to the particular choice of representation, one may appeal to a canonical representation, when one exists for the class of languages under consideration.
Kolmogorov and automata-size based measures.
In this spirit, [15] proposes using Kolmogorov complexity and defines the Kolmogorov distance between languages and as , where denotes the Kolmogorov complexity of the language . For regular languages, the minimal DFA can serve as a canonical representation, allowing to be replaced by the number of DFA states. Alternatively, [15] suggests using the size of a minimal NFA, defined as the sum of its states, initial and final states, and transitions.
It is straightforward to see that these notions induce a pseudo-metric. (Indeed, any function of the form , where maps languages to , induces a pseudo-metric on the space of languages.) However, and its variants measure differences in the complexity of languages rather than differences between the languages themselves. For example, , since the two languages are equally simple, even though every word in needs a complete rewrite to transform into a word in . In contrast, our goal is to define a pseudo-metric under which and are as far apart as possible.
Set-theoretic similarity measures
As mentioned in the introduction, since languages are sets, any metric on sets can be used to induce a metric on languages.
Jaccard.
One of the earliest notions of set similarity, dating back to the 19th century, is the Jaccard index, , along with its dual notion, the Jaccard distance , which serves as a dissimilarity measure. These are defined as follows:
| (1) |
where denotes symmetric set difference. The Jaccard index and distance are undefined when is or , and thus are inapplicable for infinite languages.
Cesáro-Jaccard.
For infinite languages, one approach is to consider one of the limits
| (2) |
where (resp. ) denotes the set of words in of length (resp. at most ). However, as shown in [22], these limits need not exist. For example, considering the left limit, if and , then the fraction evaluates to for even and to for odd .
To address this issue, [22] propose smoothing the sequence using the Cesàro average, yielding the following distance measure between languages:
They show that is a pseudo-metric.
However, while the Cesàro-Jaccard distance successfully addresses the fact that formal-language theory is primarily concerned with infinite languages, and is finite-subset indifferent, it does not incorporate any notion of similarity between individual words. As a result, both and , despite the fact that in the former pair the edit cost per word is uniformly bounded (one edit per word), while in the latter it grows unboundedly with word length. Our goal, by contrast, is a metric that reflects this asymptotic discrepancy, identifying the first pair as close and the second as far.
Discounted-sum Jaccard.
Another approach to addressing the potential non-convergence of the limits in Equation 2 is to employ a discounted-sum construction. This idea was recently proposed in [5]. In particular, they show that
is a pseudo-metric for every .
However, this construction places greater weight on discrepancies at smaller word lengths, which limits its ability to capture asymptotic percentage behavior. In particular, differences among short words dominate the value of the distance, even when they become negligible relative to word length. For example, for we obtain and , despite the fact that the former pair differs only on finitely many short words, while the latter exhibits a persistent asymptotic discrepancy.
Shortlex vector approach.
Another approach explored in [15] begins by ordering all words over the alphabet using the shortlex order (first by length and then lexicographically). A language is then represented by an infinite binary vector , where if and only if the -th word belongs to . The distance between two languages and is defined by applying a chosen distance measure between binary vectors to and .
Since precisely when the -th word either belongs to both and or to neither, this construction effectively accounts for words in the symmetric difference (and intersection) of the two languages. The precise behavior of the resulting language distance depends on the specific choice of vector distance. Nevertheless, this approach does not incorporate any notion of similarity between words themselves, making it difficult to see how it could determine and as being closer than and .
Word-level lifted similarity measures
Predicate-lifted Jaccard.
In [7], it is observed that language similarity notions can benefit from enriching set-based similarity measures, such as the Jaccard distance, with an explicit notion of distance between individual words. Rather than using the strict symmetric difference the approach of [7] introduces a predicate that captures when two words are considered sufficiently close. The comparison between and considers only pairs of words deemed sufficiently close according to . For example, the predicate may be defined as , where hamming denotes the Hamming distance between words and is a fixed constant. Another example from [7] is , where lcs denotes the length of the longest common subsequence.
Given such a predicate , they define where . This construction essentially replaces the strict intersection in the standard Jaccard index with the set of all words that are sufficiently close according to the predicate .
Predicate-lifted information-rate.
The focus in [7] is on extending the notion of information rate introduced by Shannon and Weaver [24] and applied to formal languages by Chomsky and Miller [6]. The information rate of a language is defined as This notion pertains to a single language rather than a pair of languages and is intended to capture the density of a language. In [7], an extension for two languages, which additionally incorporates a predicate, is suggested:
Since the information rate is not a (pseudo-)metric, it does not serve as a candidate for our purposes.
Predicate-lifted Cesàro-Jaccard.
While not suggested in the literature, we note that the use of a predicate can also be applied to the Cesàro–Jaccard distance. For example, one can define analogously to , by replacing with where . Taking the predicate to be , we obtain , which is desirable and resolves the issue that . Similarly, defining as gives while .444The formal definition of ed and ned are deferred to Subsection 4.1. Intuitively, ed counts minimal number of edit operations, and ned minimal percentage of edits. In particular, for every we have , and .
However, the use of a predicate necessitates choosing a fixed threshold, which prevents distances from degrading gradually and makes it impossible to satisfy the monotonicity property described in Requirement 1.
We now turn to language similarity notions that lift a similarity measure between words.
Infinitum and Supremum based.
As mentioned in the introduction, the measure , where d assigns a cost to string transformations, has been used in the literature [19, 23, 9]. However, this measure is not a metric. For example, taking d to be the normalized edit distance ned, we have . Moreover, , illustrating the outlier sensitivity of (the outlier being ). By similar reasoning, the dual notion is also not a metric and suffers from outlier sensitivity; for instance, .
Prefix-distance based.
Considering variations in word-level similarity, several works examine the prefix distance between words. The prefix distance between two strings and , denoted , is defined as the number of characters in and that do not belong to their longest common prefix. The Hausdorff lifting of prf, denoted , has been studied in various works [20]. We note that , being a Hausdorff lifting, is sensitive to outliers. In addition, it is unbounded: for example, , and it is also sensitive to bounded edits, as seen from .
, Benedikt et al.’s measure.
As mentioned in the introduction, Benedikt et al. [3] studied the number of edits required to repair a word in into a word in . Since this quantity is often unbounded, their subsequent work [4] focused on capturing the percentage of edits required in the limit. Their work forms the basis for our approach.
The exact formula they use is
It is straightforward to see that for any pair of languages .
We note that is asymmetric by nature. For applications requiring symmetry, one can define .
However, the critical issue is that (and thus ) violates the triangle inequality.
Claim 2.1.
does not satisfy the triangle inequality.
In summary, while a variety of language similarity measures have been proposed, existing approaches face key limitations. Complexity-based measures capture overall representation size but ignore word-level structure; set- and vector-based measures fail to account for word similarity or are sensitive to infinite languages; and word-level or edit-distance–based liftings can be outlier-sensitive or fail to satisfy fundamental metric properties such as the triangle inequality. These observations motivate the need for a pseudo-metric that simultaneously accounts for word-level similarity, is robust to outliers, and preserves essential metric properties. In the next section, we introduce such a construction: the Asymptotic Hausdorff metric, , which generalizes the Hausdorff lifting to capture asymptotic behavior at the language level. We first present it at an abstract level, and later specialize to , the lifting of the normalized edit distance ned [18].
3 Asymptotic Hausdorff
We turn to define the central notion of the paper, the Asymptotic Hausdorff lifting. In what follows we assume is a domain (set) and is a metric or a pseudo-metric. That is, we assume satisfies the three pseudo-metric requirements:
-
1.
Reflexivity: for all .
-
2.
Symmetry: for all .
-
3.
Triangle inequality: for all .
3.1 Defining the Asymptotic Hausdorff Lifting
As discussed in the introduction, we are interested in domains whose elements are equipped with a natural notion of size. This allows us to distinguish between bounded and unbounded behavior and to focus on asymptotic phenomena.
Definition 3.1 (Size notion).
A size notion for is a map .
Henceforth, we assume is equipped with such a size notion . We are interested in sets with increasing size of elements. To capture this we introduce the following definition, which considers infinite sequences of elements (rather than sets).
Definition 3.2 (-bounded sequence).
A sequence is called -bounded if there exists some such that Otherwise we say it is -unbounded.
We are interested in element-level distances that separate -bounded sequences from -unbounded sequences, in the sense that the asymptotic distance is as large as possible.
Definition 3.3 (Asymptotic separation property).
We say has the asymptotic separation property if for every -unbounded sequence , and -bounded sequence , satisfies where is the supremum of the image of .
We now have all the ingredients needed to define the Asymptotic Hausdorff lifting. As in the Hausdorff lifting, we first define a directional distance, and then take the maximum of going from to and in the other direction.
Definition 3.4 (Asymptotic Hausdorff lifting).
Given a pseudo-metric on , the two functions and of type
are defined as follows
We refer to as the asymptotic directional distance from to and to as the asymptotic Hausdorff lifting of .
3.2 Properties of the Asymptotic Hausdorff Lifting
We first establish that the asymptotic directional distance is well defined.
Claim 3.5.
is well defined for all .
Equivalently, the asymptotic directional distance can be defined as the supremum over all sequences with , of the limit . That is, as the worst-case asymptotic distance to attained along sequences of elements of whose size grows unboundedly.
Claim 3.6 (Equivalent definition to asymptotic directional distance).
Let and be sets. Then
We next show that the asymptotic separation property is sufficient to lift the triangle inequality to the asymptotic setting.
Theorem 3.7 ( satisfies the triangle inequality).
Let be a pseudo-metric that has the asymptotic separation property. Then satisfies the triangle inequality.
The proof makes use of the following claim.
Claim 3.8 (Point-wise triangle inequality).
Let be a set, a pseudo-metric on and a subset of . Then for every
Proof.
Let and . As is a pseudo-metric for every . Thus
Proof of Theorem 3.7.
Let be subsets of and let . Let such that
| (3) |
If all infinite sequences are -bounded, then is an -bounded set and thus
Otherwise, let be some arbitrary sequence that has an infinite subsequence such that is a non decreasing sequence that tends to . We consider two cases:
Case 1.
There exists a subsequence such that for every that satisfies , we have that .
Let be the set of elements that satisfy . We get that where . Therefore as has the asymptotic separation property
Since bounds for any , together we get that and
Case 2.
There exists such that for every there exists that satisfies and also satisfies
| (4) |
As is a pseudo-metric by Claim 3.8 we know that
| (5) |
And because and for any we get
| (6) |
Putting it all together we get
| (7) | ||||
Note that this inequality is satisfied for an arbitrary sequence in with size that tends to . Using Claim 3.6 there exists a sequence that satisfies
| (8) |
Thus, we finally get
As is arbitrary small we get that .
Since obviously satisfies reflexivity and symmetry, an immediate corollary of Theorem 3.7 is that is a pseudo-metric.
Theorem 3.9 (Asymptotic Hausdorff is a pseudo-metric).
is a pseudo-metric when has the asymptotic separation property.
Returning to our primary motivating setting of words and language, in the next section, we show that many word similarity measures satisfy the asymptotic separation property, and therefore can be lifted to pseudo-metrics between languages. We expect that similar results hold for metrics on trees and graphs, but we do not pursue this direction here.
More broadly, this naturally raises a stronger question: how restrictive is the asymptotic separation property in general? We therefore turn to identifying broad classes of distance functions for which this property holds. The following theorem shows that the asymptotic separation property is in fact quite common: every metric induced by a norm satisfies it.
Theorem 3.10 (Norm-induced metrics satisfy asymptotic separation).
Let be a normed space with and . Then has the asymptotic separation property.
Corollary 3.11.
Let be a normed space and . Then is a pseudo-metric on .
As an immediate consequence of the corollary, the Asymptotic Hausdorff lifting applies to all metrics induced by norms. This includes, in particular, the Euclidean distance and, more generally, distances for any . Hence, can be meaningfully applied in continuous settings alongside the discrete ones considered earlier.
While we arrived at this notion from the perspective of formal languages, as discussed in the introduction, we believe that it is applicable in a much broader range of settings. In particular, it is well suited to contexts in which infinite sets arise from finite representations or generators, are equipped with a natural notion of size, and contain elements whose size is unbounded. This situation commonly occurs, for example, when considering functions defined over infinite domains, where each input induces an element in the set generated by the function.
4 Formal Languages and Distances
We now focus on language similarity notions obtained via the Asymptotic Hausdorff lifting. This construction applies to a variety of word-level metrics, including both classical and normalized edit distances. While all are suitable for lifting, only normalized metrics are compatible with the percentage-based requirements discussed in the introduction. Among these, the normalized edit distance ned will play a central role and serve as our main point of reference. We begin by briefly reviewing standard distances between words.
4.1 Metrics on words (Preliminaries)
Let be a finite alphabet. For a word , we write for its length and for its -th letter. The empty word is denoted by .
One of the oldest metrics on words is the Hamming distance. It measures the distance between and as the number of letters on which they differ plus the difference between their lengths. Formally, if and then . Another common simple metric between words is the prefix distance [20]. It measures the number of letters in and that are not in the longest common prefix of and . Formally, . Beyond these position-based notions, many widely used distances between words are defined in terms of edit operations, which we review next.
Edit operations and edit paths.
We work with the standard edit operations: insertion, deletion, substitution, and no-op. Let , and write for the pair . The set of edit operations is , where denotes substitution of by , a no-op, deletion of , and insertion of .
An edit path from to is a finite sequence over such that, writing , we have and . We denote by the length of . Let assign weights to edit operations. The weight of an edit path is . Its cost is defined as . We write to denote that is an edit path from to .
Edit-distance notions.
Several notions of edit distance have been proposed in the literature, differing mainly in how edit operations are aggregated and normalized. The most basic notion is the Levenshtein (edit) distance [17], denoted ed, which returns the minimum total weight of an edit path transforming into .
For applications involving words of substantially different lengths, normalization becomes essential. When words have equal length, normalizing by the word length is straightforward; however, for unequal lengths, naïve normalizations – such as dividing by the maximum, minimum, or sum of the lengths – generally fail to preserve the metric properties (cf. [26]). To address this issue, several normalized variants of edit distance have been proposed, including the normalized edit distance ned [18], the generalized edit distance ged [26], and the contextual edit distance ced [8]. Despite differing in their normalization schemes, all these notions yield bounded distances that satisfy the metric axioms, and are therefore well suited for comparing words of varying lengths.
A common choice of weights is the uniform weight, in which no-op operations have cost and all other edit operations have weight . All notions but ced allow non-uniform weights. When non-uniform weights are considered, they must satisfy additional conditions to ensure that the induced distances , , and are metrics [26, 10, 11]. Unless stated otherwise, we henceforth assume the uniform weight.
Definition 4.1 (Edit-distance notions).
Let .
-
Levenshtein (edit) distance [17]: minimizes the weight of an edit path:
-
Generalized edit distance [26]: ged is another way to obtain an averaged cost:
-
Contextual edit distance [8]: Last, ced provides an averaged cost by considering the context of the edits. Formally, for strings for which , one defines . For a sequence satisfying for every , let . Then
Example 4.2.
Consider and . One edit path from to is . Another edit path is . We have . Since no edit path has smaller weight, it follows that . Applying this value in the definition of ged, we obtain . Since and , we have and . As no edit path has smaller cost, we conclude that . For ced, consider the sequence of strings , , , and . Note that for all . Therefore, . However, a different sequence yields a smaller value. In particular, . Thus, .
The values of ed are clearly unbounded. In contrast, the values of ned and ged are bounded by and may attain this bound. The values of ced are unbounded; however, they can be made bounded by considering the variant [10, 8]. Finally, we note that for all ; see the full version.
We establish that each of these notions satisfies the asymptotic separation property, and thus is amenable to lifting via the Asymptotic Hausdorff construction.
Claim 4.3.
The asymptotic separation property holds for ed, ned, ged, ced, and prf.
Corollary 4.4.
, , , , and , are all pseudo-metrics on the set of languages.
4.2 for Normalized Edit Distances
Recall the three requirements given in the introduction. We claim that the three notions of normalized edit distance ned, ged and ced, all satisfy these properties.
Claim 4.5 ( are finite-subset indifferent).
Let be infinite language and a finite languages. Then for every .
Claim 4.6 ( are bounded-edits insensitive).
Let . If such that for every there exists such that and vice versa then for every .
Claim 4.7 ( have a percentage nature).
For every we have that for all
Corollary 4.8.
satisfy all of our requirements.
Recall (cf. Requirement 1) that one may expect and, more generally, . We deliberately adopted a more relaxed requirement, as enforcing these equalities exactly would be overly restrictive. Nevertheless, we show that does satisfy this stricter behavior, whereas and do not.
Requirement 4 (Strict percentage property).
We say that a language metric satisfies the strict percentage property if the following hold:
-
for all with ;
-
for all .
Claim 4.9 (Satisfaction of the strict percentage property).
The strict percentage property is satisfied by , but not by or .
For this reason, we prefer over and .
Remark 4.10 (Relations).
The following relations between the metrics hold for every pair of languages :
While satisfies all of our stated requirements, its insensitivity to finite outliers and bounded local edits implies that it is a pseudo-metric rather than a metric. In scenarios where one wishes to additionally distinguish, for example, from (i.e., to forgo outlier insensitivity), or from (i.e., to forgo bounded-edit insensitivity), this can be achieved by combining with an additional language metric (e.g. , , etc.). Specifically, one may consider the refined generalized metric ordered lexicographically so that the component is dominant.555Here we use the term generalized metric to refer to a distance function whose codomain is a totally ordered set (rather than ), and which satisfies the triangle inequality with respect to that order; see, e.g., [16, 12]. If takes values in a domain that is bounded above by a constant (as is the case, for example, for ), then this generalized metric can be collapsed into a genuine metric by defining which preserves the dominance of the asymptotic distance.
4.3 Asymptotic Essence of Regular Languages
A pseudo-metric naturally induces an equivalence relation , where if and only if . In our context, two languages are considered equivalent with respect to language-metric if their -distance is zero.
Recalling the discussion in the introduction, which was illustrated using regular expressions, it appears that removing all parts of a regular expression that are not under a Kleene star yields a language that is equivalent to the original one under the desired pseudo-metric . Intuitively, the asymptotic essence of a regular expression is captured by the subexpressions occurring under Kleene closure, whereas all other subexpressions are asymptotically negligible. Accordingly, we denote by the regular expression obtained by retaining only these Kleene-starred subexpressions.
Note that applying this procedure to different regular expressions defining the same language may result in different languages. For example, consider and . Although , we have , since whereas .
Since regular languages do not admit a canonical regular expression, we seek to define asymptotic essence at the level of automata, so that it can later be applied to the minimal DFA, which is canonical up to isomorphism. Given a DFA or an NFA recognizing a language , we seek an intuitively simpler language that is equivalent to under . To this end, we decompose into its strongly connected components (SCCs) and replace all transitions that are not contained in some SCC by -transitions. The resulting automaton is a simpler NFA that recognizes a language which, intuitively, preserves exactly the asymptotically significant behavior of .666Here, we use the strict notion of SCC: a singleton state forms an SCC only if it has a self-loop.
We denote by the NFA obtained by this construction. The asymptotic essence of a regular language , denoted , is then defined as , where is the minimal DFA recognizing .
The following claim shows that these constructions – whether applied to regular expressions or to automata – yield languages that are equivalent under when is instantiated as one of the Asymptotic Hausdorff distances using a normalized word metric.
Claim 4.11 (Asymptotic Essence Equivalences).
Let be a regular language, and and a regular expression and a NFA recognizing , resp. Then, for every
Sketch proof.
The proof follows from 4.5 since and (and thus ) yield languages that differ from the original language by finitely many words and require finitely many edits.
We note that this claim does not hold in general for . For example, it fails for , since while implying .
5 On the Computation of
We now turn to the computation of , our primary notion of interest (cf. Corollary 4.8 and Claim 4.9). We begin, in Subsection 5.1, by considering regular languages, where we establish a Pspace-hardness result and present an approximation algorithm in coNExp. We then move to Subsection 5.2, where we develop a detailed algorithm for bounded context-free languages.
5.1 for Regular Languages
For regular languages, we establish both a hardness result and an approximation bound for computing .
Hardness.
We begin by showing that, as in the case of , which serves as our starting point, the problem is already Pspace-hard when languages are regular and .
Theorem 5.1 (Pspace-hardness).
Let be an alphabet, an NFA over and a rational number. The problem of deciding whether is Pspace-hard.
Sketch proof.
The proof proceeds by a reduction from the universality problem for NFAs, adapting ideas from the Pspace-hardness construction for [4] to account for normalization by edit-path length rather than word length. Given an NFA over , we construct an NFA over the extended alphabet that recognizes the language . We then show that if is universal, i.e. , then , whereas if is not universal, then .
To establish the latter case, we consider the sequence defined by for some . We first analyze the cost of an optimal edit path for and then study the limit behavior arising in the computation of .
The complete proof makes use of the following claim which we also use for the approximation result.
Claim 5.2.
Let be a word and let be a language recognized by an NFA with states. Then there exists a word attaining the value . Moreover, there exists an optimal edit path from to of length at most .
Approximation.
Next, we establish bounds relating and . In particular, is sandwiched between and , where is the number of states in the minimal DFA for .
Claim 5.3.
Let and be regular languages. Then .
Claim 5.4.
Let and be regular languages. Then where is the number of states in the minimal DFA of .
Using these bounds and the coNExp-time algorithm for developed in [4], which relies on substantial technical machinery, we obtain a coNExp-time approximation algorithm for .
Lemma 5.5.
Let and be regular languages. Then there is a coNExp algorithm finding such that where is the number of states in the minimal DFA of .
5.2 for Bounded Context-Free Languages
We now turn to bounded context-free languages (BCFLs), a well-studied subclass of context-free languages with rich structural properties and strong decidability results. We assume familiarity with standard definitions and basic properties of context-free languages, and provide the necessary definitions for bounded CFLs.
Bounded CFL (Preliminaries).
A language is said to be bounded if there exist fixed words such that A language is a bounded context-free language if it is both bounded and context-free.
Let . The Parikh vector of a word is the -tuple , where is the number of occurrences of in . For bounded languages, it is often convenient to work with a Parikh representation relative to the bounding words. Specifically, given and a word we define the Parikh vector of with respect to these words to be .
Parikh’s Theorem states that for every context-free language , the set of Parikh vectors is a semi-linear set [21]. A set is linear if there exist vectors such that The vector is called the constant vector, and the remaining vectors are called period vectors. The matrix in whose columns are the vectors is called the generator matrix of . A set is semi-linear if it is a finite union of linear sets.
Example 5.6.
As an example, consider the language This language is bounded, since for , , , , and . For the word , the corresponding Parikh vector is . The Parikh image of is generated by the constant vector together with the period vectors , , , that correspond to the powers , , and , respectively.
Ginsburg and Spanier [13] characterized bounded context-free languages by showing that a bounded language is context-free if and only if its Parikh image (with respect to the bounding words) is a stratified semi-linear set. A linear set is stratified if (i) each of its period vectors has at most two non-zero coordinates, and (ii) the non-zero coordinates of distinct period vectors do not interleave. Formally, if has non-zero coordinates at indices and has non-zero coordinates at , then it is not the case that . In the example above, the period vectors satisfy these conditions: each has at most two non-zero entries, and the index sets , , and are pairwise non-interleaving.
Towards solving for BCFLs
For simplicity, we focus on BCFLs whose Parikh image is a stratified linear (rather than semi-linear) set. Henceforth, let and be BCFLs with generators and , respectively. Let be the BCFL with the same generating vector set as but where . Thus with edit operations we can move between and and hence by 4.6 . From this point onward we thus assume and hence and .
Example 5.7.
We use and as a running example. The generators of and are and where and .
We compute for BCFLs using linear programs. This requires several auxiliary notions, which we introduce next.
The edit graph.
Let and . The edit graph is the directed graph whose vertices are the grid points , with edges from to , , and whenever the target vertex exists. An edge to corresponds to deleting , and has weight ; an edge to corresponds to inserting , and has weight ; and an edge to corresponds either to a no-op (with weight ) if , or to a substitution of by (with weight ) otherwise.
Example 5.8.
The edit graph of and is given in Figure 1 (left). Edges that weigh are dashed whereas edges that weigh are solid. A minimum cost-path is marked on the graph in black. It corresponds to the edit path . It has 10 edges, out of which 4 weigh so its cost is . Accordingly .
Blocks.
Note that words in (resp. ) are parameterized by vectors in (resp. ). Let and . These vectors induce the words and , resp. We denote their lengths by and . Consider now and . The edit graph of and can be partitioned into rectangular subgrids, which we henceforth call blocks. The -block corresponds to the edit graph of and .
Example 5.9.
Let and . Let and Figure 1 (middle) shows the edit graph of and partitioned into its six blocks by the bold gray lines.
Interleavings.
Let be an edit path from to . We consider the grid, and say that a cell is lit if the edit path passes through the -block. We write for the set of cells lit by , and refer to as the interleaving imposed by . Observe that forms a monotone path from to in the grid. We use to denote the set of all interleavings from to .
Example 5.10.
Continuing Ex.5.9, the grid below shows that the marked edit path passes through blocks , , , and ; the cells corresponding to these blocks are diagonally hatched.
Remark 5.11.
Note that , since any such interleaving consists of a total of rightward steps and downward steps, and is therefore determined by the choice of which of the steps are rightward.
Sub-blocks and the cyclic-edit graph.
In our linear program, we want to reason simultaneously about many words and . To this end, we observe that each -block is composed of identical copies of the edit graph of and , which we call sub-blocks. Figure 1 (right) illustrates the partition of the -block into four identical sub-blocks corresponding to the edit graph of to .
To reason uniformly about powers of and , we introduce the cyclic edit graph of and , denoted . Let and . The vertices of form a quotient of the set where is identified with , and is identified with .
These identifications reflect the adjacency of sub-blocks: for instance, the vertex of a given sub-block (e.g., the orange block in Figure 1 (right)) coincides with of the sub-block to its left (the cyan one), and analogously for the vertical direction.
As a result, there is a bijection between edit paths from to and walks in , where and are suffixes and and are prefixes of and , respectively. This bijection preserves both weight and length. Moreover, every walk starting at corresponds to an edit path from some word in to some word in , and similarly for walks ending at and words in and .
Example 5.12.
Consider the edit path in the -block of Figure 1 (right). It induces the edit path which corresponds to the walk in . This walk represents the alignment of the words and , that is, and , respectively, where is indeed a suffix of .
Directions.
Another means to simultaneously reason on many and is the notion of direction. Consider again , , and . Note that . The direction induced by is the vector , where .
Example 5.13.
For instance, for , , we get that , and .
For every , the induced direction lies in the set Note that each direction is induced by infinitely many vectors in : if induces , then so does for any .
Not every vector induces a word that belongs to . We therefore restrict attention to directions that are realizable by words in , at least asymptotically. Let denote the Parikh image of with respect to . We define the direction set of as where the closure is taken in the standard topology on . Since is a linear set, is a polytope.
The Linear Program
Fix a direction and an interleaving . Our goal is to describe an optimal edit path from a word in inducing the direction to a word in , under the restriction that the path respects the interleaving . We use a linear program to solve the infimum for any and , where is the “closest” direction to a in and if is a corresponding edit path then represents the fraction . To this aim we define a linear program over the following set of variables:
Intuitively the variable holds the fraction corresponding to the number of times edge is used in along divided by the length of the edit path . Thus, the sum overall these variables should be , and this is Constraint 1 below. Working with edit paths whose length is normalized to simplifies the reasoning for ned: under this condition the cost of the path is the same as its weight.
Not every assignment to the variables corresponds to a valid edit path. In a path, for every vertex (except for the source and target) every visited vertex has a corresponding entry and exit edges. This gives rise to Constraint 2 below.777The discrepancy for the source and target is taken care of in the proof details.
We further require that the resulting edit path corresponds to a word in with direction , and transforms it into an optimal word in . We use the indicators and where equals if the edge consumes a letter of – that is, if it corresponds to a substitution, deletion, or no-op – and equals otherwise (and analogously for ). Constraint 3 ensures that the total use of edit operations reading symbols from the source block matches the contribution prescribed by the direction . Recall that parameter corresponds to the ratio . It is introduced to account for the fact that the expression naturally yields normalized by , whereas the direction vector is defined in terms of normalization by . Multiplication by therefore corrects the denominator, aligning the normalization with that of the source direction.
For the target word, we do not fix a direction in advance. Instead, we allow the linear program to choose an optimal target direction. This is achieved using the variables , which induce a normalized direction vector for via Intuitively, the variables normalize by a natural combination of the generators of . Accordingly, represents the -normalized contribution of the target block . Constraint 4 ensures that the edit path consumes symbols from the target blocks in accordance with this induced direction.
Constraints.
The constraints of the linear program are as follows:
-
1.
Normalization constraint:
-
2.
Flow conservation in each block: for every and every vertex
-
3.
Source-consumption constraints: for all
-
4.
Target-consumption constraints: for all
Objective.
where is the weight of . The objective thus looks for an edit path with minimum cost.
For a fixed direction , each interleaving induces a linear program . We denote by the optimal value of . We aggregate these by taking the best value over all interleavings, and define Continuity of is used in the proof of the following theorem.
Claim 5.14.
The function is continuous.
We can finally state the main theorem for this section.
Theorem 5.15.
Let and be bounded CFLs, and let and be the corresponding interleaving and directions set. Then
Putting the above ingredients together, we obtain the following complexity bound for computing on bounded context-free languages.
Lemma 5.16.
There exists an Exp algorithm that computes for bounded context-free languages and .
Proof sketch.
By the LP characterization, , where and is the optimum of a linear program whose constraints are independent of and whose right-hand side depends affinely on it. By LP duality, for each , is a convex, piecewise-linear function of . Hence, is also piecewise linear.
Since is a compact polytope, the supremum of is attained at a vertex of the arrangement induced by these linear pieces. The number of defining hyperplanes is exponential, as it depends on the number of interleavings and dual vertices. Standard bounds on hyperplane arrangements imply that the number of candidate vertices is exponential.
The algorithm enumerates these candidates, evaluates at each by solving the corresponding linear programs, and returns the maximum. This yields an exponential algorithm.
6 Discussion
The Asymptotic Hausdorff lifting captures a notion of similarity that is inherently asymptotic, comparing infinite sets by their long-run behavior while deliberately abstracting away from finite deviations. This choice is intentional: in formal verification, two languages are considered similar when similarity is witnessed on increasingly long words. In particular, in applications such as repair, robustness, and automata learning, asymptotic behavior with respect to a normalized edit distance between words reflects the semantic core of the compared languages.
Our results highlight an inherent trade-off between robustness and sensitivity. Classical Hausdorff-style constructions are highly sensitive to outliers, whereas asymptotic notions necessarily collapse certain local distinctions. The Asymptotic Hausdorff lifting makes this trade-off explicit and allows additional sensitivity to be reintroduced in a controlled manner by combining it with complementary distances.
Although formal languages serve as a central motivating domain, the lifting scheme itself is more general and applies to any infinite domain equipped with a meaningful notion of size and an element-level metric.
From an algorithmic perspective, our results indicate that computing is already challenging for regular and bounded context-free languages, and extending the proposed techniques to richer classes or more efficient exact algorithms is an interesting direction for future work.
References
- [1] Michael Benedikt, Gabriele Puppis, and Cristian Riveros. The cost of traveling between languages. In Luca Aceto, Monika Henzinger, and Jirí Sgall, editors, Automata, Languages and Programming - 38th International Colloquium, ICALP 2011, Zurich, Switzerland, July 4-8, 2011, Proceedings, Part II, volume 6756 of Lecture Notes in Computer Science, pages 234–245. Springer, 2011. doi:10.1007/978-3-642-22012-8_18.
- [2] Michael Benedikt, Gabriele Puppis, and Cristian Riveros. Regular repair of specifications. In Proceedings of the 26th Annual IEEE Symposium on Logic in Computer Science, LICS 2011, June 21-24, 2011, Toronto, Ontario, Canada, pages 335–344. IEEE Computer Society, 2011. doi:10.1109/LICS.2011.43.
- [3] Michael Benedikt, Gabriele Puppis, and Cristian Riveros. Bounded repairability of word languages. J. Comput. Syst. Sci., 79(8):1302–1321, 2013. doi:10.1016/J.JCSS.2013.06.001.
- [4] Michael Benedikt, Gabriele Puppis, and Cristian Riveros. The per-character cost of repairing word languages. Theoretical Computer Science, 539:38–67, 2014. doi:10.1016/j.tcs.2014.04.021.
- [5] Florian Bruse, Maurice Herwig, and Martin Lange. A similarity measure for formal languages based on convergent geometric series. In Implementation and Application of Automata: 26th International Conference, CIAA 2022, Rouen, France, June 28 – July 1, 2022, Proceedings, Berlin, Heidelberg, 2022. Springer-Verlag. doi:10.1007/978-3-031-07469-1_6.
- [6] Noam Chomsky and George A. Miller. Finite state languages. Inf. Control., 1(2):91–112, 1958. doi:10.1016/S0019-9958(58)90082-2.
- [7] Cewei Cui, Zhe Dang, Thomas R. Fischer, and Oscar H. Ibarra. Similarity in languages and programs. Theoretical Computer Science, 498:58–75, 2013. doi:10.1016/j.tcs.2013.05.040.
- [8] Colin de la Higuera and Luisa Micó. A contextual normalised edit distance. In Proceedings of the 24th International Conference on Data Engineering Workshops, ICDE 2008, April 7-12, 2008, Cancún, Mexico, pages 354–361. IEEE Computer Society, 2008. doi:10.1109/ICDEW.2008.4498345.
- [9] Emmanuel Filiot, Nicolas Mazzocchi, Jean-François Raskin, Sriram Sankaranarayanan, and Ashutosh Trivedi. Weighted transducers for robustness verification. In Igor Konnov and Laura Kovács, editors, 31st International Conference on Concurrency Theory, CONCUR 2020, Vienna, Austria (Virtual Conference), September 1-4, 2020, volume 171 of LIPIcs, pages 17:1–17:21. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020. doi:10.4230/LIPIcs.CONCUR.2020.17.
- [10] Dana Fisman, Joshua Grogin, Oded Margalit, and Gera Weiss. The normalized edit distance with uniform operation costs is a metric. In 33rd Annual Symposium on Combinatorial Pattern Matching, CPM 2022, June 27-29, 2022, Prague, Czech Republic. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2022. doi:10.4230/LIPIcs.CPM.2022.17.
- [11] Dana Fisman and Ilay Tzarfati. When is the normalized edit distance over non-uniform weights a metric? In Shunsuke Inenaga and Simon J. Puglisi, editors, 35th Annual Symposium on Combinatorial Pattern Matching, CPM 2024, Fukuoka, Japan, June 25-27, 2024, volume 296 of LIPIcs, pages 14:1–14:17. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2024. doi:10.4230/LIPIcs.CPM.2024.14.
- [12] Robert C. Flagg. Quantales and continuity spaces. Algebra Universalis, 37(3):257–276, 1997.
- [13] Seymour Ginsburg and Edwin H. Spanier. Semigroups, Presburger formulas, and languages. Pacific Journal of Mathematics, 16(2):285–296, 1966.
- [14] Thomas A. Henzinger, Jan Otop, and Roopsha Samanta. Lipschitz robustness of finite-state transducers. In Venkatesh Raman and S. P. Suresh, editors, 34th International Conference on Foundation of Software Technology and Theoretical Computer Science, FSTTCS 2014, New Delhi, India, December 15-17, 2014, volume 29 of LIPIcs, pages 431–443. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2014. doi:10.4230/LIPIcs.FSTTCS.2014.431.
- [15] Manfred Kudlek and Benedek Nagy. Distances of formal languages. PU.M.A. Pure Mathematics and Applications, 17, January 2006.
- [16] F. William Lawvere. Metric spaces, generalized logic, and closed categories. Rendiconti del Seminario Matematico e Fisico di Milano, 43(1):135–166, 1973.
- [17] Vladimir Iosifovich Levenshtein. Binary codes capable of correcting deletions, insertions and reversals. Soviet Physics Doklady, 10(8):707–710, February 1966. Doklady Akademii Nauk SSSR, V163 No4 845-848 1965.
- [18] A. Marzal and E. Vidal. Computation of normalized edit distance and applications. IEEE Transactions on Pattern Analysis and Machine Intelligence, 15(9):926–932, 1993. doi:10.1109/34.232078.
- [19] Mehryar Mohri. Edit-distance of weighted automata. In Proceedings of the 7th International Conference on Implementation and Application of Automata, CIAA’02, pages 1–23, Berlin, Heidelberg, 2002. Springer-Verlag. doi:10.1007/3-540-44977-9_1.
- [20] Timothy Ng, David Rappaport, and Kai Salomaa. Relative prefix distance between languages. In International Conference on Developments in Language Theory, pages 284–295. Springer, 2017. doi:10.1007/978-3-319-62809-7_21.
- [21] Rohit J. Parikh. On context-free languages. Journal of the Association for Computing Machinery, 13(4):570–581, 1966. doi:10.1145/321356.321364.
- [22] Austin J. Parker, Kelly B. Yancey, and Matthew P. Yancey. Regular language distance and entropy, 2016. arXiv:1602.07715.
- [23] Roopsha Samanta, Jyotirmoy V. Deshmukh, and Swarat Chaudhuri. Robustness analysis of string transducers. In Dang Van Hung and Mizuhito Ogawa, editors, Automated Technology for Verification and Analysis, pages 427–441, Cham, 2013. Springer International Publishing. doi:10.1007/978-3-319-02444-8_30.
- [24] C. E. Shannon. A mathematical theory of communication. SIGMOBILE Mob. Comput. Commun. Rev., 5(1):3–55, January 2001. doi:10.1145/584091.584093.
- [25] Robert A. Wagner. Order-n correction for regular languages. Commun. ACM, 17(5):265–268, 1974. doi:10.1145/360980.360995.
- [26] Li Yujian and Liu Bo. A normalized levenshtein distance metric. IEEE Transactions on Pattern Analysis and Machine Intelligence, 29(6):1091–1095, 2007. doi:10.1109/TPAMI.2007.1078.
