<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Javaero</title>
	<atom:link href="http://javaero.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://javaero.org</link>
	<description>Just another WordPress.com site</description>
	<lastBuildDate>Sun, 20 Nov 2011 14:39:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='javaero.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Javaero</title>
		<link>http://javaero.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://javaero.org/osd.xml" title="Javaero" />
	<atom:link rel='hub' href='http://javaero.org/?pushpress=hub'/>
		<item>
		<title>Divided We Compute, United We Reduce</title>
		<link>http://javaero.org/2011/11/20/divided-we-compute-united-we-reduce/</link>
		<comments>http://javaero.org/2011/11/20/divided-we-compute-united-we-reduce/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 14:39:39 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[High-Performance Computing]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[algorithms and complexity]]></category>
		<category><![CDATA[complexity]]></category>
		<category><![CDATA[complexity of parallel algorithms]]></category>
		<category><![CDATA[height of binary tree]]></category>
		<category><![CDATA[MapReduce algorithm]]></category>
		<category><![CDATA[merge sort]]></category>
		<category><![CDATA[multicore programming]]></category>
		<category><![CDATA[Newton's method]]></category>
		<category><![CDATA[nonlinear equation]]></category>
		<category><![CDATA[parallel computing]]></category>
		<category><![CDATA[parallel merge sort]]></category>
		<category><![CDATA[R for statistical computing]]></category>
		<category><![CDATA[running time]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1400</guid>
		<description><![CDATA[Once upon a time, in a far away village lay a dying old man. He called his sons to his deathbed and spoke to them one last time. He said &#8220;Sons, see that bundle of sticks? Each one of you try to break it. The one who can break it will inherit all my riches&#8221;. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1400&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Once upon a time, in a far away village lay a dying old man. He called his sons to his deathbed and spoke to them one last time. He said &#8220;Sons, see that bundle of sticks? Each one of you try to break it. The one who can break it will inherit all my riches&#8221;. Each son, being greedy, wanted all the riches for himself. So each one of them tried to break the bundle of sticks but none of them succeeded. The old man asked his servant to untie the bundle and said to his sons, &#8220;Each one of you now get one stick and break it&#8221;. Without any effort, each son was able to break the stick. The old man said &#8220;You see, when you unite, no task will be difficult. The riches that I told you was a lie. We are broke. When i&#8217;m dead, make sure  you unite so that you can survive.&#8221;</p>
<p>Fast forward to modern times. You can think of the bundle of sticks to be a complex problem that is itself composed of smaller problems. The sons are the processors of your computer. When each processor was given the task to solve the complex problem, it fails to solve it in a reasonable amount of time. When the complex problem is decomposed into smaller problems and given to each processor, each processor is now able to solve the smaller problems quickly thereby solving the big problem quickly as well.</p>
<p>The process of decomposing a problem into smaller problems and solving them in separate processors is called Parallel Computing. In this article, we will compute how fast a certain algorithm will run when parallelized. The problem we want to investigate is sorting an array of a million (<img src='http://s0.wp.com/latex.php?latex=2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2^{20}' title='2^{20}' class='latex' />) integers. </p>
<p><strong>Efficient Sorting </strong></p>
<p>Suppose you have an array <img src='http://s0.wp.com/latex.php?latex=%5C%7B+a_1%2C+a_2%2C+a_3%2C+...%2C+a_n+%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{ a_1, a_2, a_3, ..., a_n &#92;}' title='&#92;{ a_1, a_2, a_3, ..., a_n &#92;}' class='latex' /> that you want to sort based on pairwise comparison.  The sorted array is just one of the many permutations of the array <img src='http://s0.wp.com/latex.php?latex=%5C%7Ba_1%2C+a_2%2C+a_3%2C%5Cldots%2C+a_n%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{a_1, a_2, a_3,&#92;ldots, a_n&#92;}' title='&#92;{a_1, a_2, a_3,&#92;ldots, a_n&#92;}' class='latex' />.  In fact, if you have <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> different objects to sort, then there are exactly <img src='http://s0.wp.com/latex.php?latex=n%21&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n!' title='n!' class='latex' /> ways to arrange these objects, and one of them is the sorted state. You can imagine the sorting process as a decision tree. Say, for example we have array A={ a,b,c }. To sort this, we first compare a with b and there are 2 ways this can go. Either <img src='http://s0.wp.com/latex.php?latex=a+%5Cle+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a &#92;le b' title='a &#92;le b' class='latex' /> or <img src='http://s0.wp.com/latex.php?latex=a+%3E+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a &gt; b' title='a &gt; b' class='latex' />. If <img src='http://s0.wp.com/latex.php?latex=a+%5Cle+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a &#92;le b' title='a &#92;le b' class='latex' />, we then compare b and c. This also give either <img src='http://s0.wp.com/latex.php?latex=b+%5Cle+c&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b &#92;le c' title='b &#92;le c' class='latex' /> or <img src='http://s0.wp.com/latex.php?latex=b+%3E+c&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b &gt; c' title='b &gt; c' class='latex' />. As you can see from the diagram below, this is nothing but a decision tree.  </p>
<p>
<a href="http://javaero.files.wordpress.com/2011/11/sorting_decision_tree.jpg"><img src="http://javaero.files.wordpress.com/2011/11/sorting_decision_tree.jpg?w=300&#038;h=287" alt="" title="sorting_decision_tree" width="300" height="287" class="aligncenter size-medium wp-image-1429" /></a><br />
</br></p>
<p>Since the height of this binary tree is lg(n!), then we have</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Clg%28n%21%29+%3D+%5Clg%5CBig%5B+n+%5Ccdot+%28n+-+1%29+%5Ccdot+%28n-2%29+%5Ccdots+1%5CBig%5D+%5Cle+%5Clg+n+%2B+%5Clg+%28n-1%29+%5Ccdots+%5Clg1+%5Cle+%5Cunderbrace%7B%5Clg+n+%5Ccdots+%5Clg+n%7D_%5Ctext%7B+n+times%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;lg(n!) = &#92;lg&#92;Big[ n &#92;cdot (n - 1) &#92;cdot (n-2) &#92;cdots 1&#92;Big] &#92;le &#92;lg n + &#92;lg (n-1) &#92;cdots &#92;lg1 &#92;le &#92;underbrace{&#92;lg n &#92;cdots &#92;lg n}_&#92;text{ n times} ' title='&#92;lg(n!) = &#92;lg&#92;Big[ n &#92;cdot (n - 1) &#92;cdot (n-2) &#92;cdots 1&#92;Big] &#92;le &#92;lg n + &#92;lg (n-1) &#92;cdots &#92;lg1 &#92;le &#92;underbrace{&#92;lg n &#92;cdots &#92;lg n}_&#92;text{ n times} ' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Clg%28n%21%29+%5Cle+n%5Ccdot+%5Clg+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;lg(n!) &#92;le n&#92;cdot &#92;lg n' title='&#92;lg(n!) &#92;le n&#92;cdot &#92;lg n' class='latex' /></p>
<p>There are efficient algorithms that are able to sort of this complexity. For example, the merge sort has this complexity. Therefore, if you have an array of <img src='http://s0.wp.com/latex.php?latex=2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2^{20}' title='2^{20}' class='latex' /> elements, then the complexity is</p>
<p><img src='http://s0.wp.com/latex.php?latex=2%5E%7B20%7D+%5Ccdot+%5Clg%282%5E%7B20%7D%29+%3D+2%5E%7B20%7D+%5Ccdot+%2820%29+%3D+20971520+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2^{20} &#92;cdot &#92;lg(2^{20}) = 2^{20} &#92;cdot (20) = 20971520 ' title='2^{20} &#92;cdot &#92;lg(2^{20}) = 2^{20} &#92;cdot (20) = 20971520 ' class='latex' /></p>
<p>that is, it takes about 20 million comparisons to sort an array of 1 million.  Could we do any better than this? We can either upgrade the cpu of the machine doing the sorting or use two or more machines to divide the work among those machines. In this article, we are going to investigate the impact of dividing the work into smaller chunks and farming it to other processors.</p>
<p><strong>Divide and Conquer</strong></p>
<p>Assume we have an array <img src='http://s0.wp.com/latex.php?latex=n%3D2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n=2^{20}' title='n=2^{20}' class='latex' /> elements that we need to sort and suppose we have two identical processors we can use.  Divide the array into 2 equal sized arrays. Give the first array to the first processor and the other half to the second processor. Apply an efficient sorting algorithm to the subarrays to produce a sorted array for each processor. We then combine the result of processor 1 and processor 2 to one big array by merging the two sorted arrays. The diagram below illustrates the process of computation:</p>
<p>
<a href="http://javaero.files.wordpress.com/2011/11/parallel_sorting_merging.jpg"><img src="http://javaero.files.wordpress.com/2011/11/parallel_sorting_merging.jpg?w=300&#038;h=250" alt="" title="parallel_sorting_merging" width="300" height="250" class="aligncenter size-medium wp-image-1431" /></a><br />
</br></p>
<p>This is also known as the <strong>MapReduce</strong> algorithm. <em>Mapping</em> is the process of assigning subsets of the input data to processors where each processor computes the partial result. <em>Reducing</em> is the process of aggregating the results of each processor to the final solution of the problem.</p>
<p>The process of merging is straightforward. Given two sorted arrays, begin by comparing the first element of each array. The smaller of the two will then occupy the first slot in the big array. The second element of the array from which we took the smallest element will now become the first element of that array.  Repeat the process until all elements of both arrays have already occupied slots in the big array. The diagram below illustrates the algorithm of merging. </p>
<p>
<a href="http://javaero.files.wordpress.com/2011/11/merge_sort.jpg"><img src="http://javaero.files.wordpress.com/2011/11/merge_sort.jpg?w=300&#038;h=216" alt="" title="merge_sort" width="300" height="216" class="aligncenter size-medium wp-image-1430" /></a><br />
</br><br />
If you count the total number of comparisons that you need to merge two sorted arrays, you will find that it takes <img src='http://s0.wp.com/latex.php?latex=n-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n-1' title='n-1' class='latex' /> comparisons. Therefore, the complexity of the merging process is <img src='http://s0.wp.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' />.</p>
<p>Since each processor has <img src='http://s0.wp.com/latex.php?latex=n%2F2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n/2' title='n/2' class='latex' /> sized subarrays, the sorting complexity is therefore <img src='http://s0.wp.com/latex.php?latex=n%2Fp+%5Clg+%28n%2Fp%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n/p &#92;lg (n/p)' title='n/p &#92;lg (n/p)' class='latex' />. Furthermore, since the  merging process takes <img src='http://s0.wp.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' /> comparisons, the total complexity of the parallel sorting process is therefore</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+n%2Fp+%5Clg%28n%2Fp%29+%2B+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle n/p &#92;lg(n/p) + n' title='&#92;displaystyle n/p &#92;lg(n/p) + n' class='latex' /></p>
<p>In our example, <img src='http://s0.wp.com/latex.php?latex=C%3D2%5E%7B20%7D%2F2+%5Clg%282%5E%7B20%7D%2F2%29+%2B+2%5E%7B20%7D%3D++11010048&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='C=2^{20}/2 &#92;lg(2^{20}/2) + 2^{20}=  11010048' title='C=2^{20}/2 &#92;lg(2^{20}/2) + 2^{20}=  11010048' class='latex' /> comparisons compared to <img src='http://s0.wp.com/latex.php?latex=2%5E%7B20%7D+%5Clg%282%5E%7B20%7D%29+%3D+20971520&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2^{20} &#92;lg(2^{20}) = 20971520' title='2^{20} &#92;lg(2^{20}) = 20971520' class='latex' /> when run sequentially. For large values of <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=n%2Fp+%5Clg%28n%2Fp%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n/p &#92;lg(n/p)' title='n/p &#92;lg(n/p)' class='latex' /> dominates <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />, therefore the complexity of the parallel algorithm is <img src='http://s0.wp.com/latex.php?latex=O%28n%2Fp+%5Clg%28n%2Fp%29%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n/p &#92;lg(n/p))' title='O(n/p &#92;lg(n/p))' class='latex' />. </p>
<p>Can we do any better?</p>
<p>For a given value of <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />, what do you think is the value of <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> that reduces the running time to <img src='http://s0.wp.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' />? If we take <img src='http://s0.wp.com/latex.php?latex=n%3D2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n=2^{20}' title='n=2^{20}' class='latex' /> and plot complexity against <img src='http://s0.wp.com/latex.php?latex=p+%3D+%5C%7B+2%2C+4%2C+8%2C+16%2C+32%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p = &#92;{ 2, 4, 8, 16, 32&#92;}' title='p = &#92;{ 2, 4, 8, 16, 32&#92;}' class='latex' /> we get the diagram below.</p>
<p>
<a href="http://javaero.files.wordpress.com/2011/11/parallel_algo_complexity.png"><img src="http://javaero.files.wordpress.com/2011/11/parallel_algo_complexity.png?w=300&#038;h=300" alt="" title="parallel_algo_complexity" width="300" height="300" class="aligncenter size-medium wp-image-1432" /></a><br />
</br></p>
<p>In this diagram, we also plotted the horizontal line <img src='http://s0.wp.com/latex.php?latex=y%3D2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y=2^{20}' title='y=2^{20}' class='latex' />. The intersection of this line with the plot of <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28p%29+%3D+%5Cfrac%7Bn%7D%7Bp%7D+%5Clg%28%5Cfrac%7Bn%7D%7Bp%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle f(p) = &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p})' title='&#92;displaystyle f(p) = &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p})' class='latex' /> gives us the value of <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> such that the total comparisons is already linear, that is,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28+p+%29+%3D+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle f( p ) = n' title='&#92;displaystyle f( p ) = n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bn%7D%7Bp%7D+%5Clg%28%5Cfrac%7Bn%7D%7Bp%7D%29++%3D+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p})  = n' title='&#92;displaystyle &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p})  = n' class='latex' /></p>
<p>To get the value of <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> numerically, we have to solve the root of the equation</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g%28+p+%29+%3D+%5Cfrac%7Bn%7D%7Bp%7D+%5Clg%28%5Cfrac%7Bn%7D%7Bp%7D%29+-+n+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle g( p ) = &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p}) - n = 0' title='&#92;displaystyle g( p ) = &#92;frac{n}{p} &#92;lg(&#92;frac{n}{p}) - n = 0' class='latex' /></p>
<p>Simplifying,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7B1%7D%7Bp%7D+%5Clg%28%5Cfrac%7Bn%7D%7Bp%7D%29+-+1+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;frac{1}{p} &#92;lg(&#92;frac{n}{p}) - 1 = 0' title='&#92;displaystyle &#92;frac{1}{p} &#92;lg(&#92;frac{n}{p}) - 1 = 0' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Clg%28%5Cfrac%7Bn%7D%7Bp%7D%29+%3D+p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;lg(&#92;frac{n}{p}) = p' title='&#92;displaystyle &#92;lg(&#92;frac{n}{p}) = p' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bn%7D%7Bp%7D+%3D+2%5Ep&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;frac{n}{p} = 2^p' title='&#92;displaystyle &#92;frac{n}{p} = 2^p' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p2%5Ep+-+n+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle p2^p - n = 0' title='&#92;displaystyle p2^p - n = 0' class='latex' /></p>
<p>Since this is a non-linear equation, we can solve this using the Newton&#039;s method. It is a method to compute the roots by approximation given an initial value of the solution. Starting from a guess solution <img src='http://s0.wp.com/latex.php?latex=p_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p_1' title='p_1' class='latex' />, the root can be approximated using the recursive formula</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p_%7Bn%2B1%7D+%3D+p_n+-+%5Cfrac%7Bg%28+p_n%29%7D%7Bg%5Cprime+%28+p_n%29%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle p_{n+1} = p_n - &#92;frac{g( p_n)}{g&#92;prime ( p_n)}' title='&#92;displaystyle p_{n+1} = p_n - &#92;frac{g( p_n)}{g&#92;prime ( p_n)}' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=g%5Cprime+%28+p+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g&#92;prime ( p )' title='g&#92;prime ( p )' class='latex' /> is the first derivative of <img src='http://s0.wp.com/latex.php?latex=g%28+p+%29+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g( p ) ' title='g( p ) ' class='latex' />. Applying the rules of derivatives, we get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g%5Cprime+%28+p+%29+%3D+p%5Ccdot+2%5Ep+%5Cln+2+%2B+2%5Ep&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle g&#92;prime ( p ) = p&#92;cdot 2^p &#92;ln 2 + 2^p' title='&#92;displaystyle g&#92;prime ( p ) = p&#92;cdot 2^p &#92;ln 2 + 2^p' class='latex' /></p>
<p>Substituting this to the formula for Newton&#039;s method, we get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+p_%7Bn%2B1%7D+%3D+p_n+-+%5Cfrac%7Bp2%5Ep+-+n%7D%7Bp2%5Ep+%5Cln+2+-+2%5Ep%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle p_{n+1} = p_n - &#92;frac{p2^p - n}{p2^p &#92;ln 2 - 2^p}' title='&#92;displaystyle p_{n+1} = p_n - &#92;frac{p2^p - n}{p2^p &#92;ln 2 - 2^p}' class='latex' /></p>
<p>Below is an R code using newton&#8217;s method to compute the root of the equation <img src='http://s0.wp.com/latex.php?latex=g%28p%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g(p)' title='g(p)' class='latex' />.</p>
<p><pre class="brush: r;">
g=function(n,p){
	p* 2^p - n
}

gprime=function(n,p){
	p*2^p *log(2) - 2^p
}

newton=function(p,n,iter){
	tmp = p
	for(i in 1:iter){
		p=p-g(n,p)/gprime(n,p)
		
		if(abs(p-tmp)&lt; 0.0001){
			break		
		}

		print(p)
		tmp=p
	}
	print(p)
}

</pre></p>
<p>Running this code, we get the value of <img src='http://s0.wp.com/latex.php?latex=p+%3D+16&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p = 16' title='p = 16' class='latex' />: </p>
<p><pre class="brush: r;">
&gt; newton(15,n,100)
[1] 16.80905
[1] 16.08829
[1] 15.98603
[1] 16.00286
[1] 15.99944
[1] 16.00011
[1] 15.99998
[1] 16
</pre></p>
<p>Ignoring network latency, by distributing the input evenly into 16 processors, we get a running time of <img src='http://s0.wp.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' /> time complexity for <img src='http://s0.wp.com/latex.php?latex=n%3D2%5E%7B20%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n=2^{20}' title='n=2^{20}' class='latex' /> array of items. Therefore, instead of doing 20 million comparisons, you only need 1 million comparisons to sort 1 million objects.</p>
<p>In this age of multicore processors, parallel computing is fast becoming the norm than the exception. Learning to harness the power of multicores is becoming an extremely handy skill to have.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1400/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1400&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/11/20/divided-we-compute-united-we-reduce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/11/sorting_decision_tree.jpg?w=300" medium="image">
			<media:title type="html">sorting_decision_tree</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/11/parallel_sorting_merging.jpg?w=300" medium="image">
			<media:title type="html">parallel_sorting_merging</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/11/merge_sort.jpg?w=300" medium="image">
			<media:title type="html">merge_sort</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/11/parallel_algo_complexity.png?w=300" medium="image">
			<media:title type="html">parallel_algo_complexity</media:title>
		</media:content>
	</item>
		<item>
		<title>Divine Comedy*</title>
		<link>http://javaero.org/2011/04/10/divine-comedy/</link>
		<comments>http://javaero.org/2011/04/10/divine-comedy/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 15:38:45 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[concurrent algorithms]]></category>
		<category><![CDATA[correctness of concurrent algorithms]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[Dining Philosophers Problem]]></category>
		<category><![CDATA[mutual exclusion]]></category>
		<category><![CDATA[semaphore invariants]]></category>
		<category><![CDATA[semaphores]]></category>
		<category><![CDATA[starvation]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1321</guid>
		<description><![CDATA[Last night I had a dream. In my dream, an angel showed me heaven and hell. The first place we went to was a large hall filled with round tables. Around each table were 5 souls. All of them wearing white. The place was quiet and the only sound you can hear are the noise [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1321&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last night I had a dream. In my dream, an angel showed me heaven and hell. The first place we went to was a large hall filled with round tables. Around each table were 5 souls. All of them wearing white. The place was quiet and the only sound you can hear are the noise of the chopsticks as each soul eats the spaghetti  from his or her plate. The spaghetti was unlimited and placed in a big bowl at the center of each table. The chopsticks were placed in such a way that there is one stick between each plate. No soul can eat unless he or she has taken his or her left and right chopsticks. If a soul is not eating, he or she spends this time thinking. </p>
<p>I asked the angel &#8220;What is this place?&#8221; The angel said &#8220;This is hell.&#8221; I asked the angel &#8220;But why is this hell? This seems to be a very peaceful place with people doing nothing but eat and think.&#8221; Then the angel pointed me to one table and said &#8220;What do you see?&#8221;. I looked at the table and I saw souls starving but never dying.  As I looked closer, I noticed that all of them were able to pick their right chopsticks but not the left chopsticks. Suddenly I realized that many tables have souls starving forever.</p>
<p>I was about to ask the angel why they starved when the angel suddenly showed me another place. People were also wearing white and they also have a bowl of spaghetti at the center of each table. Two things were noticeably different from hell. Each table was inside its own room and there was a waiting area per room that can accommodate 4 souls. There were 5 seats per table, but the maximum number of souls per table was only 4. I observed the entire hall and I did not see a single soul starved. Everyone had a happy look on their face and were either eating or thinking. I asked the angel &#8220;What is this place?&#8221; The angel said &#8220;This is heaven. You can see that there are a maximum of 4 souls per table. A soul that is not seated at the table is in the waiting area.&#8221; </p>
<p>Suddenly, I woke up from my dream without any idea of what it means. What could the dream mean? I spent the whole day thinking about it.</p>
<p><strong>Interpretation of the Dream</strong></p>
<p>The chopsticks used by the souls are <em>binary semaphores</em> since no two of them can grab the same stick at any time.  To model the behavior of the souls in hell, we need an array of 5 semaphores (chopsticks) and the following algorithm:</p>
<p><pre class="brush: r;">
#loop forever
think()
wait(left chopstick)
wait(right chopstick)
eat()
signal(left chopstick)
signal(right chopstick)
</pre></p>
<p>The figure below shows a round table with 5 plates. The chopsticks are colored green and placed in between the plates. For a soul to eat, he/she should have both chopsticks first.</p>
<p>
<a href="http://javaero.files.wordpress.com/2011/04/dining_philosophers_starvation.jpg"><img src="http://javaero.files.wordpress.com/2011/04/dining_philosophers_starvation.jpg" alt="" title="dining_philosophers_starvation" width="436" height="451" class="aligncenter size-full wp-image-1343" /></a><br />
</p>
<p>Since the chopsticks are semaphores, then no two souls can be holding the same chopstick at the same time. To see this, let&#8217;s make use of the following properties from the previous <a href="http://javaero.org/2011/04/03/semaphorically-speaking/">post</a>:</p>
<p>1. <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5C%23CS+%3D+%5C%23wait+-+%5C%23signal+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;#CS = &#92;#wait - &#92;#signal ' title='&#92;displaystyle &#92;#CS = &#92;#wait - &#92;#signal ' class='latex' />. For chopstick i, the number of souls holding this chopstick at any time is equal to <img src='http://s0.wp.com/latex.php?latex=%5C%23CS&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS' title='&#92;#CS' class='latex' />.<br />
2. <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%3D+1+-+%5C%23CS&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V = 1 - &#92;#CS' title='&#92;displaystyle S.V = 1 - &#92;#CS' class='latex' />. Using this property to compute <img src='http://s0.wp.com/latex.php?latex=%5C%23CS&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS' title='&#92;#CS' class='latex' />, we get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5C%23CS+%3D+1+-+S.V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;#CS = 1 - S.V' title='&#92;displaystyle &#92;#CS = 1 - S.V' class='latex' /></p>
<p>Since <img src='http://s0.wp.com/latex.php?latex=S.V+%5Cge+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S.V &#92;ge 0' title='S.V &#92;ge 0' class='latex' />, then <img src='http://s0.wp.com/latex.php?latex=%5C%23CS+%5Cle+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS &#92;le 1' title='&#92;#CS &#92;le 1' class='latex' />, that is, at most one soul is holding the chopstick i at any given time.</p>
<blockquote><p>The reason why the souls got starved is because it is possible that all of the souls grabbed their right forks at the same time leaving no single left fork to use.</p></blockquote>
<p>The setup in heaven is subtly different. Instead of having 5 souls eating on the table at the same time, at least one of them waits for his turn in the waiting area (which is guarded by a room semaphore). To model this, we need an array of 5 semaphores (chopsticks) and a room semaphore initialized to 4. The following is the algorithm used by the souls in heaven:</p>
<p><pre class="brush: r;">
# loop forever
think()
wait(room)
wait(left chopstick)
wait(right chopstick)
eat()
signal(left chopstick)
signal(right chopstick)
signal(room)

</pre></p>
<p>The diagram below shows a round table inside a room. The plates and chopsticks are arranged as before, but at least one seat is empty as indicated by the red plate corresponding to it. Any soul that is not inside the room is in the waiting area as shown by the stick figure.</p>
<p>
<a href="http://javaero.files.wordpress.com/2011/04/dining_philosophers_solution.jpg"><img src="http://javaero.files.wordpress.com/2011/04/dining_philosophers_solution.jpg" alt="" title="dining_philosophers_solution" width="590" height="688" class="aligncenter size-full wp-image-1342" /></a><br />
<br />
Initially, all the souls are in the waiting area and the door to the room is guarded by the room semaphore. Since the initial value of the room semaphore is 4, at most 4 souls can enter the room at a time. Inside the room, the souls take their seats. Since at most 4 souls can be in the room, at least one seat is not taken. No soul is ever left starved in this system. Otherwise, a soul[i] can be blocked in one of three cases:</p>
<p>1. Blocked on his/her left chopstick. This means there is a soul[i-1] to his/her left that is using chopstick[i] as his/her right chopstick. By assumption of progress, this means that soul[i-1] will eventually execute signal(right chopstick). Hence, soul[i] will be able to pickup his/her left chopstick.<br />
2. Blocked on his/her right chopstick. Soul[i] is not able to pick up his/her right chopstick because soul[i+1] is using this chopstick as his/her left chopstick and will never release it until he/she has eaten. But the only way for soul[i+1] to eat is if he/she is able to pick up the right chopstick which, by induction, is also being used by another soul to the right of soul[i+1]. However, since there is at least one seat that is vacant, there is a right chopstick that is not being used by any soul and hence by progress, at least one soul can eat and eventually soul[i] can eat.<br />
3. Blocked from entering the room. If we use a first come first served policy, eventually, all souls can enter the room.</p>
<p>By using an extra locking mechanism, the souls in heaven are infinitely happier than the ones in hell.</p>
<p>* <em>Disclaimer: This is a work of fiction and should not be taken literally aside from it&#8217;s algorithmic content. In concurrency literature, this is called the Dining Philosophers problem. For more information, please consult the book “Principles of Concurrent and Distributed Programming” by M. Ben-Ari. </em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1321/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1321&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/04/10/divine-comedy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/04/dining_philosophers_starvation.jpg" medium="image">
			<media:title type="html">dining_philosophers_starvation</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/04/dining_philosophers_solution.jpg" medium="image">
			<media:title type="html">dining_philosophers_solution</media:title>
		</media:content>
	</item>
		<item>
		<title>Semaphorically Speaking</title>
		<link>http://javaero.org/2011/04/03/semaphorically-speaking/</link>
		<comments>http://javaero.org/2011/04/03/semaphorically-speaking/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 06:45:04 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[concurrent algorithms]]></category>
		<category><![CDATA[correctness of concurrent algorithms]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[edsger dijkstra]]></category>
		<category><![CDATA[mutual exclusion]]></category>
		<category><![CDATA[semaphore]]></category>
		<category><![CDATA[semaphore invariants]]></category>
		<category><![CDATA[starvation]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1277</guid>
		<description><![CDATA[When I was child, our family lived about 60 kilometers away from the city. Once or twice a month we go to the city which took about an hour and a half one way. On the way to the city, we have to pass a stretch of very narrow road that was at the side [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1277&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When I was child, our family lived about 60 kilometers away from the city. Once or twice a month we go to the city which took about an hour and a half one way. On the way to the city, we have to pass a stretch of very narrow road that was at the side of a very deep cliff. Many people have already lost their lives in that cliff when the bus they rode plunged into the cliff. In order to prevent this, a mechanism was setup that allowed only one bus at a time to enter that section of road, whether going one way or the other. In order for each end of the road to determine if a bus is traversing at any moment, they used a telephone to signal the other party so that the other buses can wait before taking their turn. It was always a frightening experience each time we pass that road. In computer science, we can view the dangerous narrow section of road as a <em>critical section</em> and the buses traversing this road as the <em>processes</em>. To guarantee mutual exclusion, the telephone system that signals each end of the road is called a <em><strong>semaphore</strong></em>.</p>
<p>We see semaphores everyday. The traffic light is a semaphore that protects the vehicles from bumping each other in that critical section of the road called the intersection. Semaphores are also used in the navy. Below is an example of a semaphore used in the US Navy.</p>
<p>
<a href="http://javaero.files.wordpress.com/2011/04/semphore_navy2.jpg"><img src="http://javaero.files.wordpress.com/2011/04/semphore_navy2.jpg" alt="" title="Semphore_Navy2" width="800" height="571" class="aligncenter size-full wp-image-1280" /></a><br />
 </p>
<p>In the last <a href="http://javaero.org/2011/03/15/a-mutually-beneficial-arrangement/">post</a>, we talked about concurrency and an algorithm to ensure correctness of concurrent algorithms. In this post, we will present another mechanism to ensure correctness of concurrent programs. That mechanism is called a semaphore and was first proposed by <a href="http://en.wikipedia.org/wiki/Edsger_W._Dijkstra">Edsger Dijkstra</a>. A semaphore* is a data structure S that is composed of an integer V and a set L and being modified by two atomic functions. The first function is called <code>wait</code> and is defined as follows:</p>
<p><pre class="brush: plain;">
wait(S)
     if S.V &gt; 0
        S.V &lt;- S.V -1
     else
        S.L &lt;- S.L union p
        p.state &lt;- blocked
</pre></p>
<p>The <code>wait</code> function is called by a process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' />  on a semaphore before it enters the critical section. If the value of S.V is not zero, S.V is decremented by 1 and the process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is allowed to continue its execution. Otherwise, the process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is blocked and is put on the set S.L.</p>
<p>The <code>signal</code> function is defined as</p>
<p><pre class="brush: plain;">
signal(S)
    if S.L = empty set
       S.V &lt;- S.V + 1
    else 
       select a process q in S.L
       S.L &lt;- S.L - {q}
       q.state &lt;- ready

</pre></p>
<p>When a process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is done in its critical section, it will call the <code>signal</code> operation on the semaphore S. If the set S.L has no blocked processes, it will increment the value of S.V by 1, otherwise a process <img src='http://s0.wp.com/latex.php?latex=q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='q' title='q' class='latex' /> is selected from the set S.L and its state is set to &#8220;ready&#8221;.</p>
<p>To use the semaphore construct, a typical concurrent program will be written like the one below:</p>
<p><pre class="brush: python;">
# loop forever
noncriticalSection()
wait(S)
criticalSection()
signal(S)
</pre></p>
<p><strong>Simple Properties of Semaphores</strong></p>
<p>In this section, we list some simple properties of semaphores that we can use in proving correctness. By properties, we mean those mathematical properties that remain invariant under whatever state of the computation. </p>
<p>1.  From the definition of the <code>wait</code> and <code>signal</code> functions, we can see that the value of S.V never goes negative, that is</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%5Cge+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V &#92;ge 0' title='&#92;displaystyle S.V &#92;ge 0' class='latex' /></p></blockquote>
<p>2. If initially <img src='http://s0.wp.com/latex.php?latex=S.V+%3D+k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S.V = k' title='S.V = k' class='latex' />, then we can compute the subsequent values of S.V by counting the number of calls to <code>wait</code> and <code>signal</code> (with some qualifications). If <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='k' title='k' class='latex' /> processes call the <code>wait</code> function, then the value of S.V goes down to 0. If all of these processes call the <code>signal</code> function, then the value of S.V goes back to k. If however, a process calls <code>wait</code> and the value of S.V is already 0, then S.V will just remain zero and the process is blocked. We say that the call to <code>wait</code> has failed an we will not count this call. If another process calls on <code>signal</code> and seeing that S.L is not empty, the S.V value will remain the same. This call to <code>signal</code> is also not counted. Having laid down the rules for counting the calls to <code>wait</code> and <code>signal</code>, we can see that </p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%3D+k+-+%5C%23wait+%2B+%5C%23signal&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V = k - &#92;#wait + &#92;#signal' title='&#92;displaystyle S.V = k - &#92;#wait + &#92;#signal' class='latex' /></p></blockquote>
<p>where <img src='http://s0.wp.com/latex.php?latex=%5C%23wait&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#wait' title='&#92;#wait' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5C%23signal&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#signal' title='&#92;#signal' class='latex' /> are the number of calls to <code>wait</code> and <code>signal</code>, respectively.</p>
<p>3. At anytime, the number of processes executing the critical section is found by counting the number of processes entering the critical section by a successful call to <code>wait</code>, minus the number of processes leaving the critical section by a successful call to <code>signal</code>:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5C%23CS+%3D+%5C%23wait+-+%5C%23signal&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;#CS = &#92;#wait - &#92;#signal' title='&#92;displaystyle &#92;#CS = &#92;#wait - &#92;#signal' class='latex' /></p></blockquote>
<p><strong>Correctness of the Semaphore Solution for Two Processes</strong></p>
<p>As we have discussed in the last post, to prove correctness we need to show that the algorithm is mutually exclusive, free from deadlock and starvation. </p>
<p>1. <em>Mutually Exclusion</em> &#8211; Let S.V = 1 initially. At anytime during the execution, the number of processes in the critical section is given by property 3 above, that is, <img src='http://s0.wp.com/latex.php?latex=%5C%23CS+%3D+%5C%23wait+-+%5C%23signal&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS = &#92;#wait - &#92;#signal' title='&#92;#CS = &#92;#wait - &#92;#signal' class='latex' />. From property 2,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%3D+1+-+%5C%23CS&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V = 1 - &#92;#CS' title='&#92;displaystyle S.V = 1 - &#92;#CS' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%2B+%5C%23CS+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V + &#92;#CS = 1' title='&#92;displaystyle S.V + &#92;#CS = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5C%23CS+%3D+1+-+S.V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;#CS = 1 - S.V' title='&#92;displaystyle &#92;#CS = 1 - S.V' class='latex' /></p>
<p>Since by property 1 <img src='http://s0.wp.com/latex.php?latex=S.V+%5Cge+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S.V &#92;ge 0' title='S.V &#92;ge 0' class='latex' />, therefore <img src='http://s0.wp.com/latex.php?latex=%5C%23CS+%5Cle+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS &#92;le 1' title='&#92;#CS &#92;le 1' class='latex' />. That is, there can be at most one process in the critical section at anytime.</p>
<p>2. <em>Freedom from deadlock</em> &#8211; Two processes can deadlock if the value of S.V = 0 and neither are in the critical section (<img src='http://s0.wp.com/latex.php?latex=%5C%23CS+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS = 0' title='&#92;#CS = 0' class='latex' />). By property 2 and 3,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+S.V+%3D+1+-+%5C%23CS&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle S.V = 1 - &#92;#CS' title='&#92;displaystyle S.V = 1 - &#92;#CS' class='latex' /></p>
<p>Since S.V = 0 and <img src='http://s0.wp.com/latex.php?latex=%5C%23CS+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;#CS = 0' title='&#92;#CS = 0' class='latex' />, substituting this to the equation above gives us</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+1+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle 1 = 0' title='&#92;displaystyle 1 = 0' class='latex' />,</p>
<p>a contradiction</p>
<p>3. <em>Freedom from Starvation</em> &#8211; Suppose a process <img src='http://s0.wp.com/latex.php?latex=q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='q' title='q' class='latex' />, upon calling <code>wait</code>, sees that <img src='http://s0.wp.com/latex.php?latex=S.V+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S.V = 0' title='S.V = 0' class='latex' />, and is blocked to starvation in S.L. By property 2 and 3, </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5C%23CS+%3D+1+-+S.V+%3D+1+-+0+%3D+1+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;#CS = 1 - S.V = 1 - 0 = 1 ' title='&#92;displaystyle &#92;#CS = 1 - S.V = 1 - 0 = 1 ' class='latex' /></p>
<p>which means that the other process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is in the critical section. By assumption of progress, process <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> will eventually call <code>signal</code> and will pick one process from S.L to enter the critical section. Since there is only one process that is blocked, and that is <img src='http://s0.wp.com/latex.php?latex=q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='q' title='q' class='latex' />, process <img src='http://s0.wp.com/latex.php?latex=q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='q' title='q' class='latex' /> can then enter its critical section, contradicting the assumption that it is starved.</p>
<p>* For more information, consult the book &#8220;Principles of Concurrent and Distributed Programming&#8221; by M. Ben-Ari.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1277/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1277&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/04/03/semaphorically-speaking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/04/semphore_navy2.jpg" medium="image">
			<media:title type="html">Semphore_Navy2</media:title>
		</media:content>
	</item>
		<item>
		<title>A Mutually Beneficial Arrangement</title>
		<link>http://javaero.org/2011/03/15/a-mutually-beneficial-arrangement/</link>
		<comments>http://javaero.org/2011/03/15/a-mutually-beneficial-arrangement/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 14:55:16 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[mutual exclusion]]></category>
		<category><![CDATA[starvation]]></category>
		<category><![CDATA[state diagram]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1235</guid>
		<description><![CDATA[It&#8217;s been said that the programmers of today are the pizza delivery boys of tomorrow. This has happened the last time the world witnessed a severe financial crisis. Usually, those who are not able to cope up with the current trends in IT are the first ones to go. One trend that is here to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1235&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been said that the programmers of today are the pizza delivery boys of tomorrow. This has happened the last time the world witnessed a severe financial crisis. Usually, those who are not able to cope up with the current trends in IT are the first ones to go. One trend that is here to stay is the increasing number of cores of our computers. In order to maximize the power of multicore architecture, the programmers of today should be able to do concurrent programming correctly.  Concurrent programming is the art of using the extra CPU capacity of your computer by exploiting parallelism to speed up your applications. This is a non-trivial task and requires a different mindset. In these series of articles, we will explore what concurrent programming is and its mathematical basis.</p>
<p>Imagine a concurrent program composed of two threads trying to read or write a value into an array. Let&#8217;s suppose that we have an array of 10 numbers and 2 threads. Thread 1 writes a number to the next empty array location. If the array is full, thread 1 will not write anything but will wait until at least one location is empty. Thread 2 will read and delete the last number in the array. If the array is empty, thread 2 will not read anything. How do we ensure that thread 1 will not write past the array length? How do we ensure that thread 2 will not read an empty element?</p>
<p>The section of code that does the updating or reading of the array is called the <strong>critical section</strong>. This is the portion of the code where we need to ensure that only one thread is executing at a time. To do this, we employ a mechanism for the two threads to know whose turn it is to enter the critical section. Below is a code that tries to solve this problem. First we define a global variable that is shared by both threads. This variable called <code>turn</code> will specify which thread can enter the critical section. The value of turn is either 1 or 2. A value of 1 means that thread 1 can enter the critical section while a value of 2 means that thread 2 can enter the critical section.</p>
<p><pre class="brush: plain;">

global turn = 1
</pre></p>
<p>This code is to be executed by thread 1:</p>
<p><pre class="brush: plain;">
#loop forever
1 execute non-critical section
2 await turn = 1
3 execute critical section
4 turn = 2

</pre></p>
<p>The code to be executed by thread 2 is similar:</p>
<p><pre class="brush: plain;">
#loop forever
1 execute non-critical section
2 await turn = 2
3 execute critical section
4 turn = 1
</pre></p>
<p>The critical section has be coded in such a way that it takes a short time as possible. We shall assume that any thread that enters the critical section should exit from it eventually. This is called the <strong>progress assumption</strong>. </p>
<p>We can imagine the execution of this program by the two  threads to be described by a state specified by three numbers:</p>
<p>1. the pointer to the line that thread 1 is to execute.<br />
2. the pointer to the line that thread 2 is to execute.<br />
3. the value of the <code>turn</code> variable.</p>
<p>For example, the initial state is described by the triple (1,1,1). This means that thread 1 pointer is at line 1, thread 2 pointer is at line 1 and the value of the <code>turn</code> variable is 1. Let us first make it clear that when the pointer is at line 1, the thread has not yet executed that line. It only means that if the CPU scheduler will allow thread 1 to execute, it will execute line 1. For example, if the pointer of thread 1  is at line 4, it does not mean that the value of <code>turn = 2</code>. It only means that when thread 1 goes from state 4 to state 1, then the value of <code>turn</code> should have been set to 2.</p>
<p>We can draw the entire execution of the two threads using a state diagram as shown below:<br />
<br />
<a href="http://javaero.files.wordpress.com/2011/03/concurrent_algo1.gif"><img src="http://javaero.files.wordpress.com/2011/03/concurrent_algo1.gif" alt="" title="concurrent_algo1" width="410" height="668" class="aligncenter size-full wp-image-1244" /></a><br />
</p>
<p>Let&#8217;s try to understand this diagram. The execution begins with the state (1,1,1) which means  that thread 1 pointer is at 1, thread 2 pointer is at 1, and the value of <code>turn = 1</code>. This state can either go to (2,1,1) or (1,2,1) depending on which thread the CPU scheduler wants to run first. The state transitions are indicated by arrows. From the diagram, you will see that when the value of <code>turn = 1</code>, thread 2 cannot go past pointer 2. In the same way, when the value of<code> turn = 2</code>, thread 1 cannot go past pointer 2. </p>
<p><strong>Correctness Properties of Concurrent Programs</strong></p>
<p>Writing concurrent programs is not easy. You have to demonstrate that your program is correct. What does correct mean? A concurrent program is correct if:</p>
<p>1. <strong>Mutual exclusion holds</strong>. There is at most one thread that enters the critical section.<br />
2. <strong>No deadlock occurs</strong>. This means that no thread holds a resource that is needed by another thread, and vice versa. If this occurs, then each thread will be waiting for the other thread to release the resource and the application will appear to hang.<br />
3. <strong>No starvation occurs</strong>. If a thread wants to enter the critical section, then it should eventually be able to do so.</p>
<p>Is our algorithm correct? First, the algorithm satisfies mutual exclusion. There is no state in which both thread pointers are pointing to line 3, which is the critical section. This means you cannot see states (3,3,1) or (3,32). There is no deadlock. The two threads do not get stuck on it&#8217;s preprotocol. Suppose that the value of <code>turn = 1</code> and both threads execute line 2. Since the <code>turn = 1</code>, then thread 1 will enter its critical section. By the assumption of progress, it will exit its critical section and set <code>turn = 2</code>. This will then allow thread 2 to enter its critical section. Therefore, no thread gets stuck trying to enter its critical section.</p>
<p>There is however starvation. There is no assumption of progress on the non-critical section. This means that if turn = 1 and thread 1 will get stuck in its non-critical section, this will prevent thread 2 from entering its critical section. Therefore, the algorithm is not correct as it does not satisfy all conditions.</p>
<p>In the next post, we will examine a mechanism, called a semaphore, that is able to solve this problem.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1235/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1235&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/03/15/a-mutually-beneficial-arrangement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/03/concurrent_algo1.gif" medium="image">
			<media:title type="html">concurrent_algo1</media:title>
		</media:content>
	</item>
		<item>
		<title>Way Up High A Binary Tree</title>
		<link>http://javaero.org/2011/03/07/way-up-high-a-binary-tree/</link>
		<comments>http://javaero.org/2011/03/07/way-up-high-a-binary-tree/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 02:41:57 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[algorithms and complexity]]></category>
		<category><![CDATA[approximating fibonacci numbers]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[fibonacci numbers]]></category>
		<category><![CDATA[height of AVL tree]]></category>
		<category><![CDATA[logarithmic]]></category>
		<category><![CDATA[non homogeneous recurrence relation]]></category>
		<category><![CDATA[phi]]></category>
		<category><![CDATA[recurrence relation]]></category>
		<category><![CDATA[self-balancing binary tree]]></category>
		<category><![CDATA[solving recurrence relations]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1169</guid>
		<description><![CDATA[Way up high in the binary tree Two binary bits smiled at me I shook that tree as hard as I could Down came the binary bits, Mmmm&#8211;were they good! -based on a children&#8217;s song Now it turns out that this binary tree is an AVL tree. I wonder how high this tree is! Why [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1169&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>Way up high in the binary tree<br />
Two binary bits smiled at me<br />
I shook that tree as hard as I could<br />
Down came the binary bits,<br />
Mmmm&#8211;were they good!   -based on a children&#8217;s song
</p></blockquote>
<p>Now it turns out that this binary tree is an AVL tree. I wonder how high this tree is! Why does it concern us ? The reason is that the height will give us the worst case running time of the binary search tree algorithm. An AVL tree maintains its height by making sure the difference in height between the left and right subtree is at most 1. If <img src='http://s0.wp.com/latex.php?latex=N_h&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='N_h' title='N_h' class='latex' /> is the number of nodes contained in the tree of height h, then <img src='http://s0.wp.com/latex.php?latex=N_h&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='N_h' title='N_h' class='latex' /> is equal to the number of nodes in the left subtree plus the number of nodes in the right subtree PLUS 1 (the root node). In the worst case, the two subtrees differ in height by at most 1, therefore</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%3D+N_%7Bh-1%7D+%2B+N_%7Bh-2%7D+%2B+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h = N_{h-1} + N_{h-2} + 1' title='&#92;displaystyle N_h = N_{h-1} + N_{h-2} + 1' class='latex' /></p>
<p>with initial conditions <img src='http://s0.wp.com/latex.php?latex=N_0+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='N_0 = 0' title='N_0 = 0' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=N_1+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='N_1 = 1' title='N_1 = 1' class='latex' />.</p>
<p>By adding 1 to both sides of this recurrence relation we get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%2B+1+%3D+N_%7Bh-1%7D+%2B+1+%2B+N_%7Bh-2%7D+%2B+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h + 1 = N_{h-1} + 1 + N_{h-2} + 1' title='&#92;displaystyle N_h + 1 = N_{h-1} + 1 + N_{h-2} + 1' class='latex' /></p>
<p>If we let <img src='http://s0.wp.com/latex.php?latex=b_h+%3D+N_h+%2B+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_h = N_h + 1' title='b_h = N_h + 1' class='latex' />, we can rewrite the above recurrence relation as</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_h+%3D+b_%7Bh-1%7D+%2B+b_%7Bh-2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_h = b_{h-1} + b_{h-2}' title='&#92;displaystyle b_h = b_{h-1} + b_{h-2}' class='latex' /></p>
<p>The initial conditions are now</p>
<p><img src='http://s0.wp.com/latex.php?latex=b_0+%3D+N_0+%2B+1+%3D+0+%2B+1+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_0 = N_0 + 1 = 0 + 1 = 1' title='b_0 = N_0 + 1 = 0 + 1 = 1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=b_1+%3D+N_1+%2B+1+%3D+1+%2B+1+%3D+2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_1 = N_1 + 1 = 1 + 1 = 2' title='b_1 = N_1 + 1 = 1 + 1 = 2' class='latex' /></p>
<p>Now this is interesting! This recurrence relation is very similar to the Fibonacci recurrence relation except that the initial conditions are different. From the previous <a href="http://javaero.org/2011/02/28/mathematical-beauty/">post</a>, we found that the general solution of this recurrence relation is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+%5Calpha_1+%5Cphi%5En+%2B+%5Calpha_2+%281-%5Cphi%29%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = &#92;alpha_1 &#92;phi^n + &#92;alpha_2 (1-&#92;phi)^n' title='&#92;displaystyle b_n = &#92;alpha_1 &#92;phi^n + &#92;alpha_2 (1-&#92;phi)^n' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' /> are constants yet to be determined. Let us solve this recurrence relation in the general case where <img src='http://s0.wp.com/latex.php?latex=b_0+%3D+s&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_0 = s' title='b_0 = s' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=b_1%3Dt&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_1=t' title='b_1=t' class='latex' />. Computing the first 2 terms of the recurrence relation, we get two simultaneous equations in <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' />.</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_0+%3D+s+%3D+%5Calpha_1+%2B+%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_0 = s = &#92;alpha_1 + &#92;alpha_2' title='&#92;displaystyle b_0 = s = &#92;alpha_1 + &#92;alpha_2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_1+%3D+t+%3D+%5Calpha_1+%5Cphi+%2B+%5Calpha_2+%281-%5Cphi%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_1 = t = &#92;alpha_1 &#92;phi + &#92;alpha_2 (1-&#92;phi)' title='&#92;displaystyle b_1 = t = &#92;alpha_1 &#92;phi + &#92;alpha_2 (1-&#92;phi)' class='latex' /></p>
<p>Solving for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> from the first equation, we get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_1+%3D+s-+%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_1 = s- &#92;alpha_2' title='&#92;displaystyle &#92;alpha_1 = s- &#92;alpha_2' class='latex' /></p>
<p>Plugging this into the second equation and solving for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' /></p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+t+%3D+%28s-%5Calpha_2%29+%5Cphi+%2B+%5Calpha_2+-+%5Calpha_2%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle t = (s-&#92;alpha_2) &#92;phi + &#92;alpha_2 - &#92;alpha_2&#92;phi' title='&#92;displaystyle t = (s-&#92;alpha_2) &#92;phi + &#92;alpha_2 - &#92;alpha_2&#92;phi' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+t+%3D+s%5Cphi+%2B+%5Calpha_2%281-+2%5Cphi%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle t = s&#92;phi + &#92;alpha_2(1- 2&#92;phi)' title='&#92;displaystyle t = s&#92;phi + &#92;alpha_2(1- 2&#92;phi)' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_2+%3D+%5Cfrac%7Bt-s%5Cphi%7D%7B1-2%5Cphi%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_2 = &#92;frac{t-s&#92;phi}{1-2&#92;phi}' title='&#92;displaystyle &#92;alpha_2 = &#92;frac{t-s&#92;phi}{1-2&#92;phi}' class='latex' /></p>
<p>From which we solve for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /></p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_1+%3D+s+-+%5Cfrac%7Bt-s%5Cphi%7D%7B1-2%5Cphi%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_1 = s - &#92;frac{t-s&#92;phi}{1-2&#92;phi}' title='&#92;displaystyle &#92;alpha_1 = s - &#92;frac{t-s&#92;phi}{1-2&#92;phi}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Cfrac%7Bs%281-%5Cphi%29+-+t+%7D%7B+1-2%5Cphi%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;frac{s(1-&#92;phi) - t }{ 1-2&#92;phi}' title='&#92;displaystyle = &#92;frac{s(1-&#92;phi) - t }{ 1-2&#92;phi}' class='latex' /></p>
<p>Substituting this to the recurrence relation of <img src='http://s0.wp.com/latex.php?latex=b_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_n' title='b_n' class='latex' />, we have</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+%5Cfrac%7Bs%281-%5Cphi%29+-t+%7D%7B1-2%5Cphi%7D+%5Cphi%5En+%2B+%5Cfrac%7Bt+-+s%5Cphi%7D%7B1-2%5Cphi%7D+%281-%5Cphi%29%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = &#92;frac{s(1-&#92;phi) -t }{1-2&#92;phi} &#92;phi^n + &#92;frac{t - s&#92;phi}{1-2&#92;phi} (1-&#92;phi)^n' title='&#92;displaystyle b_n = &#92;frac{s(1-&#92;phi) -t }{1-2&#92;phi} &#92;phi^n + &#92;frac{t - s&#92;phi}{1-2&#92;phi} (1-&#92;phi)^n' class='latex' /></p>
<p>Expanding this expression you&#8217;ll end up with a rather complex expression involving cross terms of <img src='http://s0.wp.com/latex.php?latex=%5Cphi%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi^n' title='&#92;phi^n' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%281-%5Cphi%29%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(1-&#92;phi)^n' title='(1-&#92;phi)^n' class='latex' /></p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+%5Cfrac%7Bs%5Cphi%5En+%28+1-+%5Cphi%29+-+t%5Cphi%5En+%2B+t%281-%5Cphi%29%5En+-+s%5Cphi%281-%5Cphi%29%5En%7D%7B1-2%5Cphi%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = &#92;frac{s&#92;phi^n ( 1- &#92;phi) - t&#92;phi^n + t(1-&#92;phi)^n - s&#92;phi(1-&#92;phi)^n}{1-2&#92;phi}' title='&#92;displaystyle b_n = &#92;frac{s&#92;phi^n ( 1- &#92;phi) - t&#92;phi^n + t(1-&#92;phi)^n - s&#92;phi(1-&#92;phi)^n}{1-2&#92;phi}' class='latex' /></p>
<p>But that is where the complexity ends. Observe that</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cphi%281-%5Cphi%29+%3D+%5Cfrac%7B1%2B%5Csqrt%7B5%7D%7D%7B2%7D%5Ccdot+%5Cfrac%7B1-%5Csqrt%7B5%7D%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;phi(1-&#92;phi) = &#92;frac{1+&#92;sqrt{5}}{2}&#92;cdot &#92;frac{1-&#92;sqrt{5}}{2}' title='&#92;displaystyle &#92;phi(1-&#92;phi) = &#92;frac{1+&#92;sqrt{5}}{2}&#92;cdot &#92;frac{1-&#92;sqrt{5}}{2}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Cfrac%7B1-5%7D%7B4%7D+%3D+-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;frac{1-5}{4} = -1' title='&#92;displaystyle = &#92;frac{1-5}{4} = -1' class='latex' /></p>
<p>Using this result, we can write </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cphi%5E%7Bn%7D%281-%5Cphi%29+%3D+%5Cphi%5E%7Bn-1%7D%5Cphi+%281-%5Cphi%29+%3D+-%5Cphi%5E%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi^{n}(1-&#92;phi) = &#92;phi^{n-1}&#92;phi (1-&#92;phi) = -&#92;phi^{n-1}' title='&#92;phi^{n}(1-&#92;phi) = &#92;phi^{n-1}&#92;phi (1-&#92;phi) = -&#92;phi^{n-1}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cphi+%281-%5Cphi%29%5En+%3D+%5Cphi+%281-%5Cphi%29+%281-%5Cphi%29%5E%7Bn-1%7D+%3D+-+%281-%5Cphi%29%5E%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi (1-&#92;phi)^n = &#92;phi (1-&#92;phi) (1-&#92;phi)^{n-1} = - (1-&#92;phi)^{n-1}' title='&#92;phi (1-&#92;phi)^n = &#92;phi (1-&#92;phi) (1-&#92;phi)^{n-1} = - (1-&#92;phi)^{n-1}' class='latex' /></p>
<p>Furthermore, we also observe that</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+1-2%5Cphi+%3D+1+-+%5Cfrac%7B1%2B+%5Csqrt%7B5%7D%7D%7B2%7D+%3D+-%5Csqrt%7B5%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle 1-2&#92;phi = 1 - &#92;frac{1+ &#92;sqrt{5}}{2} = -&#92;sqrt{5}' title='&#92;displaystyle 1-2&#92;phi = 1 - &#92;frac{1+ &#92;sqrt{5}}{2} = -&#92;sqrt{5}' class='latex' /></p>
<p>Using all these results we have</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+-+s%5Cphi%5E%7Bn-1%7D+-+t%5Cphi%5En+%2B+t%281-%5Cphi%29%5En+%2B+s%281-%5Cphi%29%5E%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = - s&#92;phi^{n-1} - t&#92;phi^n + t(1-&#92;phi)^n + s(1-&#92;phi)^{n-1}' title='&#92;displaystyle b_n = - s&#92;phi^{n-1} - t&#92;phi^n + t(1-&#92;phi)^n + s(1-&#92;phi)^{n-1}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+s+%5CBig%5B+%5Cfrac%7B%28%5Cphi%5E%7Bn-1%7D+-+%281-%5Cphi%29%5E%7Bn-1%7D%7D%7B%5Csqrt%7B5%7D%7D+%2B%5CBig%5D++t+%5CBig%5B+%5Cfrac%7B%28%5Cphi%5En+-+%281-%5Cphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D%5CBig%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = s &#92;Big[ &#92;frac{(&#92;phi^{n-1} - (1-&#92;phi)^{n-1}}{&#92;sqrt{5}} +&#92;Big]  t &#92;Big[ &#92;frac{(&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}}&#92;Big]' title='&#92;displaystyle = s &#92;Big[ &#92;frac{(&#92;phi^{n-1} - (1-&#92;phi)^{n-1}}{&#92;sqrt{5}} +&#92;Big]  t &#92;Big[ &#92;frac{(&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}}&#92;Big]' class='latex' /></p>
<p>Notice that the expressions in square brackets are just fibonacci expressions for n-1 and n, respectively. If we use them in the expression for <img src='http://s0.wp.com/latex.php?latex=b_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_n' title='b_n' class='latex' />, we can express it in terms of the fibonacci numbers</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++b_n+%3D+sf_%7Bn-1%7D+%2B+tf_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle  b_n = sf_{n-1} + tf_n' title='&#92;displaystyle  b_n = sf_{n-1} + tf_n' class='latex' /></p></blockquote>
<p>Returning to our computation for the height of the AVL tree, since <img src='http://s0.wp.com/latex.php?latex=b_0+%3D+s+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_0 = s = 1' title='b_0 = s = 1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=b_1+%3D+t+%3D+2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_1 = t = 2' title='b_1 = t = 2' class='latex' />, we finally have</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+f_%7Bn-1%7D+%2B+2f_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = f_{n-1} + 2f_n' title='&#92;displaystyle b_n = f_{n-1} + 2f_n' class='latex' /></p>
<p>We can further simplify this by </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+b_n+%3D+f_%7Bn-1%7D+%2B+f_n+%2B+f_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle b_n = f_{n-1} + f_n + f_n' title='&#92;displaystyle b_n = f_{n-1} + f_n + f_n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5CBig%28f_%7Bn-1%7D+%2B+f_n%5CBig%29+%2B+f_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;Big(f_{n-1} + f_n&#92;Big) + f_n' title='&#92;displaystyle = &#92;Big(f_{n-1} + f_n&#92;Big) + f_n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+f_%7Bn%2B1%7D+%2B+f_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = f_{n+1} + f_n' title='&#92;displaystyle = f_{n+1} + f_n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+f_%7Bn%2B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = f_{n+2}' title='&#92;displaystyle = f_{n+2}' class='latex' /></p>
<p>Since <img src='http://s0.wp.com/latex.php?latex=b_n+%3D+N_h+%2B+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b_n = N_h + 1' title='b_n = N_h + 1' class='latex' />,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%2B+1+%3D+f_%7Bn%2B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h + 1 = f_{n+2}' title='&#92;displaystyle N_h + 1 = f_{n+2}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%3D+f_%7Bn%2B2%7D+-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h = f_{n+2} -1' title='&#92;displaystyle N_h = f_{n+2} -1' class='latex' /></p>
<p>The formula above expresses the number of nodes of the AVL tree in terms of the height h. In terms of the golden ratio, we can write the last expression as </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%3D+%5Cfrac%7B%5Cphi%5E%7Bn%2B2%7D+-+%281-%5Cphi%29%5E%7Bn%2B2%7D%7D%7B%5Csqrt%7B5%7D%7D+-+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h = &#92;frac{&#92;phi^{n+2} - (1-&#92;phi)^{n+2}}{&#92;sqrt{5}} - 1' title='&#92;displaystyle N_h = &#92;frac{&#92;phi^{n+2} - (1-&#92;phi)^{n+2}}{&#92;sqrt{5}} - 1' class='latex' /></p>
<p><b>Approximating the nth Fibonacci Number</b></p>
<p>We can further simplify the above expression by observing that the nth fibonacci number is approximately equal to <img src='http://s0.wp.com/latex.php?latex=%5Cphi%5En%2F%5Csqrt%7B5%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi^n/&#92;sqrt{5}' title='&#92;phi^n/&#92;sqrt{5}' class='latex' />. To see this, we compute the distance of <img src='http://s0.wp.com/latex.php?latex=f_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='f_n' title='f_n' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%5Cphi%5En%2F%5Csqrt%7B5%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi^n/&#92;sqrt{5}' title='&#92;phi^n/&#92;sqrt{5}' class='latex' />:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%7Cf_n+-+%5Cfrac%7B%5Cphi%5En%7D%7B%5Csqrt%7B5%7D%7D%7C+%3D+%7C%5Cfrac%7B%5Cphi%5En+-+%281-%5Cphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D+-+%5Cfrac%7B%5Cphi%5En%7D%7B%5Csqrt%7B5%7D%7D+%7C+%3D+%7C%5Cfrac%7B%281-%5Cphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D%7C+%3C+%5Cfrac%7B1%7D%7B%5Csqrt%7B5%7D%7D+%3C+%5Cfrac%7B1%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle |f_n - &#92;frac{&#92;phi^n}{&#92;sqrt{5}}| = |&#92;frac{&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}} - &#92;frac{&#92;phi^n}{&#92;sqrt{5}} | = |&#92;frac{(1-&#92;phi)^n}{&#92;sqrt{5}}| &lt; &#92;frac{1}{&#92;sqrt{5}} &lt; &#92;frac{1}{2}' title='&#92;displaystyle |f_n - &#92;frac{&#92;phi^n}{&#92;sqrt{5}}| = |&#92;frac{&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}} - &#92;frac{&#92;phi^n}{&#92;sqrt{5}} | = |&#92;frac{(1-&#92;phi)^n}{&#92;sqrt{5}}| &lt; &#92;frac{1}{&#92;sqrt{5}} &lt; &#92;frac{1}{2}' class='latex' /></p>
<p>Using this approximation, we can write </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+N_h+%3D+%5Cfrac%7B%5Cphi%5E%7Bh%2B2%7D%7D%7B%5Csqrt%7B5%7D%7D+-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle N_h = &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}} -1' title='&#92;displaystyle N_h = &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}} -1' class='latex' /></p>
<p>So if n is the number of nodes in an AVL tree, we can estimate the height using the above formula, that is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+n+%5Capprox+%5Cfrac%7B%5Cphi%5E%7Bh%2B2%7D%7D%7B%5Csqrt%7B5%7D%7D+-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle n &#92;approx &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}} -1' title='&#92;displaystyle n &#92;approx &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}} -1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+n+%2B+1+%5Capprox+%5Cfrac%7B%5Cphi%5E%7Bh%2B2%7D%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle n + 1 &#92;approx &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}}' title='&#92;displaystyle n + 1 &#92;approx &#92;frac{&#92;phi^{h+2}}{&#92;sqrt{5}}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csqrt%7B5%7D%28n%2B1%29+%5Capprox+%5Cphi%5E%7Bh%2B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;sqrt{5}(n+1) &#92;approx &#92;phi^{h+2}' title='&#92;displaystyle &#92;sqrt{5}(n+1) &#92;approx &#92;phi^{h+2}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+h+%5Capprox+%5Clog_%7B%5Cphi%7D+%5CBig%28+%5Csqrt%7B5%7D+%28n%2B1%29%5CBig%29+-2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle h &#92;approx &#92;log_{&#92;phi} &#92;Big( &#92;sqrt{5} (n+1)&#92;Big) -2' title='&#92;displaystyle h &#92;approx &#92;log_{&#92;phi} &#92;Big( &#92;sqrt{5} (n+1)&#92;Big) -2' class='latex' /></p>
<p>Using a change of base</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+n+%5Capprox+%5Cfrac%7B%5Clog_2+%5CBig%28+%5Csqrt%7B5%7D%28n%2B1%29%5CBig%29%7D%7B%5Clog_2+%5Cphi%7D+-2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle n &#92;approx &#92;frac{&#92;log_2 &#92;Big( &#92;sqrt{5}(n+1)&#92;Big)}{&#92;log_2 &#92;phi} -2' title='&#92;displaystyle n &#92;approx &#92;frac{&#92;log_2 &#92;Big( &#92;sqrt{5}(n+1)&#92;Big)}{&#92;log_2 &#92;phi} -2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Capprox+%5Cfrac%7B%5Clog_2%5CBig%28+%5Csqrt%7B5%7D%28n%2B1%29%5CBig%29+-+2%5Clog_2%5Cphi%7D%7B%5Clog_2+%5Cphi%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;approx &#92;frac{&#92;log_2&#92;Big( &#92;sqrt{5}(n+1)&#92;Big) - 2&#92;log_2&#92;phi}{&#92;log_2 &#92;phi}' title='&#92;displaystyle &#92;approx &#92;frac{&#92;log_2&#92;Big( &#92;sqrt{5}(n+1)&#92;Big) - 2&#92;log_2&#92;phi}{&#92;log_2 &#92;phi}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Capprox+%5Cfrac%7B1%7D%7B%5Clog_2+%5Cphi%7D+%5Ccdot+%5Clog_2%5CBig%28%5Cfrac%7B%5Csqrt%7B5%7D%28n%2B1%29%7D%7B%5Cphi%5E2%7D%5CBig%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;approx &#92;frac{1}{&#92;log_2 &#92;phi} &#92;cdot &#92;log_2&#92;Big(&#92;frac{&#92;sqrt{5}(n+1)}{&#92;phi^2}&#92;Big)' title='&#92;displaystyle &#92;approx &#92;frac{1}{&#92;log_2 &#92;phi} &#92;cdot &#92;log_2&#92;Big(&#92;frac{&#92;sqrt{5}(n+1)}{&#92;phi^2}&#92;Big)' class='latex' /></p>
<p>Again using the fibonacci number approximation, we have <img src='http://s0.wp.com/latex.php?latex=%5Csqrt%7B5%7D%2F%5Cphi%5E2+%5Capprox+f_2+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sqrt{5}/&#92;phi^2 &#92;approx f_2 = 1' title='&#92;sqrt{5}/&#92;phi^2 &#92;approx f_2 = 1' class='latex' />. This simplifies our estimate to</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+h+%5Capprox+1.44%5Clog_2%28n%2B1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle h &#92;approx 1.44&#92;log_2(n+1)' title='&#92;displaystyle h &#92;approx 1.44&#92;log_2(n+1)' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=1%2F%5Clog_2+%5Cphi+%3D+1.44&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='1/&#92;log_2 &#92;phi = 1.44' title='1/&#92;log_2 &#92;phi = 1.44' class='latex' />.</p>
<p>If we have an array of a million names, then the worst case running time of the binary search is </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+1.44%5Clog_2%281000000+%2B+1%29+%3D+28.70&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle 1.44&#92;log_2(1000000 + 1) = 28.70' title='&#92;displaystyle 1.44&#92;log_2(1000000 + 1) = 28.70' class='latex' />.</p>
<p>That&#039;s incredibly efficient!</p>
<p></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1169&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/03/07/way-up-high-a-binary-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>
	</item>
		<item>
		<title>Eating The Fruit Of The AVL Tree</title>
		<link>http://javaero.org/2011/03/06/eating-the-fruit-of-the-avl-tree/</link>
		<comments>http://javaero.org/2011/03/06/eating-the-fruit-of-the-avl-tree/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 04:23:19 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[rotations]]></category>
		<category><![CDATA[self-balancing]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1143</guid>
		<description><![CDATA[We have seen that searching a name in a sorted array of a million names is very efficient when using binary search. In fact, the running time is logarithmic, which means we only need 19 comparisons, in the worst case, to figure out if our search is successful or not. We have also seen that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1143&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We have seen that searching a name in a sorted array of a  million names is very efficient when using binary search. In fact, the running time is logarithmic, which means we only need 19 comparisons, in the worst case, to figure out if our search is successful or not. We have also seen that by using a binary search tree, we can save more efficiencies when we are searching on a dynamic array of names, that is, when the list of names grows. However, we also showed that if the BST is fed with a sorted array of  names, then the search becomes very inefficient. In fact, in the worst case, we need to compare 1 million times before we realize that we don&#8217;t have a name in the array. </p>
<p>In the last <a href="http://javaero.org/2011/02/20/trees-are-made-by-fools-like-me/">post</a>, we built a BST from an array of a sorted list of animals. Below is the result of that construction, repeated here for convenience. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst1.gif"><img src="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst1.gif" alt="" title="extreme_unbalanced_bst" width="399" height="457" class="aligncenter size-full wp-image-1158" /></a></p>
<p>This is a sad state of affairs. We have found a very good data structure for searching but it fails when fed with sorted data. Is there anything we can do to minimize the height of this tree?  Fortunately, there is! The data structure is called a <strong>self-balancing binary search tree</strong> and there are many of them. We will only take a look at one and it is called <strong>AVL tree*</strong>. </p>
<p>A self-balancing binary search tree is able to maintain it&#8217;s height by keeping track of a variable in each node called the <em>balance factor</em>. The balance factor  for a node <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> is defined as </p>
<blockquote><p>Height of left subtree of node minus the height of the right subtree. </p></blockquote>
<p>Below is an example of a binary tree with balance factors indicated inside the circles. The triangles that  you see in the diagram are subtrees. They may or may not be present in the node but we show them in the diagram for some reason which will become clear later. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/tree_with_sample_balance_factors.gif"><img src="http://javaero.files.wordpress.com/2011/02/tree_with_sample_balance_factors.gif" alt="" title="tree_with_sample_balance_factors" width="292" height="306" class="aligncenter size-full wp-image-1161" /></a></p>
<p>An AVL tree employs mechanisms to limit the difference in height between subtrees to 1, 0 or -1. These mechanisms are called <strong>rotations</strong>. There are only 4 rotations that are needed to balance an AVL tree. The figure below shows you how rotations work. </p>
<p>The first thing to look for after inserting a node is find a balance factor that is -2 or +2. in the first tree, the balance factor is +2. this means that the left subtree is imbalanced. Next, examine the balance factor of the left child. If the balance factor is -1, then the right subtree is higher than the left. To balance this, the child in yellow replaces its parent (green node) which now becomes its left child. What used to be the right child of the node in yellow is now the right child of the green node. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/avl_left_tree_balance.gif"><img src="http://javaero.files.wordpress.com/2011/02/avl_left_tree_balance.gif" alt="" title="avl_left_tree_balance" width="958" height="320" class="aligncenter size-full wp-image-1156" /></a></p>
<p>However, this rotation does not change the balance factor of the root node. Another rotation will establish the balance. It involves replacing the root node ( in red) with the yellow  node and making the root node the right child of the yellow node. What used to be the right child of the yellow node now becomes the left child of the red node.</p>
<p>When the balance factor of the root node is -2 it means that the right subtree is imbalanced. If you observe carefully, this is just symmetric to the case above and the rotation can easily be figured out from the figure.</p>
<p><a href="http://javaero.files.wordpress.com/2011/02/avl_right_tree_balance.gif"><img src="http://javaero.files.wordpress.com/2011/02/avl_right_tree_balance.gif" alt="" title="avl_right_tree_balance" width="959" height="329" class="aligncenter size-full wp-image-1157" /></a></p>
<p><b>Balancing the Animal Tree</b></p>
<p>Let us show how we can use these transformations to fix our tree. From the figure below, you can see that the imbalance occurs after we insert the <code>cobra</code>. At this point, the balance factor of the &#8220;alligator&#8221; node is 2. Rotating this tree by making <code>bat</code> the root node fixes the tree. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl1.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl1.gif" alt="" title="build_animal_avl1" width="626" height="199" class="aligncenter size-full wp-image-1150" /></a></p>
<p>We then insert <code>dog</code> and <code>fox</code> before we encounter another imbalance. Replacing <code>cobra</code> with <code>dog</code> and making <code>cobra</code> the left child of <code>dog</code> fixes the imbalance. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl2.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl2.gif" alt="" title="build_animal_avl2" width="711" height="256" class="aligncenter size-full wp-image-1151" /></a><br />
<a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl3.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl3.gif" alt="" title="build_animal_avl3" width="354" height="270" class="aligncenter size-full wp-image-1152" /></a></p>
<p>Inserting <code>horse</code> to this new configuration creates an imbalance at the root of the tree. Replacing <code>bat</code> with <code>dog</code> and making <code>cobra</code> the right child of <code>bat</code> fixes the tree. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl4.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl4.gif" alt="" title="build_animal_avl4" width="783" height="310" class="aligncenter size-full wp-image-1153" /></a></p>
<p>You can follow the rest of the process until we have all ten words inserted into the BST. The result is a tree with a maximum height of 4. </p>
<p><a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl5.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl5.gif" alt="" title="build_animal_avl5" width="898" height="583" class="aligncenter size-full wp-image-1154" /></a><br />
<a href="http://javaero.files.wordpress.com/2011/02/build_animal_avl6.gif"><img src="http://javaero.files.wordpress.com/2011/02/build_animal_avl6.gif" alt="" title="build_animal_avl6" width="579" height="855" class="aligncenter size-full wp-image-1155" /></a></p>
<p>What does this mean? It means that it only takes a maximum of 4 comparisons to find any animal in this structure. It also means it only takes a maximum of 4 comparisons to determine if an animal is not in the structure.</p>
<p>In the next post, we are going to compute the the height of an AVL tree to get an estimate of the complexity of the search.</p>
<p>* AVL is named after Adelson-Velskii and EM Landis. </p>
<p></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1143&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/03/06/eating-the-fruit-of-the-avl-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst1.gif" medium="image">
			<media:title type="html">extreme_unbalanced_bst</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/tree_with_sample_balance_factors.gif" medium="image">
			<media:title type="html">tree_with_sample_balance_factors</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/avl_left_tree_balance.gif" medium="image">
			<media:title type="html">avl_left_tree_balance</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/avl_right_tree_balance.gif" medium="image">
			<media:title type="html">avl_right_tree_balance</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl1.gif" medium="image">
			<media:title type="html">build_animal_avl1</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl2.gif" medium="image">
			<media:title type="html">build_animal_avl2</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl3.gif" medium="image">
			<media:title type="html">build_animal_avl3</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl4.gif" medium="image">
			<media:title type="html">build_animal_avl4</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl5.gif" medium="image">
			<media:title type="html">build_animal_avl5</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/build_animal_avl6.gif" medium="image">
			<media:title type="html">build_animal_avl6</media:title>
		</media:content>
	</item>
		<item>
		<title>Mathematical Beauty</title>
		<link>http://javaero.org/2011/02/28/mathematical-beauty/</link>
		<comments>http://javaero.org/2011/02/28/mathematical-beauty/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 11:27:32 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[1.618034]]></category>
		<category><![CDATA[beauty]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[fibonacci numbers]]></category>
		<category><![CDATA[golden ratio]]></category>
		<category><![CDATA[golden rectangle]]></category>
		<category><![CDATA[linear homogeneous recurrence relations with constant coefficients]]></category>
		<category><![CDATA[phi]]></category>
		<category><![CDATA[recurrence relations]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1101</guid>
		<description><![CDATA[It is said that beauty is in the eye of the beholder. As a person is more than the sum of its parts, physical beauty is just one aspect of the total beauty. Physical beauty, however, has some sort of mathematical standard. Why is it that we find movie stars beautiful? What is that standard [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1101&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is said that beauty is in the eye of the beholder. As a person is more than the sum of its parts, physical beauty is just one aspect of the total beauty. Physical beauty, however, has some sort of mathematical standard. Why is it that we find movie stars beautiful? What is that standard that defines beauty? That standard is hidden in the sequence of numbers 0, 1, 1, 2, 3, 5, 8&#8230;</p>
<p>The careful eye will realize that, beginning on the second term, the terms in this sequence is equal to the sum of the previous two numbers. For example, the second term is just 0+1 = 1, the third term is just 1+2 = 3, and so on. This sequence is called the fibonacci sequence after Leonardo of Pisa, also known as Fibonacci. We can model this sequence as a recurrence relation. If we let <img src='http://s0.wp.com/latex.php?latex=F_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_n' title='F_n' class='latex' /> be the nth Fibonacci number, then by definition it is equal to the sum of the previous 2 Fibonacci numbers <img src='http://s0.wp.com/latex.php?latex=F_%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_{n-1}' title='F_{n-1}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=F_%7Bn-2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_{n-2}' title='F_{n-2}' class='latex' />. Mathematically we write this as</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+F_n+%3D+F_%7Bn-1%7D+%2B+F_%7Bn-2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle F_n = F_{n-1} + F_{n-2}' title='&#92;displaystyle F_n = F_{n-1} + F_{n-2}' class='latex' /></p>
<p>Looking at the recurrence relation above, we realize that it is an instance of a Homogeneous Linear Recurrence Relation With Constant Coefficients. The good news is we know how to solve this kind of recurrence relation. The characteristic equation is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r%5E2+-+r+-1+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r^2 - r -1 = 0' title='&#92;displaystyle r^2 - r -1 = 0' class='latex' /></p>
<p>Using the quadratic formula, the roots are</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r+%3D+%5Cfrac%7B+-%28-1%29+%5Cpm+%5Csqrt%7B+%28-1%29%5E2+-+4%5Ccdot+1+%5Ccdot+%28-1%29%7D%7D%7B2%5Ccdot+1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r = &#92;frac{ -(-1) &#92;pm &#92;sqrt{ (-1)^2 - 4&#92;cdot 1 &#92;cdot (-1)}}{2&#92;cdot 1}' title='&#92;displaystyle r = &#92;frac{ -(-1) &#92;pm &#92;sqrt{ (-1)^2 - 4&#92;cdot 1 &#92;cdot (-1)}}{2&#92;cdot 1}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Cfrac%7B1+%5Cpm+%5Csqrt%7B5%7D%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;frac{1 &#92;pm &#92;sqrt{5}}{2}' title='&#92;displaystyle = &#92;frac{1 &#92;pm &#92;sqrt{5}}{2}' class='latex' /></p>
<p>If we define </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cphi+%3D+%5Cfrac%7B1+%2B+%5Csqrt%7B5%7D%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;phi = &#92;frac{1 + &#92;sqrt{5}}{2}' title='&#92;displaystyle &#92;phi = &#92;frac{1 + &#92;sqrt{5}}{2}' class='latex' /></p>
<p>then we  can write negative root as</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7B1+-+%5Csqrt%7B5%7D%7D%7B2%7D+%3D+1-%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;frac{1 - &#92;sqrt{5}}{2} = 1-&#92;phi' title='&#92;displaystyle &#92;frac{1 - &#92;sqrt{5}}{2} = 1-&#92;phi' class='latex' /></p>
<p>From the previous <a href="http://javaero.org/2011/02/27/a-method-to-the-madness/">post</a>, we know that we can express the solution of <img src='http://s0.wp.com/latex.php?latex=F_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_n' title='F_n' class='latex' /> as a linear combination of <img src='http://s0.wp.com/latex.php?latex=%5Cphi%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi^n' title='&#92;phi^n' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%281-%5Cphi%29%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(1-&#92;phi)^n' title='(1-&#92;phi)^n' class='latex' />. Therefore, the solution of the Fibonacci recurrence relation is just </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+F_n+%3D+%5Calpha_1+%5Cphi%5En+%2B+%5Calpha_2+%281-%5Cphi%29%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle F_n = &#92;alpha_1 &#92;phi^n + &#92;alpha_2 (1-&#92;phi)^n' title='&#92;displaystyle F_n = &#92;alpha_1 &#92;phi^n + &#92;alpha_2 (1-&#92;phi)^n' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' /> are constants. Since <img src='http://s0.wp.com/latex.php?latex=F_0+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_0 = 0' title='F_0 = 0' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=F_1+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_1 = 1' title='F_1 = 1' class='latex' />, we can solve for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' />:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+F_0+%3D+0+%3D+%5Calpha_1+%5Cphi%5E0+++%2B+%5Calpha_2+%281-%5Cphi%29%5E0+%3D+%5Calpha_1+%2B+%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle F_0 = 0 = &#92;alpha_1 &#92;phi^0   + &#92;alpha_2 (1-&#92;phi)^0 = &#92;alpha_1 + &#92;alpha_2' title='&#92;displaystyle F_0 = 0 = &#92;alpha_1 &#92;phi^0   + &#92;alpha_2 (1-&#92;phi)^0 = &#92;alpha_1 + &#92;alpha_2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+F_1+%3D+1+%3D+%5Calpha_1+%5Cphi%5E1+++%2B+%5Calpha_2+%281-%5Cphi%29%5E1+%3D+%5Calpha_1+%5Cphi%2B+%5Calpha_2+%281-%5Cphi%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle F_1 = 1 = &#92;alpha_1 &#92;phi^1   + &#92;alpha_2 (1-&#92;phi)^1 = &#92;alpha_1 &#92;phi+ &#92;alpha_2 (1-&#92;phi)' title='&#92;displaystyle F_1 = 1 = &#92;alpha_1 &#92;phi^1   + &#92;alpha_2 (1-&#92;phi)^1 = &#92;alpha_1 &#92;phi+ &#92;alpha_2 (1-&#92;phi)' class='latex' /></p>
<p>From the first equation, we solve for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' />:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_1+%3D+-+%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_1 = - &#92;alpha_2' title='&#92;displaystyle &#92;alpha_1 = - &#92;alpha_2' class='latex' /></p>
<p>Substituting this into the second equation and solving for <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' /> we get:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+-%5Calpha_2+%5Cphi+%2B+%5Calpha_2+-+%5Calpha_2+%5Cphi+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle -&#92;alpha_2 &#92;phi + &#92;alpha_2 - &#92;alpha_2 &#92;phi = 1' title='&#92;displaystyle -&#92;alpha_2 &#92;phi + &#92;alpha_2 - &#92;alpha_2 &#92;phi = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+-2%5Calpha_2%5Cphi+%2B+%5Calpha_2+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = -2&#92;alpha_2&#92;phi + &#92;alpha_2 = 1' title='&#92;displaystyle = -2&#92;alpha_2&#92;phi + &#92;alpha_2 = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_2+%28-2+%5Cphi+%2B1%29+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_2 (-2 &#92;phi +1) = 1' title='&#92;displaystyle = &#92;alpha_2 (-2 &#92;phi +1) = 1' class='latex' /></p>
<p>Observe that</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++1-2%5Cphi+%3D+1+-+2%5Cfrac%7B1%2B%5Csqrt%7B5%7D%7D%7B2%7D+%3D+1+-+%281+%2B+%5Csqrt%7B5%7D%29+%3D+-%5Csqrt%7B5%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle  1-2&#92;phi = 1 - 2&#92;frac{1+&#92;sqrt{5}}{2} = 1 - (1 + &#92;sqrt{5}) = -&#92;sqrt{5}' title='&#92;displaystyle  1-2&#92;phi = 1 - 2&#92;frac{1+&#92;sqrt{5}}{2} = 1 - (1 + &#92;sqrt{5}) = -&#92;sqrt{5}' class='latex' /></p>
<p>This means that</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+-%5Csqrt%7B5%7D+%5Ccdot+%5Calpha_2+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle -&#92;sqrt{5} &#92;cdot &#92;alpha_2 = 1' title='&#92;displaystyle -&#92;sqrt{5} &#92;cdot &#92;alpha_2 = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_2+%3D+-+%5Cfrac%7B1%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_2 = - &#92;frac{1}{&#92;sqrt{5}}' title='&#92;displaystyle &#92;alpha_2 = - &#92;frac{1}{&#92;sqrt{5}}' class='latex' /></p>
<p>and</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Calpha_1+%3D+%5Cfrac%7B1%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;alpha_1 = &#92;frac{1}{&#92;sqrt{5}}' title='&#92;displaystyle &#92;alpha_1 = &#92;frac{1}{&#92;sqrt{5}}' class='latex' /></p>
<p>Therefore, the solution to the Fibonacci recurrence relation is </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+F_n+%3D+%5Cfrac%7B%5Cphi%5En+-+%281-%5Cphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle F_n = &#92;frac{&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}}' title='&#92;displaystyle F_n = &#92;frac{&#92;phi^n - (1-&#92;phi)^n}{&#92;sqrt{5}}' class='latex' /></p>
<p><strong>Beauty and Fibonacci numbers</strong></p>
<p>After all that tedious computations, so what? What does Fibonacci have anything to do with beauty? If you take the successive ratio of the fibonacci numbers</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Clim_%7Bn+%5Crightarrow+%5Cinfty%7D+%5Cfrac%7BF_%7Bn%2B1%7D%7D%7BF_n%7D+%3D+%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;lim_{n &#92;rightarrow &#92;infty} &#92;frac{F_{n+1}}{F_n} = &#92;phi' title='&#92;displaystyle &#92;lim_{n &#92;rightarrow &#92;infty} &#92;frac{F_{n+1}}{F_n} = &#92;phi' class='latex' /></p>
<p>Here is a list of the first few fibonacci numbers starting at 1 and the corresponding ratios:</p>
<p><pre class="brush: r;">
1        1       1        1.000000
2        2       1        2.000000
3        3       2        1.500000
4        5       3        1.666667
5        8       5        1.600000
6       13       8        1.625000
7       21      13        1.615385
8       34      21        1.619048
9       55      34        1.617647
10      89      55        1.618182
11     144      89        1.617978
12     233     144        1.618056
13     377     233        1.618026
14     610     377        1.618037
15     987     610        1.618033
16    1597     987        1.618034
17    2584    1597        1.618034
18    4181    2584        1.618034
19    6765    4181        1.618034
20   10946    6765        1.618034

</pre></p>
<p>The first column in the table above is just a line number. The second column is <img src='http://s0.wp.com/latex.php?latex=F_%7Bn%2B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_{n+1}' title='F_{n+1}' class='latex' />, the third column is <img src='http://s0.wp.com/latex.php?latex=F_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_n' title='F_n' class='latex' /> and the last column is <img src='http://s0.wp.com/latex.php?latex=F_%7Bn%2B1%7D%2FF_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F_{n+1}/F_n' title='F_{n+1}/F_n' class='latex' />. You can see that the ratio approaches value of <img src='http://s0.wp.com/latex.php?latex=%5Cphi%3D1.618034&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi=1.618034' title='&#92;phi=1.618034' class='latex' />.</p>
<p>The constant <img src='http://s0.wp.com/latex.php?latex=%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi' title='&#92;phi' class='latex' /> is called the <strong>Golden Ratio</strong> by the Greeks. Any structure that follows the golden ratio is structurally beautiful to the eye. Below is an image of a rectangle with labeled sides.<br />
<br />
<a href="http://javaero.files.wordpress.com/2011/02/golden_rectangle.gif"><img src="http://javaero.files.wordpress.com/2011/02/golden_rectangle.gif" alt="" title="golden_rectangle" width="230" height="198" class="aligncenter size-full wp-image-1115" /></a><br />
<br />
The rectangle is called a <strong>Golden Rectangle</strong> if</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Ba%2Bb%7D%7Ba%7D+%3D+%5Cfrac%7Ba%7D%7Bb%7D+%3D+%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;frac{a+b}{a} = &#92;frac{a}{b} = &#92;phi' title='&#92;displaystyle &#92;frac{a+b}{a} = &#92;frac{a}{b} = &#92;phi' class='latex' />.</p>
<p>The Golden Ratio can be found in many aesthetic works. Leonardo Da Vinci used this in his Vitruvian Man. This is probably why the fibonacci sequence was featured in the beginning of the movie (book) Da Vinci Code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1101/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1101&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/02/28/mathematical-beauty/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/golden_rectangle.gif" medium="image">
			<media:title type="html">golden_rectangle</media:title>
		</media:content>
	</item>
		<item>
		<title>A Method To The Madness</title>
		<link>http://javaero.org/2011/02/27/a-method-to-the-madness/</link>
		<comments>http://javaero.org/2011/02/27/a-method-to-the-madness/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 05:46:28 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[complexity]]></category>
		<category><![CDATA[compound interest]]></category>
		<category><![CDATA[linear homogeneous]]></category>
		<category><![CDATA[linear homogeneous recurrence relations with constant coefficients]]></category>
		<category><![CDATA[recurrence relations]]></category>
		<category><![CDATA[solving recurrence relations]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=1053</guid>
		<description><![CDATA[In the previous post on recurrence relations, we derived a general method to solve divide and conquer relations. In another article, we derived the formula of the sum of the first n non-negative integers: If you think carefully about it, you can express the sum of the first n integers as a recurrence relation. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1053&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the previous <a href="http://javaero.org/2011/01/30/conquering-divide-and-conquer/">post</a> on recurrence relations, we derived a general method to solve divide and conquer relations. In another <a href="http://javaero.org/2011/01/06/how-do-i-love-thee-let-me-count-the-ways/">article</a>, we derived the formula of the sum of the first n non-negative integers:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bi%3D0%7D%5En+i+%3D+%5Cfrac%7Bn%28n%2B1%29%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;sum_{i=0}^n i = &#92;frac{n(n+1)}{2}' title='&#92;displaystyle &#92;sum_{i=0}^n i = &#92;frac{n(n+1)}{2}' class='latex' /></p>
<p>If you think carefully about it, you can express the sum of the first n integers as a recurrence relation. If you denote <img src='http://s0.wp.com/latex.php?latex=s_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='s_n' title='s_n' class='latex' /> to be the sum of the first n integers, then it is just equal to n plus the sum of the first n-1 integers. Symbolically, we write this as</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+s_n+%3D+s_%7Bn-1%7D+%2B+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle s_n = s_{n-1} + n' title='&#92;displaystyle s_n = s_{n-1} + n' class='latex' /></p>
<p>A good question now comes to mind: Is there also a method to solve this kind of recurrence relation? Surprise! Surprise! The answer is YES! But first, let us handle the easier cases.</p>
<p>A good example of a recurrence relation we frequently encounter is in our financial life, when we compute how much money we will get at the end of one year when we put it in a bank. Suppose, you deposit 1 million bucks in a bank with 10% interest compounded yearly. How much money will you get after 10 years? If we let <img src='http://s0.wp.com/latex.php?latex=P_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='P_n' title='P_n' class='latex' /> be the amount of money we have after n years, then we can write the compounding of interest as a recurrence relation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_n+%3D+P_%7Bn-1%7D+%2B+0.1%5Ccdot+P_%7Bn-1%7D+%3D+1.1+%5Ccdot+P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle P_n = P_{n-1} + 0.1&#92;cdot P_{n-1} = 1.1 &#92;cdot P_{n-1}' title='&#92;displaystyle P_n = P_{n-1} + 0.1&#92;cdot P_{n-1} = 1.1 &#92;cdot P_{n-1}' class='latex' /></p>
<p>The recurrence relation above is just an example of a more general form called the <strong>Linear Homogeneous Recurrence Relation With Constant Coefficients</strong>. That&#8217;s a pretty long name for a seemingly simple concept. What this means is that your recurrence relation has the following form:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a_%7Bn%7D+%3D+c_1+a_%7Bn-1%7D+%2B+c_2+a_%7Bn-2%7D+%2B+%5Cldots+%2B+c_k+a_%7Bn-k%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle a_{n} = c_1 a_{n-1} + c_2 a_{n-2} + &#92;ldots + c_k a_{n-k}' title='&#92;displaystyle a_{n} = c_1 a_{n-1} + c_2 a_{n-2} + &#92;ldots + c_k a_{n-k}' class='latex' /></p></blockquote>
<p>To solve this recurrence relation, we invoke the method of &#8220;Lucky Guess&#8221;, that is, we will guess that the solution of the above recurrence relation is of the form <img src='http://s0.wp.com/latex.php?latex=a_n+%3D+r%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a_n = r^n' title='a_n = r^n' class='latex' />. If we substitute this to the equation above, we will get</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r%5En+%3D+c_1+r%5E%7Bn-1%7D+%2B+c_2+r%5E%7Bn-2%7D+%2B+%5Cldots+%2B+c_k+r%5E%7Bn-k%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r^n = c_1 r^{n-1} + c_2 r^{n-2} + &#92;ldots + c_k r^{n-k}' title='&#92;displaystyle r^n = c_1 r^{n-1} + c_2 r^{n-2} + &#92;ldots + c_k r^{n-k}' class='latex' /></p>
<p>Dividing both sides by <img src='http://s0.wp.com/latex.php?latex=r%5E%7Bn+-+k%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r^{n - k}' title='r^{n - k}' class='latex' /> we get,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r%5Ek+%3D+c_1+r%5E%7Bk-1%7D+%2B+c_2+r%5E%7Bk-2%7D+%2B+%5Cldots+%2B+c_k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r^k = c_1 r^{k-1} + c_2 r^{k-2} + &#92;ldots + c_k' title='&#92;displaystyle r^k = c_1 r^{k-1} + c_2 r^{k-2} + &#92;ldots + c_k' class='latex' />.</p>
<p>This means that <img src='http://s0.wp.com/latex.php?latex=a_n++%3D+r%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a_n  = r^n' title='a_n  = r^n' class='latex' /> is a solution to this recurrence relation if r satisfies the following algebraic equation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r%5Ek+-+c_1+r%5E%7Bk-1%7D+-+c_2+r%5E%7Bk-2%7D+-+%5Cldots+-+c_k+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r^k - c_1 r^{k-1} - c_2 r^{k-2} - &#92;ldots - c_k = 0' title='&#92;displaystyle r^k - c_1 r^{k-1} - c_2 r^{k-2} - &#92;ldots - c_k = 0' class='latex' /></p>
<p>This is called the <strong>Characteristic Equation</strong> and the roots of this equation are called <strong>characteristic roots</strong>. To make things look simpler, let&#8217;s suppose we have a recurrence relation of degree 2:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a_n+%3D+c_1+a_%7Bn-1%7D+%2B+c_2+a_%7Bn-2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle a_n = c_1 a_{n-1} + c_2 a_{n-2}' title='&#92;displaystyle a_n = c_1 a_{n-1} + c_2 a_{n-2}' class='latex' /></p></blockquote>
<p>The characteristic equation of this recurrence relation is </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r%5E2+-+c_1+r+-+c_2+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r^2 - c_1 r - c_2 = 0' title='&#92;displaystyle r^2 - c_1 r - c_2 = 0' class='latex' /></p>
<p><strong>Case of distinct roots</strong></p>
<p>Suppose the roots of the above equation are <img src='http://s0.wp.com/latex.php?latex=r_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r_1' title='r_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=r_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r_2' title='r_2' class='latex' />, respectively. Then  </p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a_n+%3D+%5Calpha_1+r_1%5En+%2B+%5Calpha_2+r_2%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle a_n = &#92;alpha_1 r_1^n + &#92;alpha_2 r_2^n' title='&#92;displaystyle a_n = &#92;alpha_1 r_1^n + &#92;alpha_2 r_2^n' class='latex' /></p></blockquote>
<p>is a solution of the recurrence relation, where <img src='http://s0.wp.com/latex.php?latex=%5Calpha_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_1' title='&#92;alpha_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Calpha_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha_2' title='&#92;alpha_2' class='latex' /> are constants. To prove this, substitute the solution to the recurrence relation</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++c_1+a_%7Bn-1%7D+%2B+c_2+a_%7Bn-2%7D+%3D++c_1+%28%5Calpha_1+r_1%5E%7Bn-1%7D+%2B+%5Calpha_2+r_2%5E%7Bn-1%7D%29+%2B+c_2+%28%5Calpha_1+r_1%5E%7Bn-2%7D+%2B+%5Calpha_2+r_2%5E%7Bn-2%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle  c_1 a_{n-1} + c_2 a_{n-2} =  c_1 (&#92;alpha_1 r_1^{n-1} + &#92;alpha_2 r_2^{n-1}) + c_2 (&#92;alpha_1 r_1^{n-2} + &#92;alpha_2 r_2^{n-2})' title='&#92;displaystyle  c_1 a_{n-1} + c_2 a_{n-2} =  c_1 (&#92;alpha_1 r_1^{n-1} + &#92;alpha_2 r_2^{n-1}) + c_2 (&#92;alpha_1 r_1^{n-2} + &#92;alpha_2 r_2^{n-2})' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+c_1%5Calpha_1+r_1%5E%7Bn-1%7D+%2B+c_1+%5Calpha_2+r_2%5E%7Bn-1%7D+%2B+c_2++%5Calpha_1+r_1%5E%7Bn-2%7D+%2B+c_2%5Calpha_2+r_2%5E%7Bn-2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = c_1&#92;alpha_1 r_1^{n-1} + c_1 &#92;alpha_2 r_2^{n-1} + c_2  &#92;alpha_1 r_1^{n-2} + c_2&#92;alpha_2 r_2^{n-2}' title='&#92;displaystyle = c_1&#92;alpha_1 r_1^{n-1} + c_1 &#92;alpha_2 r_2^{n-1} + c_2  &#92;alpha_1 r_1^{n-2} + c_2&#92;alpha_2 r_2^{n-2}' class='latex' /></p>
<p>Collecting the <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' /> coefficients:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+%28+c_1+r_1%5E%7Bn-1%7D+%2B+c_2+r_1%5E%7Bn-2%7D%29+%2B+%5Calpha_2+%28c_1+r_2%5E%7Bn-1%7D+%2B+c_2+r_2%5E%7Bn-2%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 ( c_1 r_1^{n-1} + c_2 r_1^{n-2}) + &#92;alpha_2 (c_1 r_2^{n-1} + c_2 r_2^{n-2})' title='&#92;displaystyle = &#92;alpha_1 ( c_1 r_1^{n-1} + c_2 r_1^{n-2}) + &#92;alpha_2 (c_1 r_2^{n-1} + c_2 r_2^{n-2})' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+r_1%5E%7Bn-2%7D+%28c_1+r_1+%2B+c_2%29+%2B+%5Calpha_2+r_2%5E%7Bn-2%7D+%28c_1+r_2+%2B+c_2%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} (c_1 r_1 + c_2) + &#92;alpha_2 r_2^{n-2} (c_1 r_2 + c_2)' title='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} (c_1 r_1 + c_2) + &#92;alpha_2 r_2^{n-2} (c_1 r_2 + c_2)' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+r_1%5E%7Bn-2%7D+r_1%5E2+%2B+%5Calpha_2+r_2%5E%7Bn-2%7D+r_2%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} r_1^2 + &#92;alpha_2 r_2^{n-2} r_2^2' title='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} r_1^2 + &#92;alpha_2 r_2^{n-2} r_2^2' class='latex' /></p>
<p>Using the characteristic equation <img src='http://s0.wp.com/latex.php?latex=r%5E2%3Dc_1+r+%2B+c_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r^2=c_1 r + c_2' title='r^2=c_1 r + c_2' class='latex' />, we have</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+r_1%5En+%2B+%5Calpha_2+r_2%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 r_1^n + &#92;alpha_2 r_2^2' title='&#92;displaystyle = &#92;alpha_1 r_1^n + &#92;alpha_2 r_2^2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+a_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = a_n' title='&#92;displaystyle = a_n' class='latex' /></p>
<p>as was to be shown.</p>
<p><strong>Case of Repeated Roots</strong></p>
<p>If the roots of the characteristic equation is repeated, that is, the quadratic equation is a perfect square, then if <img src='http://s0.wp.com/latex.php?latex=r_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r_1' title='r_1' class='latex' /> is the root, the solution to the recurrence relation can be expressed as </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a_n+%3D+%5Calpha_1+r_1%5En+%2B+n%5Calpha_2+r_1%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle a_n = &#92;alpha_1 r_1^n + n&#92;alpha_2 r_1^n' title='&#92;displaystyle a_n = &#92;alpha_1 r_1^n + n&#92;alpha_2 r_1^n' class='latex' />.</p>
<p>To see this, substitute this formula to the recurrence relation</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+c_1+a_%7Bn-1%7D+%2B+c_2+a_%7Bn-2%7D+%3D+c_1+%28%5Calpha_1+r_1%5E%7Bn-1%7D+%2B+n%5Calpha_2+r_1%5E%7Bn-1%7D%29+%2B+c_2+%28%5Calpha_1+r_1%5E%7Bn-2%7D+%2B+n%5Calpha_2+r_1%5E%7Bn-2%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle c_1 a_{n-1} + c_2 a_{n-2} = c_1 (&#92;alpha_1 r_1^{n-1} + n&#92;alpha_2 r_1^{n-1}) + c_2 (&#92;alpha_1 r_1^{n-2} + n&#92;alpha_2 r_1^{n-2})' title='&#92;displaystyle c_1 a_{n-1} + c_2 a_{n-2} = c_1 (&#92;alpha_1 r_1^{n-1} + n&#92;alpha_2 r_1^{n-1}) + c_2 (&#92;alpha_1 r_1^{n-2} + n&#92;alpha_2 r_1^{n-2})' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+%28+c_1+r_1%5E%7Bn-1%7D+%2B+c_2+r_1%5E%7Bn-2%7D%29+%2B+n%5Calpha_2+%28c_1+r_1%5E%7Bn-1%7D+%2B+c_2+r_1%5E%7Bn-2%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 ( c_1 r_1^{n-1} + c_2 r_1^{n-2}) + n&#92;alpha_2 (c_1 r_1^{n-1} + c_2 r_1^{n-2})' title='&#92;displaystyle = &#92;alpha_1 ( c_1 r_1^{n-1} + c_2 r_1^{n-2}) + n&#92;alpha_2 (c_1 r_1^{n-1} + c_2 r_1^{n-2})' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+r_1%5E%7Bn-2%7D+%28+c_1+r_1+%2B+c_2%29+%2B+n+%5Calpha_2+r_1%5E%7Bn-2%7D+%28c_1+r_1+%2B+c_2%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} ( c_1 r_1 + c_2) + n &#92;alpha_2 r_1^{n-2} (c_1 r_1 + c_2)' title='&#92;displaystyle = &#92;alpha_1 r_1^{n-2} ( c_1 r_1 + c_2) + n &#92;alpha_2 r_1^{n-2} (c_1 r_1 + c_2)' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Calpha_1+r_1%5En+%2B+n+%5Calpha_2+r_1%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;alpha_1 r_1^n + n &#92;alpha_2 r_1^n' title='&#92;displaystyle = &#92;alpha_1 r_1^n + n &#92;alpha_2 r_1^n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+a_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = a_n' title='&#92;displaystyle = a_n' class='latex' /></p>
<p><strong>Compound Interest</strong></p>
<p>Applying this to our compound interest recurrence relation, we see that the characteristic equation of </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_n+%3D+P_%7Bn-1%7D+%2B+0.1%5Ccdot+P_%7Bn-1%7D+%3D+1.1+%5Ccdot+P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle P_n = P_{n-1} + 0.1&#92;cdot P_{n-1} = 1.1 &#92;cdot P_{n-1}' title='&#92;displaystyle P_n = P_{n-1} + 0.1&#92;cdot P_{n-1} = 1.1 &#92;cdot P_{n-1}' class='latex' /></p>
<p>is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r+-+1.1+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r - 1.1 = 0' title='&#92;displaystyle r - 1.1 = 0' class='latex' /> </p>
<p>whose characteristic root is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+r+%3D+1.1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle r = 1.1' title='&#92;displaystyle r = 1.1' class='latex' /></p>
<p>Therefore, the solution to the compound interest recurrence relation has the form </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_n+%3D+%5Calpha+%5Ccdot+1.1%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle P_n = &#92;alpha &#92;cdot 1.1^n' title='&#92;displaystyle P_n = &#92;alpha &#92;cdot 1.1^n' class='latex' /></p>
<p>If at n=0, the amount of money  you have in the bank is <img src='http://s0.wp.com/latex.php?latex=P_0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='P_0' title='P_0' class='latex' />, we can solve for <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' />:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_0+%3D+%5Calpha+%5Ccdot+1.1%5E0+%3D+%5Calpha+%5Ccdot+1+%3D+%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle P_0 = &#92;alpha &#92;cdot 1.1^0 = &#92;alpha &#92;cdot 1 = &#92;alpha' title='&#92;displaystyle P_0 = &#92;alpha &#92;cdot 1.1^0 = &#92;alpha &#92;cdot 1 = &#92;alpha' class='latex' /></p>
<p>This means that at the end of period n, the amount of money you have in the bank is</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_n+%3D+1.1%5En+%5Ccdot+P_0+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle P_n = 1.1^n &#92;cdot P_0 ' title='&#92;displaystyle P_n = 1.1^n &#92;cdot P_0 ' class='latex' /></p>
<p>Or substituting 1 million to <img src='http://s0.wp.com/latex.php?latex=P_0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='P_0' title='P_0' class='latex' />, we get <img src='http://s0.wp.com/latex.php?latex=P_%7B10%7D+%3D+2593742&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='P_{10} = 2593742' title='P_{10} = 2593742' class='latex' />.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/1053/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=1053&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/02/27/a-method-to-the-madness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>
	</item>
		<item>
		<title>Trees Are Made By Fools Like Me</title>
		<link>http://javaero.org/2011/02/20/trees-are-made-by-fools-like-me/</link>
		<comments>http://javaero.org/2011/02/20/trees-are-made-by-fools-like-me/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 13:59:38 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[asymptotic analysis]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[logarithmic]]></category>
		<category><![CDATA[recursive algorithm]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=991</guid>
		<description><![CDATA[We have seen in the previous post that searching a name in a sorted array of 1 million names can be done very efficiently using the binary search. But that was only the second half of the story. What was not told was how we ended up with a sorted array. Was it handed to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=991&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We have seen in the previous <a href="http://javaero.org/2011/01/22/divide-et-impera/">post</a> that searching a name in a sorted array of 1 million names can be done very efficiently using the binary search. But that was only the second half of the story. What was not told was how we ended up with a sorted array. Was it handed to us already sorted or were we the ones who sorted it? </p>
<p>Now suppose that someone gave us an unsorted list of 10 different names to append to the original 1 million. How do we search now for a name? We cannot just append these 10 names to the array of sorted names because it will destroy the &#8220;sorted&#8221; property of the original array. What we can do is increase the length of the original array by 10 more slots and reinsert the 1 million and 10 names to this new array and sorting the new array. Reinserting would take linear time and re-sorting on a 99% sorted array takes quadratic time using quicksort. So is there a better way to do it? Yes there is and we are going to build a tree for it.</p>
<p>Suppose we are given a list of animals:</p>
<p><code>leopard, cobra, shark, horse, alligator, bat, tiger, fox, dog, pig</code></p>
<p>Our task is to create a structure that will allow adding more entries and still be able to search as fast as a binary search. To do this, let&#8217;s construct a tree with the following properties </p>
<p>- each node will contain at most 2 children<br />
- each node is identified by a key. For this example, we will use the name of the animal as key.<br />
- the left child of each node should have a key that is less than the value of the node.<br />
- the right child of each node should have a key that is greater than the value of the node.</p>
<p>We call the result of this construction a <strong>Binary Search Tree</strong> or <strong>BST</strong> for short.</p>
<p>Given these rules, we can start constructing our BST starting from the word <code>leopard</code> as the root of the tree. The next word is <code>cobra</code>. Since <code>cobra</code> is less than <code>leopard</code> (lexicographically speaking), we put <code>cobra</code> as the left child of <code>leopard</code>. The next word is <code>shark</code>. Since <code>shark</code> is greater than <code>leopard</code>, we put shark as the right child of <code>leopard</code>. Since <code>leopard</code> already has 2 children, we expect the other words to be children of either <code>cobra</code> or <code>shark</code>. </p>
<p>The next word is <code>horse</code>. Since <code>horse</code> is less than <code>leopard</code>, we compare it with <code>cobra</code>. Since <code>horse</code> is greater than <code>cobra</code>, we make <code>horse</code> the right child of <code>cobra</code>. Below is a step by step visual construction of the BST.<br />
<br />
<a href="http://javaero.files.wordpress.com/2011/02/binary_search_tree_construction.gif"><img src="http://javaero.files.wordpress.com/2011/02/binary_search_tree_construction.gif" alt="" title="binary_search_tree_construction" width="713" height="568" class="aligncenter size-full wp-image-999" /></a><br />
<br />
The words in bold orange are the newly inserted words into the BST structure. The resulting BST is shown below.<br />
<br />
<a href="http://javaero.files.wordpress.com/2011/02/unbalanced_bst.gif"><img src="http://javaero.files.wordpress.com/2011/02/unbalanced_bst.gif" alt="" title="unbalanced_BST" width="372" height="319" class="aligncenter size-full wp-image-1002" /></a><br />
<br />
Searching a word on a BST is almost the same as inserting an entry. You first compare your search term with the key of the root. If they are equal, you have found the entry. If the search term is less than the root key, then search the left subtree. If the search term is greater than the root key, then search the right subtree. If the search term is not equal to the node key and the node has no more children, then the word is not in the list. This algorithm is recursive and is shown below:<br />
<br />
<pre class="brush: plain;">
boolean search_BST(Node node, String searchTerm):
    if node is null
        return false
    if node key is equal to searchTerm:
        return true
    if node key is less than searchTerm:
        search_BST(leftChild, searchTerm)
    else
        search_BST(rightChild, searchTerm)

</pre></p>
<p>Let&#8217;s trace how to search for the word <code>bat</code>. We start at the root <code>leopard</code>. Since <code>bat</code> &lt; <code>leopard</code>, we search the left subtree of <code>leopard</code> which is rooted at <code>cobra</code>. We now compare <code>bat</code> and <code>cobra</code>. Since <code>bat</code> &lt; <code>cobra</code>, we search the left subtree of <code>cobra</code>, which is rooted at <code>alligator</code>. Since <code>bat</code> &gt; <code>alligator</code>, we search the right subtree of <code>alligator</code> which is rooted at <code>bat</code>. However, since the search term is equal to <code>bat</code>, we have found our word and return true. It only took us 4 comparison to find the word <code>bat</code>. </p>
<p>If we look at our data structure closely, we observe that the running time of the search algorithm is proportional to the height of the tree. It is therefore in our best interest to make our tree as small as possible. But how small?  Given a tree of n nodes, how small can be we build our binary tree? At the root (level 0) we have 1 node. At level 1, we can only attach 2 nodes. At level 2, we can attach 4 nodes. At level <img src='http://s0.wp.com/latex.php?latex=h&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='h' title='h' class='latex' />, which we denote as the height of the tree, we can have a maximum of </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+n+%5Cle++2%5E0+%2B+2%5E1+%2B+2%5E2+%2B+%5Cldots+%2B+2%5Eh+%3D+%5Csum_%7Bi%3D0%7D%5Eh+2%5Ei+%3D+2%5E%7Bh%2B1%7D+-1+%3C+2%5E%7Bh%2B1%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle n &#92;le  2^0 + 2^1 + 2^2 + &#92;ldots + 2^h = &#92;sum_{i=0}^h 2^i = 2^{h+1} -1 &lt; 2^{h+1} ' title='&#92;displaystyle n &#92;le  2^0 + 2^1 + 2^2 + &#92;ldots + 2^h = &#92;sum_{i=0}^h 2^i = 2^{h+1} -1 &lt; 2^{h+1} ' class='latex' /></p>
<p>This means that the smallest possible height of the binary tree we can create from n nodes is  <img src='http://s0.wp.com/latex.php?latex=h+%3D+%5Clfloor+%5Clog_2+n+%5Crfloor&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='h = &#92;lfloor &#92;log_2 n &#92;rfloor' title='h = &#92;lfloor &#92;log_2 n &#92;rfloor' class='latex' />. </p>
<p>Looking at our example, we can see that the left subtree is 2 levels deeper than the right subtree. This means that our tree is not optimal. Ideally, our tree should have a height of <img src='http://s0.wp.com/latex.php?latex=%5Clfloor+%5Clog_2+10+%5Crfloor+%3D+3&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;lfloor &#92;log_2 10 &#92;rfloor = 3' title='&#92;lfloor &#92;log_2 10 &#92;rfloor = 3' class='latex' />.  In fact, if we built our BST from a sorted list, say</p>
<p><code>alligator, bat, cobra, dog, fox, horse, leopard, pig,shark, tiger</code></p>
<p>we will get this structure:<br />
<br />
<a href="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst.gif"><img src="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst.gif" alt="" title="extreme_unbalanced_bst" width="399" height="457" class="aligncenter size-full wp-image-1012" /></a><br />
<br />
With the structure above, the worst-case running time is <img src='http://s0.wp.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' />.  So how do we optimize our BST?  That will be the topic of the next post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/991/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=991&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/02/20/trees-are-made-by-fools-like-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/binary_search_tree_construction.gif" medium="image">
			<media:title type="html">binary_search_tree_construction</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/unbalanced_bst.gif" medium="image">
			<media:title type="html">unbalanced_BST</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/extreme_unbalanced_bst.gif" medium="image">
			<media:title type="html">extreme_unbalanced_bst</media:title>
		</media:content>
	</item>
		<item>
		<title>A Match Made In Heaven</title>
		<link>http://javaero.org/2011/02/11/a-match-made-in-heaven/</link>
		<comments>http://javaero.org/2011/02/11/a-match-made-in-heaven/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 19:45:51 +0000</pubDate>
		<dc:creator>Bobby Corpus</dc:creator>
				<category><![CDATA[Algorithms and Complexity]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[asymptotic analysis]]></category>
		<category><![CDATA[Big Oh approximation]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[recursive algorithm]]></category>
		<category><![CDATA[tree traversal]]></category>
		<category><![CDATA[trees]]></category>
		<category><![CDATA[vertex]]></category>

		<guid isPermaLink="false">http://javaero.org/?p=930</guid>
		<description><![CDATA[There are some things which go naturally with each other. For some, it is a person whom they consider a soulmate. In the case of coffee, it is a creamer. For algorithms, it is the data structure. For example, a recursive algorithms suits very well with a tree data structure and a tree data structure [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=930&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are some things which go naturally with each other. For some, it is a person whom they consider a soulmate. In the case of coffee, it is a creamer. For algorithms, it is the data structure. For example, a recursive algorithms suits very well with a tree data structure and a tree data structure suits well with a recursive algorithm. </p>
<p>I assume that you are a programmer or know how to program and that you know what a data structure is. Examples of data structures we usually encounter in daily programming chore is the array. An array is a collection of objects, usually of the same kind. Arrays go well with iterative algorithms like the for loop or while loop. A more interesting kind of data structure is the tree. You can imagine a tree as a hierarchical structure, much like the what you see in Windows Explorer where a filesystem is displayed as a tree of directories. I have encountered programmers, in my career, who don&#8217;t know how to navigate a directory structure in code. They usually come up with a spaghetti-like code but which are not able to go deep into the directory structure. However, a good programmer can navigate a directory tree with a few lines of code.</p>
<p>Let&#8217;s have an example. Below is a screenshot of a directory structure. It is composed of files and directories (surprise!). We know that directories can contain other files and directories whereas files don&#8217;t have that property. In tree language, we call files the &#8220;leaves&#8221; while directories are the nodes. Well not exactly, because if a directory is empty, it can appear as a leaf. To avoid confusion, let us treat both files and directories in a generic manner and call them nodes. A directory is a special kind of node in that it can contain other nodes, which in this case are files and directories. To traverse a directory structure, we follow the algorithm below:</p>
<p><pre class="brush: plain;">
algorithm traverseTree(Node node):
    print node name
    if node is a directory
        list the children of the directory
        for each child
            traverseTree(child)
</pre></p>
<p><a href="http://javaero.files.wordpress.com/2011/02/directory_structure.png"><img src="http://javaero.files.wordpress.com/2011/02/directory_structure.png" alt="" title="directory_structure" width="182" height="218" class="alignleft size-full wp-image-939" /></a><br />
Notice that the algorithm above calls itself and accepts any node as input, whether it&#8217;s a file or a directory. This is an example of a recursive algorithm applied to a tree data structure. Simple and elegant!</p>
<p>You might ask &#8220;What is the complexity of the traverseTree algorithm?&#8221;. Before we can answer that, let&#8217;s agree first on some terminology. We define a <strong>graph</strong> to be a set of <strong>nodes</strong> connected to each other by edges. An abstract model of the directory structure above is shown below. The circles represent the nodes while the lines connecting them represent the <strong>edges</strong>. Therefore a graph <img src='http://s0.wp.com/latex.php?latex=G&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G' title='G' class='latex' /> is composed of  a set of nodes <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> and a set of edges <img src='http://s0.wp.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='E' title='E' class='latex' /> and we denote a graph as <img src='http://s0.wp.com/latex.php?latex=G%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G(V,E)' title='G(V,E)' class='latex' />. Using this definition, you can see that a <strong>tree</strong> is just a special case of a graph. Let <img src='http://s0.wp.com/latex.php?latex=v%5Cin+V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v&#92;in V' title='v&#92;in V' class='latex' /> be a node. The degree of a node <img src='http://s0.wp.com/latex.php?latex=%5Cdeg%28v%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;deg(v)' title='&#92;deg(v)' class='latex' /> is the number of edges it has. For example, if a node is connected to 3 other nodes, then the degree of that node is 3. </p>
<p>An interesting property of the degree of a node is that the total sum of the degrees of each node is equal to 2 times the number of edges, that is, </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bv%5Cin+V%7D+%5Cdeg%28v%29+%3D+2%5Ccdot+%7CE%7C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;sum_{v&#92;in V} &#92;deg(v) = 2&#92;cdot |E|' title='&#92;displaystyle &#92;sum_{v&#92;in V} &#92;deg(v) = 2&#92;cdot |E|' class='latex' />.</p>
<p><a href="http://javaero.files.wordpress.com/2011/02/photo-4.jpg"><img src="http://javaero.files.wordpress.com/2011/02/photo-4.jpg" alt="" title="photo-4" width="425" height="438" class="aligncenter size-full wp-image-973" /></a></p>
<p>To see why this is, assume you have two nodes <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v' title='v' class='latex' /> connected by an edge. Now, what is the degree of <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> and the degree of <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v' title='v' class='latex' />? By definition, the degree of a node is the number of edges it has. Since <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> has one edge, <img src='http://s0.wp.com/latex.php?latex=%5Cdeg%28u%29+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;deg(u) = 1' title='&#92;deg(u) = 1' class='latex' />. In the same way, since <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v' title='v' class='latex' /> has one edge, <img src='http://s0.wp.com/latex.php?latex=%5Cdeg%28v%29+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;deg(v) = 1' title='&#92;deg(v) = 1' class='latex' />. Therefore, <img src='http://s0.wp.com/latex.php?latex=%5Cdeg%28u%29+%2B+%5Cdeg%28v%29+%3D+1+%2B+1+%3D+2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;deg(u) + &#92;deg(v) = 1 + 1 = 2' title='&#92;deg(u) + &#92;deg(v) = 1 + 1 = 2' class='latex' />, which is equal to twice the number of edges of graph <img src='http://s0.wp.com/latex.php?latex=G%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G(V,E)' title='G(V,E)' class='latex' />.</p>
<p>Let&#8217;s now go back to our main task of computing the complexity of the algorithm above. For each node <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v' title='v' class='latex' />, the number of times &#8220;print node name&#8221; is executed is equal to 1 (surprise!). The number of times the statement &#8220;if node is a directory&#8221; is executed is also 1. Finally, the number of times the for loop is executed is equal to the degree of node v, that is, <img src='http://s0.wp.com/latex.php?latex=%5Cdeg%28v%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;deg(v)' title='&#92;deg(v)' class='latex' />. Since we do this for each node of graph <img src='http://s0.wp.com/latex.php?latex=G%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G(V,E)' title='G(V,E)' class='latex' />, the total executions is equal to </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bv%5Cin+V%7D+%5CBig%28+1+%2B+1+%2B+%5Cdeg%28v%29+%5CBig%29+%3D+%5Csum_%7Bv%5Cin+V%7D+%5CBig%28+2+%2B+%5Cdeg%28v%29+%5CBig%29+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle &#92;sum_{v&#92;in V} &#92;Big( 1 + 1 + &#92;deg(v) &#92;Big) = &#92;sum_{v&#92;in V} &#92;Big( 2 + &#92;deg(v) &#92;Big) ' title='&#92;displaystyle &#92;sum_{v&#92;in V} &#92;Big( 1 + 1 + &#92;deg(v) &#92;Big) = &#92;sum_{v&#92;in V} &#92;Big( 2 + &#92;deg(v) &#92;Big) ' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D+%5Csum_%7Bv%5Cin+V%7D+2+%2B+%5Csum_%7Bv%5Cin+V%7D+%5Cdeg%28v%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle = &#92;sum_{v&#92;in V} 2 + &#92;sum_{v&#92;in V} &#92;deg(v)' title='&#92;displaystyle = &#92;sum_{v&#92;in V} 2 + &#92;sum_{v&#92;in V} &#92;deg(v)' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%3D++2%5Ccdot+%5Csum_%7Bv%5Cin+V%7D+1+%2B+%5Csum_%7Bv%5Cin+V%7D+%5Cdeg%28v%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle =  2&#92;cdot &#92;sum_{v&#92;in V} 1 + &#92;sum_{v&#92;in V} &#92;deg(v)' title='&#92;displaystyle =  2&#92;cdot &#92;sum_{v&#92;in V} 1 + &#92;sum_{v&#92;in V} &#92;deg(v)' class='latex' /></p>
<p>Now, the first summation is just equal to the number of nodes of <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' />, while the second summation is just twice the number of edges. This gives us,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+2%5Ccdot+%7CV%7C+%2B+2%5Ccdot+%7CE%7C+%3D+2%5Ccdot+%5Cbig%28%7CV%7C+%2B+%7CE%7C+%5Cbig%29+%3D+O%28%7CV%7C+%2B+%7CE%7C%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;displaystyle 2&#92;cdot |V| + 2&#92;cdot |E| = 2&#92;cdot &#92;big(|V| + |E| &#92;big) = O(|V| + |E|)' title='&#92;displaystyle 2&#92;cdot |V| + 2&#92;cdot |E| = 2&#92;cdot &#92;big(|V| + |E| &#92;big) = O(|V| + |E|)' class='latex' />.</p>
<p>Therefore, the complexity of the algorithm above is Big Oh of the sum of the number of nodes and the number of edges of graph <img src='http://s0.wp.com/latex.php?latex=G%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G(V,E)' title='G(V,E)' class='latex' />.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaero.wordpress.com/930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaero.wordpress.com/930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaero.wordpress.com/930/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaero.org&amp;blog=18627200&amp;post=930&amp;subd=javaero&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaero.org/2011/02/11/a-match-made-in-heaven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0720898d1dc33e790cf902304f77dc6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bobbycorpus</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/directory_structure.png" medium="image">
			<media:title type="html">directory_structure</media:title>
		</media:content>

		<media:content url="http://javaero.files.wordpress.com/2011/02/photo-4.jpg" medium="image">
			<media:title type="html">photo-4</media:title>
		</media:content>
	</item>
	</channel>
</rss>
