When the algorithm is a table

A band as wide as the answer

If two strings are close, the optimal route stays near the diagonal and nine cells in ten cannot be on it. A band of three finds the right answer on a pair 300 characters long — and a band of thirty-two is needed before anything can prove it.

A route through the distance grid that has reached cell (i,j)(i, j) has made at least ij\lvert i - j \rvert insertions or deletions, because those are the only steps that change the difference between the two coordinates and each of them changes it by one.

That single observation is the whole of this essay. If the answer is at most kk, then no optimal route ever reaches a cell with ij>k\lvert i - j\rvert > k, so every cell outside a band of width 2k+12k+1 around the diagonal can be left uncomputed — not estimated, not approximated, left out, with the answer unchanged.

The same table inside a band of 2: 2Each cell holds the distance between a prefix of abracadabra and a prefix of abrocadabro. The shaded run from the top left to the bottom right is one optimal alignment; where the table has ties there are others, and this one breaks them towards the diagonal. 54 of 144 cells, 90 skipped.abrocadabroabracadabra012101221012210122112222123321233212332123321233212322one unit = one subproblem given a value54 of 144 cells, 90 skipped
Fig. 1 Abracadabra against abrocadabro, computed inside a band of two. The outlined cells were never given a value; the shaded ones are the band. Fifty-four of the 144 cells were computed, the answer is two, and two is no larger than the band — so this run is not merely right, it is provably right.
Edit distance between abracadabra and abrocadabro: 2Each cell holds the distance between a prefix of abracadabra and a prefix of abrocadabro. The shaded run from the top left to the bottom right is one optimal alignment; where the table has ties there are others, and this one breaks them towards the diagonal. 144 cells, 144 held at once.abrocadabroabracadabra012345678910111012345678910210123456789321012345678432112234567543221234567654332123456765443212345876554321234987665432123109877654321211109887654322one unit = one subproblem given a value144 cells, 144 held at once
Fig. 2 The same pair with the whole rectangle filled, for comparison. Identical answer, 144 cells instead of 60, and the two substitutions that make up the distance are both within one step of the diagonal — which is what a small answer looks like and is the reason the band exists.

The guarantee is one-sided, and the shape of that matters

Restricting the table to a band cannot make the answer smaller: every route the band allows was already allowed, so the value it computes is an upper bound on the true distance, always.

It can make the answer larger. If the true optimal route leaves the band, the band’s best route is worse than the true one and the value comes out too high.

So the band’s output has exactly one useful property: if the value is no larger than the band, it is exact. A route achieving that value stayed inside the band, and any route beating it would have had to use more than kk indels, which would have cost more than kk. That is Ukkonen’s condition and it is the certificate.

What it does not say is anything at all about a run whose value exceeds its band. Such a run has proved that the distance is greater than kk, which is genuine information and is what makes the doubling below work — but the number it reports is an upper bound of unknown quality.

Right, and unable to say so

Here is what that produces in practice, and it was not anticipated when this field was planned.

Two strings of three hundred characters, thirty edits apart, true distance 25. Sweeping the band from 1 upwards:

band kk value reported cells certified
1 87 899 no
2 30 1,496 no
3 25 2,091 no
4 25 2,684 no
8 25 5,036 no
16 25 9,644 no
32 25 18,476 yes

The band of three has the right answer. It is exactly 25, it is not close to 25, and it stays 25 for every wider band all the way to the full table. And nothing available to that run can distinguish it from a run that happens to be wrong: the value exceeds the band, the theorem is silent, and the only honest report is “at most 25”.

Certification costs a factor of 8.8 in cells over the run that already had the number.

The distance reported, against the width of the bandTwo strings of 300 and 299 characters at a true distance of 25. The narrowest band drawn that reports that number is 3; the narrowest that can certify it — a run whose answer is no larger than its own band — is 32. The full table computes 90,300 cells.110100band width kthe distance reportedBand, cells computed · -0.19one unit = one subproblem given a valuethe distance reported, k from 1 to 48
Fig. 3 The distance reported against the width of the band, on the pair above. It falls to 25 at a band of three and is flat from there — the sequence is non-increasing, which the gate asserts, because a wider band can only add routes. The flatness is not a signal a run can use: an algorithm inside one of these runs cannot see the other points.

The observation generalises past this instance and it is worth naming, because this site has met it before under other headings. A method can hold a correct answer and no certificate for it, and the cost of the certificate is a separate quantity. The streaming field found the same shape from the other side: the guarantee that is per query measured a sketch whose bound permitted seven hundred failures and which produced none, so the bound and the behaviour were both correct and about different things.

How little of the table survives

The fraction that matters is 2k+1m\frac{2k+1}{m}, and on the inputs this method is used for it is very small.

A spell-checker comparing a mistyped word against a dictionary entry runs on strings of eight to twelve characters with a threshold of one or two, so the band covers a third of a small table and the saving is modest. The interesting case is long strings that are nearly identical: two versions of a source file, two genomes, two revisions of a document. There nn is large, dd is small, and the band is a sliver.

Measured on two strings of two hundred characters six edits apart, a band of eight computes 3,290 cells of 39,396 — 8.35%, with 36,106 cells skipped. At a thousand characters and two edits, Ukkonen’s whole run is 8,003 cells against a full table’s 1,003,002, which is 0.8%.

Doubling, and why the wasted rounds are not wasted

The band is useful only if kk is known in advance, and it never is. Ukkonen’s answer is to guess and double.

Start at k=nmk = \lvert n - m\rvert — no smaller band can reach the far corner at all, because getting there requires that many indels — and run. If the value is certified, stop. Otherwise double kk and run again.

The rounds that fail are the interesting part of the accounting. A round with band kk that comes back uncertified has proved the distance exceeds kk, so the next round is not a guess in the dark. And because the bands double, the final band is under twice the answer, and the total cells summed over every round are under twice the last round’s — a geometric series with the largest term last.

The measured result is that the total is proportional to ndn \cdot d, with dd the answer, across a range of dd:

edits applied true distance Ukkonen’s total cells cells per ndn \cdot d
2 2 8,003 4.00
8 7 25,866 3.70
32 27 130,767 4.84

The right-hand column varies by a factor of 1.31 across a thirteen-fold change in the answer, which is what “proportional to ndnd” looks like when it is measured rather than asserted. The full table on the same strings computes 1,003,002 cells at every one of those points, because it cannot see the answer.

Subproblems given a value, against the length of the stringsFull table at a measured slope of 2.00; Ukkonen doubling at a measured slope of 1.35; Band of 16 at a measured slope of 1.03. The strings are 6 edits apart, over an alphabet of 4. On these axes a slope of 2 is a rectangle filled and a slope of 1 is a line.10³10⁴10⁵10⁶length of each stringsubproblems given a valueFull table · 2.00Ukkonen doubling · 1.35Band of 16 · 1.03one unit = one subproblem given a valuesubproblems given a value, n from 128 to 1024
Fig. 4 Cells computed against the length of the strings, holding the number of edits fixed at six. The full table is at slope 2.00 and the two banded series are at slope 1.00 — because with dd fixed the band is fixed and the work is n(2k+1)n(2k+1). At a thousand characters that is a factor of 33 between the top line and Ukkonen’s.

What it costs when the strings are far apart

The method is output-sensitive, and the honest half of that is what happens when the output is large.

If dd is comparable to nn — two unrelated strings — then the final band is comparable to nn, the band covers the whole table, and the doubling has run every earlier round for nothing. At three hundred characters and a distance of 25, Ukkonen’s total is 38,235 cells against a full table’s 90,300: a saving of 58%, which is real and is a constant factor rather than the factor of a hundred available on close strings.

At a distance of n/2n/2 there is no saving at all and a small loss. That is the correct behaviour for a method whose whole premise is that the answer is small, and it is why the doubling starts at nm\lvert n-m\rvert rather than at 1 — on strings of very different lengths the first few rounds would be pure waste.

The method that does not have this failure mode is in the next essay: it computes every cell of the full table and does sixty-four of them at a time, so its cost is nm/wnm/w regardless of the answer. Banding and bit-parallelism are the two ways to attack the same product and they attack different factors of it, which is why real implementations use both — bit-parallel within a band.

The same table inside a band of 1: 3, not certifiedEach cell holds the distance between a prefix of kitten and a prefix of sitting. The shaded run from the top left to the bottom right is one optimal alignment; where the table has ties there are others, and this one breaks them towards the diagonal. 20 of 56 cells, 36 skipped.sittingkitten01112212212212223323one unit = one subproblem given a value20 of 56 cells, 36 skipped
Fig. 5 Kitten against sitting inside a band of one: twenty cells computed of fifty-six, and a value of three that the band cannot certify. Three exceeds one, so this run reports “at most three” — and three is the answer. The certificate needs a band of four.
The same table inside a band of 1: 1Each cell holds the distance between a prefix of structure and a prefix of stricture. The shaded run from the top left to the bottom right is one optimal alignment; where the table has ties there are others, and this one breaks them towards the diagonal. 28 of 100 cells, 72 skipped.stricturestructure0110110110111221221221221221one unit = one subproblem given a value28 of 100 cells, 72 skipped
Fig. 6 And the case where the band works as advertised: structure against stricture, distance one, inside a band of one. Twenty-eight cells of a hundred, and the value is no larger than the band, so it is exact and known to be. This is the shape every spell-checker and every version-control diff is built on — the strings being compared are nearly identical, which is why they are being compared at all.

The order the band forces

A band is a statement about which cells to compute, and it interacts with the fill order in a way worth making explicit.

Row order works: for each row, run jj from max(0,ik)\max(0, i-k) to min(m,i+k)\min(m, i+k). Reads that fall outside the band are not missing cells — they are infinite, because no route goes there — and the implementation has to say so rather than skipping them, or the minimum silently takes whatever the array held.

That is the second time in this field that the difference between absent and zero decides correctness, and it is why the cells outside the band in every figure here are drawn as outlines rather than filled with anything. A picture that shaded them the colour of a low value would be showing the mistake.

The same table filled in diagonal order: 144 cellsThe same 144 cells as the plate above, shaded by when they were filled rather than by what they hold. The recurrence says only what a cell depends on; it does not say when to compute it, and diagonal order is one of at least three that work. Every one of them produces a table agreeing cell for cell, which is asserted in the gate rather than assumed.abrocadabroabracadabra0136101521283645556624711162229374656677858121723303847576879899131824313948586980909914192532404959708191100108202633415060718292101109116273442516172839310211011712335435262738494103111118124129445363748595104112119125130134546475869610511312012613113513865768797106114121127132136139141778898107115122128133137140142143one unit = one subproblem given a value144 cells, filled in diagonal order
Fig. 7 Anti-diagonal order over the same pair, shaded by step. A band is a set of consecutive anti-diagonals in this ordering — the band is horizontal here — which is why the diagonal fill is the natural one for a banded implementation and why the bit-parallel methods, which advance one diagonal at a time, compose with banding so cleanly.

The same bound, reached from the other side

There is a second algorithm with the same Θ(nd)\Theta(nd) behaviour that arrives at it from a completely different direction, and it is worth naming because it is the one most people have actually run.

Instead of computing cells and restricting which ones, it tracks for each diagonal the furthest cell along that diagonal reachable with ee edits, and increases ee until the far corner is reached. The state is one number per diagonal rather than a table, the loop is over edit counts rather than over rows, and the cost is O(nd)O(nd) for the same reason: at ee edits only 2e+12e+1 diagonals are live.

That is the algorithm behind the diff in most version-control systems, and its parameterisation explains a behaviour anybody who has used one has seen — a diff between two nearly identical files returns instantly, and a diff between two unrelated files of the same size takes noticeably longer. The cost is in the answer, not in the input.

It is named here and not implemented, for the reason this site names rather than implements elsewhere: it would be a second route to a bound already measured, and the measurement above is of a method whose cells this field’s counter can count. A furthest-reaching-diagonal search does not fill a table, so the ninth counter would have nothing to report about it.

The same table inside a band of 2: 3, not certifiedEach cell holds the distance between a prefix of algorithm and a prefix of logarithm. The shaded run from the top left to the bottom right is one optimal alignment; where the table has ties there are others, and this one breaks them towards the diagonal. 44 of 100 cells, 56 skipped.logarithmalgorithm01211232123422234233453434544345543455434543one unit = one subproblem given a value44 of 100 cells, 56 skipped
Fig. 8 Algorithm against logarithm inside a band of two: forty-four cells of a hundred, and a value of three. Three is larger than two, so this run reports “at most three” and cannot certify it — on a pair where the full table agrees the answer is exactly three. The same gap as before, on a ten-character instance where the whole of it can be seen at once, and it is a reminder that the gap is not an artefact of scale.

Why the lower bound has to be exact

The band works because ij\lvert i - j\rvert is a lower bound on the cost of any route through the cell, computable from the cell’s coordinates and nothing else, and because that bound is never optimistic in a way that could exclude the true optimum.

Both halves matter and the second is the one that fails in practice. A pruning rule built on a bound that is sometimes too large discards routes that were cheaper than it claimed, and the result is not an approximation with a stated error — it is an answer that is wrong in a way nothing reports. A rule built on a bound that is merely weak, never optimistic, prunes less and stays exact.

That is the difference between this band and the heuristics that look like it. A search that cuts on “this partial alignment is already worse than the best complete one found so far” is also exact, for the same reason. A search that cuts on “this partial alignment looks unpromising” is not, and the two are one line apart in an implementation.

The general form worth carrying: a pruning rule is exact exactly when the quantity it cuts on is a lower bound that provably cannot be beaten from where it is applied. Everything else is a heuristic, and a heuristic in place of a bound turns a computation into an estimate without changing the shape of the code.

Two rounds always suffice

The doubling schedule treats a failed round as having produced nothing but a refutation. It produces more than that, and using it turns an unbounded number of rounds into exactly two.

A round with band kk returns a value vv that is an upper bound on the true distance, always — that is the one-sided guarantee the essay opens with. So run the next round at band vv rather than at 2k2k. The true optimal route uses at most dvd \le v indels, so it lies inside a band of vv; the round therefore returns dd exactly; and dvd \le v means the value does not exceed the band, so the certificate is automatic. Two rounds, whatever the strings.

On the pair the table measures, starting from a band of three: round one costs 2,091 cells and returns 25, round two runs at a band of 25 and costs about 15,300, and the total is 17,391 with a certificate. The doubling schedule reaches its certificate at 38,235, and the single band-32 run that certifies costs 18,476. The two-round schedule is cheaper than either.

The catch is in the first round’s quality and it is worth stating plainly, because it is why doubling is the schedule everybody actually uses. The second round’s cost is set by vv, not by dd, and a bad first round makes vv enormous — the band of one on this pair returns 87, and a second round at 87 would compute 52,500 cells to certify an answer of 25. The schedule is only as good as the upper bound it starts from, and doubling is the schedule that refuses to trust any upper bound at all.

Which points at the sensible hybrid rather than at a winner. Start at nm\lvert n - m \rvert as before; each round, take the next band as the smaller of twice the current band and the value just returned. That never runs a band larger than doubling would and takes the shortcut whenever the reported value is informative, which on close strings is immediately. Nothing about it needs a new theorem — both halves are the guarantee already stated, read in the two directions it can be read in.

The band assumes what an indel costs

Every route argument here counts indels: a step off the diagonal is one insertion or deletion, so a cell at distance ij\lvert i-j\rvert from the diagonal has cost at least ij\lvert i-j\rvert. That inequality is where the band comes from, and it is a statement about the cost model rather than about the grid.

Change the model and the arithmetic changes with it. With an indel costing gg and a substitution ss, a cell ij\lvert i-j\rvert off the diagonal has cost at least gijg\,\lvert i-j\rvert, so a threshold of kk admits a band of width k/gk/g — narrower for an expensive gap, wider for a cheap one. That is still a clean bound and the method survives intact, with kk read in cost units rather than in edits.

Affine gaps break it, and the way they break it is worth seeing. Under a gap charged to open and then per character, a run of LL indels costs o+Leo + L e rather than LgLg, so the cost of straying LL from the diagonal grows with slope ee — and ee is deliberately small, because the whole point of an affine model is that a long gap should not be charged as many separate events.

So the band that a threshold kk admits is (ko)/e(k - o)/e wide, and with a typical opening of eight and an extension of one, a threshold of twenty admits a band of twelve where a unit-cost model would have admitted twenty. That direction is fine. The trouble is that a realistic affine model has ee well under one relative to a substitution, and then the admissible band is several times wider than the answer — which is exactly the regime where a sliver becomes a stripe and the saving evaporates.

The band is therefore not a general-purpose optimisation but a consequence of one particular cost model’s geometry, and a table built under a model that makes long gaps cheap has given up most of it. That is not a small caveat given who uses these tables: sequence aligners use affine gaps precisely because biological insertions are single events, and they recover their savings from filtering and from bit-parallelism rather than from banding.

What is being traded

The band trades a guarantee for work, and the trade has three positions rather than two.

Full table. nmnm cells, exact, no assumption.

Band with a known bound. n(2k+1)n(2k+1) cells, exact whenever the bound holds, and a report of “greater than kk” when it does not. This is the position a spell-checker is in: it does not want the distance between a typo and every dictionary word, it wants the words within two, and a run that reports “more than two” has answered the question that was asked.

Band with a guessed bound, doubled. Θ(nd)\Theta(nd) cells, exact always, at a constant factor over the previous line. This is the position a program is in when it must produce the number.

The middle line is the one usually left out of the description, and it is the one that matters most in practice — because most uses of edit distance are threshold queries wearing the clothes of a distance computation, and a threshold query does not need the distance at all.

One last thing about the band is worth keeping, because it is the transferable part. The band is a pruning rule derived from a bound, and it works because a lower bound on the cost of any route through a cell is available in closed form — ij\lvert i - j\rvert, computable from the cell’s coordinates alone, needing no lookahead and no search. That is what makes it exact rather than heuristic, and it is the same structure branch-and-bound uses everywhere: a cheap bound that provably cannot be beaten, applied to a region rather than to a point. Where such a bound exists the pruning is free; where it does not, no amount of cleverness about the order of evaluation recovers it.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

The 8 essays that link to this one and share the most of its objects, of 14 that link here.

The objects this essay names

Each one links to every other essay that touches it.

AlignmentApproximate matchingBandCost modelDynamic programmingEdit distanceEvaluation orderGuaranteeOutput-sensitivePruningSubproblemUkkonenUpper bound