Edit Distance and LCS under SETH
The edit distance between two strings of length is computed by the textbook dynamic program in 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 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 can be computed in time for some , 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 , which refutes SETH.
The quadratic algorithms
Both problems fill an table, each cell from three neighbours. The program fills the tables for random strings of growing length and counts cells: doubling quadruples the work, and nothing in the dynamic program suggests a shortcut.
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 (the Chvátal-Sankoff constant for four letters, known only numerically) and the edit distance a little over . The table sizes are the point: at , typical for comparing genomes or long documents, the table has cells.
The shape of the reduction
The reductions build, from an OV instance , two strings and of length such that the LCS (or edit distance) of and crosses a threshold exactly when an orthogonal pair exists. They have three layers.
1. Coordinate gadgets. Strings for the side and for the side such that the similarity of and is high when and one lower when . For LCS a tiny choice works: , , , . Then except for , where it is 0.
2. Vector gadgets. Concatenate the coordinate gadgets with separators that force coordinate to align with coordinate : with a separator ,
Matching all separator letters is worth more than any misaligned matching can gain, and the result is
so orthogonality is exactly "LCS equals ". The program checks this identity on random vectors.
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 vector gadgets of in and all of in , interleaved with padding designed so that an optimal alignment matches a contiguous run of gadgets of against gadgets of , 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 , so . An algorithm for LCS on strings of length would solve OV in time, and SETH would fail.
What remains possible
The conditional lower bound rules out polynomial improvements, not everything:
- Log factors. is known; shaving more than polylogarithmic factors, , 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 , it can be computed in 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?
- Edit distance is NP-hard
- An algorithm for edit distance would refute SETH
- Edit distance needs time unconditionally
- Edit distance has no approximation algorithm
In the LCS vector gadget with coordinate gadgets , , , and separator , what is ?
Why must the reduced strings have length rather than, say, ?
- To fit in memory
- Because a quadratic algorithm on strings of length would take time and say nothing about subquadratic OV
- Because LCS needs short strings
- It does not matter
Which of these does the SETH lower bound for edit distance NOT rule out?
- An exact algorithm
- An exact algorithm or a constant-factor approximation in near-linear time
- An exact algorithm
- An exact algorithm