Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applications of Longest Increasing Subsquence

How useful is the LIS (Longest Increasing Subsequence) problem in tackling other CS problems? There are a few algorithms, using patience sorting, dynamic programming or with decision trees. How are these used in real life -- maybe to data streams or something?

To remind you, I put in bold the longest increasing sequence

{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}.

As a bonus, is there any way to use the result that a sequence of length mn + 1 will have an increasing subsequence of length m or a decreasing subsequence of length n? E.g. Our list as length 16, so there should be an increasing sequence of length 5 or decreasing sequence of length 5. In our case 0,2,6,9,11,15.

Also an increasing sequence of length 8 or a decreasing sequence of length 3: in our case 12,10,1.

like image 495
john mangual Avatar asked Sep 17 '12 11:09

john mangual


People also ask

What is the application of the longest common subsequence?

The longest common subsequence problem is a classic computer science problem, the basis of data comparison programs such as the diff utility, and has applications in computational linguistics and bioinformatics.

What is the meaning of 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.

What is the best strategy to solve longest increasing subsequence problem?

The idea is to use recursion to solve this problem. Find as many subsequences as possible for the current number. If the current item is greater than the previous element in the subsequence, include the current item in the subsequence and recur for the remaining items.

What is LIS programming?

The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.


1 Answers

An interesting real-world application of LIS is Patience Diff, a diffing algorithm by Bram Cohen (the creator of BitTorrent) which is used in the Bazaar version control system.

The regular diff algorithm involves computing the LCS (Longest Common Subsequence) between two documents. While being efficient, this approach has a problem, which is -- the results often happen to be not quite human-friendly.

A simple example of how a regular diff may fail:

 void func1() {
     x += 1
+}
+
+void functhreehalves() {
+    x += 1.5
 }

 void func2() {
     x += 2
 }

The advantage of the Patience Diff algorithm is that it allows to compute the differences more accurately, in a manner more closely corresponding to how a human would perform.

In the previous case Patience Diff spots the difference better:

 void func1() {
     x += 1
 }

+void functhreehalves() {
+    x += 1.5
+}
+
 void func2() {
     x += 2
 }

In a nutshell, the algorithm is:

  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.

Take a look at the code.

like image 156
Max Mouratov Avatar answered Oct 19 '22 15:10

Max Mouratov