Longest increasing subsequence


In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
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, where n denotes the length of the input sequence.

Example

In the first 16 terms of the binary Van der Corput sequence
a longest increasing subsequence is
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,
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,..., n, this approach can be made much more efficient, leading to time bounds of the form O.
The largest clique in a permutation graph is defined by the longest decreasing subsequence of the permutation that defines the graph; the longest decreasing subsequence is equivalent in computational complexity, by negation of all numbers, to the longest increasing 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, X, etc. Then, after processing X, the algorithm will have stored values in two arrays:
In addition the algorithm stores a variable L representing the length of the longest increasing subsequence found so far. Because the algorithm below uses zero-based numbering, for clarity M is padded with M, which goes unused so that M corresponds to a subsequence of length j. A real implementation can skip M and adjust the indices accordingly.
Note that, at any point in the algorithm, the sequence
is increasing. For, if there is an increasing subsequence of length j ≥ 2 ending at X. Thus, we may do binary searches in this sequence in logarithmic time.
The algorithm, then, proceeds as follows:
P = array of length N
M = array of length N + 1
L = 0
for i in range 0 to N-1:
// Binary search for the largest positive j ≤ L
// such that X
lo = 1
hi = L
while lo ≤ hi:
mid = ceil
if X:
lo = mid+1
else:
hi = mid-1
// After searching, lo is 1 greater than the
// length of the longest prefix of X
newL = lo
// The predecessor of X is the last index of
// the subsequence of length newL-1
P = M
M = i

if newL > L:
// If we found a subsequence longer than any we've
// found yet, update L
L = newL
// Reconstruct the longest increasing subsequence
S = array of length L
k = M
for i in range L-1 to 0:
S = X
k = P
return S
Because the algorithm performs a single binary search per sequence element, its total time can be expressed using Big O notation as O. 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 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 comparisons in the worst case, which is optimal for a comparison-based algorithm up to the constant factor in the O term.

Length bounds

According to the Erdős–Szekeres theorem, any sequence of n2+1 distinct integers has an increasing or a decreasing subsequence of length For inputs in which each permutation of the input is equally likely, the expected length of the longest increasing subsequence is approximately 2.
In the limit as n approaches infinity, 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.
The length of the increasing subsequence selected by this optimal procedure has variance approximately equal to /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 covariance matrix of the three-dimensional process summarizing all interacting processes.

Application