Probability

The longest thread between two random strings

Write down two random strings of a thousand bits each and find the longest sequence that can be read in both, skipping freely. It is about 81% of each, and the exact share — the Chvátal–Sankoff constant — has resisted computation for fifty years. Its fluctuations are a smaller mystery with a sharper edge: the variance is known to be at most n/2, and over every length that can be computed it grows like n to the power 0.7, with nobody able to say what it does after that.
16 min read 6 figures Order out of noiseSmall cases lie

Worth reading first: Edges that crowd each other out · The longest climb of a shuffle.

Charged for the variance, not the range ended with a gap that no general inequality closes. For some quantities built from many random inputs, the true fluctuations are far smaller than any concentration bound gives, and the true scale was found only by problem-specific calculation. The longest increasing subsequence of a shuffled deck is the famous case: the longest climb of a shuffle fluctuates on the scale n1/6n^{1/6}, where the general bounds reach only n1/4n^{1/4}. A close relative has resisted every such calculation. It is the longest common subsequence of two random strings.

Take two strings of random bits, each of length nn. A common subsequence is a sequence of bits that can be read in both strings, left to right, skipping freely in each. The longest such sequence measures how similar the two strings are once insertions and deletions are allowed — the measure behind sequence alignment in genetics and the comparison that file-difference tools make. For random strings it is a sum of no independent parts at all, and two of its most basic numbers are unknown: the constant its length grows at, and the scale on which it fluctuates.

Two random strings of bits and the longest thread between them. Two random binary strings of length 36; longest common subsequence 27.
Fig. 1 Two strings of 36 random bits and one longest common subsequence: 27 bits, threaded so that no two threads cross. That is 0.75 of each string; for long strings the share settles near 0.8122, a constant whose exact value is unknown.

A thread that never crosses

The hero figure draws one longest common subsequence as threads joining equal bits, top to bottom. The threads never cross, since the subsequence must keep its order in both strings, and they skip freely — some bits in each string are left unthreaded. The longest set of non-crossing threads between equal bits has 27 threads here.

Finding it is a standard computation. Let L(i,j)L(i, j) be the length for the first ii bits of one string and the first jj of the other. Then L(i,j)L(i, j) is one more than L(i−1,j−1)L(i - 1, j - 1) if the ii-th and jj-th bits agree, and otherwise the larger of L(i−1,j)L(i - 1, j) and L(i,j−1)L(i, j - 1). Filling the table takes n2n^2 steps. For the long strings of the later figures the table’s rows are packed into machine words, 32 bits at a time, with a recurrence due to Lloyd Allison and Trevor Dix that updates a whole row with one addition and a few logical operations. The packed computation is checked against the plain table on hundreds of random cases before it is trusted.

Why anyone needed the random baseline

The question was asked for a practical reason. When two DNA or protein sequences are compared, the length of their best alignment — the longest common subsequence, or a weighted version of it that charges for gaps — is a score, and a score means something only against what unrelated sequences would score. Two random strings of length a thousand already share a thread about 810 long. An observed common subsequence of 830 between two genes is therefore weak evidence of relationship, and one of 900 is strong. The difference is entirely in knowing where the random baseline sits and how much it varies.

That is why the two numbers this essay measures are the ones that matter. The constant γ2\gamma_2 fixes the centre of the baseline, and the variance fixes its width, which turns an observed excess into a significance. Michael Waterman’s work on alignment statistics in the 1980s and 1990s took both from simulation, because neither was known in closed form, and they still are not. The same comparison underlies the file-difference tools that report the lines two versions of a program share. For random files the shared fraction would sit near four-fifths for a two-symbol alphabet and fall like 2/k2/\sqrt k for kk symbols, as the alphabet figure below shows.

About four-fifths of each string

Two random bits agree half the time, so a thread can be found for at least half the positions by pairing equal bits greedily. The skipping does the rest, and the share of each string in the longest thread climbs well past one half.

The longest common subsequence of two random bit strings: about 81% of each. Exact means/n: 1: 0.5000, 2: 0.5625, 3: 0.6042, 4: 0.6309, 5: 0.6492, 6: 0.6633, 7: 0.6745, 8: 0.6836, 9: 0.6913; sampled: 25: 0.7447, 50: 0.7660, 100: 0.7813, 200: 0.7917, 400: 0.7992, 800: 0.8036, 1600: 0.8067, 3200: 0.8088.
Fig. 2 The average length of the longest common subsequence divided by nn: exact over all pairs for nn up to 9 (cool) and sampled for nn from 25 to 3,200 (warm). The shaded band is where the limit is proved to lie, 0.788 to 0.826; the dashed line is the best numerical estimate, 0.8122.

For a single bit the share is exactly one half: two bits agree half the time. For strings of length two it is 0.56250.5625, for length nine 0.69130.6913, computed exactly over all 49=262,1444^9 = 262{,}144 pairs. Sampled at longer lengths it keeps rising — 0.78130.7813 at a hundred, 0.79920.7992 at four hundred, 0.80880.8088 at 3,200 — and the approach to the limit is slow, with the gap shrinking only like a power of nn. Kenneth Alexander proved in 1994 that the expected length falls short of its limit by at most a constant times nlog⁡n\sqrt{n \log n}, which allows exactly this kind of crawl.

Václav Chvátal and David Sankoff proved in 1975 that the limiting share exists, and called it γ2\gamma_2. Its value is still unknown. The best numerical estimates put it near 0.81220.8122. The best proved bounds, by George Lueker in 2009, place it between 0.7880.788 and 0.8260.826, and narrowing them has needed computations of enormous size, because every rigorous bound comes from analysing strings of a fixed finite length exactly. The share of a random string that another random string shares with it is a constant of nature that nobody can compute to three decimal places with proof.

Why a limit must exist

That a limit exists at all is a short argument, and the exact small cases show its ingredient.

Exact expected lengths for short strings, and why a limit must exist. 1: 0.50000; 2: 1.12500; 3: 1.81250; 4: 2.52344; 5: 3.24609; 6: 3.97998; 7: 4.72144; 8: 5.46912; 9: 6.22173.
Fig. 3 The expected length of the longest common subsequence for nn from 1 to 9, computed exactly over all 4n4^n pairs (bars), beside 0.8122 n0.8122\,n (dashed). The values are superadditive: the length for m+nm + n bits is at least the sum for mm and for nn.

Split each string into a first part of length mm and a second of length nn. A common subsequence of the two first parts followed by one of the two second parts is a common subsequence of the whole strings, so the longest for the whole is at least the sum of the longest for the parts. Taking expectations, E(m+n)≥E(m)+E(n)E(m + n) \ge E(m) + E(n). The exact values for n≤9n \le 9 satisfy this in every case, and Fekete’s lemma — the tool a walk that may not step where it has been used for counting self-avoiding walks — turns it into a limit: E(n)/nE(n)/n converges to its least upper bound.

The argument proves the limit exists and gives lower bounds, since every exact E(n)/nE(n)/n is below it. It gives no upper bounds at all, and none of the exact small cases says anything about what the limit is. The value 0.6913 at n=9n = 9 is a proved lower bound, and a weak one. Lueker’s bounds needed a different idea, a dynamic programme over pairs of strings of length a few dozen, run as a computation on probability distributions rather than on strings.

How much it varies

The length of the longest thread is a function of 2n2n random bits, and changing any one bit changes it by at most one. That is the hypothesis of the bounded-differences inequality of no single input can move it far, and its variance form, the Efron–Stein inequality, gives a bound directly. Each bit, resampled, differs from its old value half the time and then moves the length by at most one. Michael Steele used this in 1986 to show that the variance is at most n/2n/2 for two-letter strings. The fluctuations are at most of order n\sqrt n.

The variance of the common subsequence, and an exponent nobody can pin down. 25: var 2.06 (0.0825 n); 50: var 3.32 (0.0664 n); 100: var 5.32 (0.0532 n); 200: var 8.88 (0.0444 n); 400: var 13.37 (0.0334 n); 800: var 21.85 (0.0273 n); 1600: var 39.31 (0.0246 n); 3200: var 57.86 (0.0181 n); slope 0.697.
Fig. 4 The variance of the longest common subsequence against nn on logarithmic scales, with the Efron–Stein bound n/2n/2 (red) and guide lines of slope 1 and 2/32/3. The fitted slope from 200 to 3,200 is 0.70, and the ratio of variance to nn falls steadily, from 0.083 to 0.018.

The measured variance is far below the bound and grows more slowly. Between n=200n = 200 and n=3,200n = 3{,}200 it rises from 8.98.9 to 57.957.9, a fitted exponent of 0.700.70. The slope measured between successive doublings stays near 0.70.7 at every step, within its sampling error of about 0.10.1. The ratio of variance to nn, which would level off if the variance were linear, falls by a factor of four over the range.

That is a result to state carefully, because the question it bears on is open. Chvátal and Sankoff guessed in 1975 that the variance was of smaller order than nn, comparable to n2/3n^{2/3}. Michael Waterman later conjectured, from simulations, that it is linear. Jüri Lember and Heinrich Matzinger proved in 2009 that it is linear when one of the two letters is much rarer than the other. For fair bits the order of the variance is not known. Over the lengths computed here it behaves like n0.7n^{0.7}, close to Chvátal and Sankoff’s guess. But a variance that is eventually linear with a slowly emerging constant would look exactly like this at these sizes, and nothing in the figure can tell the two apart.

The gap between the bound and the measurement has a familiar cause. Efron–Stein charges each bit for the most it could matter, but in a long common subsequence most bits barely matter: change one, and the thread usually reroutes around it at no cost. The same kind of slack appeared for the edges of a random spanning tree, whose count in half a grid varied sixteen times less than independent edges would, because the structure absorbed most of each edge’s influence. Here the structure absorbs even more as nn grows, which is what a variance growing more slowly than nn means, and why no general inequality can be expected to find the true order.

The shape of the fluctuations

Whatever their scale, the fluctuations have a shape, and it can be measured at one length.

The fluctuations of the common subsequence look like a bell. n = 800, 1000 pairs: mean 642.87, sd 4.675, skewness -0.042.
Fig. 5 The lengths of the longest common subsequence for 1,000 pairs of random strings of length 800, standardised to mean nought and spread one, one bar per whole-number length, against the bell curve. The skewness is −0.04-0.04.

At length 800 the longest common subsequence averages 642.9642.9 with a standard deviation of 4.74.7 — under one per cent of its length. The standardised values follow the bell closely, with a skewness of −0.04-0.04. That is a clear difference from the longest increasing subsequence of a shuffle. There the fluctuations follow the Tracy–Widom law, visibly lopsided, with a skewness of about 0.220.22, and they come from the same structure as the largest eigenvalue of a random matrix.

A bell suggests that the common subsequence behaves like a sum of many weakly dependent pieces, and a bell with linear variance would be the ordinary central limit picture. Central limit theorems have been proved for the biased strings where the variance is known to be linear. For fair bits the shape is, like the scale, a measurement.

Many letters turn it into a climb

The connection to increasing subsequences is not only an analogy. It becomes exact as the alphabet grows.

Over many letters, the common subsequence becomes an increasing subsequence. k=2: γ 0.8083, γ√k 1.143; k=3: γ 0.7130, γ√k 1.235; k=4: γ 0.6494, γ√k 1.299; k=6: γ 0.5659, γ√k 1.386; k=8: γ 0.5104, γ√k 1.444; k=12: γ 0.4387, γ√k 1.520; k=16: γ 0.3921, γ√k 1.569; k=24: γ 0.3326, γ√k 1.629; k=32: γ 0.2946, γ√k 1.667; k=48: γ 0.2463, γ√k 1.707; k=64: γ 0.2169, γ√k 1.735.
Fig. 6 The share γ\gamma of each string in the longest common subsequence over kk letters, multiplied by k\sqrt k, for kk from 2 to 64 at length 2,400. The product climbs from 1.14 towards 2: over many letters the common subsequence becomes an increasing subsequence.

With kk letters, two random strings agree at a given pair of positions with chance 1/k1/k, so there are about n2/kn^2/k matching pairs, scattered over the n×nn \times n grid of position pairs like random points. A common subsequence is a chain of matching pairs increasing in both coordinates. When matches are rare and scattered, the longest such chain is the longest increasing chain among about n2/kn^2/k random points in a square. The longest climb of a shuffle showed that this has length about 2m2\sqrt{m} for mm points — Hammersley’s problem.

With m=n2/km = n^2/k that gives a length of 2n/k2n/\sqrt k, so γkk→2\gamma_k \sqrt k \to 2, as Marcos Kiwi, Martin Loebl and Jiří Matoušek proved in 2005. The figure shows the product rising from 1.141.14 for bits, through 1.441.44 for eight letters, to 1.741.74 for sixty-four. It approaches 2 slowly, and more slowly still at finite length, where boundary effects pull it down. For bits the problem is something else; for a large alphabet it is Hammersley’s, and the same theorem suggests that for large alphabets the fluctuations should approach the Tracy–Widom scale. Where between two letters and infinitely many the behaviour changes is not known.

A path through a grid of matches

There is a picture in which the two answers on offer are two universality classes. Draw an n×nn \times n grid whose rows are the positions of one string and whose columns are the positions of the other, and mark a cell when the two bits there agree — about half the cells for bits. A common subsequence is a path through marked cells moving strictly down and to the right, and the longest one is the path collecting the most marks. That makes the longest common subsequence a last-passage percolation time: the largest total weight along a monotone path through a random environment.

Last-passage percolation with independent weights in every cell is one of the solved members of the Kardar–Parisi–Zhang class. Its fluctuations are of order n1/3n^{1/3}, its variance n2/3n^{2/3}, and its limiting law is Tracy–Widom, by the same exact formulas that settle increasing subsequences. Growth models like the shape a random ball grows into are believed to share these exponents. If the common subsequence belonged to that class, its variance would grow like n2/3n^{2/3} and its fluctuations would be skewed.

But the weights in the common-subsequence grid are not independent. Whether cell (i,j)(i, j) is marked depends on bit ii of one string and bit jj of the other, so every row’s marks are tied together by one bit and every column’s by another. Only 2n2n random bits make n2n^2 marks. That dependence is exactly what could change the class, and it is why neither the KPZ exponents nor the linear ones can be taken over. The measured exponent of 0.700.70 sits near the KPZ value, and the measured shape sits near the bell, which belongs to the other answer. The two halves of the evidence point in different directions, which is the clearest sign that these lengths are not yet in the asymptotic regime.

What the computations can and cannot say

Every figure here is either exact — the small-nn means, the superadditivity — or a sample with an error that can be estimated. The sample means agree with the known bounds, and the variance stays below Steele’s bound as it must. What the samples cannot do is decide the asymptotic order of the variance, which is the question of most interest. A measured exponent of 0.700.70 over a range of sixteen in nn is evidence, and the competing hypotheses make predictions that differ only by a factor that grows like n1/3n^{1/3}, a factor of about 2.52.5 over the whole range. A slowly varying constant could absorb that.

Nor do the figures say anything about the structure of the longest common subsequences themselves: how many there are, how much two of them overlap, how far the threads stray from the diagonal. Those are the quantities that a proof of the fluctuation order would probably have to control, and they are harder to measure than the length.

Still open: the constant and the variance

γ2\gamma_2 is unknown, and so is every γk\gamma_k for finite kk; no closed form has been proposed for any of them that matches the numerics. The rigorous bounds narrow only with great computational effort, because each improvement needs exact analysis of longer finite strings.

The order of the variance for fair bits is open, and it matters beyond this problem. If it is linear, the common subsequence belongs with sums of nearly independent pieces, and the general concentration inequalities are sharp for it. If it is smaller, like n2/3n^{2/3}, the common subsequence belongs with the increasing subsequence and the last-passage percolation models of the Kardar–Parisi–Zhang universality class, whose fluctuations come from random-matrix theory. The bit strings sit between those two worlds, and which one they belong to is exactly the kind of question that no single input can move it far and its successors can bound but not answer.

A comparison of two strings, measured

The longest common subsequence is about the most natural similarity measure for two strings, and for random strings it has a definite average share, 0.81220.8122 to the best of anyone’s computation. That share exists by a two-line argument, has bounds that cost decades of computing to narrow, and approaches its limit too slowly to read off. The fluctuations around it are bounded by a general inequality and measured to grow like n0.7n^{0.7} across every length that can be sampled. That exponent falls between the two answers on offer, and the measurements cannot choose between them. Over a large alphabet the whole problem turns into the longest increasing subsequence, where the answers are known. For two letters, the simplest case, they are not.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

Concentration of measureConvergence rateExhaustive searchExpectationOpen problemVariance