Abstract 1 Introduction 2 Levenshtein distance computation 3 Integer linear programming formulations 4 Numerical experience 5 Conclusions References

Integer Programming Models for the Median of a 0-1 String Set Under Levenshtein Distance

Claudio Arbib ORCID Dipartimento di Ingegneria/Scienze dell’Informazione e Matematica, Università degli Studi dell’Aquila, Italy
Centre of Excellence DEWS, Coppito, Italy
   Andrea D’Ascenzo222corresponding author ORCID Department of Computer Science, Gran Sasso Science Institute, L’Aquila, Italy    Oya E. Karaşan ORCID Department of Industrial Engineering, Bilkent University, Ankara, Turkey    Andrea Pizzuti ORCID Dipartimento di Scienze Teoriche e Applicate, Università degli Studi eCampus, Novedrate, Italy
Abstract

The Median String Problem calls for finding a string that minimizes the average distance from a given set of strings. Under the Levenshtein (or edit) metric, the problem is 𝒩𝒫-hard even for binary strings. We devised two novel integer linear programming models for this case and tested them against the only formulation we are aware of in the literature. Our numerical experiments attest to the efficacy of the proposed approach.

Keywords and phrases:
Levenshtein Distance, Median String Problem, Integer Programming
Funding:
Claudio Arbib: National Innovation Ecosystem grant ECS00000041–VITALITY–CUP E13C22001060006. VITALITY: Innovation, digitalization and sustainability for the diffused economy in Central Italy (Ecosistema di Innovazione, Digitalizzazione e Sostenibilità per l’Economia Diffusa nell’Italia Centrale).
Andrea D’Ascenzo: Partially supported by the MUR (Italy) Department of Excellence 2023–2027.
Copyright and License:
[Uncaptioned image] © Claudio Arbib, Andrea D’Ascenzo, Oya E. Karaşan, and Andrea Pizzuti; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Mathematics of computing Combinatorial optimization
; Theory of computation Integer programming
Acknowledgements:
The authors are grateful to the reviewers for their helpful comments.
Editors:
Martin Aumüller and Irene Finocchi

1 Introduction

Given a set Λ={λ1,,λt} of strings formed by elements taken from a finite alphabet 𝒜, the Closest String Problem calls for finding another string 𝐱 that minimizes its distance from Λ. This 𝐱 is called a median or centre of Λ, according to whether its distance from Λ is defined as the average or the largest distance between 𝐱 and the λi, respectively.

Popular metrics for this problem are the Hamming distance, that counts the minimum number of symbols of 𝐱 one should replace to obtain λi, and the Levenshtein (or edit) distance. According to the latter metric, any two strings λ,ρ can be transformed into one another by the repeated application of three operations: addition, deletion, substitution. For example, one can transform λ=bonzo into ρ=ozono by directly replacing all elements of the first string but the last; or, also, by adding an o before the string head (obonzo), replacing b by z (ozonzo) and deleting the z before the last element. Unlike the Hamming distance, this metric is well defined for strings of different lengths.

Let a,d,s+, respectively, denote the cost of addition, deletion and substitution. Then the weighted Levenshtein distance (λ,ρ) of λ and ρ is defined as the minimum total cost to transform λ into ρ (or vice-versa). These costs can also depend, in general, on the position in the string(s) where the operation is performed. For non-negative costs, the function so defined is clearly non-negative, symmetric, subadditive, and is 0 if and only if λ=ρ.

In some applications, it is assumed that a=d=s=1, and in this case we speak of the unweighted Levenshtein distance. The Levenshtein distance between an n-string and an m-string can be computed in O(nm) time (see Section 2 below), but under the Strong Exponential Time Hypothesis it cannot be computed in strongly subquadratic time [5].

Two major types of Closest String Problem are considered in the literature:

The Median String Problem: Find a string 𝐱 that minimizes

¯(𝐱,Λ)=1ti=1t(𝐱,λi)

The Centre String Problem: Find a string 𝐱 that minimizes

max(𝐱,Λ)=max{(𝐱,λi):i=1,,t}

Both problems are 𝒩𝒫-hard under the Levenshtein metric [21, 31]. Under the Hamming metric, which is the special case of Levenshtein with a=d=, the median problem is trivial while the centre problem remains 𝒩𝒫-hard [15]: the best exact algorithms for the latter problem are based on integer linear programming, see [4], [13] (note that also sampling centre strings under the Hamming metric is 𝒩𝒫-hard [7]).

1.1 Literature

Median and centre strings are fundamental in such applications as classification [14, 26], trace reconstruction and matrix completion [10, 24], DNA/RNA sequence alignment [22], character and shapes recognition [8, 11]. Various solution methods were developed for metrics based on the Hamming distance: from heuristics and approximation algorithms [12, 16, 17, 34, 35], to exact methods based on Integer Linear Programming (ILP) [3, 4, 28]. However, the Hamming distance is not well-suited to models arising in computational biology and, to some extent, information theory, where the Levenshtein distance [6] is often more appropriate. The increasing complexity introduced by this metric explains why algorithms for this problem are, at the moment, less numerous and with comparatively poorer performance.

Several heuristics were devised for the Median String Problem, such as perturbation iterative refinement algorithms. Starting from an initial string [1], edits are done one at a time in sequence [27] or multiple at once [9], the latter achieving a faster convergence at the expense of solution quality. The procedure is interrupted when no further improvements can be found. Critical aspects lie in the definition of good initial strings and of operation ranking [29]. Alternatively, combination iterative refinement algorithms make use of subsequent averaging steps among pairwise input strings, up to finding an output string.

To our knowledge, only one ILP model has been investigated so far [20]. In that paper, a finite set of strings is given, each one weighted by the probability of its occurrence. The edit costs a,d,s are arbitrary and independent of the position of the character. The goal is to minimize the total cost paid to transform the median string into each of the strings given, weighted by string occurrence probabilities, minus an incentive (equal to s) for any character saved in the output string: in other words, the median string is penalized proportionally to its length. Although ingenious, this formulation appears of little practical benefit: up to 100 000 seconds of CPU are in fact reported to compute the median of ten 4-characters strings of length 10.

1.2 Our contribution

In this paper, we revise the ILP model proposed in [20] and compare it with two new ILP formulations for the Median String Problem. Unlike [20], we do not consider different string occurrence probabilities, nor different operation costs a,d,s, because flat coefficients in the objective function generally make the problem harder due to symmetry.

More general cases and the Centre String Problem will be approached and discussed in a subsequent study. Also, we focus on the simpler case where 𝒜 is binary: an extension to non-binary alphabets is formally straightforward, but its efficiency remains to be ascertained and will be the subject of future research.

Limiting to a binary alphabet, and in particular encoding 𝒜 in {0,1}, has the advantage of a numerical interpretation of the output string that allows us to directly use its 0-1 components in the formulation, eliminating the need for auxiliary variables. In fact, even for binary 𝒜, in [20] the median string components are generically integer and not 0-1, and for that reason their formulation requires activation constraints containing a “big M”.

Note also that the median string length cannot be predicted in a closed form from the problem data, but is an indeterminate of the problem and, as such, it is introduced in [20] as a single integer decision variable of the model. We adopt a different approach and, in our formulations, “digitalize” this variable by means of a limited amount of 0-1 variables. Another improvement we propose regards the upper bound to the median string length: the value proposed in [20], i.e., the sum of the string lengths in Λ, can in fact be replaced by twice the maximum length of a string in Λ minus 1.

Our computational findings demonstrate the advantage obtained with the modifications described.

2 Levenshtein distance computation

In this section we survey two methods to compute the distance between two given strings λ,ρ. Each method corresponds to an ILP model, that will be recalled in Section 3 in order to develop our ILP formulations.

2.1 Shortest path computation

The Levenshtein distance between two strings λ𝒜n, ρ𝒜m can be computed by a simple recursion [33], which amounts to finding the shortest path from 00 to nm in a directed acyclic graph G=(V,ADS) where

V={hk:0hn,0km}

and A,D,S respectively stand for add (or across), delete (or down) and substitute (or slant) arcs. All the arcs are directed from left to right and/or from up to down: those in A link h0 to h1, h1 to h2 and so on, up right to hn for all h; those in D link 0k to 1k, 1k to 2k and so on, down to mk; finally, those in S diagonally link hk to (h+1)(k+1) (see Figure 1). Summarizing:

A ={hkh(k+1)| 0hn, 0k<m}(across arcs)
D ={hk(h+1)k| 0h<n, 0km}(down arcs)
S ={hk(h+1)(k+1)| 0h<n, 0k<m}(slant arcs)

We identify arcs with transitions that transform λ into ρ in the following way:

  • An across arc hkh(k+1)A corresponds to updating λ by adding ρk+1 immediately after its hth element.

  • A down arc hk(h+1)kD corresponds to deleting the (h+1)th element of λ.

  • A slant arc hk(h+1)(k+1)S corresponds to replacing λh+1 with ρk+1.

When node nm is eventually reached, λ=ρ.

Refer to caption
Figure 1: Computing the unweighted Levenshtein distance between two alphabetical strings as a shortest path. Dashed arcs cost 0, solid ones cost 1: the horizontal arc 0001 adds ρ1=o before the string λ= bonzo, then the diagonal arc 0112 replaces λ1=b by ρ2=z; next, two 0-cost diagonal arcs up to node 34 replicate the substring λ2λ3=on, and the vertical arc 3444 removes λ4=z; finally 4455 replicates the entry λ5=o and terminates the path. With three dashed arcs, this path has minimum length and identifies a minimum set of operations that transforms λ= bonzo into ρ= ozono (i.e., those associated with the solid arcs: in the example, an insertion, a replacement and a deletion).

Graph G has 3nm+n+m arcs. Let ae (de,se) be the cost of adding to λ (deleting from λ, substituting) an entry in the transition identified by arc e. Then the weight of a shortest (00,nm)-path equals the weighted Levenshtein distance between λ and ρ. Defining ye{0,1} for any eE=ADS, this distance is obtained by solving

min eAaeye+eDdeye+eSseye (1)
s.t. eE00ye=eEnm+ye=1
eEu+yeeEuye=0uV:u00,nm
ye0eE

where Eu+ (Eu) is the set of arcs entering (leaving) the generic node u, with E00+=Enm=. Since the polyhedron is integral, ye does not need to be constrained in {0,1}.

2.2 Planar matching computation

The following definition is provided in [23] and [25] among many others.

Definition 1.

Given a bipartite graph G=(V,E), a matching ME is planar (or non-crossing) if no two edges in M cross in the canonical drawing of G.

Besides the method seen in § 2.1, we can represent the problem of finding the Levenshtein distance between λ𝒜n,ρ𝒜m as that of finding a planar matching M in a complete undirected bipartite graph B=(LR,L×R) with node sets L,R corresponding to the components of λ,ρ, |L|=n,|R|=m. Precisely:

  • Matching hL to kR corresponds to substituting λh with ρk.

  • An unmatched node of R in position k corresponds to adding to λ, in that position, the corresponding element of ρ.

  • An unmatched node of L corresponds instead to deleting the corresponding element from λ.

Since the matching is planar, the letters are inserted, replaced, or removed in the same order as they appear in the sequences. Then, to get the weight of the operations required to transform λ into ρ, one simply has to add the weight of the unmatched nodes to that of the edges, which represent costly substitutions (that is, those with λhρk).

Refer to caption
Figure 2: Computing the unweighted Levenshtein distance between the two alphabetical strings λ= bonzo, ρ= ozono as a planar matching M of minimum cost: edges of M are thick lines, (un)matched nodes are in grey (white); moreover, dashed edges cost 0, solid edges cost 1, unmatched nodes cost 1. The unmatched node 1R adds to λ the first letter of ρ, that is ρ1=o; edge 1-2 replaces λ1=b by ρ2=z; edge 2-3, 3-4 replicate λ2=o, λ3=n; the unmatched node 4L removes λ4=z; finally, edge 5-5 replicates λ5=o. With three dashed edges, M has minimum cost and identifies a minimum set of operations that transforms λ= bonzo into ρ= ozono (i.e., those associated with the solid edge and the two unmatched nodes: an insertion, a replacement and a deletion).

As an example, consider the planar matching depicted in Figure 2 and interpreted in the caption. This matching corresponds to the shortest path shown in Figure 1.

Defining yhk,lh,rk{0,1} for all hL,kR, with

yhk=1 if and only if hkM;

lh=1 if and only if hL is left unmatched;

rk=1 if and only if kR is left unmatched;

the distance between λ and ρ is computed by solving

min hLkRshkyhk+hLdhlh+kRakrk (2)
s.t. kRyhk+lh=1hL
hLyhk+rk=1kR
yhk+yuv1hkXuv,uL,vR
yhk,lh,rk{0,1}hL,kR

where

  • shk is the cost of replacing λh with ρk;

  • dh (ak) is the cost of deleting λh from λ (of adding ρk to λ);

  • Xuv={hk:h<u,k>v or h>u,k<v} contains all the edges that cross uv in a canonical drawing of B.

The graph B has nm edges, as many as the essential variables of model (2) (the lh and rk variables are auxiliary and can be derived from the equations).

3 Integer linear programming formulations

In this section, we formulate the Median String Problem as an ILP, based on the Levenshtein distance computation methods presented in § 2.1 and § 2.2.

3.1 Path formulation

Let us begin with the distance computation presented in § 2.1. Let ni denote the length of λi and suppose for the moment that we know the median string to be of length m. Associate then the ith string of Λ with a directed acyclic multigraph Gi=(Vi,Ei) where

Vi=V={00,,nim}Ei=AiDiSii=1,,t

The only difference from the graph of § 2.1 is that each slant arc is duplicated: that is, we define Si=CiFi, where Ci (Fi) is the set of costly (free) slant arcs of Gi. Consistent with our initial assumption of unweighted distance, we let ce=0 for eFi and ce=1 in all remaining cases. Given its shape, we call Gi a grid (see Figure 1) with nodes arranged into rows (indexed by h) and columns (indexed by k).

The path used in § 2.1 to compute the distance between 𝐱 and λi is identified by a unit flow that traverses Gi from source 00 to sink nim. Let yeIR+ denote the flow in arc e, and call Ehki+ (respectively, Ehki) the set containing the arcs that enter (leave) node hk. Considering also duplicated slant arcs, Ehki+ and Ehki contain up to four arcs; in particular, E00i+ and Enimi are empty. Finally, if e=(h1)(k1)hkFi, then we let he=h,ke=k.

To reduce the length of a path, one can take advantage of free arcs, provided that they have non-zero capacity. But the capacity of a free arc ending in hk depends on λhi and xk, being 1 if xk=λhi (because in this case replacement has no cost and one can use the free arc) and 0 if xkλhi (for in this case, replacement has a cost and the free arc must be interdicted). Thus, if λhi=1, then the capacity will be xk, while if λhi=0, it will be 1xk. This consideration leads to the following mixed-integer linear model written for i=1,,t:

eE00iye=eEnimi+ye = 1 (3)
eEhki+yeeEhkiye = 0hkVi,hk00,nim (4)
yexke 0eFi;λhei=1 (5)
ye+xke 1eFi;λhei=0 (6)
xk {0,1}k=1,,m (7)
ye 0eEi

Setting E=i=1tEi the objective reads

min¯=1teEceye (8)

The path formulation thus obtained is essentially derived from the model proposed in [20]. Equations (3) enforce that each path starts in 00 and ends in nim, and define the paths together with flow conservation conditions (4). Inequalities (5), (6), that strengthen (c1) and (c2) of [20], allow a path to use free slant arcs provided that xk=λhi. Note that the y-variables need not be constrained in {0,1}, since the constraint matrix is totally unimodular for any choice of the x-variables in {0,1}.

However, a crucial detail omitted so far in our path formulation is that the median (or centre) string length is not known, which is why in [20] a further integer variable is introduced to represent it. This variable, named m, is subject to the constraints (b) of [20] that involve a so-called “big M”: in such cases, the bounds obtained by linear relaxation are generally very loose, and the formulation performance is consequently expected to be very poor. To limit this inconvenience, we digitalize variable m by introducing the following 0-1 decision variables for p<kq, where p and q are suitable bounds to m:

  • zk=1 if the median (or centre) length m is at least k;

  • zk=0 otherwise.

Naturally,

zk zk+1for p<k<q (9)

as mk+1 implies mk. The generic grid Gi is extended up to the qth column by setting

Vi=V={00,,niq}

and the flow ye in each arc e entering column k(p,q] is capped by

ye zkfor eEhki+,1hni,p<kq (10)

By (10), then, no flow can reach a node nik for k larger than the median length.

Since m is unknown, also the sink node is not determined and depends on the zk. The second condition (3), that assumes the sink in node nim, is then replaced by

eEniki+yeeEnikiye = zkzk+1pkq (11)

(where we set by definition zp=1,zq+1=0). In fact,

  • if m=k, then zk=1,zk+1=0: the right-hand side of (11) equals 1 and node nik is fixed as the total flow terminal;

  • if m<k, then zk=0,zk+1=0: the right-hand side of (11) equals 0 and node nik is not reached by any flow because of inequalities (10). In this case, then, the sink belongs to a column that precedes the kth one;

  • if m>k, then zk=1,zk+1=1: again, the right-hand side of (11) equals 0 and so the flow that possibly enters nik also proceeds out of it as a whole, which implies that nik belongs to a column that precedes the sink.

We can prove the following upper bound on m.

Proposition 2.

Let N=maxi=1,,t(ni) and assume a=d=s. Then m2N1.

Proof.

Let λ be a median of Λ with length m, and suppose m2N. Then every input string λi would require at least N insertions just to match the median’s length. Hence, the total edit distance is atN. Let nj=N, and let us evaluate the distance of λj from Λ. Any string of Λ can be transformed into λj using N edits, and λj itself requires no edit: hence, the total distance of λj from Λ is a(t1)N, which means that λ is not median.

3.2 Matching formulation

The matching formulation is constructed after the distance computation method presented in § 2.2. For each λiΛ we start from a complete undirected bipartite graph Bi=(LiR,Li×R), where nodes in Li and in R are ordered, so that the kth node of Li corresponds to λki and the kth node of R to xk. Because we do not know the median string length m, we suppose |R|=qm. The basic idea is to associate an edge joining the hth node of Li to the kth node of R with the operation of substituting λhi with xk. To distinguish between the case λhixk and λhi=xk we duplicate the edges of Bi:

  • Ci=Li×R= the set of edges of Bi that represent a costly substitution;

  • Fi=Li×R= the set of edges of Bi that represent a free substitution.

Leaving the hth node of Li (the kth node of R) unmatched corresponds to bearing the cost of adding λhi to (of deleting xk from) the median string. Unmatched nodes are represented by self-loops added to Bi, so we define

  • Hi={hh:hLi}= the set of edges with both extremes in Li;

  • K={kk:kR}= the set of edges with both extremes in R;

and form in this way the final edge set

Ei=CiFiHiKwithE=i=1tEi

For every node of LiR we then define the set of all edges (including self-loops) that have that node as an extreme:

Ehi={eEi:e=hh or e=hk,kR}Eki={eEi:e=kk or e=hk,hLi}

Let p be a lower bound to m, and let us introduce our 0-1 decision variables:

ye=1 if and only if eEi is selected (1it);

xk= the kth element of the median string (1kq);

zk=1 if and only if the median string length is at least k (p<kq).

That is, the y-variables are as the y,l,r in § 2.2, while the x- and the z-variables are as in § 3.1. For e=hk, we let he=h,ke=k. The feasible region is then described by the following conditions written for i=1,,t:

eEhiye = 11hni (12)
eEkiye = 11kp (13)
eEkiye = zkp<kq (14)
eXfiye ni(1yf)fLi×R (15)
yexke 0eFi;λhei=1 (16)
ye+xke 1eFi;λhei=0 (17)
zk+1zk 0p<k<q (18)
ye {0,1}eEi (19)
xk,zk {0,1}k (20)

Equation (14) forbids the selection of any edge that touches kR if k is larger than the median string length m. Condition (15) rewrites the no-cross condition in § 2.2, with Xfi being the set of edges of Ei that cross f in a canonical drawing of Bi: if f is selected, then the right-hand side is 0 and no edge crossing f can in turn be selected; otherwise, the right-hand side is ni since this is the largest number of edges in a matching of Bi. This condition can be further strengthened by redefining Xfi as follows for f=uv:

Xuvi=(Euiuv){hk:h<u,kv}{hk:hu,k<v} (21)

Finally, conditions (16), (17), (18) are equivalent to (5), (6), (9) of § 3.1.

The objective is formally written as (8), but now ce=d or ce=a for self-loops, ce=0 for eFi, and ce=s for all the other edges.

4 Numerical experience

In this section, we report a computational experiment carried out with the formulations of Section 3. We use pat to denote the path formulation of § 3.1; mat for the matching formulation of § 3.2 strengthened with Xfi defined by (21); and finally, hay for the state-of-the-art formulation of [20].

We organized the testbed in two types of datasets.

  • Random (rnd): Each dataset of this type is denoted by rnd-n-t, where n is an upper bound to the lengths of the strings in the pool and t is the total number of strings. String lengths are sampled uniformly at random from the interval [n/2,n]. Each string element is drawn independently and uniformly from the alphabet 𝒜={0,1}.

  • Error Correcting Codes (ecc): These datasets consist of binary strings that are codewords of classical error–correcting codes. Each pool of strings is generated by sampling random messages and encoding them with the generator matrix of the chosen code family.

    • ham: Pool ham-t consists of t codewords from binary Hamming codes (2r1, 2r1r) for r{3,4} [19]. Each codeword is obtained by sampling a random message μ and computing c=μGmod2, where G is the corresponding generator matrix. The resulting Hamming codewords have fixed lengths 7 and 15.

    • rm: Pool rm-t contains t codewords from first-order Reed-Muller codes RM(1,r) for r{3,4} [30, 32]. A random message specifies the coefficients of an affine Boolean function, and the corresponding codeword is its evaluation vector over all inputs in {0,1}r. Reed-Muller codewords have lengths 2r, namely 8 and 16 in our setting.

For each dataset, and each combination of parameters n and t, we sampled three string pools.

The experiments were run on a MacBook M4 Pro with a 12-cores CPU with 48GB of RAM, macOS Tahoe 26.2. All formulations were implemented in Python. We used Gurobi 13 [18] to solve the ILPs.

Table 1: Experimental results with the rnd dataset.
instance pat mat hay
cpu¯ cpu^ gap¯ gap^ opt cpu¯ cpu^ gap¯ gap^ opt cpu¯ cpu^ gap¯ gap^ opt
rnd-5-10 0.02 0.03 0 0 3/3 0.04 0.05 0 0 3/3 0.20 0.34 0 0 3/3
rnd-5-20 0.05 0.07 0 0 3/3 0.10 0.11 0 0 3/3 1.14 1.58 0 0 3/3
rnd-5-30 0.12 0.14 0 0 3/3 0.20 0.25 0 0 3/3 3.38 3.77 0 0 3/3
rnd-5-40 0.13 0.15 0 0 3/3 0.31 0.42 0 0 3/3 4.34 4.72 0 0 3/3
rnd-5-50 0.17 0.18 0 0 3/3 0.55 0.67 0 0 3/3 6.76 7.13 0 0 3/3
rnd-10-10 0.25 0.26 0 0 3/3 1.59 1.87 0 0 3/3 2.29 2.40 0 0 3/3
rnd-10-20 0.72 0.79 0 0 3/3 3.71 4.01 0 0 3/3 15.12 22.19 0 0 3/3
rnd-10-30 1.29 1.44 0 0 3/3 8.84 11.02 0 0 3/3 74.96 91.46 0 0 3/3
rnd-10-40 1.83 2.05 0 0 3/3 23.72 39.43 0 0 3/3 143.45 205.83 0 0 3/3
rnd-10-50 2.83 3.07 0 0 3/3 44.61 57.56 0 0 3/3 366.24 tlr 41.49 124.48 2/3
rnd-15-10 1.52 1.95 0 0 3/3 tlr tlr 14.38 25.00 0/3 23.75 36.69 0 0 3/3
rnd-15-20 3.20 3.33 0 0 3/3 430.29 tlr 16.95 27.27 1/3 126.39 164.90 0 0 3/3
rnd-15-30 7.07 9.36 0 0 3/3 tlr tlr 35.68 41.72 0/3 368.46 525.33 0 0 3/3
rnd-15-40 20.31 30.09 0 0 3/3 tlr tlr 25.92 30.76 0/3 tlr tlr 68.89 127.40 0/3
rnd-15-50 25.00 29.45 0 0 3/3 tlr tlr 34.92 46.96 0/3 tlr tlr 116.28 135.15 0/3
rnd-20-10 9.95 12.05 0 0 3/3 tlr tlr 39.20 43.10 0/3 199.16 258.21 0 0 3/3
rnd-20-20 77.91 91.89 0 0 3/3 tlr tlr 47.30 51.38 0/3 tlr tlr 32.68 34.54 0/3
rnd-20-30 182.39 296.27 0 0 3/3 tlr tlr 49.88 55.20 0/3 tlr tlr 47.22 54.70 0/3
rnd-20-40 193.82 240.44 0 0 3/3 tlr tlr 56.24 59.03 0/3 tlr tlr 106.50 110.91 0/3
rnd-20-50 233.71 318.32 0 0 3/3 tlr tlr 56.56 60.00 0/3 tlr tlr 112.22 116.03 0/3

For each instance, we reported the runtime (cpu), that is, the time spent by Gurobi to solve the model, with a time limit of 600 seconds. If the solver stopped for time limit, we also recorded the optimality gap (gap) between the best feasible solution (bfs) and the best dual bound (bdb). The gap (in percentage) is computed as BFSBDBBFS×100. In what follows, we use cpu¯ and cpu^ to denote, respectively, the average and maximum runtime over the three instances of the same pool size; gap¯ and gap^ indicate, respectively, the average and the maximum optimality gap over the same set, and opt reports how many of the three instances were solved to optimality. The acronym tlr indicates that the time limit has been reached.

4.1 Random dataset

Figure 3: Mean CPU time in the rnd dataset for different values of n and t.

Table 1 reports the computational results of the three formulations on the rnd dataset. The data reveal clear differences among the performance of the formulations as the maximum string length n and the pool size t increase. All in all, pat is consistently the most efficient and scalable model, mat shows moderate performance for small and medium instances but quickly deteriorates as n grows, while hay becomes impractical already for moderately large values of n and t.

For short strings (n=5), all formulations solve every instance to optimality. However, runtime differs substantially: pat remains below 0.20 seconds even for t=50, mat stays below 0.70 seconds, whereas hay already requires up to 7.13 seconds. Increasing the pool size from 10 to 50 affects hay much more severely than the other two formulations.

When the maximum string length allowed increases to n=10, the performance gap between formulations increases significantly. pat still solves all instances in 3.1 seconds, while mat takes up to 57.56 seconds for t=50. The hay formulation shows a steep increase in runtime, reaching 205.83 seconds for t=40 and hitting the time limit for t=50, where it solves two instances of three, with a very large gap (124.48%) on the unsolved one.

For n=15, the differences become even more evident. pat continues to solve all instances to optimality, with runtimes up to 30.09 seconds. In contrast, for t10 mat could only solve one instance (RND-15-20), reaching the time limit in all the remaining cases and showing average gaps between 14.38% and 35.68%, with a maximum of 46.96% on rnd-15-50. The hay formulation performs better than mat for small t, solving all instances up to t=30, but reaches the time limit for t40, with gaps exceeding 100%.

The longest strings tested (n=20) clearly illustrate the scalability limits of the formulations. pat remains the only model capable of solving all instances within the time limit, with a runtime of up to 318.32 seconds. Both mat and hay hit the time limit for every instance with t20 (mat also for t=10), but the gaps of hay are substantially larger for t=40 and t=50: mat shows average gaps around 50% and a maximum of 60%, while hay reaches 116.03%. Figures 34 provide an illustrative overview of the statistics discussed.

Figure 4: Mean gap in the rnd dataset for different values of n and t.

4.2 Error Correcting Codes dataset

Table 2 reports the computational results of the three formulations on the ecc dataset. We recall that the ecc instances contain codewords with fixed lengths and strong algebraic structure. This distinction has a clear impact on the behaviour of the formulations. Across all experiments, pat remains the most efficient and reliable model. It solves every instance to optimality, with modest increases in runtime as the pool size grows. For both Hamming and Reed-Muller instances, the runtime remains below 30 seconds for t=50, and the optimality gap is always zero. This confirms that pat scales very well even when the underlying strings come from highly structured code families.

The performance of mat is more subtle. For the smallest Hamming instances (t=10), it solves all cases to optimality, albeit with runtime significantly larger than pat and hay. As the pool size increases, mat reaches the time limit more frequently, especially for Hamming codes with t20. Nevertheless, even when it cannot close the gap completely, mat often keeps it reasonably small (e.g., 5.30% for t=20 and 17.95% for t=30, on average, with a peak of 21.81%) and provides, in general, meaningful bounds and feasible solutions. A similar pattern appears in the rm dataset: although mat hits the time limit for all pool sizes, the resulting gaps remain relatively stable (between 19.24% and 25.45%, with a peak of 28.42%), indicating a performance that degrades gradually rather than sharply.

The hay formulation has a different trend. It performs very well for small and medium Hamming instances (t30), solving them all to optimality in a runtime that remains quite manageable (up to 569.50 seconds for t=30). However, its scalability deteriorates for larger pool sizes: for t40, hay reaches the time limit and returns large optimality gaps (on average, 45.05% and 50.57%, peaking at 112.14%). In rm, hay again solves all instances up to t=30, but its performance drops for larger pools: it solves two out of three instances for t=40 and none for t=50, with non-negligible gaps (although, in this case, smaller on average than mat). A graphical comparison of the mean CPU time and gap is provided in Figure 5.

Table 2: Experimental results with the ecc dataset.
instance pat mat hay
cpu¯ cpu^ gap¯ gap^ opt cpu¯ cpu^ gap¯ gap^ opt cpu¯ cpu^ gap¯ gap^ opt
ham-10 1.52 1.82 0 0 3/3 167.60 337.67 0 0 3/3 11.78 14.09 0 0 3/3
ham-20 4.97 5.86 0 0 3/3 tlr tlr 5.30 9.57 0/3 72.11 85.88 0 0 3/3
ham-30 12.83 13.74 0 0 3/3 tlr tlr 17.95 21.81 0/3 357.04 569.50 0 0 3/3
ham-40 20.18 21.68 0 0 3/3 563.01 tlr 6.97 14.70 1/3 tlr tlr 45.05 112.14 0/3
ham-50 27.87 29.01 0 0 3/3 tlr tlr 15.13 15.51 0/3 tlr tlr 50.57 109.63 0/3
rm-10 3.45 4.30 0 0 3/3 tlr tlr 19.24 22.80 0/3 37.10 47.79 0 0 3/3
rm-20 7.64 10.19 0 0 3/3 tlr tlr 19.43 21.62 0/3 85.15 118.30 0 0 3/3
rm-30 11.68 12.10 0 0 3/3 tlr tlr 23.51 25.90 0/3 196.65 255.23 0 0 3/3
rm-40 20.41 26.33 0 0 3/3 tlr tlr 25.25 25.75 0/3 434.79 tlr 9.32 27.98 2/3
rm-50 26.77 29.41 0 0 3/3 tlr tlr 25.45 28.42 0/3 tlr tlr 14.17 16.60 0/3
Figure 5: Mean CPU time (top) and mean gap (bottom) in the ham and the rm datasets.

5 Conclusions

In this paper, we consider the Median String Problem under the Levenshtein metric focusing on the case of 0-1 strings. We move from the ILP formulation proposed in [20] (here referred to as hay) and devise two novel ILPs that improve hay in several aspects:

  • The path formulation (pat), which models a sort of network design problem where a graph is modified in order to select a series of paths of minimum cost.

  • The matching formulation (mat), which computes the distance of each input string from the median as a planar matching of minimum cost.

Limiting our attention to 0-1 strings offers the advantage of a simplified set of decision variables. A general improvement is obtained by digitalizing the median string length that, being not known in advance, needs to be treated as a problem decision variable: with respect to this, we also use an upper bound on the median string length that, despite its simplicity, improves the one adopted in [20].

Summarizing the result of our computational experience, pat clearly proves to be the most scalable method in all datasets considered. A comparison between mat and hay reveals an interesting interplay between strength and scalability. Although hay is generally more effective for small and medium instances, mat tends to degrade more smoothly as the pool size increases. Except for some instances (specifically, those in the dataset rm), when mat cannot find an optimal solution it terminates however with moderate gaps, as opposed to hay which shows a sharper deterioration for large pools. Thus, although mat is not the fastest formulation, it remains valuable in challenging settings where hay becomes computationally prohibitive or unstable.

Further benefits could be gained by further reducing the upper bound on the median string length (so reducing the number of z-variables in both pat and mat), or by devising valid inequalities and computing effective heuristic solutions for warm start to reduce optimality gaps (especially in mat).

References

  • [1] José I Abreu Salas and Juan R Rico-Juan. A new iterative algorithm for computing a quality approximate median of strings based on edit operations. Pattern Recognition Letters, 36:74–80, 2014. doi:10.1016/j.patrec.2013.09.014.
  • [2] Claudio Arbib, Andrea D’Ascenzo, Oya E. Karaşan, and Andrea Pizzuti. D-hash/CenterLevenshtein. Software, swhId: swh:1:dir:29a2a7464e7ff0450026b55e399cfb749ef45866 (visited on 2026-06-01). URL: https://github.com/D-hash/CenterLevenshtein, doi:10.4230/artifacts.26233.
  • [3] Claudio Arbib, Giovanni Felici, Mara Servilio, and Paolo Ventura. Optimum solution of the closest string problem via rank distance. In Combinatorial Optimization: 4th International Symposium, ISCO 2016, Vietri sul Mare, Italy, May 16-18, 2016, Revised Selected Papers 4, pages 297–307. Springer, 2016. doi:10.1007/978-3-319-45587-7_26.
  • [4] Claudio Arbib, Mara Servilio, and Paolo Ventura. An improved integer linear programming formulation for the closest 0-1 string problem. Computers & Operations Research, 80:94–100, 2017. doi:10.1016/J.COR.2016.11.019.
  • [5] Arturs Backurs and Piotr Indyk. Edit distance cannot be computed in strongly subquadratic time (unless seth is false). In Proceedings of the forty-seventh annual ACM symposium on Theory of computing, pages 51–58, 2015. doi:10.1145/2746539.2746612.
  • [6] Bonnie Berger, Michael S Waterman, and Yun William Yu. Levenshtein distance, sequence comparison and biological database search. IEEE transactions on information theory, 67(6):3287–3294, 2020. doi:10.1109/TIT.2020.2996543.
  • [7] Christina Boucher and Mohamed Omar. On the hardness of counting and sampling center strings. IEEE/ACM Transactions on Computational Biology and Bioinformatics, 9(6):1843–1846, 2012. doi:10.1109/TCBB.2012.84.
  • [8] Horst Bunke, Xiaoyi Jiang, Karin Abegglen, and Abraham Kandel. On the weighted mean of a pair of strings. Pattern Analysis & Applications, 5:23–30, 2002. doi:10.1007/S100440200003.
  • [9] Ramón A Mollinedas Cardenas. A learning model for multiple-prototype classification of strings. In Proceedings of the 17th International Conference on Pattern Recognition, 2004. ICPR 2004., volume 4, pages 420–423 Vol.4, 2004. doi:10.1109/ICPR.2004.1333792.
  • [10] Diptarka Chakraborty, Debarati Das, and Robert Krauthgamer. Approximate Trace Reconstruction via Median String (In Average-Case). In Mikołaj Bojańczy and Chandra Chekuri, editors, 41st IARCS Annual Conference on Foundations of Software Technology and Theoretical Computer Science (FSTTCS 2021), volume 213 of Leibniz International Proceedings in Informatics (LIPIcs), pages 11:1–11:23, Dagstuhl, Germany, 2021. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.FSTTCS.2021.11.
  • [11] Sei-Wang Chen, ST Tung, Chiung-Yao Fang, Shen Cherng, and Anil K Jain. Extended attributed string matching for shape recognition. Computer Vision and Image Understanding, 70(1):36–50, 1998. doi:10.1006/CVIU.1998.0599.
  • [12] Debarati Das and Barna Saha. Approximating LCS and Alignment Distance over Multiple Sequences. In Amit Chakrabarti and Chaitanya Swamy, editors, Approximation, Randomization, and Combinatorial Optimization. Algorithms and Techniques (APPROX/RANDOM 2022), volume 245 of Leibniz International Proceedings in Informatics (LIPIcs), pages 54:1–54:21, Dagstuhl, Germany, 2022. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.APPROX/RANDOM.2022.54.
  • [13] Federico Della Croce and Michele Garraffa. The selective fixing algorithm for the closest string problem. Computers & operations research, 41:24–30, 2014. doi:10.1016/J.COR.2013.07.017.
  • [14] Liviu P Dinu and Radu Tudor Ionescu. Clustering based on median and closest string via rank distance with applications on dna. Neural Computing and Applications, 24:77–84, 2014. doi:10.1007/S00521-013-1468-X.
  • [15] Moti Frances and Ami Litman. On covering problems of codes. Theory of Computing Systems, 30:113–119, 1997. doi:10.1007/S002240000044.
  • [16] Arun Ganesh, Tomasz Kociumaka, Andrea Lincoln, and Barna Saha. How compression and approximation affect efficiency in string distance measures. In Proceedings of the 2022 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 2867–2919. SIAM, 2022. doi:10.1137/1.9781611977073.112.
  • [17] Leszek Ga̧sieniec, Jesper Jansson, and Andrzej Lingas. Efficient approximation algorithms for the hamming center problem. In Proceedings of the tenth annual ACM-SIAM symposium on Discrete algorithms, pages 905–906, 1999.
  • [18] Gurobi Optimization, LLC. Gurobi Optimizer Reference Manual, 2023. URL: https://www.gurobi.com.
  • [19] Richard W Hamming. Error detecting and error correcting codes. The Bell system technical journal, 29(2):147–160, 1950.
  • [20] Morihiro Hayashida and Hitoshi Koyano. Finding median and center strings for a probability distribution on a set of strings under levenshtein distance based on integer linear programming. In Biomedical Engineering Systems and Technologies: 9th International Joint Conference, BIOSTEC 2016, Rome, Italy, February 21–23, 2016, Revised Selected Papers 9, pages 108–121. Springer, 2017. doi:10.1007/978-3-319-54717-6_7.
  • [21] Gary Hoppenworth, Jason W Bentley, Daniel Gibney, and Sharma V Thankachan. The fine-grained complexity of median and center string problems under edit distance. In 28th Annual European Symposium on Algorithms, ESA 2020, 2020.
  • [22] Tao Jiang, Ying Xu, and Michael Q. Zhang. Bio-Sequence Comparison and Applications, pages 45–69. MIT Press, 2002.
  • [23] Marcos Kiwi and Martin Loebl. Largest planar matching in random bipartite graphs. Random Structures & Algorithms, 21(2):162–181, 2002. doi:10.1002/rsa.10048.
  • [24] Tomohiro Koana, Vincent Froese, and Rolf Niedermeier. The complexity of binary matrix completion under diameter constraints. Journal of Computer and System Sciences, 132:45–67, 2023. doi:10.1016/J.JCSS.2022.10.001.
  • [25] Federico Malucelli, Thomas Ottmann, and Daniele Pretolani. Efficient labelling algorithms for the maximum noncrossing matching problem. Discrete Applied Mathematics, 47(2):175–179, 1993. doi:10.1016/0166-218X(93)90090-B.
  • [26] Carlos D Martínez-Hinarejos, Alfons Juan, and Francisco Casacuberta. Use of median string for classification. In Proceedings 15th International Conference on Pattern Recognition. ICPR-2000, volume 2, pages 903–906. IEEE, 2000.
  • [27] Carlos D Martínez-Hinarejos, Alfons Juan-Císcar, and Francisco Casacuberta. Median strings for k-nearest neighbour classification. Pattern Recognition Letters, 24(1):173–181, 2003. doi:10.1016/S0167-8655(02)00209-X.
  • [28] Cláudio N Meneses, Zhaosong Lu, Carlos AS Oliveira, and Panos M Pardalos. Optimal solutions for the closest-string problem via integer programming. INFORMS Journal on Computing, 16(4):419–429, 2004. doi:10.1287/IJOC.1040.0090.
  • [29] Pedro Mirabal, José I Abreu Salas, and Diego Seco. Assessing the best edit in perturbation-based iterative refinement algorithms to compute the median string. Pattern Recognition Letters, 120:104–111, 2019. doi:10.1016/j.patrec.2019.02.004.
  • [30] David E Muller. Application of boolean algebra to switching circuit design and to error detection. Trans. I R E Prof. Group Electron. Comput., 3(3):6–12, 1954. doi:10.1109/IREPGELC.1954.6499441.
  • [31] François Nicolas and Eric Rivals. Hardness results for the center and median string problems under the weighted and unweighted edit distances. Journal of discrete algorithms, 3(2-4):390–415, 2005. doi:10.1016/J.JDA.2004.08.015.
  • [32] Irving S Reed. A class of multiple-error-correcting codes and the decoding scheme. Trans. IRE Prof. Group Inf. Theory, 4:38–49, 1954. doi:10.1109/TIT.1954.1057465.
  • [33] Robert A Wagner and Michael J Fischer. The string-to-string correction problem. Journal of the ACM (JACM), 21(1):168–173, 1974. doi:10.1145/321796.321811.
  • [34] Shirley Xu and David Perkins. A heuristic solution to the closest string problem using wave function collapse techniques. IEEE Access, 10:115869–115883, 2022. doi:10.1109/ACCESS.2022.3218003.
  • [35] Shota Yuasa, Zhi-Zhong Chen, Bin Ma, and Lusheng Wang. Designing and implementing algorithms for the closest string problem. Theoretical Computer Science, 786:32–43, 2019. doi:10.1016/J.TCS.2018.05.017.