<?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>sub-solution &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/sub-solution/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Longest Increasing Subsequence</title>
		<link>/2012/12/03/computer-algorithms-longest-increasing-subsequence/</link>
		<comments>/2012/12/03/computer-algorithms-longest-increasing-subsequence/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 13:22:08 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Bellman–Ford algorithm]]></category>
		<category><![CDATA[Directed acyclic graph]]></category>
		<category><![CDATA[Dynamic programming]]></category>
		<category><![CDATA[equal sub-solutions]]></category>
		<category><![CDATA[Facebook Inc]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Longest increasing subsequence]]></category>
		<category><![CDATA[Mathematical optimization]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Network theory]]></category>
		<category><![CDATA[Operations research]]></category>
		<category><![CDATA[Richard Bellman]]></category>
		<category><![CDATA[Routing algorithms]]></category>
		<category><![CDATA[Shortest path problem]]></category>
		<category><![CDATA[sub-solution]]></category>
		<category><![CDATA[Theoretical computer science]]></category>
		<category><![CDATA[Yahoo! Inc.]]></category>

		<guid isPermaLink="false">/?p=3478</guid>
		<description><![CDATA[Introduction A very common problem in computer programming is finding the longest increasing (decreasing) subsequence in a sequence of numbers (usually integers). Actually this is a typical dynamic programming problem. Dynamic programming can be described as a huge area of computer science problems that can be categorized by the way they can be solved. Unlike &#8230; <a href="/2012/12/03/computer-algorithms-longest-increasing-subsequence/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Longest Increasing Subsequence</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Computer Algorithms: Bellman-Ford Shortest Path in a Graph </a></li>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>A very common problem in computer programming is finding the longest increasing (decreasing) subsequence in a sequence of numbers (usually integers). Actually this is a typical dynamic programming problem.</p>
<p>Dynamic programming can be described as a huge area of computer science problems that can be categorized by the way they can be solved. Unlike divide and conquer, where we were able to merge the fairly equal sub-solutions in order to receive one single solution of the problem, in dynamic programming we usually try to find an optimal sub-solution and then grow it.</p>
<p>Once we have an optimal sub-solution on each step we try to upgrade it in order to cover the whole problem. Thus a typical member of the dynamic programming class is finding the longest subsequence.</p>
<p>However this problem is interesting because it can be related to graph theory. Let’s find out how.<span id="more-3478"></span></p>
<h2>Overview</h2>
<p>We already know various ways to calculate the shortest paths in a graph. Indeed finding the single-source shortest path is a typical graph problem. To model such kind of solutions we definitely need a graph represented in our solution. </p>
<p>However the single-source shortest path isn’t a straight-forward problem. It depends on many factors. Thus for positive edges <a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">the algorithm of Edsger Dijkstra</a> can be a perfect solution, but when the graph contains negative edges his algorithm is no longer useful. </p>
<p>In the presence of negative edges the Dijkstra’s algorithm doesn’t work and we’d better use the <a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Bellman-Ford algorithm</a>. It is interesting to note, that it was exactly <a href="http://en.wikipedia.org/wiki/Richard_E._Bellman" title="Richard E. Bellman" target="_blank">Richard Bellman</a> who first introduced the term “dynamic programming” in the 1940s.</p>
<p>In fact the Bellman-Ford algorithm was able to detect negative cycles. That’s too important, because in presence of negative cycles the shortest path problem is no longer well defined. </p>
<p>In the other hand when we’re talking about <a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">shortest paths in a DAG</a> (Directed Acyclic Graph) we can find a faster (linear) solution. That’s because we’re sure that there are no cycles (not even negative cycles)! </p>
<figure id="attachment_3498" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png"><img src="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png" alt="Toplogical Sort" title="Toplogical Sort" width="620" height="399" class="size-full wp-image-3498" srcset="/wp-content/uploads/2012/12/1.-Toplogical-Sort.png 620w, /wp-content/uploads/2012/12/1.-Toplogical-Sort-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Finding the shortest paths in a DAG is closely related to the topological sorting of the DAG. This gives us a linear representation of the vertices of the DAG and we can clearly calculate the distances from the starting node to all other nodes. Note that in a DAG we have one or more nodes that can be considered as starting nodes – which means they don’t have predecessors (incoming edges).</p>
<figure id="attachment_3497" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png"><img src="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png" alt="Toplogical Sort 2" title="Toplogical Sort 2" width="620" height="399" class="size-full wp-image-3497" srcset="/wp-content/uploads/2012/12/2.-Toplogical-Sort-2.png 620w, /wp-content/uploads/2012/12/2.-Toplogical-Sort-2-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Following the words above is pretty hard to find what all these graph algorithms have to do with finding the longest increasing (decreasing) subsequence. Actually this problem is very closely related to the toplogical sort of the DAG and the problem of finding shortest paths in a DAG.</p>
<p>That’s because we can represent our sequence as a DAG. The only thing we must care about is to “connect” with directed edges those elements that form an increasing (decreasing) pair. </p>
<figure id="attachment_3496" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/3.-Integer-Sequence.png"><img src="/wp-content/uploads/2012/12/3.-Integer-Sequence.png" alt="Integer Sequence as a DAG" title="Integer Sequence" width="620" height="399" class="size-full wp-image-3496" srcset="/wp-content/uploads/2012/12/3.-Integer-Sequence.png 620w, /wp-content/uploads/2012/12/3.-Integer-Sequence-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Thus the sequence from our example [1, 8, 2, 7, 3, 4, 1, 6] is going to look like this.</p>
<figure id="attachment_3495" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/12/4.-Longest-subsequence.png"><img src="/wp-content/uploads/2012/12/4.-Longest-subsequence.png" alt="Longest subsequence" title="Longest subsequence" width="620" height="399" class="size-full wp-image-3495" srcset="/wp-content/uploads/2012/12/4.-Longest-subsequence.png 620w, /wp-content/uploads/2012/12/4.-Longest-subsequence-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>Another important thing to note is that we don’t search for shortest, but for longest path, since our task is to find the longest subsequence.</p>
<h2>Pseudo Code</h2>
<p>Our pseudo code for finding the shortest paths in a DAG was something like that.</p>
<pre>
1. Get the toplogically sorted list L of the DAG;
2. The starting node is s;
3. The distance to s equals to 0;
4. All other distances are initialized to &#8734;
5. For each node (v) in L\{s} do:
5.1. If dist(v) > dist(u) + w(u, v) then
5.1.1.  dist(v) := dist(u) + w(u, v)
</pre>
<p>In the above pseudo code &#8220;u&#8221; is every predecessor of &#8220;v&#8221;!</p>
<p>Now we must “reverse” the solution above in order to find the longest increasing subsequence. Note that we don&#8217;t care any more about the weight of the edges, thus we can simply substitute them with 1.</p>
<pre>
1. Get the sequence (L) as a toplogically sorted DAG;
2. For each (i) in L do:
2.1. S(i) := 1 + max(S(j), where (i, j) is an edge from the DAG);
</pre>
<h2>Application</h2>
<p>Finding the longest increasing subsequence can be very useful not only at the Google/Yahoo/Facebook interview, but also in various fields of statistics.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/10/28/computer-algorithms-shortest-path-in-a-directed-acyclic-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Directed Acyclic Graph">Computer Algorithms: Shortest Path in a Directed Acyclic Graph </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/22/computer-algorithms-bellman-ford-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Bellman-Ford Shortest Path in a Graph">Computer Algorithms: Bellman-Ford Shortest Path in a Graph </a></li>
<li><a href="/2012/12/10/computer-algorithms-topological-sort-revisited/" rel="bookmark" title="Computer Algorithms: Topological Sort Revisited">Computer Algorithms: Topological Sort Revisited </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/12/03/computer-algorithms-longest-increasing-subsequence/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
