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

<channel>
	<title>Webneckery</title>
	<atom:link href="http://www.jeremypholland.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremypholland.com</link>
	<description>A southern technocrat spouts off</description>
	<lastBuildDate>Tue, 03 May 2011 14:29:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Cartesian products, part 1</title>
		<link>http://www.jeremypholland.com/2011/04/elementary-combinatorics-part-1/</link>
		<comments>http://www.jeremypholland.com/2011/04/elementary-combinatorics-part-1/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 23:33:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jeremypholland.com/?p=12</guid>
		<description><![CDATA[I was recently tasked with finding a solution to a particularly wonky problem that challenged me algorithmically, and I figured I'd write a little post on the experience. The fact that as software developers we are surrounded by a cornucopia of libraries, frameworks, gems, jars, etc. - while certainly beneficial to our execution of our ]]></description>
			<content:encoded><![CDATA[<p>I was recently tasked with finding a solution to a particularly wonky problem that challenged me algorithmically, and I figured I'd write a little post on the experience. The fact that as software developers we are surrounded by a cornucopia of libraries, frameworks, gems, jars, etc. - while certainly beneficial to our execution of our craft - means that the opportunity to solve a really *interesting* problem comes up relatively rarely. So when I was given the job of finding all possible combinations of 1 element each from <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> distinct sets of varying length and contents (i.e the Cartesian product of <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> sets), I leapt at the opportunity to flex my long-underutilized comp-sci muscles. The problem and my own solution are given in detail below, with a few caveats: 1) If you don't like math, well, you should. <img src='http://www.jeremypholland.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  2) I wholeheartedly reserve the right to be wrong, and if you know a better way to acquire the same result, please email me!</p>
<p>So, here's the problem:</p>
<p>Given <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> distinct sets <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_74843b08b0e33f96a2f728a25d5efaed.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}, X_{2}, ... X_{n}" /></span><script type='math/tex'>X_{1}, X_{2}, ... X_{n}</script>, find all possible sets consisting of <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> elements, one from each set. Such a result is known as the <em>Cartesian product</em>, and can be represented mathematically as:</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_41b0743d1e6fcd2cf6304f5049798191.gif' style='vertical-align: middle; border: none;' class='tex' alt="P = \prod_{j=1}^{n}X_{j} = X_{1}\times X_{2}\times ...X_{n}" /></span><script type='math/tex' mode='display'>P = \prod_{j=1}^{n}X_{j} = X_{1}\times X_{2}\times ...X_{n}</script></p></p>
<p>where <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_44c29edb103a2872f519ad0c9a0fdaaa.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="P" /></span><script type='math/tex'>P</script> is the actual product. What does this product look like? Let's see an example.</p>
<p>We'll say we have two sets <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c264bdb22a754961ef6021b6c1914161.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}" /></span><script type='math/tex'>X_{1}</script> and <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c5550be7b87cf24698427e4bdb08d21b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{2}" /></span><script type='math/tex'>X_{2}</script>:</p>
<p><span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_bc428cfab9f4180c3793fb458f2c6d8d.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1} = \{a, b\}" /></span><script type='math/tex'>X_{1} = \{a, b\}</script><br />
<span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_643853a77d3668ec5ae6183aa0e6cad3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{2} = \{1, 2\}" /></span><script type='math/tex'>X_{2} = \{1, 2\}</script></p>
<p>The Cartesian product of these two sets would be a "set of sets", or more formally a <em>collection</em> or <em>family</em> of sets, and would look like the following:</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c474d2ae085cdfcbd2b9cbd5191b4228.gif' style='vertical-align: middle; border: none;' class='tex' alt="P = X_{1}\times X_{2} = \{\{a,1\},\{a,2\},\{b,1\},\{b,2\}\}" /></span><script type='math/tex' mode='display'>P = X_{1}\times X_{2} = \{\{a,1\},\{a,2\},\{b,1\},\{b,2\}\}</script></p></p>
<p>As you can see if you examine the above, for each element of set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c264bdb22a754961ef6021b6c1914161.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}" /></span><script type='math/tex'>X_{1}</script>, there are exactly two sets in the result that contain that element - each of which also contains one or the other element of <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c5550be7b87cf24698427e4bdb08d21b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{2}" /></span><script type='math/tex'>X_{2}</script>. We have our product. Now let's figure out how we got here - and we'll start with the easy-to-compute question: How many possible combinations can there be?</p>
<p>Well, this is likely the easiest problem in <em>combinatorics</em> there is to solve. Remember above where we stated the formula for arriving at a Cartesian product?</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_41b0743d1e6fcd2cf6304f5049798191.gif' style='vertical-align: middle; border: none;' class='tex' alt="P = \prod_{j=1}^{n}X_{j} = X_{1}\times X_{2}\times ...X_{n}" /></span><script type='math/tex' mode='display'>P = \prod_{j=1}^{n}X_{j} = X_{1}\times X_{2}\times ...X_{n}</script></p></p>
<p>Well, the number we're particularly after is the <em>cardinality</em> of the final collection <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_44c29edb103a2872f519ad0c9a0fdaaa.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="P" /></span><script type='math/tex'>P</script>. In this context, the word "cardinality" means "size" - i.e. the cardinality of any set is the number of elements in that set. In mathematical notation, we represent the cardinality of a set by surrounding the name of the set by two vertical bars - very similar to what you may know as the notation for absolute value. So, the cardinality of the set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c264bdb22a754961ef6021b6c1914161.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}" /></span><script type='math/tex'>X_{1}</script> above would be represented as <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_9b6436fe8ee49a992442b63d701f76cd.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\left|X_{1}\right|" /></span><script type='math/tex'>\left|X_{1}\right|</script> and would be equal to 2, because set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c264bdb22a754961ef6021b6c1914161.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}" /></span><script type='math/tex'>X_{1}</script> has exactly 2 elements, <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_0cc175b9c0f1b6a831c399e269772661.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="a" /></span><script type='math/tex'>a</script> and <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="b" /></span><script type='math/tex'>b</script>. So, given what we now know about cardinality, how do we use that to compute the cardinality of <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_44c29edb103a2872f519ad0c9a0fdaaa.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="P" /></span><script type='math/tex'>P</script>, or <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_e2c7fd2fdf09cfcd7162a4623df6bae7.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\left|P\right|" /></span><script type='math/tex'>\left|P\right|</script>? It literally couldn't be easier:</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_dfb35c41da6d314eb0accdeaf43223a5.gif' style='vertical-align: middle; border: none;' class='tex' alt="\left|P\right| = \prod_{j=1}^{n}\left|X_{j}\right| = \left|X_{1}\right|\times \left|X_{2}\right|\times ...\left|X_{n}\right|" /></span><script type='math/tex' mode='display'>\left|P\right| = \prod_{j=1}^{n}\left|X_{j}\right| = \left|X_{1}\right|\times \left|X_{2}\right|\times ...\left|X_{n}\right|</script></p></p>
<p>That's right - you just multiply the cardinalities of all the sets in question together, and voil&aacute;: you have how many combinations there would be. Now, we've already stated that the cardinalty <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_9b6436fe8ee49a992442b63d701f76cd.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\left|X_{1}\right|" /></span><script type='math/tex'>\left|X_{1}\right|</script> of set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c264bdb22a754961ef6021b6c1914161.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{1}" /></span><script type='math/tex'>X_{1}</script> is equal to two, and it should be plain by now that the cardinality <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_ce7cd967e608d39b7e9985ef5ca9f93e.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\left|X_{2}\right|" /></span><script type='math/tex'>\left|X_{2}\right|</script> of set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_c5550be7b87cf24698427e4bdb08d21b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="X_{2}" /></span><script type='math/tex'>X_{2}</script> is <em>also</em> equal to two. As such, that would give us the answer 4 for <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_e2c7fd2fdf09cfcd7162a4623df6bae7.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\left|P\right|" /></span><script type='math/tex'>\left|P\right|</script>, and lo and behold, the result set <span class='MathJax_Preview'><img src='http://www.jeremypholland.com/wp-content/plugins/latex/cache/tex_44c29edb103a2872f519ad0c9a0fdaaa.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="P" /></span><script type='math/tex'>P</script> shown above has exactly 4 elements!</p>
<p>Now to the harder bit - how do we figure out precisely what those elements <em>are</em>? Enter my good friend and soon-to-be yours (if he isn't already), <em>recursion</em>. Below I have implemented the algorithm I worked out - though I make no claims to it being a heretofore undiscovered one - as a pure function (i.e. a function "without side-effects", which does not alter any external state) in Ruby, my programming language of choice. Note how the method calls itself as part of its execution - <em>that's</em> recursion.</p>
<pre class="brush: ruby">
def cartesian_product(*enums)
  return Set.new([Set.new]) if enums.empty?
  combos = Set.new
  first = enums.shift
  subproducts = cartesian_product(*enums)
  subproducts.each do |product|
    first.each do |element|
      combos.add(product.clone.add(element))
    end
  end
  combos
end
</pre>
<p>So there's the meat of the matter - more explanation to come in part 2, coming soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremypholland.com/2011/04/elementary-combinatorics-part-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Attempt #4</title>
		<link>http://www.jeremypholland.com/2010/07/attempt-4/</link>
		<comments>http://www.jeremypholland.com/2010/07/attempt-4/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 15:32:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jeremypholland.com/?p=5</guid>
		<description><![CDATA[OK. This time, I'm going to do it, and do it right. This time, I will become a prolific, opinionated, and sought-after blogger. This time, I will write more than a half-hearted rant condemning regular expressions and half a tutorial on a subject hardly anybody cares about. This time, I'm doing it right. That's science ]]></description>
			<content:encoded><![CDATA[<p>OK. This time, I'm going to do it, and do it right. This time, I will become a prolific, opinionated, and sought-after blogger. This time, I will write more than a half-hearted rant condemning regular expressions and half a tutorial on a subject hardly anybody cares about. This time, I'm doing it right.</p>
<p>That's science - so stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremypholland.com/2010/07/attempt-4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

