<?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>All About Algorithms</title>
	<atom:link href="http://allaboutalgorithms.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://allaboutalgorithms.wordpress.com</link>
	<description>A serene programming paradise. Do visit my personal blog at http://shreyagarwal.blog.com</description>
	<lastBuildDate>Fri, 03 Feb 2012 18:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='allaboutalgorithms.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>All About Algorithms</title>
		<link>http://allaboutalgorithms.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://allaboutalgorithms.wordpress.com/osd.xml" title="All About Algorithms" />
	<atom:link rel='hub' href='http://allaboutalgorithms.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Tries (Overview)</title>
		<link>http://allaboutalgorithms.wordpress.com/2012/01/27/tries-overview/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2012/01/27/tries-overview/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 07:51:26 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Linked List]]></category>
		<category><![CDATA[Overview]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[Trie]]></category>
		<category><![CDATA[Trees]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=180</guid>
		<description><![CDATA[What is it? Trie? Did you mean &#8220;Tree&#8221;? No! I meant &#8220;Trie&#8221;. It is a data structure (also known as &#8220;prefix tree&#8221;) that is used in form of a dictionary. It is basically a way of representing data. I don&#8217;t think I can explain this without an example. So here we see the way to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=180&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is it? Trie? Did you mean &#8220;Tree&#8221;? No! I meant &#8220;Trie&#8221;. It is a data structure (also known as &#8220;prefix tree&#8221;) that is used in form of a dictionary. It is basically a way of representing data. I don&#8217;t think I can explain this without an example. So here we see the way to represent two words in English language using a trie. Shrey and Shayne.</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2012/01/trie.jpg"><img class="alignleft size-full wp-image-181" title="trie" src="http://allaboutalgorithms.files.wordpress.com/2012/01/trie.jpg?w=600" alt=""   /></a></p>
<p>As you can see, a trie gives us words with the same prefixes. In this example, &#8220;SH&#8221;. So that is how you represent words. You can do the same for numbers (in any format &#8211; binary, decimal, etc). Also, note that both have different &#8220;Y&#8221; nodes because a trie represents words on the base of prefixes. If we associate a count with each node, we can also tell the number of words starting with a particular prefix.</p>
<p>Please note that the insertion in a trie takes O(length) time where length is that of the word being inserted. We can create a trie for english language using a structure with information and 26 pointers for the corresponding letters. Easy, eh?</p>
<p>So a trie can be of many uses. In the next post, we&#8217;ll see an example of using Trie. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>PS : For more information on Tries, visit <a href="http://en.wikipedia.org/wiki/Trie">http://en.wikipedia.org/wiki/Trie</a></p>
<p>PPS : Sorry for the long hiatus in posting!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=180&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2012/01/27/tries-overview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2012/01/trie.jpg" medium="image">
			<media:title type="html">trie</media:title>
		</media:content>
	</item>
		<item>
		<title>Forming Cricket Teams (Balanced Partition problem)</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/29/forming-cricket-teams-balanced-partition-problem/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/29/forming-cricket-teams-balanced-partition-problem/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 18:40:24 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Dynamic Programming]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=172</guid>
		<description><![CDATA[Problem Statement: You have been given N players, with a corresponding strength value S for each player (Minimum strength being 0 and maximum being K). You are supposed to divide them into two teams such that the difference in the total strengths of both teams is minimized. This is an everyday problem that we tackle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=172&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Problem Statement</em>: You have been given N players, with a corresponding strength value S for each player (Minimum strength being 0 and maximum being K). You are supposed to divide them into two teams such that the difference in the total strengths of both teams is minimized.</p>
<p>This is an everyday problem that we tackle while forming equally matched teams. <em>You might try a greedy approach here but in many cases it will fail</em>. It is actually known as a Balanced Partition problem and utilizes the method we used while solving the <a href="http://allaboutalgorithms.wordpress.com/2011/11/22/santa-claus-is-coming-to-town-integer-knapsack-10/" target="_blank">integer knapsack (1/0) problem</a>. Great! So you know half of it! But let us give it a quick look.</p>
<blockquote><p><strong>We defined a term P(i,j) which specified that I would find a subset of elements in the first &#8216;i&#8217; elements which sum up to &#8216;j&#8217;. Now we can define the term recursively, as,</strong></p>
<p><strong>P(i,j) = maximum {P(i-1,j), P(i-1, j-Si)} </strong></p>
<p><strong>While you can refer to the link given to the earlier post, what this formula specifies is that we either have a subset that sums up to &#8216;j&#8217; in the first &#8216;i-1&#8242; elements, or that we have a subset in the first &#8216;i-1&#8242; elements whose sum when added to the ith value Si gives us &#8216;j&#8217;.</strong></p></blockquote>
<p>Now that we are done refreshing our memory, we&#8217;ll move on to the new problem.</p>
<p>We have now generated a 2D array giving us the possibility of a sum (j) being formed using some (&lt;i) elements at each step. Now we need to divide this into 2 sets Team 1 and Team 2 such that the different of their totals is minimized.</p>
<blockquote><p><strong>For this, we&#8217;ll first find the mean value of the list of N elements. If a subset&#8217;s sum hits this particular value, we have found a solution with difference 0. Otherwise, we keep trying to find a solution where the sum of strengths of teams is closest to the mean. For this, we can once again check for all &#8216;i&#8217;&lt;=&#8217;Mean&#8217;,</strong></p>
<p><strong>Minimum {Mean-i : P(n,i)=1}</strong></p>
<p><strong>where 1 indicates we have such a subset. What we are doing is that we are checking for every value less than the Mean it can be formed from any subset of the N elements. Checking this one by one, we land up with the required value closest to the Mean.</strong></p></blockquote>
<p>There it is. You have your solution! Divide you teams and game on! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(Retrieving the values for the subsets is as simple as maintaining back-pointers while creating the 2D array and filling it up.)</p>
<p>Doubts? Ask away!</p>
<p>PS: Need to be spoonfed (euphemism for &#8220;Me.Want.Code.&#8221;) ? Write it yourself! <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  If you just can&#8217;t, drop me an email and I&#8217;ll mail it to you!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=172&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/29/forming-cricket-teams-balanced-partition-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>
	</item>
		<item>
		<title>Santa Claus is coming to town. (Integer Knapsack 1/0)</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/22/santa-claus-is-coming-to-town-integer-knapsack-10/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/22/santa-claus-is-coming-to-town-integer-knapsack-10/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 09:22:59 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Dynamic Programming]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=169</guid>
		<description><![CDATA[I hope you remember the US Pizza problem we talked about a while back. That wasn&#8217;t a 1/0 problem but the one we will discuss now certainly is. Problem Statement: Santa Claus is coming to your city and needs to figure out the gifts he has to take. Since North Pole is a long way [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=169&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I hope you remember the <a href="http://allaboutalgorithms.wordpress.com/2011/10/25/the-us-pizza-knapsack-problem-not-10/" target="_blank">US Pizza problem</a> we talked about a while back. That wasn&#8217;t a 1/0 problem but the one we will discuss now certainly is.</p>
<blockquote><p><strong><em>Problem Statement</em></strong>: Santa Claus is coming to your city and needs to figure out the gifts he has to take. Since North Pole is a long way off, he can only carry gifts weighing a total of &#8216;C&#8217; in his bag. He has a total of &#8216;N&#8217; gifts to select from. You are supposed to help him select the gifts in a way that would use the capacity to the greatest use, i.e., the total weight of the gifts selected should be closest to C and the most number of gifts should be selected.</p></blockquote>
<p>This is a dynamic programming problem and you should recognize it as soon as you see that the problem has optimal substructures in the fact that its solution can be built starting from 1 to i gifts. Also, this is a 1/0 knapsack problem since you can either select a gift (1) or leave it behind (0).</p>
<blockquote><p>Let us define a term <strong>M(i,j) as the optimal way of selecting gifts in a way such that selecting 1..i gifts brings the total weight up to exactly j</strong>. So your value of i varies from 1 to N and the value of j varies from 0 to C. Hence, the time complexity of the problem is equal to O(NC). (Basically, M(i,j) in a 2D array stores the number of gifts)</p>
<p>At every point, there are <strong>two ways</strong> in which the value of M(i,j) can be determined.</p>
<p><strong>1. Check if there was a subset of gifts from number 1 to i-1 which formed a subset with total weight equal to j. You have the value M(i-1,j) as one of your candidate values.</strong></p>
<p><strong>2. Check the M(i-1, j-Wi) value such that adding the ith gifts weight gives us j. (i.e, weight of ith gift is Wi). This value plus 1 (because we pick up this ith gift as well) is another candidate value.</strong></p></blockquote>
<p>Now you simply have to take the maximum of these two values and place the value at M(i,j). In this way, you end up finding M(n,C) in the generated 2D array which is the required answer that our Santa Claus requires.</p>
<p>Here, you should notice how we built the solution for the problem in a manner similar to what we might do for a balanced partition problem. I will discuss this problem in the next post.</p>
<p>Stay tuned for more Dynamic Programming solutions. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=169&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/22/santa-claus-is-coming-to-town-integer-knapsack-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>
	</item>
		<item>
		<title>Alternative numbers and characters in Linked List problem</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/10/alternative-numbers-and-characters-in-linked-list-problem/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/10/alternative-numbers-and-characters-in-linked-list-problem/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 05:41:08 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Linked List]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=161</guid>
		<description><![CDATA[Problem Statement: You have been given a linked list of the following form, 1 -&#62; 2 -&#62; 3-&#62; a  -&#62; 4 -&#62; b -&#62; c -&#62; 5 -&#62; 6 -&#62; d -&#62; e -&#62; f You are supposed to convert it to to the form, 1 -&#62; a -&#62; 2 -&#62; b -&#62; 3 -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=161&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Problem Statement: </em>You have been given a linked list of the following form,</p>
<p>1 -&gt; 2 -&gt; 3-&gt; a  -&gt; 4 -&gt; b -&gt; c -&gt; 5 -&gt; 6 -&gt; d -&gt; e -&gt; f</p>
<p>You are supposed to convert it to to the form,</p>
<p>1 -&gt; a -&gt; 2 -&gt; b -&gt; 3 -&gt; c -&gt; 4 -&gt; d -&gt; 5 -&gt; e -&gt; 6 -&gt; f</p>
<p>Ofcourse, the numbers and characters can be anything.</p>
<p>It seems to be a very simple problem to me. I am sure you would can figure out the O(n) solution for the problem as well. For those weak in linked lists, here is what you have to do.</p>
<blockquote><p><strong>Take 2 pointers, let us name them ALPHA and NUM. Now move ALPHA till you find an alphabet and move NUM till you find a number. Once you reach these, set your links in the required way so that the start becomes NUM -&gt; ALPHA and move both pointers to the next nodes. Now repeat the same process.</strong></p></blockquote>
<p>Let us apply this in the above example,</p>
<blockquote><p><em><strong>NUM at 1, ALPHA at a</strong>, modify linked list to look like,</em></p>
<p><em>1 -&gt; a -&gt; 2 -&gt; 3-&gt; 4 -&gt; b -&gt; c -&gt; 5 -&gt; 6 -&gt; d -&gt; e -&gt; f</em></p>
<p><em><strong>NUM at 2, ALPHA at b</strong>, modify linked list to look like,</em></p>
<p><em>1 -&gt; a -&gt; 2 -&gt; b -&gt; 3-&gt; 4  -&gt; c -&gt; 5 -&gt; 6 -&gt; d -&gt; e -&gt; f</em></p>
<p><em><strong>NUM at  3, ALPHA at c</strong>, modify linked list to look list,</em></p>
<p><em>1 -&gt; a -&gt; 2 -&gt; b -&gt; 3-&gt; c -&gt; 4  -&gt; 5 -&gt; 6 -&gt; d -&gt; e -&gt; f</em></p></blockquote>
<p>Proceed in a similar fashion and you end up with the required linked list. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Bingo!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=161&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/10/alternative-numbers-and-characters-in-linked-list-problem/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>
	</item>
		<item>
		<title>Given a binary tree, find a branch whose elements sum up to a given number.</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/09/given-a-binary-tree-find-a-branch-whose-elements-sum-up-to-a-given-number/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/09/given-a-binary-tree-find-a-branch-whose-elements-sum-up-to-a-given-number/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 10:15:34 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[Trees]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=157</guid>
		<description><![CDATA[Problem Statement: You have been given a binary tree with integer values. You are supposed to find if a branch exists  (ROOT node to LEAF node) such that the sum of all the values on this branch sums up to a given number &#8216;x&#8217;. Let us work on the above binary tree. Assume x=26. Now the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=157&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Problem Statement:</em> You have been given a binary tree with integer values. You are supposed to find if a branch exists  (ROOT node to LEAF node) such that the sum of all the values on this branch sums up to a given number &#8216;x&#8217;.</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/binary-tree.png"><img class="alignnone size-full wp-image-158" title="binary tree" src="http://allaboutalgorithms.files.wordpress.com/2011/11/binary-tree.png?w=600" alt=""   /></a></p>
<p>Let us work on the above binary tree. Assume x=26. Now the correct branch would be 2-&gt;7-&gt;6-&gt;11. So the answer is &#8220;Yes, such a branch exists&#8221;. I hope you understand the question now.</p>
<p>How would you go about solving this problem? If the first thought that came to your mind was &#8220;recursion&#8221;, kudos! That is in fact the correct way to solve this question. (And usually always the technique you use in questions involving trees)</p>
<p>This time we go for a top-&gt;bottom recursion and keep adding the sum everytime we move down in a branch. Now as soon as you reach the leaf, check your sum and return 1 (success) or 0 (failure) accordingly. Here is the algorithm:</p>
<blockquote><p><strong>int Check ( Node current, int sum ) //initial call with Check ( ROOT, 0)</strong></p>
<p><strong>{</strong></p>
<p><strong>sum = sum + current-&gt;VALUE</strong></p>
<p><strong>If ( current-&gt;LEFT != NULL)</strong></p>
<p><strong>int left = Check ( current-&gt;LEFT, sum )</strong></p>
<p><strong>If ( current-&gt; LEFT !=NULL)</strong></p>
<p><strong>int right = Check ( current-&gt;RIGHT, sum )</strong></p>
<p><strong>If (current-&gt;LEFT == current-&gt;RIGHT == NULL )</strong></p>
<p><strong>{ //in case of LEAF nodes</strong></p>
<p><strong>If (sum==x)</strong></p>
<p><strong>return 1</strong></p>
<p><strong>Else</strong></p>
<p><strong>return 0</strong></p>
<p><strong>}</strong></p>
<p><strong>return (left || right) //return 1 IFF we receive SUCCESS (1) from any branch, left/right</strong></p>
<p><strong>}</strong></p></blockquote>
<p>Pretty simple to understand, right? So for the first time in the blog, we have applied recursion and used it while going it from top to bottom (if you get what I mean). <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Cheerio!</p>
<p>PS: If you are wondering why we don&#8217;t return as soon as sum exceeds &#8216;x&#8217;, remember that the binary tree is said to have integers, which means negative numbers are allowed. <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Attention to detail!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/157/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=157&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/09/given-a-binary-tree-find-a-branch-whose-elements-sum-up-to-a-given-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/binary-tree.png" medium="image">
			<media:title type="html">binary tree</media:title>
		</media:content>
	</item>
		<item>
		<title>Shuffling Numbers</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/08/shuffling-numbers/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/08/shuffling-numbers/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 15:50:55 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Permutations]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=155</guid>
		<description><![CDATA[I recently talked about generating random numbers. What if you are given a set of numbers and asked to shuffle the numbers randomly. What are the different methods that you can think of? There is basically one method you should remember, because finally every algorithm will take you to the same basic thing, which is, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=155&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently talked about generating random numbers. What if you are given a set of numbers and asked to shuffle the numbers randomly. What are the different methods that you can think of? There is basically one method you should remember, because finally every algorithm will take you to the same basic thing, which is,</p>
<blockquote><p><strong>Generate a random number between 1 and n (included), where n is size of the given set. Now put the number at that position in your array in the first place. Repeat the process for the n-1 remaining places. The randomness of the shuffling here will be decided by your random number generator&#8217;s randomness.</strong></p></blockquote>
<p>(A method suggested by Fisher-Yates and multiple others as well. Big deal!)</p>
<p>That is what most algorithms to shuffle numbers are all about. You can ofcourse fine tune your method to give you your shuffled sequence in O(n). Some old algorithms tend to run for O(n*n), but those are just too stupid and you can easily figure out how to fine tune them to O(n).</p>
<p>Yeah, that was as simple a post as they come!</p>
<p><strong>PS: I don&#8217;t have a lot of time right now to write a full-blown algorithm for some problem right now. Sorry for the gap.</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=155&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/08/shuffling-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>
	</item>
		<item>
		<title>Stable Marriage Problem</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/07/stable-marriage-problem/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/07/stable-marriage-problem/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 14:22:47 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Greedy]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=152</guid>
		<description><![CDATA[Please note before diving in: This is an algorithmic problem. If you land at this page looking for solution to something different, please visit a marriage counselor. Poor jokes apart, this one is another famous problem of the computer science domain. Here is how the problem statement goes: (source: Wikipedia) Given n men and n women, where each person [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=152&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Please note before diving in: This is an algorithmic problem. If you land at this page looking for solution to something different, please visit a marriage counselor. <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Poor jokes apart, this one is another famous problem of the computer science domain. <em>Here is how the problem statement goes</em>: (source: Wikipedia)</p>
<blockquote><p><strong>Given <em>n</em> men and <em>n</em> women, where each person has ranked all members of the opposite sex with a unique number between 1 and <em>n</em> in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are &#8220;stable&#8221;.</strong></p></blockquote>
<p>So basically you have to pair people in such a way that they have no better choice. You are given the preference list of each of the n men and n women and are supposed to give the perfect pairing. Now with no other conditions imposed, you can treat this problem as a simple greedy problem. <em>Here is what your algorithm should do</em>:</p>
<blockquote><p><strong>The algorithm will work in iterations.</strong></p>
<p><strong><em>For the first time</em>, each unengaged man proposed to the woman he prefers the most (i.e., woman highest on his priority list). All the women would consider their proposals and select the proposal from the man who is higher than others on her list of preferences. They are now <em>tentatively</em> engaged. </strong></p>
<p><strong>Now in <em>every iteration from now on</em>, each <em>unengaged </em>man will propose to the woman who is next on his list of preferences. The women will compare all the proposals they are currently getting along with the one whom they are tentatively engaged to (if any). They will again select the proposal of the man who is highest on their list of preferences.</strong></p>
<p><strong>This process stops when all men are engaged after an iteration.</strong></p></blockquote>
<p>This greedy method was simple to understand and hopefully you would have got it by now. This is known as the <strong>Gale-Shapley algorithm</strong>. If anyone has any trouble in figuring it out, check out the gif animation below to see a sample case.</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/gale-shapley.gif"><img class="alignnone size-full wp-image-153" title="Gale-Shapley" src="http://allaboutalgorithms.files.wordpress.com/2011/11/gale-shapley.gif?w=600&#038;h=491" alt="" width="600" height="491" /></a></p>
<p>Brilliant! So you are now equipped with another algorithm to tackle your day-to-day marital, friendship and roommate related problems. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Have fun!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=152&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/07/stable-marriage-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/gale-shapley.gif" medium="image">
			<media:title type="html">Gale-Shapley</media:title>
		</media:content>
	</item>
		<item>
		<title>Minimizing multiplications in Chain Matrix Multiplications</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/05/minimizing-multiplications-in-chain-matrix-multiplications/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/05/minimizing-multiplications-in-chain-matrix-multiplications/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 17:18:53 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Dynamic Programming]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=144</guid>
		<description><![CDATA[Be patient and read every line. You&#8217;ll end up understanding a great problem of Dynamic Programming. Problem Statement: Parenthesize a Matrix Chain multiplication matrix in such a way that the number of multiplications required are minimized. First things first, how many multiplications are involved in multiplying 2 matrices? If your 1st matrix is of dimensions [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=144&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Be patient and read every line. You&#8217;ll end up understanding a great problem of Dynamic Programming.</em></p>
<p><em>Problem Statement: </em>Parenthesize a Matrix Chain multiplication matrix in such a way that the number of multiplications required are minimized.</p>
<p><em>First things first, how many multiplications are involved in multiplying 2 matrices?</em></p>
<blockquote><p>If your 1st matrix is of dimensions mxn and your second matrix is of dimensions nxq, you will need to perform mxnxq multiplications in this matrix multiplication.</p></blockquote>
<p>So if your matrix multiplication chain is as follows,</p>
<p>A x B x C x D , we have the following scenarios,</p>
<p>A = 50 x 20</p>
<p>B = 20 x 1</p>
<p>C = 1 x 10</p>
<p>D = 10 x 100</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/paranthesis1.jpg"><img class="alignnone size-full wp-image-147" title="paranthesis" src="http://allaboutalgorithms.files.wordpress.com/2011/11/paranthesis1.jpg?w=600&#038;h=109" alt="" width="600" height="109" /></a></p>
<p>So you see how we minimized the cost of multiplications there. Now we want to develop an algorithm for the same. You can check all possibilities but as you will realize that you will have very high complexity with such a Brute Force method.</p>
<p>As we discussed a parenthesis problem in my <a href="http://allaboutalgorithms.wordpress.com/2011/11/04/counting-possible-parenthesizations-in-matrix-chain-multiplication/" target="_blank">previous post</a>, your mind should jump to find an optimal substructure here. Now you can see all these parenthesizations as a binary tree with these matrices as the leaves and the root element being the final product of all matrices. For example, you can view ((AxB)xC)xD) as,</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/bin_tree.jpg"><img class="alignnone size-full wp-image-148" title="bin_tree" src="http://allaboutalgorithms.files.wordpress.com/2011/11/bin_tree.jpg?w=600" alt=""   /></a></p>
<p>Hopefully you understood everything till here. Now for a tree to be optimal, its subtrees should also be optimal. So here we come across our optimal substructure. Hence, subproblems are of the form,</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/random.jpg"><img class="alignnone size-full wp-image-149" title="random" src="http://allaboutalgorithms.files.wordpress.com/2011/11/random.jpg?w=600" alt=""   /></a></p>
<p>(That is the representation of any one node in some binary tree except for the leaf nodes)</p>
<p>Thus we define this sub-problem as,</p>
<p>C ( i, j ) = minimum cost of multiplying  <a href="http://allaboutalgorithms.files.wordpress.com/2011/11/random1.jpg"><img class="alignnone size-full wp-image-150" title="random" src="http://allaboutalgorithms.files.wordpress.com/2011/11/random1.jpg?w=600" alt=""   /></a></p>
<p>Size of this subproblem = j-i</p>
<p>Hence, minimum possible size is when i=j (single matrix), so C ( i, i) = 0</p>
<p>For j&gt;i, we consider optimal subtree for C ( i, j ). Now, we can divide this from Ai to Ak and Ak to Aj for some k&gt;i and k&lt;j. The cost of the subtree is then the cost of these two partial products plus the cost of combining these. So we land with the recurrence,</p>
<p>C (i, j) = min { C(i, k) + C(k+1, j) + m(i-1) x mk x mj }</p>
<p>where i&lt;=k&lt;=j.</p>
<p>Coding this is pretty simple now and I need not (and should not) spoonfeed it to you. All you need to do is generate this 2D matrix and another one which holds details about parenthesizations and you&#8217;ll end up with your answer. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>PS: If someone requires the code, sorry won&#8217;t give you that. If you need the algorithm, drop a comment or a mail. That I can write down.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=144&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/05/minimizing-multiplications-in-chain-matrix-multiplications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/paranthesis1.jpg" medium="image">
			<media:title type="html">paranthesis</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/bin_tree.jpg" medium="image">
			<media:title type="html">bin_tree</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/random.jpg" medium="image">
			<media:title type="html">random</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/random1.jpg" medium="image">
			<media:title type="html">random</media:title>
		</media:content>
	</item>
		<item>
		<title>Counting possible parenthesizations in Matrix Chain multiplication</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/04/counting-possible-parenthesizations-in-matrix-chain-multiplication/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/04/counting-possible-parenthesizations-in-matrix-chain-multiplication/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 12:32:00 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Permutations]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=140</guid>
		<description><![CDATA[Problem Statement: Given the number of matrices involved in a chain matrix multiplication, find out the number of ways in which you can parenthesize these matrices in the multiplication, i.e., you have to find the number of ways you can introduce brackets in this chain matrix multiplication. (forming groups of 2). For example, A x B [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=140&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Problem Statement:</em> Given the number of matrices involved in a chain matrix multiplication, find out the number of ways in which you can parenthesize these matrices in the multiplication, i.e., you have to find the number of ways you can introduce brackets in this chain matrix multiplication. (forming groups of 2).</p>
<p>For example,</p>
<p>A x B x C can be grouped in 2 ways,</p>
<p>(A x B) x C</p>
<p>A x (B x C)</p>
<p>You should remember that while matrix multiplication is not commutative, it is associative. Hence both the multiplications mean the same.</p>
<blockquote><p><em>How do we find the number of such parenthesizations?</em> <strong>It becomes simple when you realize that you can derive solutions for this problem using solutions to smaller subproblems which you calculated earlier. Dynamic Programming <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </strong></p>
<p>So for,</p>
<p>N=1, P(N) = 1</p>
<p>N=2, P(N) = 1 (easy to figure out)</p>
<p>N=3, P(N) = 2 (again easy)</p></blockquote>
<p>So what we are doing here is finding out the previous solution and figuring out the different ways you can introduce one more matrix in the picture to find the next. So your function looks like,</p>
<p><a href="http://allaboutalgorithms.files.wordpress.com/2011/11/parenthesis.jpg"><img class="alignnone size-full wp-image-141" title="parenthesis" src="http://allaboutalgorithms.files.wordpress.com/2011/11/parenthesis.jpg?w=600" alt=""   /></a></p>
<p>&nbsp;</p>
<p>I guess you understand how this is Dynamic Programming. And also the fact that this is a very simple permutations question. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Not really a programming problem.</p>
<blockquote><p><em>Why did I write this post</em>? <strong>There is a genre of DP problems known as Optimal Matrix Chain multiplication. You are supposed to find the way (out of these P(n) ways) in which the number of multiplications required will be the least.</strong> We will solve this problem in the next post.</p></blockquote>
<p>Till then, adios! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=140&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/04/counting-possible-parenthesizations-in-matrix-chain-multiplication/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>

		<media:content url="http://allaboutalgorithms.files.wordpress.com/2011/11/parenthesis.jpg" medium="image">
			<media:title type="html">parenthesis</media:title>
		</media:content>
	</item>
		<item>
		<title>Valid Parentheses Combinations</title>
		<link>http://allaboutalgorithms.wordpress.com/2011/11/03/valid-parentheses-combinations/</link>
		<comments>http://allaboutalgorithms.wordpress.com/2011/11/03/valid-parentheses-combinations/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 09:34:35 +0000</pubDate>
		<dc:creator>allaboutalgorithms</dc:creator>
				<category><![CDATA[Applied Algorithms]]></category>
		<category><![CDATA[Permutations]]></category>

		<guid isPermaLink="false">http://allaboutalgorithms.wordpress.com/?p=138</guid>
		<description><![CDATA[Problem Statement: You are given a number of pairs of parentheses. You are supposed to print all valid combinations using all these parentheses. This question is only an example of questions where you are supposed to find all the permutations of a set of elements. I am using this question to demonstrate the method to perform [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=138&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>Problem Statement:</em> You are given a number of pairs of parentheses. You are supposed to print all valid combinations using all these parentheses.</p>
<blockquote><p><em>This question is only an example of questions where you are supposed to find all the permutations of a set of elements. I am using this question to demonstrate the method to perform all such permutations.</em></p></blockquote>
<p>So how do you go about finding all permutations. Here is a great method to do so. Start with a single parentheses. Now start inserting the remaining parentheses at all the possible locations. Here is how it goes if we are given 2 pairs of parentheses.</p>
<p>(,),(,)    //available elements</p>
<p>Now constructing all possibilites:</p>
<blockquote><p><strong>( </strong>//start &#8212; <strong>0</strong></p>
<p><strong>()  </strong>//inserting ) to the right in 0 &#8212; <strong>1</strong></p>
<p><strong>)(  </strong>//inserting ) to the left in 0 &#8212; <strong>2</strong></p>
<p><strong>()( </strong>//inserting ( to the right in 1 or left of 2 &#8212; <strong>3</strong></p>
<p><strong>(() </strong>//inserting ( either in the middle or left of 1 &#8212; <strong>4</strong></p>
<p><strong>)(( </strong>//inserting ( in the middle of 2 &#8212; <strong>5</strong></p>
<p><strong>()(), ())( , )()( </strong>//inserting ) at different places in 3 &#8212;<strong> 6</strong></p>
<p><strong>(()) , ()() , )(() </strong>//inserting ) at different places in 4 &#8212; <strong>7</strong></p>
<p><strong>)(() , )()( , ))(( </strong>//inserting ) at different places in 5 &#8212; <strong>8</strong></p></blockquote>
<p>Now all you need to do is find the valid combinations from those generated in 6, 7 and 8. Handy technique, right?! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><em><strong>PS:</strong></em> Leave a comment if you don&#8217;t know how to check validity of parentheses combinations and I&#8217;ll write a post on that as well.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutalgorithms.wordpress.com/138/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutalgorithms.wordpress.com/138/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutalgorithms.wordpress.com/138/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutalgorithms.wordpress.com&amp;blog=28331571&amp;post=138&amp;subd=allaboutalgorithms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://allaboutalgorithms.wordpress.com/2011/11/03/valid-parentheses-combinations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b6fadba9d3e90f242ad893f8014a2f04?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutalgorithms</media:title>
		</media:content>
	</item>
	</channel>
</rss>
