TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Edit Distance and LCS under SETH

Edit Distance and LCS under SETH

The edit distance between two strings of length nn is computed by the textbook dynamic program in O(n2)O(n^2) time (Edit Distance, and the practice problems Edit Distance and Longest Common Subsequence). The algorithm goes back to the early 1970s, and the only improvement since is by logarithmic factors: Masek and Paterson (1980) reach O(n2/log⁡2n)O(n^2 / \log^2 n) for constant alphabets with the "Four Russians" technique. In 2015 two papers explained why, conditionally:

Theorem (Backurs and Indyk, 2015; stated). If edit distance on strings of length nn can be computed in time O(n2−ε)O(n^{2 - \varepsilon}) for some ε>0\varepsilon > 0, then SETH is false.

Theorem (Abboud, Backurs and Vassilevska Williams, 2015; stated). The same holds for the length of a longest common subsequence (LCS), and for dynamic time warping. Bringmann and Künnemann (2015) extended these bounds to binary strings and to further similarity measures.

Both go through Orthogonal Vectors (Orthogonal Vectors and the Split-and-List Reduction): a subquadratic algorithm for the string problem gives a subquadratic algorithm for OV with d=O(log⁡n)d = O(\log n), which refutes SETH.

The quadratic algorithms

Both problems fill an (n+1)×(n+1)(n + 1) \times (n + 1) table, each cell from three neighbours. The program fills the tables for random strings of growing length and counts cells: doubling nn quadruples the work, and nothing in the dynamic program suggests a shortcut.

python · try it yourself
import random

def edit_distance(x, y):
    prev = list(range(len(y) + 1))
    for i, a in enumerate(x, 1):
        cur = [i]
        for j, b in enumerate(y, 1):
            cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a != b)))
        prev = cur
    return prev[-1]

def lcs(x, y):
    prev = [0] * (len(y) + 1)
    for a in x:
        cur = [0]
        for j, b in enumerate(y, 1):
            cur.append(prev[j - 1] + 1 if a == b else max(prev[j], cur[j - 1]))
        prev = cur
    return prev[-1]

rng = random.Random(1)
print("   n   edit distance   LCS   table cells")
for n in [100, 200, 400]:
    x = "".join(rng.choice("ACGT") for _ in range(n))
    y = "".join(rng.choice("ACGT") for _ in range(n))
    print(f"{n:4} {edit_distance(x, y):15} {lcs(x, y):5} {(n + 1) ** 2:13}")
   n   edit distance   LCS   table cells
 100              54    65         10201
 200             110   126         40401
 400             211   256        160801

For random strings over four letters the LCS comes out near 0.65n0.65n (the Chvátal-Sankoff constant for four letters, known only numerically) and the edit distance a little over n2\frac{n}{2}. The table sizes are the point: at n=106n = 10^6, typical for comparing genomes or long documents, the table has 101210^{12} cells.

The shape of the reduction

The reductions build, from an OV instance A,B⊆{0,1}dA, B \subseteq \{0, 1\}^d, two strings XX and YY of length O(n⋅poly(d))O(n \cdot \text{poly}(d)) such that the LCS (or edit distance) of XX and YY crosses a threshold exactly when an orthogonal pair exists. They have three layers.

1. Coordinate gadgets. Strings g(0),g(1)g(0), g(1) for the AA side and h(0),h(1)h(0), h(1) for the BB side such that the similarity of g(x)g(x) and h(y)h(y) is high when x⋅y=0x \cdot y = 0 and one lower when x=y=1x = y = 1. For LCS a tiny choice works: g(0)=ag(0) = \texttt{a}, g(1)=bg(1) = \texttt{b}, h(0)=abh(0) = \texttt{ab}, h(1)=ah(1) = \texttt{a}. Then LCS⁡(g(x),h(y))=1\operatorname{LCS}(g(x), h(y)) = 1 except for x=y=1x = y = 1, where it is 0.

2. Vector gadgets. Concatenate the coordinate gadgets with separators that force coordinate ii to align with coordinate ii: with a separator cc\texttt{cc},

VG⁡A(a)=g(a1) cc g(a2) cc⋯cc g(ad),VG⁡B(b)=h(b1) cc h(b2)⋯cc h(bd).\operatorname{VG}_A(a) = g(a_1)\,\texttt{cc}\,g(a_2)\,\texttt{cc} \cdots \texttt{cc}\,g(a_d), \qquad \operatorname{VG}_B(b) = h(b_1)\,\texttt{cc}\,h(b_2) \cdots \texttt{cc}\,h(b_d).

Matching all 2(d−1)2(d-1) separator letters is worth more than any misaligned matching can gain, and the result is

LCS⁡(VG⁡A(a),VG⁡B(b))=2(d−1)+d−a⋅b,\operatorname{LCS}(\operatorname{VG}_A(a), \operatorname{VG}_B(b)) = 2(d - 1) + d - a \cdot b,

so orthogonality is exactly "LCS equals 3d−23d - 2". The program checks this identity on random vectors.

python · try it yourself
import random

def lcs(x, y):
    prev = [0] * (len(y) + 1)
    for a in x:
        cur = [0]
        for j, b in enumerate(y, 1):
            cur.append(prev[j - 1] + 1 if a == b else max(prev[j], cur[j - 1]))
        prev = cur
    return prev[-1]

g = {0: "a", 1: "b"}
h = {0: "ab", 1: "a"}
for x in (0, 1):
    print("coordinate gadget:", " ".join(f"LCS(g({x}), h({y})) = {lcs(g[x], h[y])}" for y in (0, 1)))

rng = random.Random(3)
d, sep = 8, "cc"
ok, by_dot = 0, {}
for trial in range(400):
    a = [rng.randint(0, 1) for _ in range(d)]
    b = [rng.randint(0, 1) for _ in range(d)]
    X = sep.join(g[v] for v in a)
    Y = sep.join(h[v] for v in b)
    dot = sum(u * v for u, v in zip(a, b))
    value = lcs(X, Y)
    ok += value == 2 * (d - 1) + d - dot
    by_dot[dot] = by_dot.get(dot, 0) + 1
print(f"LCS = 2(d-1) + d - a.b held in {ok} of 400 random pairs (d = {d})")
print("pairs by dot product:", dict(sorted(by_dot.items())))
coordinate gadget: LCS(g(0), h(0)) = 1 LCS(g(0), h(1)) = 1
coordinate gadget: LCS(g(1), h(0)) = 1 LCS(g(1), h(1)) = 0
LCS = 2(d-1) + d - a.b held in 400 of 400 random pairs (d = 8)
pairs by dot product: {0: 43, 1: 89, 2: 129, 3: 87, 4: 41, 5: 9, 6: 2}

3. Selection and alignment gadgets (sketch). The final strings place all nn vector gadgets of AA in XX and all of BB in YY, interleaved with padding designed so that an optimal alignment matches a contiguous run of gadgets of XX against gadgets of YY, all pairs but one scoring a fixed "default" value, and one pair scoring its true vector-gadget value. The total similarity is then a fixed baseline plus the best pair's bonus, so it crosses the threshold iff some pair is orthogonal. Getting the padding right is the technical heart of both papers; for edit distance the gadgets must also control insertions and deletions, which is why Backurs and Indyk's construction is more delicate than the LCS one.

Counting. Each vector gadget has length O(d)O(d), so ∣X∣,∣Y∣=O(nd)=O(nlog⁡n)\lvert X \rvert, \lvert Y \rvert = O(n d) = O(n \log n). An O(N2−ε)O(N^{2-\varepsilon}) algorithm for LCS on strings of length NN would solve OV in O((nlog⁡n)2−ε)=O(n2−ε′)O((n \log n)^{2 - \varepsilon}) = O(n^{2 - \varepsilon'}) time, and SETH would fail.

What remains possible

The conditional lower bound rules out polynomial improvements, not everything:

  • Log factors. O(n2/log⁡2n)O(n^2 / \log^2 n) is known; shaving more than polylogarithmic factors, n2/log⁡ω(1)nn^2 / \log^{\omega(1)} n, would imply new circuit lower bounds (Abboud, Hansen, Vassilevska Williams and Williams, 2016; stated), a different kind of evidence that this is hard.
  • Approximation. Edit distance can be approximated within a constant factor in subquadratic time (Chakraborty, Das, Goldenberg, Koucký and Saks, 2018) and in near-linear time (Andoni and Nosatzki, 2020); the SETH bound is for exact computation.
  • Small distance. If the edit distance is at most kk, it can be computed in O(n+k2)O(n + k^2) time (Landau and Vishkin, 1988): FPT in the answer, in the sense of this course's first half.
  • Special inputs. Repetitive or compressible strings, small alphabets with bit-parallelism (Myers, 1999) and highly similar strings are all handled faster in practice.

Test yourself

What does the Backurs-Indyk theorem state?

  1. Edit distance is NP-hard
  2. An O(n2−ε)O(n^{2-\varepsilon}) algorithm for edit distance would refute SETH
  3. Edit distance needs Ω(n2)\Omega(n^2) time unconditionally
  4. Edit distance has no approximation algorithm

In the LCS vector gadget with coordinate gadgets g(0)=ag(0) = \texttt{a}, g(1)=bg(1) = \texttt{b}, h(0)=abh(0) = \texttt{ab}, h(1)=ah(1) = \texttt{a} and separator cc\texttt{cc}, what is LCS⁡(VG⁡A(a),VG⁡B(b))\operatorname{LCS}(\operatorname{VG}_A(a), \operatorname{VG}_B(b))?

  1. dd
  2. 2(d−1)+d−a⋅b2(d-1) + d - a \cdot b
  3. 2d2d
  4. a⋅ba \cdot b

Why must the reduced strings have length O(n⋅poly(d))O(n \cdot \text{poly}(d)) rather than, say, n2n^2?

  1. To fit in memory
  2. Because a quadratic algorithm on strings of length n2n^2 would take n4n^4 time and say nothing about subquadratic OV
  3. Because LCS needs short strings
  4. It does not matter

Which of these does the SETH lower bound for edit distance NOT rule out?

  1. An O(n1.99)O(n^{1.99}) exact algorithm
  2. An O(n2/log⁡2n)O(n^2 / \log^2 n) exact algorithm or a constant-factor approximation in near-linear time
  3. An O(n1.5)O(n^{1.5}) exact algorithm
  4. An O(nlog⁡n)O(n \log n) exact algorithm