<?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>Intrepid Blog &#187; algorithms</title>
	<atom:link href="http://blog.affien.com/archives/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 01 Mar 2010 00:58:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Online Cycle Detection in Directed Graphs</title>
		<link>http://blog.affien.com/archives/2007/07/02/online-cycle-detection-in-directed-graphs/</link>
		<comments>http://blog.affien.com/archives/2007/07/02/online-cycle-detection-in-directed-graphs/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 21:01:45 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[datastructure]]></category>
		<category><![CDATA[directed acyclic graph]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2007/07/02/online-cycle-detection-in-directed-graphs/</guid>
		<description><![CDATA[A short while ago I came across a quite interesting problem.  Design a datastructure (and algorithms) to maintain a Directed Acyclic Graph.
There has to be only one operation: adding a link between two given nodes.  This operation must be able to detect and deny any new link that would cause a cycle. For [...]]]></description>
			<content:encoded><![CDATA[<p>A short while ago I came across a quite interesting problem.  Design a datastructure (and algorithms) to maintain a <a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph">Directed Acyclic Graph</a>.</p>
<p>There has to be only one operation: adding a link between two given nodes.  This operation must be able to detect and deny any new link that would cause a cycle. For simplicity, nodes are identified by sequential ids starting with 0.</p>
<p>An example:</p>
<p><code>addLink 0, 1 -> True<br />
addLink 1, 2 -> True<br />
addLink 2, 0 -> False # Fails because it would create a cycle</code></p>
<p><code>addLink 0, 1 -> True<br />
addLink 1, 2 -> True<br />
addLink 0, 2 -> True # This isn't a real cycle, so it's perfectly fine</code></p>
<p>It&#8217;s rather trivial to create a <img src='/wp-latexrender/pictures/2575bd27db5c6a8b775972dfd3a49dd5.png' title='\mathcal{O}\left(n+\ell\right)' alt='\mathcal{O}\left(n+\ell\right)' align=absmiddle> (where <img src='/wp-latexrender/pictures/7b8b965ad4bca0e41ab51de7b31363a1.png' title='n' alt='n' align=absmiddle> is the number of nodes and <img src='/wp-latexrender/pictures/ee5e5c003694e7cd5ae404923c665edb.png' title='\ell' alt='\ell' align=absmiddle> the number of links).  I conjecture there exists a <img src='/wp-latexrender/pictures/8d55a5f26c4b0ec9cb6eb8d2609c5c1e.png' title='\mathcal{O}\left(\log n\right)' alt='\mathcal{O}\left(\log n\right)' align=absmiddle> algorithm.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2007/07/02/online-cycle-detection-in-directed-graphs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Function Recursion Overhead</title>
		<link>http://blog.affien.com/archives/2005/07/01/function-recursion-overhead/</link>
		<comments>http://blog.affien.com/archives/2005/07/01/function-recursion-overhead/#comments</comments>
		<pubDate>Fri, 01 Jul 2005 20:22:39 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[overhead]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/?p=106</guid>
		<description><![CDATA[In courses I followed at university and in most books about programming, recursion is praised as a good way to solve some problems.
An example of recursion (not-existing-api used for the sake of simplicity):
int GetFolderSize(Folder folder)
{
&#160;int sum = 0;
&#160;foreach(File file in folder.files)
&#160;{
&#160;&#160;sum += file.Size;
&#160;}
&#160;foreach(Folder subFolder in folder.folders)
&#160;{
&#160;&#160;sum += GetFolderSize(subFolder);
&#160;}
&#160;return sum;
}
This example function calculates the combined size [...]]]></description>
			<content:encoded><![CDATA[<p>In courses I followed at university and in most books about programming, <em>recursion</em> is praised as a good way to solve some problems.</p>
<p>An example of recursion (not-existing-api used for the sake of simplicity):</p>
<p><code>int GetFolderSize(Folder folder)<br />
{<br />
&nbsp;int sum = 0;<br />
&nbsp;foreach(File file in folder.files)<br />
&nbsp;{<br />
&nbsp;&nbsp;sum += file.Size;<br />
&nbsp;}<br />
&nbsp;foreach(Folder subFolder in folder.folders)<br />
&nbsp;{<br />
&nbsp;&nbsp;sum += GetFolderSize(subFolder);<br />
&nbsp;}<br />
&nbsp;return sum;<br />
}</code></p>
<p>This example function calculates the combined size of all files in a folder and those inside any subfolder.</p>
<p>It does its job and it is very clear how it works, but it is inefficient, take another way to write the algorithm:</p>
<p><code>int GetFolderSize(Folder folder)<br />
{<br />
&nbsp;Stack&lt;Folder&gt; stack = new Stack&lt;Folder&gt;();<br />
&nbsp;int sum = 0;<br />
&nbsp;stack.Push(folder);<br />
&nbsp;while(stack.Count > 0)<br />
&nbsp;{<br />
&nbsp;&nbsp;Folder folder = stack.Pop();<br />
&nbsp;&nbsp;foreach(File file in folder.files)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;sum += file.Size;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;foreach(Folder subFolder in folder.folders)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;stack.Push(subFolder);<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;return sum;<br />
}</code></p>
<p>This version is harder to understand. Basicly it maintains a stack of folder of which the total size of the files in it are not yet calculated. While going through each of the folders in the stack it adds new sub-folders from folders and removes the ones processed.</p>
<p>The latter method is <em>way</em> more efficient.</p>
<p>For each function function call in the first (recursive) version of the algoritm a new <code>sum</code>-instance, a <code>subFolder</code>-instance and an enumerator instance over the <code>folder.Folders</code> is required which stacks up. When 5 deep in the first algorithm you already use more than double of the amount of memory ever required in the second algorithm.</p>
<p>Additionally a function call itself requires more memory of its own, which depending on the language used, can be quite significant. Debugging also gets really hard when you have got a stack trace of hundreds of functions.</p>
<p>Using your own stack instead of poluting the call stack (the thing where function calls are kept) sound great. There is only one little problem, it can get pretty complex.</p>
<p>Take for instance a program that would put the files of folders and their subfolders in a file in that same folder, for instance for a playlist generater. This requires the algorithm to detect when all child items of a certain item have been processed. The problem with this is that the parent item is already gone from the stack when the child item is processed. This requires some additional fields and a few function pointers to get it to work and it can get a mess.</p>
<p>The best way to get it to work is to mimic what a normal recursive method would have done, which involves a lot of extra helper classes, which all depend on how the original recursive algorithm has worked. In a language with a Garbage Collector (which automaticly free&#8217;s unused classes) it is managable, but in a language without it like C(++) trouble doubles when you also need to monitor the lifetime of the helper classes.</p>
<p>I noticed that it has been very tempting to use recursion and that there are almost no occasions where something like your own stack is used, espacially in the complex cases. A shame, it is challenging <img src='http://blog.affien.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2005/07/01/function-recursion-overhead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enter The Unkown: Algorithm`s A programmer Has Got To Know</title>
		<link>http://blog.affien.com/archives/2005/03/16/enter-the-unkown-algorithms-a-programmer-has-got-to-know/</link>
		<comments>http://blog.affien.com/archives/2005/03/16/enter-the-unkown-algorithms-a-programmer-has-got-to-know/#comments</comments>
		<pubDate>Wed, 16 Mar 2005 20:51:16 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[kaja fumei]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2005/03/16/enter-the-unkown-algorithms-a-programmer-has-got-to-know/</guid>
		<description><![CDATA[I don&#8217;t like to link to other blogs or articles for I am just a lame copier, but I&#8217;d like to point my select few readers to Enter The Unkown, the weblog of Kaja Fumei, where he will point out some Algorithms a Programmer should know. First one in the list are the Hash Tables.
You [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t like to link to other blogs or articles for I am just a lame copier, but I&#8217;d like to point my select few readers to <a href="http://www.entertheunknown.net">Enter The Unkown</a>, the weblog of Kaja Fumei, where he will point out some <em>Algorithms a Programmer should know</em>. First one in the list are the <a href="http://www.entertheunknown.net/?p=6">Hash Tables</a>.</p>
<p>You don&#8217;t need to know the stuff behind these kind of features a language library exposes; but it helps a lot if you know how it works and what the weak and strong points are.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2005/03/16/enter-the-unkown-algorithms-a-programmer-has-got-to-know/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
