<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kandy Software, Inc. &#187; subsequence</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/tag/subsequence/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Thu, 20 Jan 2011 04:03:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Longest Increasing Subsequence</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/lis</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/lis#comments</comments>
		<pubDate>Fri, 15 Feb 2008 00:50:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[increasing]]></category>
		<category><![CDATA[lis]]></category>
		<category><![CDATA[subsequence]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=85</guid>
		<description><![CDATA[A classic computer science problem. The problem is finding the longest increasing subset of numbers within a given set. This article presents a solution in Java. ]]></description>
			<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png" alt="" title="dna" class="alignleft size-medium wp-image-86" /></a>Finding the <a href="http://en.wikipedia.org/wiki/Longest_increasing_subsequence" target="_blank">maximum or longest increasing subsequence</a> (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.</p>
<h3><strong>Problem</strong></h3>
<p>Given a sequence of N integers, find the longest <i>strictly</i> 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:</p>
<pre>
  | 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 <font color="red">96</font> 96 36 64 31 45 86
5 | 45 99 <font color="red">68</font> <font color="red">74</font> 54 14 93 17 14 55
6 | 14 42 <font color="red">52</font> 54 34 50 22 84 32 41
7 | 90 <font color="red">44</font> 73 10 71 84 20 12 55 52
8 | 95 <font color="red">33</font> <font color="red">25</font> 31 76 45 44 84 90 52
9 | 94 64 95 <font color="red">24</font> 41 63 87 93 79 12
</pre>
<p>the longest increasing subsequence is of length 8 consisting of entries 24, 25, 33, 44, 52, 68, 74, 96 as highlighted in red.</p>
<h3><strong>Solution</strong></h3>
<p>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.</p>
<p>Unzip the attachment and <code>cd</code> into the newly created folder. Compile the classes run as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\MaximumIncreasingSubsequence<span style="color: #33cc33;">&gt;</span>javac -d bin src\*.java
&nbsp;
C:\MaximumIncreasingSubsequence<span style="color: #33cc33;">&gt;</span>java -classpath bin MaximumIncreasingSubsequence small-grid.txt
<span style="color: #33cc33;">(</span>24, 0<span style="color: #33cc33;">)</span> with value 129 <span style="color: #33cc33;">(</span>weight = 15<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>24, 1<span style="color: #33cc33;">)</span> with value 138 <span style="color: #33cc33;">(</span>weight = 14<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>25, 2<span style="color: #33cc33;">)</span> with value 167 <span style="color: #33cc33;">(</span>weight = 13<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>25, 3<span style="color: #33cc33;">)</span> with value 236 <span style="color: #33cc33;">(</span>weight = 12<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>25, 4<span style="color: #33cc33;">)</span> with value 300 <span style="color: #33cc33;">(</span>weight = 11<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>26, 4<span style="color: #33cc33;">)</span> with value 338 <span style="color: #33cc33;">(</span>weight = 10<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>27, 5<span style="color: #33cc33;">)</span> with value 438 <span style="color: #33cc33;">(</span>weight = 9<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>28, 4<span style="color: #33cc33;">)</span> with value 469 <span style="color: #33cc33;">(</span>weight = 8<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>27, 4<span style="color: #33cc33;">)</span> with value 596 <span style="color: #33cc33;">(</span>weight = 7<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>27, 3<span style="color: #33cc33;">)</span> with value 709 <span style="color: #33cc33;">(</span>weight = 6<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>26, 2<span style="color: #33cc33;">)</span> with value 723 <span style="color: #33cc33;">(</span>weight = 5<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>27, 1<span style="color: #33cc33;">)</span> with value 778 <span style="color: #33cc33;">(</span>weight = 4<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>28, 1<span style="color: #33cc33;">)</span> with value 866 <span style="color: #33cc33;">(</span>weight = 3<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>27, 0<span style="color: #33cc33;">)</span> with value 939 <span style="color: #33cc33;">(</span>weight = 2<span style="color: #33cc33;">)</span>
<span style="color: #33cc33;">(</span>26, 0<span style="color: #33cc33;">)</span> with value 953 <span style="color: #33cc33;">(</span>weight = 1<span style="color: #33cc33;">)</span>
Program took: 380 milliseconds</pre></div></div>

<h3><strong>Attachments</strong></h3>
<p><a href='http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/maximumincreasingsubsequence.zip' title='Source code'>Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/lis/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

