An Optimal Algorithm for Binary Closest String


Abstract

We revisit the Binary Closest String problem, which asks, given a set of binary strings \(X \subseteq \{0, 1\}^n\), to compute a string minimizing the maximum Hamming distance to \(X\). A long line of work has focused on parameterized algorithms with respect to the optimal distance \(d\), yielding a sequence of improvements from \(O^*(d^d)\) through \(O^*(16^d)\), \(O^*(9.513^d)\), \(O^*(8^d)\), \(O^*(6.731^d)\) to the current best-known running time of \(O^*(5^d)\) [Chen, Ma, Wang; Algorithmica ’16].

We present a faster randomized algorithm running in time \(O^*(4^d)\). Our result matches a recent fine-grained lower bound [Abboud, Fischer, Goldenberg, Karthik C.S., Safier; ESA ’23], and is therefore conditionally optimal. As an extra benefit, our algorithm is remarkably simple.

1 Introduction↩︎

The Closest String problem asks, given a set of strings \(X \subseteq \Sigma^n\), to find a string \(y^* \in \Sigma^n\) that minimizes the maximum Hamming distance to the strings in \(X\). It constitutes one of the most fundamental clustering tasks on strings, and is also of great importance in bioinformatics, for instance in the computer-aided design of universal PCR primers [1], motif finding [2], and drug design [3].

The Closest String problem is extremely well-studied, from both the theory and applied computational biology communities. It is known to be NP-hard [1], [4] (even for binary strings), and well-understood through the lens of approximation [1], [5][9]. Here we focus on the parameterized setting, where the goal is to design algorithms whose running time depends on the optimal distance \(d\). This setting has concrete practical relevance, as for the aforementioned biology applications \(d\) is typically very small. Over the past two decades, a long sequence of papers has developed progressively faster parameterized algorithms [9][15]. And yet, despite this sustained effort, it remained open to pinpoint the precise complexity of Closest String—even in the most basic regime of binary strings (i.e., for \(|\Sigma| = 2\)).

Concretely, the first parameterized algorithm was given by Gramm, Niedermeier, and Rossmanith [10] and runs in time1 \(O^*(d^d)\). The subsequent milestones for Binary Closest String are algorithms in time \(O^*(16^d)\) [9], \(O^*(9.513^d)\) [12], \(O^*(8^d)\) [13], \(O^*(6.731^d)\) [14], before culminating in the current state-of-the-art \(O^*(5^d)\) due to Chen, Ma, and Wang [15]. On the converse side, a recent lower bound construction due to Abboud, Fischer, Goldenberg, Karthik C.S., and Safier [16] implies that there is no \(O^*((4-\epsilon)^d)\)-time algorithm for Binary Closest String, for any constant , unless the Strong Exponential-Time Hypothesis (SETH) from fine-grained complexity fails.2

This leaves an exponential gap between the \(5^d\) upper bound and the \(4^d\) lower bound. In this paper we finally close this gap, and thereby resolve the parameterized complexity of Binary Closest String:

Theorem 1. Binary Closest String can be solved in randomized time \(O^*(4^d)\).

Notably, our algorithm is not only conditionally optimal, but also pleasingly simple. It can be stated in 6 lines of pseudocode (see 1), and is also conceptually simple. In fact, our algorithm is almost identical to a simple local search algorithm by Chen, Ma, and Wang [15] that runs in time \(O^*((2e)^d) = O^*(5.437^d)\) (from same paper that established the previous state of the art). We propose a technical yet important nuance: instead of running \(O^*((2e)^d)\) independent local searches of length \(O(d)\), we run one local search of length \(O^*(4^d)\). Our analysis is based on Markov chains and conceptually inspired by Schöning’s seminal algorithm [17] for \(k\)-SAT. The specific Markov chain turns out to be more complicated than in Schöning’s algorithm, but viewed the right way its analysis does not take more than a page.

To conclude, while our new algorithm will perhaps not have immediate practical impact, it does constitute the first optimal result in a long line of work [9][15]. For this reason we are confident that it may form the baseline to make progress on more general variants, e.g., for larger alphabets or for the strictly more general and even more practically important Closest Substring problem.

2 Formal Definitions↩︎

The Hamming distance of two strings \(x, y \in \Sigma^n\) is defined as . The Closest String problem is, given a set \(X \subseteq \Sigma^n\), to find a string \(y^*\) that minimizes \(\max_{x \in X} \HD(x, y^*)\). We define the parameter \(d = \max_{x \in X} \HD(x, y^*)\) as the optimal distance. The Binary Closest String problem is the restriction to alphabets of size \(|\Sigma| = 2\).

3 Algorithm↩︎

This section is devoted to the proof of 1. Throughout, to solve the Closest String problem, we assume that as part of the input we are given a parameter \(1 \leq d < n\) and the goal is to decide if there is a center string \(y^* \in \set{0, 1}^n\) within distance \(\max_{x \in X} \HD(x, y) \leq d\). The optimal \(d\) can then be identified by testing \(d \gets 1, 2, \dots\) one by one; the overhead of this search is only a constant factor in our final \(O^*(4^d)\)-time algorithm.

Figure 1: The O^*(4^d)-time algorithm for Binary Closest String

Consider the pseudocode in 1. The algorithm is designed in such a way that it will never return an incorrect output. In particular, if there is no solution then the algorithm does not terminate. In the following we assume that there is a solution \(y^*\), and argue that the algorithm will terminate in time \(O^*(4^d)\) with high probability.

Throughout fix an optimal solution \(y^*\) with \(\max_{x \in X} \HD(x, y^*) \leq d\) (if there are several, fix an arbitrary one). We say that the algorithm is in state \(i\) if . Initially, the algorithm starts in some state \(i \leq d\) (as \(\HD(y, y^*) \leq d\) for whichever element  we picked). In each iteration the algorithm flips exactly one bit in \(y\), and thus the current state \(i\) either increases or decreases by exactly \(1\). Once the algorithm reaches state \(0\) it certainly terminates (though it might also terminate before if there is more than one optimal solution). The following lemma is based on the ideas from [15].

Lemma 1. In any iteration with state \(i > 0\) the algorithm transitions to state \(i-1\) with probability at least \(p_i = \min\set{\frac{i}{2d}, \frac{1}{2}}\).

Proof. Let \(y, x\) be as in the current iteration. We distinguish three types of indices \(j\): The good ones with \(x[j] = y^*[j] \neq y[j]\), the bad ones with \(x[j] \neq y[j] = y^*[j]\), and the useless ones with \(x[j] = y[j] \neq y^*[j]\). (As the alphabet is binary, this only leaves the indices \(j\) with \(x[j] = y[j] = y^*[j]\) which play no role in the analysis.) Let \(G, B, U\) denote the number of good, bad and useless indices, respectively. Observe that \[\begin{align} {3} &\HD(x, y) &&= G + B &&> d, \\ &\HD(x, y^*) &&= B + U &&\leq d, \\ &\HD(y, y^*) &&= G + U &&= i. \end{align}\] By design the algorithm picks a uniformly random index among the good and bad ones, and succeeds in decreasing the state of the algorithm if it picks a good one. In other words, the probability to move to state \(i - 1\) is exactly \[\frac{G}{G + B} = \frac{(G + B) - (B + U) + (G + U)}{2(G + B)} \geq \frac{(G + B) - d + i}{2(G + B)}.\] If \(d \geq i\), then the function \(\frac{x - d+i}{2x}\) is non-decreasing for positive \(x\). If \(d < i\), the function is instead decreasing, but always lower-bounded by \(\frac{1}{2}\). It follows that \[\frac{G}{G + B} \geq \min\set*{\frac{d - d + i}{2d}, \frac{1}{2}} = \min\set*{\frac{i}{2d}, \frac{1}{2}},\] which is as claimed. ◻

1 allows us to model the execution by means of a Markov chain with transition probabilities as described in the lemma. More precisely, the textbook approach is to define a Markov chain that is coupled with the state of the algorithm, and to analyze the hitting time of our target state \(0\) in that coupled Markov chain. However, to minimize the notational overhead we here prefer a direct approach.

Lemma 2. The expected number of iterations to reach state \(0\) is at most \(4^d \cdot n^2\).

Proof. Let \(D_i\) denote the expected number of decrements (i.e., iterations that decrease the state) to move from state \(i\) to state . We trivially have \(D_n = 1\). For any state \(0 < i < n\), 1 shows that with probability at least \(p_i\) we move to \(i-1\) in one transition. Otherwise, with probability at most \(1 - p_i\), we first transition to \(i+1\) and then the expected number of decrements to visit state \(i - 1\) is at most \(D_{i+1} + D_i\). This yields the recurrence \[D_i \leq p_i \cdot 1 + (1 - p_i) \cdot (D_{i+1} + D_i),\] or equivalently, \[D_i \leq 1 + \frac{1 - p_i}{p_i} \cdot D_{i+1}.\] Unrolling the recurrence gives, for all \(0 < i \leq n\), \[D_i \leq \sum_{j=i}^n \prod_{m=i}^{j-1} \frac{1-p_m}{p_m}.\] Now recall that \(p_m = \min\set{\frac{m}{2d}, \frac{1}{2}}\). Thus, for \(m \geq d\) the quotient \(\frac{1-p_m}{p_m}\) becomes exactly \(1\), and for \(m < d\) it is certainly at least \(1\). It follows that \[D_i \leq \sum_{j=i}^n \prod_{m=i}^{d} \frac{1-p_m}{p_m} \leq n \prod_{m=1}^d \frac{1-p_m}{p_m} = n \prod_{m=1}^d \frac{2d - m}{m} = n \cdot \binom{2d - 1}{d}.\] Therefore, the expected number of decrements to reach state \(0\) from an arbitrary starting state \(i_0\) is at most \[\sum_{i=1}^{i_0} D_i \leq \sum_{i=1}^n D_i \leq n^2 \cdot \binom{2d-1}{d} \leq \frac{4^d \cdot n^2}{2}.\] Finally, in any sequence from state \(i_0\) to \(0\) the total number of increments is always upper bounded by the number of decrements, and so the expected total number of iterations is at most \(4^d \cdot n^2\). ◻

The proof of 1 is now a simple consequence of 2: Within \(4^d \cdot n^2\) iterations the algorithm is expected to reach state \(0\). Thus, within \(4^d \cdot 2n^2\) iterations the algorithm terminates with probability at least \(1/2\) (by Markov’s inequality), and within \(4^d \cdot n^3\) iterations the algorithm fails to terminate with exponentially small error probability \(\exp(-\Omega(n))\). In particular, after \(4^d \cdot n^3\) iterations we can interrupt 1 to confidently claim that there is no center in distance \(d\). Each iteration runs in polynomial time, and thus the total running time is \(O^*(4^d)\).

We finally comment on a micro-optimization of 1. The term \(n^2\) in 2 (and in the resulting running time) can easily be replaced by \(d^2\) by making sure that the algorithm only traverses the states \(\set{0, \dots, O(d)}\). To achieve this, one option is to add another line to the algorithm: When \(\max_{x \in X} \HD(x, y) > 2d\) then reset \(y\) to an arbitrary string in \(X\). Indeed, in this case the triangle inequality implies \(\HD(y, y^*) > d\), and hence the reset never increases the state. Combined with a careful implementation of 1, this results in an algorithm with expected running time \(O(n |X| + 4^d d^2 |X|)\).

References↩︎

[1]
J. K. Lanctôt, M. Li, B. Ma, S. Wang, and L. Zhang, “Distinguishing string selection problems,” Inf. Comput., vol. 185, no. 1, pp. 41–55, 2003, doi: 10.1016/S0890-5401(03)00057-9.
[2]
J. Gramm, F. Hüffner, and R. Niedermeier, “Closest strings, primer design, and motif search,” in Currents in computational molecular biology, poster abstracts of RECOMB, 2002, vol. 2002, pp. 74–75.
[3]
X. Deng, G. Li, Z. Li, B. Ma, and L. Wang, “Genetic design of drugs without side-effects,” SIAM J. Comput., vol. 32, no. 4, pp. 1073–1090, 2003, doi: 10.1137/S0097539701397825.
[4]
M. Frances and A. Litman, “On covering problems of codes,” Theory Comput. Syst., vol. 30, no. 2, pp. 113–119, 1997, doi: 10.1007/S002240000044.
[5]
A. Ben-Dor, G. Lancia, J. Perone, and R. Ravi, “Banishing bias from consensus sequences,” in 8th annual symposium on combinatorial pattern matching (CPM 1997), 1997, vol. 1264, pp. 247–261, doi: 10.1007/3-540-63220-4\_63.
[6]
L. Gasieniec, J. Jansson, and A. Lingas, “Efficient approximation algorithms for the hamming center problem,” in 10th ACM-SIAM symposium on discrete algorithms (SODA 1999), 1999, pp. 905–906, [Online]. Available: http://dl.acm.org/citation.cfm?id=314500.315081.
[7]
M. Li, B. Ma, and L. Wang, “On the closest string and substring problems,” J. ACM, vol. 49, no. 2, pp. 157–171, 2002, doi: 10.1145/506147.506150.
[8]
A. Andoni, P. Indyk, and M. Pătraşcu, “On the optimality of the dimensionality reduction method,” in 47th IEEE symposium on foundations of computer science (FOCS 2006), 2006, pp. 449–458, doi: 10.1109/FOCS.2006.56.
[9]
B. Ma and X. Sun, “More efficient algorithms for closest string and substring problems,” SIAM J. Comput., vol. 39, no. 4, pp. 1432–1443, 2009, doi: 10.1137/080739069.
[10]
J. Gramm, R. Niedermeier, and P. Rossmanith, “Fixed-parameter algorithms for CLOSEST STRING and related problems,” Algorithmica, vol. 37, no. 1, pp. 25–42, 2003, doi: 10.1007/S00453-003-1028-3.
[11]
Z.-Z. Chen and L. Wang, “Fast exact algorithms for the closest string and substring problems with application to the planted (l, d)-motif model,” IEEE ACM Trans. Comput. Biol. Bioinform., vol. 8, no. 5, pp. 1400–1410, 2011, doi: 10.1109/TCBB.2011.21.
[12]
L. Wang and B. Zhu, “Efficient algorithms for the closest string and distinguishing string selection problems,” in Frontiers in algorithmics, 2009, pp. 261–270, doi: 10.1007/978-3-642-02270-8_27.
[13]
R. Zhao and N. Zhang, “A more efficient closest string problem.” in 2nd international conference on bioinformatics and computational biology, 2010, pp. 210–215.
[14]
Z.-Z. Chen, B. Ma, and L. Wang, “A three-string approach to the closest string problem,” J. Comput. Syst. Sci., vol. 78, no. 1, pp. 164–178, 2012, doi: 10.1016/J.JCSS.2011.01.003.
[15]
Z.-Z. Chen, B. Ma, and L. Wang, “Randomized fixed-parameter algorithms for the closest string problem,” Algorithmica, vol. 74, no. 1, pp. 466–484, 2016, doi: 10.1007/S00453-014-9952-Y.
[16]
A. Abboud, N. Fischer, E. Goldenberg, Karthik C. S., and R. Safier, “Can you solve closest string faster than exhaustive search?” in 31st annual european symposium on algorithms, ESA 2023, amsterdam, the netherlands, september 4-6, 2023, 2023, vol. 274, pp. 3:1–3:17, doi: 10.4230/LIPICS.ESA.2023.3.
[17]
U. Schöning, “A probabilistic algorithm for k-SAT and constraint satisfaction problems,” in 40th IEEE symposium on foundations of computer science (FOCS 1999), 1999, pp. 410–414, doi: 10.1109/SFFCS.1999.814612.

  1. Here and throughout, the \(O^*(\cdot)\) notation hides polynomial factors in the input size, \(\poly(|X|, n)\).↩︎

  2. In detail: Abboud, Fischer, Goldenberg, Karthik C.S., and Safier [16] show that Binary Closest String cannot be solved in time \(O^*((2-\epsilon)^n)\). Inspecting their lower bound reveals that it applies to instances with \(d = (1/2 \pm o(1)) n\), implying that Binary Closest String cannot be solved in time in the regime where \(d = (1/2 \pm o(1)) n\). A simple padding argument extends this lower bound to the full range \(d \leq n/2\). For \(d > n/2\) the brute-force \(O^*(2^n)\)-time algorithm becomes more efficient.↩︎