# Longest increasing subsequence

In computer science, the longest increasing subsequence problem aims to find a subsequence of a given sequence in which the subsequence's elements are sorted in an ascending order and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous or unique. The longest increasing subsequences are studied in the context of various disciplines related to mathematics, including algorithmics, random matrix theory, representation theory, and physics. The longest increasing subsequence problem is solvable in time $O(n\log n),$ where $n$ denotes the length of the input sequence.

## Example

In the first 16 terms of the binary Van der Corput sequence
    0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
one of the longest increasing subsequences is
    0, 2, 6, 9, 11, 15.
This subsequence has length six; the input sequence has no seven-member increasing subsequences. The longest increasing subsequence in this example is not the only solution: for instance,
    0, 4, 6, 9, 11, 15
    0, 2, 6, 9, 13, 15
    0, 4, 6, 9, 13, 15
are other increasing subsequences of equal length in the same input sequence.

## Relations to other algorithmic problems

The longest increasing subsequence problem is closely related to the longest common subsequence problem, which has a quadratic time dynamic programming solution: the longest increasing subsequence of a sequence $S$ is the longest common subsequence of $S$ and $T,$ where $T$ is the result of sorting $S.$ However, for the special case in which the input is a permutation of the integers $1,2,\ldots ,n,$ this approach can be made much more efficient, leading to time bounds of the form $O(n\log \log n).$
The largest clique in a permutation graph corresponds to the longest decreasing subsequence of the permutation that defines the graph (assuming the original non-permuted sequence is sorted from lowest value to highest). Similarly, the maximum independent set in a permutation graph corresponds to the longest non-decreasing subsequence. Therefore, longest increasing subsequence algorithms can be used to solve the clique problem efficiently in permutation graphs.
In the Robinson–Schensted correspondence between permutations and Young tableaux, the length of the first row of the tableau corresponding to a permutation equals the length of the longest increasing subsequence of the permutation, and the length of the first column equals the length of the longest decreasing subsequence.

## Efficient algorithms

The algorithm outlined below solves the longest increasing subsequence problem efficiently with arrays and binary searching. It processes the sequence elements in order, maintaining the longest increasing subsequence found so far. Denote the sequence values as $X[0],X[1],\ldots ,$ etc. Then, after processing $X[i],$ the algorithm will have stored an integer $L$ and values in two arrays:
* $L$ — stores the length of the longest increasing subsequence found so far.
* $M[l]$ — stores the index $k$ of the smallest value $X[k]$ such that there is an increasing subsequence of length $l$ ending at $X[k]$ in the range $k\leq i.$ Explicitly, suppose that $K_{i,l}$ denotes the set of all indices $j$ such that $j\leq i$ and there exists an increasing subsequence of length $l$ ending at $X[j].$ Then $k=M[l]$ is the index in $K_{i,l}$ for which $X[M[l]]$ is minimized; meaning that $M[l]\in K_{i,l}$ and $X[M[l]]=\min _{j\in K_{i,l}}X[j]$ (or equivalently, $M[l]\in K_{i,l}$ and for every $j\in K_{i,l},$ $X[M[l]]\leq X[j]$); if multiple indices satisfy this condition then $M[l]$ is the largest one.
** To clarify, "there exists an increasing subsequence of length $l$ ending at $X[k]$" means that there exist $l$ indices $i_{1}<i_{2}<\cdots <i_{l}=k$ ending at $k$ such that $X\left[i_{1}\right]<X\left[i_{2}\right]<\cdots <X[k].$
** Note that $1\leq l\leq i+1,$ because $l\geq 1$ represents the length of the increasing subsequence, and $k\geq 0$ represents the index of its termination.
** The length of $M$ is $1$ more than the length of $X$ but it is possible that not all elements in this array are used by the algorithm (in fact, if the longest increasing sequence has length $L$ then only $M[1],\ldots ,M[L]$ are used by the algorithm). If however $M[l]$ is used/defined then $l-1\leq M[l]$ (and moreover, at iteration $i,$ $M[l]\leq i$ will also hold). $M[0]$ is undefined since sequences of length $0$ have no ending index ($M[0]$ can be any value).
* $P[k]$ — stores the index of the predecessor of $X[k]$ in the longest increasing subsequence ending at $X[k].$
** The length of $P$ is equal to that of $X,$
** If $k>0$ then $P[k]<k$ while $P[0]$ is undefined since $X[0]$ has no predecessor ($P[0]$ can be any value).
Because the algorithm below uses zero-based numbering, for clarity $M$ is padded with $M[0],$ which goes unused so that $M[l]$ corresponds to a subsequence of length $l.$ A real implementation can skip $M[0]$ and adjust the indices accordingly.
Note that, at any point in the algorithm, the sequence $X[M[1]],X[M[2]],\ldots ,X[M[L]]$ is increasing. For, if there is an increasing subsequence of length $l\geq 2$ ending at $X[M[l]],$ then there is also a subsequence of length $l-1$ ending at a smaller value: namely the one ending at $X[P[M[l]]].$ Thus, we may do binary searches in this sequence in logarithmic time.
The algorithm, then, proceeds as follows:
Because the algorithm performs a single binary search per sequence element, its total time can be expressed using Big O notation as $O(n\log n).$ Fredman (1975) discusses a variant of this algorithm, which he credits to Donald Knuth; in the variant that he studies, the algorithm tests whether each value $X[i]$ can be used to extend the current longest increasing sequence, in constant time, prior to doing the binary search. With this modification, the algorithm uses at most $n\log _{2}n-n\log _{2}\log _{2}n+O(n)$ comparisons in the worst case, which is optimal for a comparison-based algorithm up to the constant factor in the $O(n)$ term.
Example run
Using X = [2, 8, 9, 5, 6, 7, 1] :
| Values stored in variables | X[i] | newL | P | M | X[M] | L |
| Before for i loop | | | P = | M = [-1] | X[M] = [N/A] | L = 0 |
| At end of loop i = 0 | X[i] = 2 | newL = 1 | P = [-1] | M = [-1, 0] | X[M] = [N/A, 2] | L = 1 |
| At end of loop i = 1 | X[i] = 8 | newL = 2 | P = [-1, 0] | M = [-1, 0, 1] | X[M] = [N/A, 2, 8] | L = 2 |
| At end of loop i = 2 | X[i] = 9 | newL = 3 | P = [-1, 0, 1] | M = [-1, 0, 1, 2] | X[M] = [N/A, 2, 8, 9] | L = 3 |
| At end of loop i = 3 | X[i] = 5 | newL = 2 | P = [-1, 0, 1, 0] | M = [-1, 0, 3, 2] | X[M] = [N/A, 2, 5, 9] | L = 3 |
| At end of loop i = 4 | X[i] = 6 | newL = 3 | P = [-1, 0, 1, 0, 3] | M = [-1, 0, 3, 4] | X[M] = [N/A, 2, 5, 6] | L = 3 |
| At end of loop i = 5 | X[i] = 7 | newL = 4 | P = [-1, 0, 1, 0, 3, 4] | M = [-1, 0, 3, 4, 5] | X[M] = [N/A, 2, 5, 6, 7] | L = 4 |
| At end of loop i = 6 | X[i] = 1 | newL = 1 | P = [-1, 0, 1, 0, 3, 4, -1] | M = [-1, 6, 3, 4, 5] | X[M] = [N/A, 1, 5, 6, 7] | L = 4 |
| Values stored in variables | S | S | S | S | k | X[k] |
| Before for j loop | S = [N/A, N/A, N/A, N/A] | S = [N/A, N/A, N/A, N/A] | S = [N/A, N/A, N/A, N/A] | S = [N/A, N/A, N/A, N/A] | k = M[4] = 5 | X[5] = 7 |
| At end of loop j = 3 | S = [N/A, N/A, N/A, 7] | S = [N/A, N/A, N/A, 7] | S = [N/A, N/A, N/A, 7] | S = [N/A, N/A, N/A, 7] | k = P[5] = 4 | X[4] = 6 |
| At end of loop j = 2 | S = [N/A, N/A, 6, 7] | S = [N/A, N/A, 6, 7] | S = [N/A, N/A, 6, 7] | S = [N/A, N/A, 6, 7] | k = P[4] = 3 | X[3] = 5 |
| At end of loop j = 1 | S = [N/A, 5, 6, 7] | S = [N/A, 5, 6, 7] | S = [N/A, 5, 6, 7] | S = [N/A, 5, 6, 7] | k = P[3] = 0 | X[0] = 2 |
| At end of loop j = 0 | S = [2, 5, 6, 7] | S = [2, 5, 6, 7] | S = [2, 5, 6, 7] | S = [2, 5, 6, 7] | k = P[0] = -1 | X[-1] = N/A |

## Length bounds

According to the Erdős–Szekeres theorem, any sequence of $n^{2}+1$ distinct integers has an increasing or a decreasing subsequence of length $n+1.$ For inputs in which each permutation of the input is equally likely, the expected length of the longest increasing subsequence is approximately $2{\sqrt {n}}.$
In the limit as $n$ approaches infinity, the Baik-Deift-Johansson theorem says, that the length of the longest increasing subsequence of a randomly permuted sequence of $n$ items has a distribution approaching the Tracy–Widom distribution, the distribution of the largest eigenvalue of a random matrix in the Gaussian unitary ensemble.

## Online algorithms

The longest increasing subsequence has also been studied in the setting of online algorithms, in which the elements of a sequence of independent random variables with continuous distribution $F$ – or alternatively the elements of a random permutation – are presented one at a time to an algorithm that must decide whether to include or exclude each element, without knowledge of the later elements. In this variant of the problem, which allows for interesting applications in several contexts, it is possible to devise an optimal selection procedure that, given a random sample of size $n$ as input, will generate an increasing sequence with maximal expected length of size approximately ${\sqrt {2n}}.$
The length of the increasing subsequence selected by this optimal procedure has variance approximately equal to ${\sqrt {2n}}/3,$ and its limiting distribution is asymptotically normal after the usual centering and scaling. The same asymptotic results hold with more precise bounds for the corresponding problem in the setting of a Poisson arrival process. A further refinement in the Poisson process setting is given through the proof of a central limit theorem for the optimal selection process which holds, with a suitable normalization, in a more complete sense than one would expect. The proof yields not only the "correct" functional limit theorem but also the (singular) covariance matrix of the three-dimensional process summarizing all interacting processes.