Home » Featured, Java

Longest Increasing Subsequence

14 February 2008 3,952 views No Comment

Introduction

Finding the maximum or longest increasing subsequence (LIS) within a given a set of numbers is a classic Computer Science problem. The algorithm is central to many tools and is used in various disciplines related to mathematics, physics etc. In the field of Bioinformatics, LIS is used to find DNA subsequences matching a certain criteria. By recognizing certain patterns in DNA sequences scientists can determine diseases, genetic disorders etc.

Problem

Given a sequence of N integers, find the longest strictly increasing subsequence. For example in the sequence 56, 21, 33, 22, 34, 78, 11, 35, 46, the longest increasing subsequence is 21, 33, 34, 35, 46. In this context we will consider a NXN matrix of integers and find a sequence which is simply a series of adjacent squares. A square may not be used more than once. In the following 10 X 10 grid:

  | 0  1  2  3  4  5  6  7  8  9
--+------------------------------
0 | 95 47 30 36 60 31 57 66 12 55
1 | 35 57 41 13 82 80 71 93 31 62
2 | 89 36 98 75 91 24 95 53 37 99
3 | 25 45 26 17 15 84 80 73 96 21
4 | 75 22 43 96 96 36 64 31 45 86
5 | 45 99 68 74 54 14 93 17 14 55
6 | 14 42 52 54 34 50 22 84 32 41
7 | 90 44 73 10 71 84 20 12 55 52
8 | 95 33 25 31 76 45 44 84 90 52
9 | 94 64 95 24 41 63 87 93 79 12

the longest increasing subsequence is of length 8 consisting of entries 24, 25, 33, 44, 52, 68, 74, 96 as highlighted in red.

Solution

Each number and its position in the sequence are stored in an array. In the first pass, neighbors for each item in the array are found and are stored along with the item. Items are then sorted by their descending value. In the second and final pass, weights are assigned to the neighbors. The neighbor with the least value is assigned weight 1, the next highest neighbor is assigned 2 and so on. To get the longest increasing subsequence, find the element with the highest weight and print its neighbors in the descending order of weights.

Unzip the attachment and cd into the newly created folder. Compile the classes run as follows:

C:\MaximumIncreasingSubsequence>javac -d bin src\*.java
 
C:\MaximumIncreasingSubsequence>java -classpath bin MaximumIncreasingSubsequence small-grid.txt
(24, 0) with value 129 (weight = 15)
(24, 1) with value 138 (weight = 14)
(25, 2) with value 167 (weight = 13)
(25, 3) with value 236 (weight = 12)
(25, 4) with value 300 (weight = 11)
(26, 4) with value 338 (weight = 10)
(27, 5) with value 438 (weight = 9)
(28, 4) with value 469 (weight = 8)
(27, 4) with value 596 (weight = 7)
(27, 3) with value 709 (weight = 6)
(26, 2) with value 723 (weight = 5)
(27, 1) with value 778 (weight = 4)
(28, 1) with value 866 (weight = 3)
(27, 0) with value 939 (weight = 2)
(26, 0) with value 953 (weight = 1)
Program took: 380 milliseconds

Attachments

Source code

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.