Discrete

The longest climb of a shuffle

Erdős and Szekeres guarantee that any n distinct numbers hold a climb or a fall of √n, and there are orders that allow nothing more. Shuffle a deck at random instead and the longest climb is almost exactly 2√n — with fluctuations of size n to the power one-sixth, distributed exactly as the largest eigenvalue of a large random matrix.

Worth reading first: The sequence that cannot avoid a staircase · Every crowd holds a bowl or a dome.

Any nn distinct numbers, in any order, contain a run of about n\sqrt n of them that climbs or a run of that length that falls — a pigeonhole argument on two counters per number. That is Erdős and Szekeres’s theorem, and it is exact: there are orders in which no run, up or down, is any longer. It is a statement about the worst order. In 1961 Stanisław Ulam asked about a typical one. Shuffle a deck of nn cards thoroughly and deal them out; how long is the longest run of cards that increase?

The answer turned out to be one of the deepest results in probability of the last half-century. The longest increasing run of a random shuffle is almost exactly 2n2\sqrt n — twice the guarantee, and in both directions at once. How much it varies from shuffle to shuffle was settled only in 1999, and the answer connects card shuffles to the eigenvalues of random matrices, the growth of crystals and the edges of fluctuating interfaces.

The figure below is the problem in its most visual form. Four hundred points dropped at random in a square, and the longest chain of them that rises to the right in both coordinates: thirty-four points, where 2400=402\sqrt{400} = 40.

400 random points and their longest rising chain, 34 long. 400 uniform random points in a unit square with the longest chain increasing in both coordinates marked: 34 points, against 2√n = 40.0.
Fig. 1 Four hundred random points in a square and the longest chain of them rising to the right.

Points in a square

The square and the shuffle are the same object. Drop nn points at random, sort them left to right, and record the order of their heights: the result is a uniformly random shuffle of nn cards, because every order of heights is equally likely. A chain rising to the right in both coordinates is then an increasing subsequence of the shuffle, and the longest such chain is the longest increasing subsequence.

The square version, due to John Hammersley in 1972, makes one fact nearly obvious. Double the side of the square and keep the density of points the same: there are four times as many points, and a rising chain across the big square can be built from rising chains across two small squares placed corner to corner along the diagonal. So the length is at least additive along the diagonal, and a length proportional to the side — that is, to n\sqrt n — is the natural scale. Hammersley proved that Ln/nL_n / \sqrt n settles to a constant, and conjectured it was 22.

Why 22 and not something else took five years and two independent proofs. In 1977 Benjamin Logan and Lawrence Shepp, and independently Anatoly Vershik and Sergei Kerov, proved it by identifying not just the longest run but the whole shape of a structure the shuffle determines — the same shape this essay ends with.

Patience sorting counts it

Finding the longest increasing run of a given shuffle does not require checking every subsequence. A card game does it in one pass.

Patience sorting a deal of 16 cards into 5 piles. The deal 10, 11, 14, 12, 13, 7, 8, 16, 9, 1, 6, 3, 2, 4, 5, 15 sorted into 5 piles by patience sorting: 10 7 1 | 11 8 6 3 2 | 14 12 9 4 | 13 5 | 16 15.
Fig. 2 A shuffled deck of sixteen cards dealt onto piles, each card placed on the leftmost pile whose top card is larger than it, or on a new pile if there is none. Five piles form, and five is exactly the length of the longest increasing run in the deal.

Deal the cards one at a time. Put each card on the leftmost pile whose top card is larger, and if there is none, start a new pile on the right. The number of piles at the end is the length of the longest increasing subsequence.

Two facts make that true. No increasing run can put two of its cards on the same pile, because each pile decreases from bottom to top, so a run has at most as many cards as there are piles. And when a card is placed on pile jj, the top card of pile j−1j - 1 is smaller than it and was dealt earlier — otherwise the card would have gone there — so following those links back from any card on the last pile traces an increasing run through every pile. The game is a proof as well as an algorithm, and the chain-covering argument of Dilworth and Mirsky is the same proof in the language of ordered sets.

It is also fast. Finding the pile for each card is a binary search, so a million cards take about twenty million comparisons. Every length in the figures below was computed this way, and on the smaller cases checked against a search of every pair.

Two, approached very slowly

With patience sorting, the average longest run can be measured for shuffles of any reasonable size.

The longest rising run of a shuffle, divided by √n, creeping up to 2. Mean longest increasing subsequence over √n for random permutations of size 25, 50, 100, 200, 400, 800, 1600, 3200, 6400: 1.489, 1.598, 1.681, 1.723, 1.795, 1.826, 1.864, 1.887, 1.907, against the curve 2 − 1.7711 n^(−1/3).
Fig. 3 The average longest increasing run of a random shuffle of n cards, divided by n\sqrt n, for nn from 25 to 6,400, two hundred shuffles at each size. The ratio climbs toward 2, from 1.5 at twenty-five cards to about 1.9 at six thousand, and it closes the gap slowly: the dashed curve is 2 − 1.77 n^(−1/3).

The ratio rises steadily and never reaches 22. At twenty-five cards it is about 1.51.5, at four hundred about 1.81.8, at six thousand four hundred about 1.91.9. The approach is slow because the correction shrinks only like n−1/3n^{-1/3} — to halve the distance to 22 the deck has to grow eightfold. The dashed curve is the leading correction that the 1999 theorem below implies, and the measured points sit a little above it, as a further, smaller correction says they should.

This is a place where small cases genuinely mislead. A reader who shuffled a hundred decks of a hundred cards would measure a ratio near 1.71.7 and could reasonably guess the constant was 3\sqrt 3 or π/3\pi/\sqrt 3 or e−1e - 1. The limit is 22, and nothing short of a proof — or decks of millions of cards — would reveal it.

The worst order and a typical one

Erdős and Szekeres’s theorem has an extremal order, and it is worth setting beside a random one.

A sequence of 3² with no climb and no fall longer than 3. 10 terms plotted in order, each labelled with the longest climb and the longest fall ending at it; the first 9 keep both counters at 3 or below and the last one cannot.
Fig. 4 The order that escapes as long as possible: blocks that each fall, arranged so that each block lies wholly above the one before. A climb takes one number from each block and a fall stays inside one block, so neither is longer than the number of blocks, and nine numbers in three blocks of three hold no climb or fall of four.

In the extremal order both the longest climb and the longest fall are about n\sqrt n, the least the theorem allows. In a random order, the longest climb is about 2n2\sqrt n and so is the longest fall, since reversing a random shuffle gives another random shuffle. The random order has both, twice over, where the theorem promises one, once.

That gap between guarantee and typical is the same one the previous essay found for convex polygons, where random crowds hold far larger convex polygons than the worst crowds are forced to. Extremal orders are rigid and rare: the blocks must be arranged exactly, and a single misplaced card lengthens a run. Out of all n!n! orders, the ones with both runs close to n\sqrt n are a vanishing fraction, and the probability that a random shuffle’s longest climb falls below (2−ε)n(2 - \varepsilon)\sqrt n decays like e−c ne^{-c\,n} — faster than for almost any other statistic of a shuffle.

The comparison is worth setting against the rest of Ramsey theory, where randomness usually plays the opposite part. For the party problem, a random colouring is the best escape anyone knows: Erdős proved in 1947 that colouring each pair by a coin toss avoids a large one-coloured group far better than any explicit colouring does, and nobody has constructed colourings that match it. For sequences it is the other way round. A random order is a poor escape — its runs are twice as long as necessary — and the best escape is a rigid construction of blocks. Whether randomness helps or hinders escaping depends on whether the structure being avoided is rare, as a one-coloured group is, or everywhere, as a rising chain is.

Fluctuations of size n to the one-sixth

Knowing the average is not knowing how much a single shuffle can differ from it, and here the problem produced its biggest surprise.

How far the longest run of a 2000-card shuffle strays from 2√n. Histogram of (L − 2√n)/n^(1/6) for 1500 random permutations of 2000: mean -1.615, standard deviation 0.862.
Fig. 5 The longest increasing run of 1,500 random shuffles of 2,000 cards, one bar for each length that occurred, placed at (L−2n)/n1/6(L - 2\sqrt n)/n^{1/6}. The rescaled lengths have mean near −1.6 and spread near 0.86; their limiting distribution, with mean −1.77 and spread 0.90, is the one that governs the largest eigenvalue of a large random matrix.

If the longest run were a sum of many independent pieces, its fluctuations would be of size mean\sqrt{\text{mean}}, which here is about n1/4n^{1/4}. They are much smaller: of size n1/6n^{1/6}, about three and a half cards when n=2,000n = 2{,}000. A shuffle of two thousand cards almost always has a longest climb within a handful of cards of 2n−1.77 n1/62\sqrt n - 1.77\,n^{1/6}.

And the shape of the fluctuations is not the bell curve. In 1999 Jinho Baik, Percy Deift and Kurt Johansson proved that (Ln−2n)/n1/6(L_n - 2\sqrt n)/n^{1/6} converges to a distribution Craig Tracy and Harold Widom had found five years earlier in a completely different place: the largest eigenvalue of a large random Hermitian matrix, rescaled at the edge of the spectrum. It is lopsided, with a thin tail to the right and a thinner one to the left, mean −1.77-1.77 and spread 0.900.90. The histogram shows its lopsidedness at two thousand cards: the bars fall off more slowly on the right than on the left.

That a card game and a random matrix share a limit law is the surprising connection this essay turns on, and it has since spread. The same Tracy–Widom distribution describes the fluctuations of a growing crystal’s edge in certain models, the position of the fastest particle in a simple model of traffic in which cars hop forward only into empty spaces, and the interface of a turbulent liquid crystal measured in the laboratory in 2010. The shared structure is a random growth process in which each step depends on a maximum over earlier steps — which is exactly what the longest rising chain is.

The shape a shuffle grows

The 1977 proofs of the constant 22 did not study the longest run alone. They studied a whole shape attached to the shuffle, of which the longest run is one measurement.

The Robinson–Schensted correspondence inserts the cards of a shuffle one at a time into rows, each card bumping the smallest larger card from its row down into the next. The result is a staircase-shaped arrangement of nn boxes — a partition of nn, drawn as a diagram — and Craige Schensted proved in 1961 that the length of its first row is the longest increasing subsequence, and of its first column the longest decreasing one. Curtis Greene later showed that the first kk rows together give the largest union of kk increasing subsequences, so the whole shape measures the shuffle’s increasing structure at every scale.

The shape a random shuffle of 2500 grows, against its limit curve. The Robinson–Schensted shape of a random permutation of 2500, rescaled by √n and rotated 45 degrees, with first row 93 and first column 95, against the Logan–Shepp–Vershik–Kerov limit curve; greatest gap 0.049.
Fig. 6 The Robinson–Schensted shape of a random shuffle of 2,500 cards, scaled by n\sqrt n and turned through 45°, against the limit curve found in 1977. The staircase lies within about 0.05 of the curve; its first row is the longest increasing run, 93 against 2n=1002\sqrt n = 100, and it is the curve’s meeting with the lines at u = ±2 that fixes the constant 2.

Scaled by n\sqrt n and turned on its corner, the shape of a random shuffle’s diagram approaches a single curve,

Ω(u)=2π(uarcsin⁡u2+4−u2),∣u∣≤2,\Omega(u) = \frac{2}{\pi}\Big(u \arcsin\frac u2 + \sqrt{4 - u^2}\Big), \qquad |u| \le 2,

meeting the two lines v=∣u∣v = |u| at u=±2u = \pm 2. The first row of the diagram runs out along one of those lines, and it reaches to u=2u = 2 — which, undoing the scaling and rotation, is a row of length 2n2\sqrt n. The constant in the longest-run problem is where the limit curve touches the corner.

It is worth comparing with the shape a random partition takes, which is a different curve for a different reason. There, every partition of nn was equally likely. Here, a partition’s chance is the number of shuffles that produce it, which by the correspondence is the square of the number of ways to fill its diagram with 1,…,n1, \ldots, n increasing along rows and columns — the Plancherel weighting, which favours balanced shapes. Two natural ways of choosing a random partition, two different limit curves, and both found by solving a variational problem for the most likely shape.

Counting shuffles with random matrices

The link to random matrices is not only a coincidence of limit laws. There is an exact identity underneath it, found by Ira Gessel in 1990 and put in its sharpest form by Eric Rains in 1998.

Pick a k×kk \times k unitary matrix at random — uniformly, in the sense that makes every rotation of complex kk-space equally likely — and take its trace, the sum of its diagonal entries. The trace is a complex number whose size fluctuates from matrix to matrix. Rains proved that the average of its size raised to the power 2n2n is a whole number, and that the whole number is the count of shuffles of nn cards whose longest increasing run is at most kk:

E ∣tr⁡U∣2n=#{shuffles of n=#{with longest climb≤k}.\begin{aligned} \mathbb{E}\,\bigl|\operatorname{tr} U\bigr|^{2n} &= \#\{\text{shuffles of } n \\ &\phantom{= \#\{}\text{with longest climb} \le k\}. \end{aligned}

For k≥nk \ge n every shuffle qualifies and the average is n!n!. For k=1k = 1 only the fully decreasing order qualifies, and a 1×11 \times 1 unitary matrix is a point on the unit circle, whose size is one: the average is 11. In between, the identity turns a question about cards into an integral over a group of matrices, and the integral can be analysed by the same methods that describe how eigenvalues crowd together — which is how Baik, Deift and Johansson proved their theorem.

Matrix integrals counting combinatorial objects have appeared before: the Gaussian integral that counts ways to pair a polygon’s edges, organised by the genus of the surface each pairing makes. The shuffle identity is a relative. In both, an average over random matrices is secretly a sum over arrangements, and the matrix side is where the asymptotics are tractable.

A process that sorts itself

There is a way to watch the constant 22 emerge rather than compute it, and it comes from turning the square on its side.

Sweep a vertical line across Hammersley’s square from left to right, and keep, at each moment, a set of marks on the line: one mark for each pile of the patience-sorting game run on the points passed so far, placed at the height of that pile’s top card. When the sweeping line meets a new point, the lowest mark above the point jumps down to it — the card goes on the leftmost pile whose top is larger — or a new mark appears if none is above. The number of marks when the line reaches the far side is the longest rising chain.

David Aldous and Persi Diaconis showed in 1995 that this system of jumping marks behaves, at large scale, like a fluid obeying a simple conservation law. The marks thin out as they are pushed downward and replenished from above, and solving the fluid equation for their density gives the answer: 2n2\sqrt n marks at the end. It is a proof by physics — a hydrodynamic limit — and it explains the constant as the solution of a differential equation rather than as the corner of a limit shape.

That two such different proofs give the same 22, one through the shape of a tableau and one through the flow of a fluid of marks, is part of why the answer is trusted as deeply as it is. Neither proof, though, gives the fluctuations, and it was the matrix identity above that finally did.

What sampling cannot settle

Every number in these figures is a sample. Two hundred shuffles at each size estimate the mean to about one per cent; fifteen hundred shuffles show the shape of the fluctuation law only roughly; one shuffle of two thousand five hundred cards shows the limit shape once. The claims that the ratio tends to exactly 22, that the fluctuations are exactly of order n1/6n^{1/6}, and that their law is exactly Tracy–Widom’s are theorems, and the figures are consistent with them rather than evidence strong enough to have found them.

The finite sizes also hide how slowly everything converges. The histogram’s mean is −1.6-1.6 at two thousand cards, not the limiting −1.77-1.77, and the difference is a correction of relative size n−1/3n^{-1/3}, the same slowness the growth figure shows. Anybody estimating the constants from simulation would be misled in the same direction at every size they could afford.

And the pictures are all of the uniform shuffle. Shuffles that are not uniform — a deck given only a few riffle shuffles, or points concentrated along the diagonal of the square — have longest runs governed by other laws, and the rigidity of 2n2\sqrt n is a property of complete randomness.

Still open: the constant in higher dimensions

Hammersley’s square has a natural extension. Drop nn points at random in a dd-dimensional cube and ask for the longest chain rising in every coordinate at once. Its length is about cd n1/dc_d\, n^{1/d}, and Béla Bollobás and Peter Winkler proved in 1988 that the constant cdc_d exists for every dd and that cdc_d tends to ee as the dimension grows.

For d=2d = 2 the constant is 22, by the theorems above. For d=3d = 3 and every higher dimension, the value of cdc_d is not known. It lies between known bounds, and simulations estimate it, but there is no Robinson–Schensted correspondence for three-dimensional orders, no limit shape to compute, and none of the algebraic structure that made the planar problem exactly solvable. The same question that has a closed answer in the plane has, one dimension up, only an estimate.