The Longest Increasing Subsequence problem is to find subsequence from the give input sequence in which subsequence’s elements are sorted in lowest to highest order. All subsequence are not contiguous or unique.

Application of Longest Increasing Subsequence:

Algorithms like Longest Increasing Subsequence, Longest Common Subsequence are used in version control systems like Git and etc.

Simple form of Algorithm:

  1. Find unique lines which are common to both documents.
  2. Take all such lines from the first document and order them according to their appearance in the second document.
  3. Compute the LIS of the resulting sequence (by doing a Patience Sort), getting the longest matching sequence of lines, a correspondence between the lines of two documents.
  4. Recurse the algorithm on each range of lines between already matched ones.

Now let us consider a simpler example of the LCS problem. Here, input is only one sequence of distinct integers a1,a2,...,an., and we want to find the longest increasing subsequence in it. For example, if input is 7,3,8,4,2,6 then the longest increasing subsequence is 3,4,6.

The easiest approach is to sort input elements in increasing order, and apply the LCS algorithm to the original and sorted sequences. However, if you look at the resulting array you would notice that many values are the same, and the array looks very repetitive. This suggest that the LIS (longest increasing subsequence) problem can be done with dynamic programming algorithm using only one-dimensional array.

Pseudo Code:

  1. Describe an array of values we want to compute. For 1 <= i <= n, let A(i) be the length of a longest increasing sequence of input. Note that the length we are ultimately interested in is max{A(i)|1 ≤ i ≤ n}.
  2. Give a recurrence. For 1 <= i <= n, A(i) = 1 + max{A(j)|1 ≤ j < i and input(j) < input(i)}.
  3. Compute the values of A.
  4. Find the optimal solution.

The following program uses A to compute an optimal solution. The first part computes a value m such that A(m) is the length of an optimal increasing subsequence of input. The second part computes an optimal increasing subsequence, but for convenience we print it out in reverse order. This program runs in time O(n), so the entire algorithm runs in time O(n^2).

Part 1:

m ← 1 
for i : 2..n 
    if A(i) > A(m) then 
        m ← i 
    end if 
end for

Part 2:

put a
while A(m) > 1 do 
    i ← m−1 
    while not(ai < am and A(i) = A(m)−1) do 
        i ← i−1 
    end while 
    m ← i 
    put a
 end while