<?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>Bosker Blog</title>
	<atom:link href="http://bosker.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bosker.wordpress.com</link>
	<description>Miscellaneous maundering</description>
	<lastBuildDate>Mon, 09 Jan 2012 14:40:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bosker.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Bosker Blog</title>
		<link>http://bosker.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bosker.wordpress.com/osd.xml" title="Bosker Blog" />
	<atom:link rel='hub' href='http://bosker.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to record a screencast video for free on Mac OS X 10.7 ‘Lion’</title>
		<link>http://bosker.wordpress.com/2011/12/11/how-to-record-a-screencast-video-for-free-on-mac-os-x-10-7-lion/</link>
		<comments>http://bosker.wordpress.com/2011/12/11/how-to-record-a-screencast-video-for-free-on-mac-os-x-10-7-lion/#comments</comments>
		<pubDate>Sun, 11 Dec 2011 00:42:37 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[chatter]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=680</guid>
		<description><![CDATA[It’s hard to find any detailed information on the web about how to record a screen video without buying expensive software. I found out how to do it, so here I’m going to explain what I did for the benefit of anyone else who’s trying to do the same. This is only guaranteed to work [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=680&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It’s hard to find any detailed information on the web about how to record a screen video without buying expensive software. I found out how to do it, so here I’m going to explain what I did for the benefit of anyone else who’s trying to do the same.<br />
<span id="more-680"></span><br />
This is only guaranteed to work on Mac OS X 10.7 ‘Lion’, which is what my computer is running. I think it should work in 10.6 ‘Snow Leopard’ too.</p>
<p>Taking a screen video is very easy, once you know where the feature is hidden. The QuickTime Player application has an option to record a screen video: <img src="http://bosker.files.wordpress.com/2011/12/quicktime-player_s-file-menu-truncated.png?w=480" alt="" title="QuickTime Player’s File menu truncated"   class="aligncenter size-full wp-image-732" /></p>
<p>When you choose New Screen Recording from the menu, a small black window appears:<br />
<img src="http://bosker.files.wordpress.com/2011/12/screen-recording-window.png?w=480" alt="" title="screen-recording-window"   class="aligncenter size-full wp-image-685" /></p>
<p>The little triangle reveals a menu that allows you to add an audio track from a microphone, and change a couple of other settings:<br />
<img src="http://bosker.files.wordpress.com/2011/12/screen-recording-dropdown-closeup2.png?w=480" alt="" title="screen-recording-dropdown-closeup"   class="aligncenter size-full wp-image-694" /></p>
<p>This works very nicely, and you can record a voiceover using an internal or external mic. The only trouble is the video doesn’t include the sounds made by the app itself. In some cases that probably doesn’t matter much, but it’s a real problem if you’re trying to demonstrate an app whose audio matters, like a game.</p>
<p>Fortunately there is a clever little app called <a href="http://jackosx.com/">Jack OS X</a> that you can use to fix it.<sup><a href="#footnote-1">1</a></sup> So download and install Jack 0.89 or later.<sup><a href="#footnote-2">2</a></sup> Right now it’s still in beta, so you’ll have to look at the <a href="http://tech.groups.yahoo.com/group/jackosx/messages">mailing list messages</a> to find a link to the latest beta release. Make sure you have beta 20 or later. (Jack 0.89 should be released pretty soon, then this part will be easier.)</p>
<p>Open JackPilot. The first time you run it, the Preferences window will open. Make sure the option “Auto-Connect with physical ports” is ticked, and press Save. Then you’ll see a little window like this. Click Start.<br />
<img src="http://bosker.files.wordpress.com/2011/12/jackpilot-window.png?w=480" alt="" title="jackpilot-window"   class="aligncenter size-full wp-image-706" /></p>
<p>Now open System Preferences &gt; Sound:<br />
<img src="http://bosker.files.wordpress.com/2011/12/sound-prefs.png?w=480&#038;h=387" alt="" title="sound-prefs" width="480" height="387" class="aligncenter size-full wp-image-701" /><br />
and change the sound output device to JackRouter.</p>
<p>Next open QuickTime Player and choose New Screen Recording from the File menu. Using the dropdown menu in the Screen Recording window, choose JackRouter as the microphone. Then open the app you want to record.</p>
<p>Go back to JackPilot, and click Routing. Select QuickTime Player in the middle column (the column labelled Receive Ports). In the left-hand “Send Ports” column, double-click on the name of the app you want to record. If you’re planning to record a live voiceover, double-click system as well. After you double-click these items, they’ll turn red, so it should look something like this:</p>
<p><img src="http://bosker.files.wordpress.com/2011/12/jackpilot-routing.png?w=480&#038;h=374" alt="" title="jackpilot-routing" width="480" height="374" class="aligncenter size-full wp-image-708" /></p>
<p>Finally you can go to QuickTime Player and click the red button to start recording.</p>
<p>When you’ve finished recording your video, you should connect the QuickTime Player Send Port to the system Receive Port using JackRouter, or else you won’t hear the audio when you play it back.</p>
<p>If you have any trouble with this, post a comment below.</p>
<p><strong>Footnotes:</strong></p>
<p id="footnote-1">1. Some blogs recommend using an app called SoundFlower for this. However, there are reports of SoundFlower causing serious problems on Lion (examples: <a href="https://discussions.apple.com/thread/3389720">one</a>, <a href="http://code.google.com/p/soundflower/issues/detail?id=113">two</a>), as well as <a href="http://code.google.com/p/soundflower/issues/detail?id=105">distorting the sound</a>. The SoundFlower code has not been updated for more than a year, so these problems seem unlikely to be resolved soon. At the moment it does not look like a good choice for Lion users.</p>
<p id="footnote-2">2. The released version of Jack didn’t quite work for me when I tried, so I <a href="http://tech.groups.yahoo.com/group/jackosx/message/3567">emailed the project mailing list</a> and Stéphane Letz <a href="http://tech.groups.yahoo.com/group/jackosx/message/3586">fixed it the very same day</a>. Open source development at its best.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/680/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=680&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/12/11/how-to-record-a-screencast-video-for-free-on-mac-os-x-10-7-lion/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/quicktime-player_s-file-menu-truncated.png" medium="image">
			<media:title type="html">QuickTime Player’s File menu truncated</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/screen-recording-window.png" medium="image">
			<media:title type="html">screen-recording-window</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/screen-recording-dropdown-closeup2.png" medium="image">
			<media:title type="html">screen-recording-dropdown-closeup</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/jackpilot-window.png" medium="image">
			<media:title type="html">jackpilot-window</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/sound-prefs.png" medium="image">
			<media:title type="html">sound-prefs</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/12/jackpilot-routing.png" medium="image">
			<media:title type="html">jackpilot-routing</media:title>
		</media:content>
	</item>
		<item>
		<title>Challenging the Power of Twitter</title>
		<link>http://bosker.wordpress.com/2011/08/03/challenging-the-power-of-twitter/</link>
		<comments>http://bosker.wordpress.com/2011/08/03/challenging-the-power-of-twitter/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 16:44:05 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[chatter]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=672</guid>
		<description><![CDATA[Most of the time I use this blog to write about things I understand, so it was something of an experiment when on Sunday evening I wrote a short post about something I did not understand. I wrote the post quickly, and posted a link to Twitter. In a shameless bid to get a few [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=672&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Most of the time I use this blog to write about things I understand, so it was something of an experiment when on Sunday evening I wrote <a href="http://bosker.wordpress.com/2011/07/31/something-i-don%e2%80%99t-understand-about-homomorphic-encryption/" title="Something I don’t understand about homomorphic encryption">a short post</a> about something I did not understand.<br />
<span id="more-672"></span><br />
I wrote the post quickly, and posted a link to Twitter. In a shameless bid to get a few more people interested, I then tweeted:</p>
<blockquote><p>A test of the power of Twitter. Can I reach someone who understands homomorphic encryption, and is willing to help a confused soul? (<a href="https://twitter.com/#!/robinhouston/status/97716051378323456">link</a>)</p></blockquote>
<p>I should have known better than to challenge the Power of Twitter, because that sparked a frenzy of retweeting. And so I learnt that Twitter is home to a great many kind generous people who like nothing better than to come to the aid of a stranger who calls for help — though unfortunately none of these people knows anything about homomorphic encryption.</p>
<p>Colin Wright was also kind enough to post my question to Hacker News, which led to <a href="http://news.ycombinator.com/item?id=2828887">a depressingly ill-informed but mercifully brief discussion</a> there.</p>
<p>But it worked out well, because somehow or other <a href="http://cseweb.ucsd.edu/~cdcash/">David Cash</a> found my post. David understood what I was asking, knew the answers, and explained them clearly and succinctly. Thanks, David.</p>
<p>For the benefit of anyone else who is curious, I should explain briefly what I’ve learnt – though I suggest you also look at <a href="http://bosker.wordpress.com/2011/07/31/something-i-don’t-understand-about-homomorphic-encryption/#comments">David’s comments</a>.</p>
<p>In the simplest proof-of-principle presentation of homomorphic encryption, each bit is encrypted separately. I failed to realise this, because I hadn’t understood that the encryption is randomised so the encryption process is unlikely to give the same result twice even when encrypting with the same key. This randomisation protects against the re-encryption attack I mentioned, though of course it means that each encrypted bit must be a lot bigger than one bit, so there is a substantial blow-up in size when encrypting the data.</p>
<p>So as long as there is a way to perform (say) the AND and XOR operations on encrypted values – which there is, since they correspond to multiplication and addition modulo 2 – it’s possible to perform some arbitrary computation with the encrypted values.</p>
<p>There is a little mathematics to be found here as well. It’s something that I probably ought to have known all along; but I didn’t, or if I ever did then I had forgotten, so perhaps you will also find it interesting.</p>
<p><b>Claim</b>. If F is a finite field, then every function F<sup>n</sup>→F can be represented by a polynomial in n variables.</p>
<p>This is not hard to prove, and it’s always more enlightening to prove something oneself than to read someone else’s proof, so I shan’t give it away. If you haven’t seen this before, and you like that sort of thing, do feel free to write your proof in the comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/672/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=672&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/08/03/challenging-the-power-of-twitter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>
	</item>
		<item>
		<title>Something I don’t understand about homomorphic encryption</title>
		<link>http://bosker.wordpress.com/2011/07/31/something-i-don%e2%80%99t-understand-about-homomorphic-encryption/</link>
		<comments>http://bosker.wordpress.com/2011/07/31/something-i-don%e2%80%99t-understand-about-homomorphic-encryption/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 17:05:40 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[chatter]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=662</guid>
		<description><![CDATA[Ever since Craig Gentry’s seminal work in 2009, there has been a certain amount of excitement about the potential of fully homomorphic encryption. The idea, I gather, is that using a fully homomorphic encryption scheme makes it possible to perform computations on encrypted data without decrypting it, yielding an encrypted result. For example, this would [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=662&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever since Craig Gentry’s seminal work in 2009, there has been a certain amount of excitement about the potential of fully homomorphic encryption. The idea, I gather, is that using a fully homomorphic encryption scheme makes it possible to perform computations on encrypted data <em>without decrypting it</em>, yielding an encrypted result. For example, this would make it possible to process confidential data in an untrusted data centre.</p>
<p>The technology is edging towards practicality as better schemes are devised, and it may soon reach the point where it’s useful in some real-world situations.</p>
<p>But there’s something I don’t understand. I’m hoping that someone who <em>does</em> understand it may come across this and explain it.<br />
<span id="more-662"></span><br />
The techniques used seem to allow the computation of arbitrary polynomials over various finite rings, and – at least in everything I’ve read – it’s always treated as obvious that this is equivalent to allowing arbitrary computations. Is that actually true? If so, is it really obvious? I’d love some enlightenment here.</p>
<p>A related source of confusion is that clearly any encryption scheme that <em>actually</em> supported arbitrary computations on encrypted values would be hopelessly insecure. For example, suppose you have encrypted a ten-character ascii password. I then run the function that extracts the first character. The result I get is still encrypted, but since there are only 128 possible values for an ascii character, I can encrypt each of these values and compare the result to the result of my computation. (I can do this because I can always encrypt any data of my choice, just by running a constant-valued function inside the encryption.) Then I do the same for the second character, and so on, till I have your complete password. Encryption fail!</p>
<p>The only reasonable conclusion I can draw from this is that fully homomorphic encryption does not really permit arbitrary computations on the encrypted data. In that case,</p>
<ol>
<li>Why do people keep saying it does?</li>
<li>What is the class of computations that are in fact permitted? Is it just the evaluation of polynomials?</li>
</ol>
<p>People of the Internet, I thank you.</p>
<p><b>Added later</b>: After reading some more about it, I think Gentry’s scheme has the property that each plaintext can be represented by a large number of possible ciphertexts. This could potentially have the effect that the decoding scheme I described can’t be made to work. Is that relevant?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/662/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=662&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/07/31/something-i-don%e2%80%99t-understand-about-homomorphic-encryption/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>
	</item>
		<item>
		<title>Computing Fibonacci numbers using Binet’s formula</title>
		<link>http://bosker.wordpress.com/2011/07/27/computing-fibonacci-numbers-using-binet%e2%80%99s-formula/</link>
		<comments>http://bosker.wordpress.com/2011/07/27/computing-fibonacci-numbers-using-binet%e2%80%99s-formula/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 22:55:48 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Fibonacci numbers]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=626</guid>
		<description><![CDATA[A few months ago I wrote something about algorithms for computing Fibonacci numbers, which was discussed in some of the nerdier corners of the internet (and even, curiously, made it into print). Several people suggested that Binet’s closed-form formula for Fibonacci numbers might lead to an even faster algorithm. That’s an interesting idea, which we’re [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=626&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few months ago I wrote something about algorithms for <a href="http://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/">computing Fibonacci numbers</a>, which was discussed in some of the <a href="http://news.ycombinator.com/item?id=2537811">nerdier</a> <a href="http://www.reddit.com/r/programming/comments/h9x3r/">corners</a> of the internet (and even, curiously, made it into <a href="http://hackermonthly.com/issue-14.html">print</a>).</p>
<p>Several people suggested that Binet’s closed-form formula for Fibonacci numbers might lead to an even faster algorithm. That’s an interesting idea, which we’re going to explore in this post. So, what is Binet’s formula? It goes like this:</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Br%40%7B%5C%3B%7Dc%40%7B%5C%3B%7Dl%7D%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%29%26%3D%26%5Cdisplaystyle%5Cfrac%7B%5Cvarphi%5En+%2B+%281+-+%5Cvarphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}&#92;mathop{&#92;mathrm{fib}}(n)&amp;=&amp;&#92;displaystyle&#92;frac{&#92;varphi^n + (1 - &#92;varphi)^n}{&#92;sqrt{5}}&#92;end{array}' title='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}&#92;mathop{&#92;mathrm{fib}}(n)&amp;=&amp;&#92;displaystyle&#92;frac{&#92;varphi^n + (1 - &#92;varphi)^n}{&#92;sqrt{5}}&#92;end{array}' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi%3D%5Cfrac%7B1%2B%5Csqrt%7B5%7D%7D%7B2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;varphi=&#92;frac{1+&#92;sqrt{5}}{2}' title='&#92;varphi=&#92;frac{1+&#92;sqrt{5}}{2}' class='latex' /> is the golden ratio.<br />
<span id="more-626"></span><br />
I’d like to explain why this formula works, because it’s a lovely story; and, despite what you might think from most of the explanations on the web, it’s perfectly possible to understand and appreciate it without knowing what “eigenvalue” means. But I fear that would be too long a digression here, so I’ll save it for another post.</p>
<p>There is a nice trick we can use to simplify the computation of this expression, taking advantage of the fact we know it always comes out as an integer. Since <em>φ</em> is about 1.618, its negative cousin (1-<em>φ</em>) is about -0.618, which means that the term</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle%5Cfrac%7B%281+-+%5Cvarphi%29%5En%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle&#92;frac{(1 - &#92;varphi)^n}{&#92;sqrt{5}}' title='&#92;displaystyle&#92;frac{(1 - &#92;varphi)^n}{&#92;sqrt{5}}' class='latex' /></p>
<p>is always between -1/2 and 1/2. So we can just compute</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle%5Cfrac%7B%5Cvarphi%5En%7D%7B%5Csqrt%7B5%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle&#92;frac{&#92;varphi^n}{&#92;sqrt{5}}' title='&#92;displaystyle&#92;frac{&#92;varphi^n}{&#92;sqrt{5}}' class='latex' /></p>
<p>and round to the nearest integer.</p>
<p>The simplest way to implement this idea is to use ordinary floating-point arithmetic. For example, in C we might write:</p>
<p><pre class="brush: cpp;">
double fib_fixed(long n)
{
    double phi = (sqrt(5) + 1) / 2.0;
    return round(pow(phi, n) / sqrt(5));
}
</pre></p>
<p>This is simple and fast. The only trouble is that it almost always gives the wrong answer. On my machine the answer is wrong whenever <em>n</em> &gt; 70. For example <code>fib_fixed(71)</code> gives <code>308061521170130</code>, whereas the correct answer is <code>308061521170129</code>, and it only gets worse from there.</p>
<p>We can improve the situation a little by tweaking the computation, but, however cunningly we do that, we’ll soon run into a fundamental obstacle: <code>fib(79)</code> is <code>14472334024676221</code>, which can’t even be represented in an <a href="http://en.wikipedia.org/wiki/Double_precision_floating-point_format">IEEE double</a>. You can see that illustrated as a <a href="http://www.google.com/search?q=14472334024676221+-+14472334024676220">comedy Google calculator fail</a>.</p>
<p>So if we want to implement this in a general way then we need to use arbitrary-precision floating-point arithmetic. For some reason this has not gone mainstream in the way that big ints have done, but the <a href="http://www.gmplib.org/">GMP</a> library has a good implementation, so we’ll use that. Here’s the code:</p>
<p><pre class="brush: cpp;">
void fib_float(mpf_t *result, long n)
{
    mpf_t sqrt5, phi;
    mp_bitcnt_t bitcnt;

    /* We need about n lg(phi) bits of precision */
    bitcnt = n * 7 / 10;

    /* Initialise sqrt5 to the square root of 5 */
    mpf_init2(sqrt5, bitcnt);
    mpf_sqrt_ui(sqrt5, 5);

    /* Initialise phi to the Golden Ratio */
    mpf_init2(phi, bitcnt);
    mpf_set(phi, sqrt5);
    mpf_add_ui(phi, phi, 1);
    mpf_div_2exp(phi, phi, 1);

    /* Compute phi ^ n / sqrt5 */
    mpf_init2(*result, bitcnt);
    mpf_pow_ui(*result, phi, n);
    mpf_div(*result, *result, sqrt5);

    /* Dispose of the temporary variables */
    mpf_clear(sqrt5);
    mpf_clear(phi);
}</pre></p>
<p>This works fine, gives correct answers, and is reasonably fast. But how does it compare to the integer methods? For comparison, I rewrote the <code>fib_fast</code> algorithm from my last post to use GMP integers. Click to see the code:</p>
<p><pre class="brush: cpp; collapse: true; light: false; toolbar: true;">
void fib_int(mpz_t *result, long n)
{
    mpz_t a, b, c;
    long bit, mask;

    /* Set 'bit' to the most-significant 1-bit of 'n' */
    bit = 1; mask = 1;
    while (n &gt; mask) {
        bit = bit &lt;&lt; 1;
        mask = (mask &lt;&lt; 1) | 1;
    }
     
    mpz_init_set_ui(a, 1);
    mpz_init_set_ui(b, 0);
    mpz_init_set_ui(c, 1);
    
    while (bit &gt; 0) {
        if ( (n &amp; bit) != 0 ) {
            /*  a, b = (a + c) * b, (b * b) + (c * c)  */
            mpz_add(a, a, c);
            mpz_mul(a, a, b);

            mpz_mul(b, b, b);
            mpz_addmul(b, c, c);
        }
        else {
            /*  a, b = (a * a) + (b * b), b * (c + a)  */
            mpz_add(c, c, a); /* Temporarily set c := c + a */

            mpz_mul(a, a, a);
            mpz_addmul(a, b, b);

            mpz_mul(b, b, c);
        }

        mpz_add(c, a, b);
        bit &gt;&gt;= 1;
    }

    mpz_init_set(*result, b);
    mpz_clear(a); mpz_clear(b); mpz_clear(c);
}
</pre></p>
<p>Then I made a graph showing the execution time of both methods, against the input <em>n</em>.</p>
<p><img class="alignnone size-full wp-image-643" title="Computation time of Fibonacci numbers" src="http://bosker.files.wordpress.com/2011/07/linear-graph.png?w=480&#038;h=319" alt="" width="480" height="319" /></p>
<p>Clearly the integer method is faster, but the curves are not obviously different shapes. A log-log plot shows the relationship more clearly:</p>
<p><img class="alignnone size-full wp-image-648" title="A log-log plot of the same data" src="http://bosker.files.wordpress.com/2011/07/graph-002.png?w=480&#038;h=356" alt="" width="480" height="356" /></p>
<p>which supports the idea (consistent with what you’d expect in theory) that both algorithms have the same big-O complexity, but the integer method is consistently about five times faster.</p>
<p>Floating-point arithmetic has a reputation for being difficult and unreliable; a technique that is good for finding approximate answers quickly, but best avoided when a precise answer is required. And actually I think that’s a fair assessment on the whole, but it’s interesting to see how arbitrary-precision floating point arithmetic can give us an exact answer very easily in this case. Of course we lose a lot of the speed advantage that floating-point conventionally offers, because our hardware doesn’t have the same fast native support for these “big floats” that it does for IEEE doubles.</p>
<p>A related idea is to take advantage of the fact that all the calculations produce numbers of a special form, e.g. <em>a</em>+<em>bφ</em> for integers <em>a</em> and <em>b</em>. This works essentially because <em>φ</em><sup>2</sup> = 1 + <em>φ</em>, and so any polynomial in <em>φ</em> can be reduced to that form.</p>
<p>This turns out to be precisely equivalent to the matrix technique we used last time, because <em>φ</em><sup><em>n</em></sup> = fib(<em>n</em>-1) + fib(<em>n</em>)<em>φ</em>, and so computing powers of <em>φ</em> in this representation is just directly equivalent to computing Fibonacci numbers.</p>
<p>A variation of this – as <a href="http://news.ycombinator.com/item?id=2538724">suggested on HN</a> – is to use the fact that <em>φ</em><sup><em>n</em></sup> is always of the form</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Ba+%2B+b%5Csqrt%7B5%7D%7D%7B2%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle &#92;frac{a + b&#92;sqrt{5}}{2} ' title='&#92;displaystyle &#92;frac{a + b&#92;sqrt{5}}{2} ' class='latex' /></p>
<p>for integers <em>a</em> and <em>b</em>. This idea leads to the <a href="http://en.wikipedia.org/wiki/Lucas_number">Lucas numbers</a>, because</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cvarphi%5En+%3D+%5Cfrac%7B%7B%5Cmathop%7B%5Cmathrm%7Bluc%7D%7D%28n%29+%2B+%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%29+%5Csqrt%7B5%7D%7D%7D%7B2%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle &#92;varphi^n = &#92;frac{{&#92;mathop{&#92;mathrm{luc}}(n) + &#92;mathop{&#92;mathrm{fib}}(n) &#92;sqrt{5}}}{2} ' title='&#92;displaystyle &#92;varphi^n = &#92;frac{{&#92;mathop{&#92;mathrm{luc}}(n) + &#92;mathop{&#92;mathrm{fib}}(n) &#92;sqrt{5}}}{2} ' class='latex' /></p>
<p>where luc(<i>n</i>) = fib(<i>n</i>-1) + fib(<i>n</i>+1), and hence to an efficient integer algorithm based on the identities</p>
<p>  fib(2<i>n</i>) = luc(<i>n</i>) fib(<i>n</i>)<br />
  luc(2<i>n</i>) = 5 fib(<i>n</i>)^2 + 2 (-1)<sup><i>n</i></sup></p>
<p>  luc(2<i>n</i> + 1) = (5/2) fib(<i>n</i>) (fib(<i>n</i>) + luc(<i>n</i>)) + (-1)<sup><i>n</i></sup><br />
  fib(2<i>n</i> + 1) = luc(2<i>n</i> + 1) &#8211; 2 luc(<i>n</i>) fib(<i>n</i>)</p>
<p>The complete code for this algorithm is <a href="https://github.com/robinhouston/fibonacci-float-calc/blob/master/lucas.c">here</a>, and more generally all the code used in this post is <a href="https://github.com/robinhouston/fibonacci-float-calc">on GitHub</a>. Remarks and critique would be very welcome. You can follow me on Twitter at <a href="http://twitter.com/robinhouston">@robinhouston</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/626/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=626&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/07/27/computing-fibonacci-numbers-using-binet%e2%80%99s-formula/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/07/linear-graph.png" medium="image">
			<media:title type="html">Computation time of Fibonacci numbers</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/07/graph-002.png" medium="image">
			<media:title type="html">A log-log plot of the same data</media:title>
		</media:content>
	</item>
		<item>
		<title>Counting small mazes</title>
		<link>http://bosker.wordpress.com/2011/05/19/counting-small-mazes/</link>
		<comments>http://bosker.wordpress.com/2011/05/19/counting-small-mazes/#comments</comments>
		<pubDate>Thu, 19 May 2011 17:01:03 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[mazes]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=314</guid>
		<description><![CDATA[Before we get into general algorithms for counting mazes, I thought it would be interesting to do some small cases by hand. It’s quite fun to do, and it’ll give us a few numbers to check against the results of more general methods. There is one 1×1 maze. Not much to say about that. There [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=314&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Before we get into general algorithms for counting mazes, I thought it would be interesting to do some small cases by hand. It’s quite fun to do, and it’ll give us a few numbers to check against the results of more general methods.<br />
<span id="more-314"></span><br />
There is one 1×1 maze. Not much to say about that.</p>
<p>There are four 2×2 mazes: <img class="alignnone size-full" style="vertical-align:bottom;margin-top:0;" title="2x2-0" src="http://bosker.files.wordpress.com/2011/05/2x2-01.png?w=480" alt=""   /><img class="alignnone size-full" style="vertical-align:bottom;margin-top:0;" title="2x2-0" src="http://bosker.files.wordpress.com/2011/05/2x2-11.png?w=480" alt=""   /><img class="alignnone size-full" style="vertical-align:bottom;margin-top:0;" title="2x2-0" src="http://bosker.files.wordpress.com/2011/05/2x2-21.png?w=480" alt=""   /><img class="alignnone size-full" style="vertical-align:bottom;margin-top:0;" title="2x2-0" src="http://bosker.files.wordpress.com/2011/05/2x2-31.png?w=480" alt=""   /></p>
<p style="clear:both;">So the first interesting case is 3×3. For this, we’ll classify the 3×3 mazes according to which of its four neighbours the central cell is connected to:</p>
<h3>One neighbour</h3>
<p>Suppose the central cell is connected to the cell above:<br />
<img class="aligncenter size-full" style="margin-top:0;border:1px solid #888;" title="one-neighbour" src="http://bosker.files.wordpress.com/2011/05/one-neighbour.png?w=480" alt=""   /><br />
Then the rest of the maze must consist of two arms growing from the upper cell, with a combined length of 7, so there are eight possibilities:<br />
<img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-0.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-1.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-2.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-3.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-4.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-5.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-6.png?w=480" alt=""   /><img class="alignnone size-full" title="one-neighbour-0" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-7.png?w=480" alt=""   /><br />
These could also be rotated so the single neighbour is at the right, bottom, or left:</p>
<p><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-0-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-1-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-2-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-3-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-4-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-5-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-6-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-r.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-7-r" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-r.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-0-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-1-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-2-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-3-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-4-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-5-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-6-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-b.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-7-b" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-b.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-0-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-1-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-2-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-3-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-4-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-5-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-6-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-l.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="one-neighbour-7-l" src="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-l.png?w=480" alt=""   /><br />
making a total of 32 mazes of this type.</p>
<h3>Two opposite neighbours</h3>
<p>If the central cell is connected to the cells above and below:<br />
<img class="aligncenter size-full" style="margin-top:0;border:1px solid #888;" title="opposite-neighbours" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours.png?w=480" alt=""   /><br />
then there are two independent arms – one on the left, and one on the right, and there are four possibilities for each arm:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:120px;border-right:1px solid #888;" title="opposite-neighbours-0" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;border-right:1px solid #888;" title="opposite-neighbours-1" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;border-right:1px solid #888;" title="opposite-neighbours-2" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3.png?w=480" alt=""   /></p>
<p>So there are sixteen of these:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-0-0" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-1" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-2" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-3" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-3.png?w=480" alt=""   /><br /><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-1-0" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-1" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-2" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-3" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-3.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-2-0" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-1" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-2" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-3" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-3.png?w=480" alt=""   /><br /><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-3-0" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-1" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-2" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-3" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-3.png?w=480" alt=""   /></p>
<p>And there are another sixteen rotated versions, where the central cell is connected to the two cells to its left and right:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-0-0-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-0-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-1-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-1-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-2-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-2-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-0-3-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-3-r1.png?w=480" alt=""   /><br /><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-1-0-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-0-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-1-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-1-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-2-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-2-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-1-3-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-3-r1.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-2-0-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-0-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-1-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-1-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-2-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-2-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-2-3-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-3-r1.png?w=480" alt=""   /><br /><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="opposite-neighbours-3-0-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-0-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-1-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-1-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-2-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-2-r1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="opposite-neighbours-3-3-r" src="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-3-r1.png?w=480" alt=""   /></p>
<h3>Two adjacent neighbours</h3>
<p>If the central cell is connected to two adjacent neighbours:</p>
<p><img class="aligncenter size-full" style="margin-top:0;border:1px solid #888;" title="adjacent-neighbours" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours.png?w=480" alt=""   /></p>
<p>then there are two possible ways for the corner cell to be connected:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:180px;border-right:1px solid #888;" title="adjacent-neighbours-0x" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-0x.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-1x" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-1x.png?w=480" alt=""   /></p>
<p>and six possibilities for the long arms:<br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-x0" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x0.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-x1" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x1.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-x2" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x2.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-x3" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x3.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-x4" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x4.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-x5" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x5.png?w=480" alt=""   /></p>
<p>for a total of twelve possibilities in this orientation:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-00" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-00.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-01" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-01.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-02" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-02.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-03" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-03.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-04" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-04.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-05" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-05.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-10" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-10.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-11" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-11.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-12" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-12.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-13" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-13.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-14" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-14.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-15" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-15.png?w=480" alt=""   /></p>
<p>and the other three orientations make a total of 12×4 = 48:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-tr-00" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-00.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-01" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-01.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-02" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-02.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-03" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-03.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-04" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-04.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-05" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-05.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-tr-10" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-10.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-11" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-11.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-12" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-12.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-13" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-13.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-14" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-14.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-tr-15" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-15.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-rb-00" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-00.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-01" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-01.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-02" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-02.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-03" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-03.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-04" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-04.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-05" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-05.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-rb-10" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-10.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-11" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-11.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-12" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-12.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-13" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-13.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-14" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-14.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-rb-15" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-15.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-bl-00" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-00.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-01" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-01.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-02" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-02.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-03" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-03.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-04" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-04.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-05" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-05.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:60px;" title="adjacent-neighbours-bl-10" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-10.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-11" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-11.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-12" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-12.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-13" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-13.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-14" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-14.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="adjacent-neighbours-bl-15" src="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-15.png?w=480" alt=""   /></p>
<h3>Three neighbours</h3>
<p>If the central cell is connected to three neighbours:</p>
<p><img class="aligncenter size-full" style="margin-top:0;border:1px solid #888;" title="three-neighbours" src="http://bosker.files.wordpress.com/2011/05/three-neighbours.png?w=480" alt=""   /></p>
<p>then there are two possibilities for each corner, again, and four for the arms, for a total of 2×2×4 = 16 in this orientation:</p>
<p><img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="three-neighbours-000" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-0001.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-001" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-0011.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-002" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-0021.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-003" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-0031.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="three-neighbours-010" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-010.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-011" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-011.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-012" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-012.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-013" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-013.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="three-neighbours-100" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-100.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-101" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-101.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-102" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-102.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-103" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-103.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;margin-left:120px;" title="three-neighbours-110" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-110.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-111" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-111.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-112" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-112.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="three-neighbours-113" src="http://bosker.files.wordpress.com/2011/05/three-neighbours-113.png?w=480" alt=""   /></p>
<p>and the other three orientations bring the total to 16×4 = 64.</p>
<h3>Four neighbours</h3>
<p>The last case is where the central cell is connected to all four of its neighbours.<br />
<img class="aligncenter size-full" style="margin-top:0;border:1px solid #888;" title="four-neighbours" src="http://bosker.files.wordpress.com/2011/05/four-neighbours.png?w=480" alt=""   /><br />
Two possibilities for each corner makes 2×2×2×2 = 16:</p>
<p><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0000" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0000.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0001" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0001.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0010" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0010.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0011" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0011.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0100" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0100.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0101" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0101.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0110" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0110.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-0111" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-0111.png?w=480" alt=""   /><br />
<img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1000" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1000.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1001" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1001.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1010" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1010.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1011" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1011.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1100" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1100.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1101" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1101.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1110" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1110.png?w=480" alt=""   /><img class="alignnone size-full" style="margin-top:0;" title="four-neighbours-1111" src="http://bosker.files.wordpress.com/2011/05/four-neighbours-1111.png?w=480" alt=""   /></p>
<p>So there are <strong>a total of 192</strong> 3×3 mazes.</p>
<p>Unfortunately there’s no obvious way to generalise this nice classification to larger mazes: the next post in this series will look at a general algorithm.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=314&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/05/19/counting-small-mazes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/2x2-01.png" medium="image">
			<media:title type="html">2x2-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/2x2-11.png" medium="image">
			<media:title type="html">2x2-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/2x2-21.png" medium="image">
			<media:title type="html">2x2-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/2x2-31.png" medium="image">
			<media:title type="html">2x2-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour.png" medium="image">
			<media:title type="html">one-neighbour</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-0.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-1.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-2.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-3.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-4.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-5.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-6.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-7.png" medium="image">
			<media:title type="html">one-neighbour-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-r.png" medium="image">
			<media:title type="html">one-neighbour-0-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-r.png" medium="image">
			<media:title type="html">one-neighbour-1-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-r.png" medium="image">
			<media:title type="html">one-neighbour-2-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-r.png" medium="image">
			<media:title type="html">one-neighbour-3-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-r.png" medium="image">
			<media:title type="html">one-neighbour-4-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-r.png" medium="image">
			<media:title type="html">one-neighbour-5-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-r.png" medium="image">
			<media:title type="html">one-neighbour-6-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-r.png" medium="image">
			<media:title type="html">one-neighbour-7-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-b.png" medium="image">
			<media:title type="html">one-neighbour-0-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-b.png" medium="image">
			<media:title type="html">one-neighbour-1-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-b.png" medium="image">
			<media:title type="html">one-neighbour-2-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-b.png" medium="image">
			<media:title type="html">one-neighbour-3-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-b.png" medium="image">
			<media:title type="html">one-neighbour-4-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-b.png" medium="image">
			<media:title type="html">one-neighbour-5-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-b.png" medium="image">
			<media:title type="html">one-neighbour-6-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-b.png" medium="image">
			<media:title type="html">one-neighbour-7-b</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-0-l.png" medium="image">
			<media:title type="html">one-neighbour-0-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-1-l.png" medium="image">
			<media:title type="html">one-neighbour-1-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-2-l.png" medium="image">
			<media:title type="html">one-neighbour-2-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-3-l.png" medium="image">
			<media:title type="html">one-neighbour-3-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-4-l.png" medium="image">
			<media:title type="html">one-neighbour-4-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-5-l.png" medium="image">
			<media:title type="html">one-neighbour-5-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-6-l.png" medium="image">
			<media:title type="html">one-neighbour-6-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/one-neighbour-7-l.png" medium="image">
			<media:title type="html">one-neighbour-7-l</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours.png" medium="image">
			<media:title type="html">opposite-neighbours</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0.png" medium="image">
			<media:title type="html">opposite-neighbours-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1.png" medium="image">
			<media:title type="html">opposite-neighbours-1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2.png" medium="image">
			<media:title type="html">opposite-neighbours-2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3.png" medium="image">
			<media:title type="html">opposite-neighbours-3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-0.png" medium="image">
			<media:title type="html">opposite-neighbours-0-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-1.png" medium="image">
			<media:title type="html">opposite-neighbours-0-1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-2.png" medium="image">
			<media:title type="html">opposite-neighbours-0-2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-3.png" medium="image">
			<media:title type="html">opposite-neighbours-0-3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-0.png" medium="image">
			<media:title type="html">opposite-neighbours-1-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-1.png" medium="image">
			<media:title type="html">opposite-neighbours-1-1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-2.png" medium="image">
			<media:title type="html">opposite-neighbours-1-2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-3.png" medium="image">
			<media:title type="html">opposite-neighbours-1-3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-0.png" medium="image">
			<media:title type="html">opposite-neighbours-2-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-1.png" medium="image">
			<media:title type="html">opposite-neighbours-2-1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-2.png" medium="image">
			<media:title type="html">opposite-neighbours-2-2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-3.png" medium="image">
			<media:title type="html">opposite-neighbours-2-3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-0.png" medium="image">
			<media:title type="html">opposite-neighbours-3-0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-1.png" medium="image">
			<media:title type="html">opposite-neighbours-3-1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-2.png" medium="image">
			<media:title type="html">opposite-neighbours-3-2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-3.png" medium="image">
			<media:title type="html">opposite-neighbours-3-3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-0-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-0-0-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-1-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-0-1-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-2-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-0-2-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-0-3-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-0-3-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-0-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-1-0-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-1-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-1-1-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-2-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-1-2-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-1-3-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-1-3-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-0-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-2-0-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-1-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-2-1-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-2-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-2-2-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-2-3-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-2-3-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-0-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-3-0-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-1-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-3-1-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-2-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-3-2-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/opposite-neighbours-3-3-r1.png" medium="image">
			<media:title type="html">opposite-neighbours-3-3-r</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours.png" medium="image">
			<media:title type="html">adjacent-neighbours</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-0x.png" medium="image">
			<media:title type="html">adjacent-neighbours-0x</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-1x.png" medium="image">
			<media:title type="html">adjacent-neighbours-1x</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x0.png" medium="image">
			<media:title type="html">adjacent-neighbours-x0</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x1.png" medium="image">
			<media:title type="html">adjacent-neighbours-x1</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x2.png" medium="image">
			<media:title type="html">adjacent-neighbours-x2</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x3.png" medium="image">
			<media:title type="html">adjacent-neighbours-x3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x4.png" medium="image">
			<media:title type="html">adjacent-neighbours-x4</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-x5.png" medium="image">
			<media:title type="html">adjacent-neighbours-x5</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-00.png" medium="image">
			<media:title type="html">adjacent-neighbours-00</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-01.png" medium="image">
			<media:title type="html">adjacent-neighbours-01</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-02.png" medium="image">
			<media:title type="html">adjacent-neighbours-02</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-03.png" medium="image">
			<media:title type="html">adjacent-neighbours-03</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-04.png" medium="image">
			<media:title type="html">adjacent-neighbours-04</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-05.png" medium="image">
			<media:title type="html">adjacent-neighbours-05</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-10.png" medium="image">
			<media:title type="html">adjacent-neighbours-10</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-11.png" medium="image">
			<media:title type="html">adjacent-neighbours-11</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-12.png" medium="image">
			<media:title type="html">adjacent-neighbours-12</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-13.png" medium="image">
			<media:title type="html">adjacent-neighbours-13</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-14.png" medium="image">
			<media:title type="html">adjacent-neighbours-14</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-15.png" medium="image">
			<media:title type="html">adjacent-neighbours-15</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-00.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-00</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-01.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-01</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-02.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-02</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-03.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-03</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-04.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-04</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-05.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-05</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-10.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-10</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-11.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-11</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-12.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-12</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-13.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-13</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-14.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-14</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-tr-15.png" medium="image">
			<media:title type="html">adjacent-neighbours-tr-15</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-00.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-00</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-01.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-01</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-02.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-02</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-03.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-03</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-04.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-04</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-05.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-05</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-10.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-10</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-11.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-11</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-12.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-12</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-13.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-13</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-14.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-14</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-rb-15.png" medium="image">
			<media:title type="html">adjacent-neighbours-rb-15</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-00.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-00</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-01.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-01</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-02.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-02</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-03.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-03</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-04.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-04</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-05.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-05</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-10.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-10</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-11.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-11</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-12.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-12</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-13.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-13</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-14.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-14</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/adjacent-neighbours-bl-15.png" medium="image">
			<media:title type="html">adjacent-neighbours-bl-15</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours.png" medium="image">
			<media:title type="html">three-neighbours</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-0001.png" medium="image">
			<media:title type="html">three-neighbours-000</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-0011.png" medium="image">
			<media:title type="html">three-neighbours-001</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-0021.png" medium="image">
			<media:title type="html">three-neighbours-002</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-0031.png" medium="image">
			<media:title type="html">three-neighbours-003</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-010.png" medium="image">
			<media:title type="html">three-neighbours-010</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-011.png" medium="image">
			<media:title type="html">three-neighbours-011</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-012.png" medium="image">
			<media:title type="html">three-neighbours-012</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-013.png" medium="image">
			<media:title type="html">three-neighbours-013</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-100.png" medium="image">
			<media:title type="html">three-neighbours-100</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-101.png" medium="image">
			<media:title type="html">three-neighbours-101</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-102.png" medium="image">
			<media:title type="html">three-neighbours-102</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-103.png" medium="image">
			<media:title type="html">three-neighbours-103</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-110.png" medium="image">
			<media:title type="html">three-neighbours-110</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-111.png" medium="image">
			<media:title type="html">three-neighbours-111</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-112.png" medium="image">
			<media:title type="html">three-neighbours-112</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/three-neighbours-113.png" medium="image">
			<media:title type="html">three-neighbours-113</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours.png" medium="image">
			<media:title type="html">four-neighbours</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0000.png" medium="image">
			<media:title type="html">four-neighbours-0000</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0001.png" medium="image">
			<media:title type="html">four-neighbours-0001</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0010.png" medium="image">
			<media:title type="html">four-neighbours-0010</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0011.png" medium="image">
			<media:title type="html">four-neighbours-0011</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0100.png" medium="image">
			<media:title type="html">four-neighbours-0100</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0101.png" medium="image">
			<media:title type="html">four-neighbours-0101</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0110.png" medium="image">
			<media:title type="html">four-neighbours-0110</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-0111.png" medium="image">
			<media:title type="html">four-neighbours-0111</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1000.png" medium="image">
			<media:title type="html">four-neighbours-1000</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1001.png" medium="image">
			<media:title type="html">four-neighbours-1001</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1010.png" medium="image">
			<media:title type="html">four-neighbours-1010</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1011.png" medium="image">
			<media:title type="html">four-neighbours-1011</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1100.png" medium="image">
			<media:title type="html">four-neighbours-1100</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1101.png" medium="image">
			<media:title type="html">four-neighbours-1101</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1110.png" medium="image">
			<media:title type="html">four-neighbours-1110</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/four-neighbours-1111.png" medium="image">
			<media:title type="html">four-neighbours-1111</media:title>
		</media:content>
	</item>
		<item>
		<title>Counting mazes: before we begin</title>
		<link>http://bosker.wordpress.com/2011/05/17/counting-mazes-before-we-begin/</link>
		<comments>http://bosker.wordpress.com/2011/05/17/counting-mazes-before-we-begin/#comments</comments>
		<pubDate>Mon, 16 May 2011 23:00:31 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[mazes]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=263</guid>
		<description><![CDATA[If you’re following the maze series, I hope you’re persuaded that thinking about how to count mazes is a reasonable line of attack on the random access problem. I’ve thought a lot about how best to explain this part. I don’t really want to talk about Kirchhoff’s theorem yet, but if I don’t mention it then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=263&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you’re following the <a href="http://bosker.wordpress.com/category/mazes">maze series</a>, I hope you’re persuaded that thinking about how to count mazes is a reasonable line of attack on the random access problem.<br />
<span id="more-263"></span><br />
I’ve thought a lot about how best to explain this part. I don’t really want to talk about <a href="http://en.wikipedia.org/wiki/Kirchhoff's_theorem">Kirchhoff’s theorem</a> yet, but if I don’t mention it then those of you that know it will wonder why on earth I haven’t.</p>
<p>So I’ll say it: Kirchhoff”s theorem. It’s a wonderful theorem, which relates the problem of counting mazes to the problem of computing the determinant of a matrix. But that’s all I’m going to say about it for now.</p>
<p>The trouble is it’s not at all obvious how to compute the exact determinant of a large integer matrix efficiently, so it doesn’t automatically make our task any easier. I don’t just want to describe how to count mazes; I want to <em>explain</em> it — and bringing some fairly complicated linear algebra into the picture at this stage doesn’t really help to keep the story straightforward and comprehensible.</p>
<p>More importantly, it turns out there is a fairly natural way to tell the whole story purely in terms of mazes, so that’s what I’m going to do. Later I’ll explain how mazes relate to matrices, just to round it off.</p>
<p>Before we start, we need to formulate the problem precisely. It’s not enough just to count the number of mazes on a rectangular grid, because we also need to count the number of completions of partly-completed grids. So we need something more general.</p>
<p>There’s a simple trick that will prove incredibly useful: we’re going to think about <em>directed</em> mazes. Suppose we have a maze, say this one:</p>
<p><a href="http://bosker.files.wordpress.com/2011/05/maze-example-undirected.png"><img class="aligncenter size-full wp-image-268" title="maze-example-undirected" src="http://bosker.files.wordpress.com/2011/05/maze-example-undirected.png?w=480" alt=""   /></a></p>
<p>To convert a maze into a directed maze, you choose a cell to be the root. We could choose any of the 8×8=64 cells in this example maze, but let’s plump for the top-left. Then, in all the other cells except that one, draw an arrow in the direction of the path to the root. Like this:</p>
<p><a href="http://bosker.files.wordpress.com/2011/05/maze-example-directed-tl.png"><img class="aligncenter size-full wp-image-267" title="maze-example-directed-tl" src="http://bosker.files.wordpress.com/2011/05/maze-example-directed-tl.png?w=480" alt=""   /></a></p>
<p>Because there is always just one path from every cell to every other, so there is just one path from every other cell to the root, wherever we chose the root to be. So every cell – apart from the root itself – has an arrow pointing out of it in a direction determined unambiguously by the maze and the choice of root.</p>
<p>Once we’ve drawn the arrows, we can remove the walls. You can get them back by looking at the arrows: just draw a wall on any cell boundary that has no arrow pointing to it from either side.</p>
<p>So this gives a useful way to represent a maze:</p>
<ul>
<li>Choose one cell to be the root;</li>
<li>For every cell other than the root, draw an arrow from that cell to one neighbouring cell,</li>
<li>In such a way that there are no cycles (chains of consecutive arrows that return to the starting point).</li>
</ul>
<p>Also, we don’t only want to count the number of mazes on a grid. Just as importantly, we need to count the number of possible completions of a partial maze. As it turns out – not terribly surprisingly – the simplest way to formulate the problem in sufficient generality is to look at (directed) mazes on general graphs, rather than just grids.</p>
<p>For example, suppose we have a partially-completed 3×3 maze, like this:</p>
<p><a href="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3.png"><img class="aligncenter size-full wp-image-295" title="maze-partial-3x3" src="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3.png?w=480" alt=""   /></a></p>
<p>We are considering the possible ways this could be completed – and we’re thinking of those completions as directed mazes, so we have to choose a root. Let’s choose the top-left cell. The two cells that are definitely connected to this cell are also part of the root, so we remove all three of them:</p>
<p><a href="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-root-removed.png"><img class="aligncenter size-full wp-image-296" title="maze-partial-3x3-root-removed" src="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-root-removed.png?w=480" alt=""   /></a></p>
<p>Now we mark the possible arrows that a directed maze might have, consistent with the partial completion:<br />
<a href="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-graph.png"><img class="aligncenter size-full wp-image-294" title="maze-partial-3x3-graph" src="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-graph.png?w=480" alt=""   /></a></p>
<p>This is a sort of directed graph, but it has one slightly unusual feature: arrows that point away from a node but don’t point to another node. (We’re thinking of the root as being outside our graph, remember.) We’ll call them <em>outgoing arrows</em>.</p>
<p>So these are the graphs we’re going to think about, the substrate for our mazes: directed graphs with outgoing arrows.</p>
<p>The scene is set. Let us count!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/263/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=263&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/05/17/counting-mazes-before-we-begin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/maze-example-undirected.png" medium="image">
			<media:title type="html">maze-example-undirected</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/maze-example-directed-tl.png" medium="image">
			<media:title type="html">maze-example-directed-tl</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3.png" medium="image">
			<media:title type="html">maze-partial-3x3</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-root-removed.png" medium="image">
			<media:title type="html">maze-partial-3x3-root-removed</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/maze-partial-3x3-graph.png" medium="image">
			<media:title type="html">maze-partial-3x3-graph</media:title>
		</media:content>
	</item>
		<item>
		<title>Random access to mazes: plan of attack</title>
		<link>http://bosker.wordpress.com/2011/05/10/random-access-to-mazes-plan-of-attack/</link>
		<comments>http://bosker.wordpress.com/2011/05/10/random-access-to-mazes-plan-of-attack/#comments</comments>
		<pubDate>Tue, 10 May 2011 09:15:10 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[mazes]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=204</guid>
		<description><![CDATA[In the last post, I explained what I’m trying to do: to assign each maze an index number in such a way that we can generate a maze given its index number, and find out the index number of any maze. One way to think of it is as a perfect compression algorithm for mazes. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=204&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the <a title="What’s all this about mazes? A statement of intent." href="http://bosker.wordpress.com/2011/05/08/what%e2%80%99s-all-this-about-mazes-a-statement-of-intent/">last post</a>, I explained what I’m trying to do: to assign each maze an index number in such a way that we can generate a maze given its index number, and find out the index number of any maze. One way to think of it is as a perfect compression algorithm for mazes.</p>
<p>But how can we do that? There are a huge number of possible mazes on any decent-sized grid, so it&#8217;s not feasible to loop through them one-by-one.<br />
<span id="more-204"></span><br />
To put this in perspective, if we could somehow generate mazes at a rate of a quadrillion mazes per second – one every attosecond – it would still take ten million times the age of the universe to generate all the mazes that can be drawn on a modest 10×10 grid.</p>
<p>A source of possible hope is the fact that the mazes naturally organise themselves into a binary tree. Here’s what it looks like for the 2×2 grid:</p>
<p style="text-align:center;"><img class="aligncenter size-full wp-image-207" title="tree-of-2x2-x" src="http://bosker.files.wordpress.com/2011/05/tree-of-2x2-x.png?w=480" alt=""   /></p>
<p>We are taking the boundaries between cells – the places where wall segments might go – and considering them one-by-one. First the boundary between the top two cells is considered: on the left is the partially-drawn maze without a wall on that boundary, and on the right is the one with a wall. Then the other three boundaries are considered in order. If an impossible configuration is reached, we cross it out.</p>
<p>At the end we are left with the four possible 2×2 mazes as the remaining leaves of the tree.</p>
<p>The trees will not be very nicely balanced, as you can see in this case where three of the four mazes are on the left-hand branch. But the depth of the tree is equal to the number of cell boundaries, which in general is <strong>much</strong> smaller than the number of possible mazes.</p>
<p>On this picture I’ve written on the branches the number of mazes that branch ultimately leads to. Because the point is this: if we could, somehow, efficiently compute those numbers, then we could use these trees to solve our problem.</p>
<p>Let’s just number the mazes in the order they appear along the leaves of the tree. Then we can use the branching information to derive each of those mazes from its index number, by considering the cell boundaries in turn. In our example, we know that mazes 0–2 are on the left-hand branch, and maze 3 is on the right-hand branch. So maze 3 has a wall at twelve o’clock, and the others don’t. Now the wall at nine o’clock: on the right, it leads to an impossible configuration, so leave it out; on the left, maze 2 has it and mazes 0–1 don’t. And so on, down the tree.</p>
<p>So next time we’ll start to look at how to count mazes – or rather how to count the number of possible completions of a partial maze, which turns out to amount to more or less the same problem. If we can solve that, then we can use it to solve the random access problem.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/204/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=204&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/05/10/random-access-to-mazes-plan-of-attack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/tree-of-2x2-x.png" medium="image">
			<media:title type="html">tree-of-2x2-x</media:title>
		</media:content>
	</item>
		<item>
		<title>What’s all this about mazes? A statement of intent.</title>
		<link>http://bosker.wordpress.com/2011/05/08/what%e2%80%99s-all-this-about-mazes-a-statement-of-intent/</link>
		<comments>http://bosker.wordpress.com/2011/05/08/what%e2%80%99s-all-this-about-mazes-a-statement-of-intent/#comments</comments>
		<pubDate>Sun, 08 May 2011 21:11:59 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[mazes]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=186</guid>
		<description><![CDATA[I’m trying something that I think is going to be fun and interesting – and, who knows, maybe even useful. I want to write a program that allows “random access” to all possible mazes, and does it quickly enough to be practical. I think I know how it can be done. That’s why I’m writing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=186&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’m trying something that I think is going to be fun and interesting – and, who knows, maybe even useful.</p>
<p>I want to write a program that allows “random access” to all possible mazes, and does it quickly enough to be practical. I think I know how it can be done. That’s why I’m writing about mazes now.<br />
<span id="more-186"></span><br />
When I say “maze”, I’m talking about the sort of thing you see in cheap puzzle-books at railway stations, like this:</p>
<p><a href="http://bosker.files.wordpress.com/2011/05/23x23-rb.png"><img class="aligncenter size-full wp-image-187" title="23x23-rb" src="http://bosker.files.wordpress.com/2011/05/23x23-rb.png?w=480&#038;h=480" alt="" width="480" height="480" /></a></p>
<p>Every part of the maze is reachable from every other by precisely one path (so there are no loops). In technical terms, a maze is a spanning tree of a finite rectangular two-dimensional grid.</p>
<p>By random access I mean the following: there are about 8 × 10<sup>249</sup> different possible mazes on a 23×23 grid like the one above. To be precise, there are</p>
<p><small>8,092,641,670,349,801,721,804,781,137,713,499,621,097,587,118,741,157,248,189,969,<br />
007,805,754,000,654,382,335,518,531,150,471,089,108,161,995,245,369,248,205,451,808,<br />
922,452,989,483,589,738,382,020,054,505,965,237,374,754,791,692,455,943,268,962,118,<br />
313,156,618,241,194,351,661,577,177,461,488,002,356,030,814,981,288,323,446,734,848</small></p>
<p>of them. I want to assign each of these mazes an index number, from 0 to that huge number above, and to write a pair of programs:</p>
<ul>
<li>one that can take an index number and generate the corresponding maze,</li>
<li>one that can take a description of a maze, and return the index number of that maze.</li>
</ul>
<p>I’m not aware that anyone has done precisely this before – if they have, I’d love to hear about it.</p>
<p>But there won’t be much in the way of actual new mathematics: I think I can do it using well-established theories. On the other hand most of the mathematics is new to me, so perhaps it’s new to you as well, and it’s ever so beautiful.</p>
<p>I haven’t worked everything out yet. My desire to write about anything peaks when I think I understand it completely, and diminishes exponentially after that, with a half-life of only a week or two. If I worked everything out before starting to write this, I am sure I should never finish it. But I believe the summit is attainable, and I’ve explored enough of the route that I’m certain the journey will be enjoyable. The whole landscape is interesting enough that I mean to take a scenic route, with the occasional day-trip to enticing bordering lands.</p>
<p>It’s going to be an adventure! Do join me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=186&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/05/08/what%e2%80%99s-all-this-about-mazes-a-statement-of-intent/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/05/23x23-rb.png" medium="image">
			<media:title type="html">23x23-rb</media:title>
		</media:content>
	</item>
		<item>
		<title>The worst algorithm in the world?</title>
		<link>http://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/</link>
		<comments>http://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 01:25:02 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Fibonacci numbers]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[fibonacci]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=118</guid>
		<description><![CDATA[You know the Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … Each number is the sum of the previous two. Let’s say the zeroth Fibonacci number is zero, so: And let’s say you want to write some code to compute this function. How would you do it? Perhaps [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=118&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You know the Fibonacci numbers:</p>
<p>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …</p>
<p>Each number is the sum of the previous two. Let’s say the zeroth Fibonacci number is zero, so:</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Br%40%7B%5C%3B%7Dc%40%7B%5C%3B%7Dl%7D%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%280%29%26%3D%260%5C%5C+%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%281%29%26%3D%261%5C%5C+%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%2B2%29%26%3D%26%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%29+%2B+%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%2B1%29%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}&#92;mathop{&#92;mathrm{fib}}(0)&amp;=&amp;0&#92;&#92; &#92;mathop{&#92;mathrm{fib}}(1)&amp;=&amp;1&#92;&#92; &#92;mathop{&#92;mathrm{fib}}(n+2)&amp;=&amp;&#92;mathop{&#92;mathrm{fib}}(n) + &#92;mathop{&#92;mathrm{fib}}(n+1)&#92;end{array}' title='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}&#92;mathop{&#92;mathrm{fib}}(0)&amp;=&amp;0&#92;&#92; &#92;mathop{&#92;mathrm{fib}}(1)&amp;=&amp;1&#92;&#92; &#92;mathop{&#92;mathrm{fib}}(n+2)&amp;=&amp;&#92;mathop{&#92;mathrm{fib}}(n) + &#92;mathop{&#92;mathrm{fib}}(n+1)&#92;end{array}' class='latex' /></p>
<p>And let’s say you want to write some code to compute this function. How would you do it? Perhaps something like this? (Python code)</p>
<p><pre class="brush: python;">
def fib_rec(n):
  assert n &gt;= 0
  if n &lt; 2: return n
  return fib_rec(n-2) + fib_rec(n-1)
</pre></p>
<p>This is a pretty popular algorithm, which can be found in dozens of places online. It is also a strong candidate for the title of <em>Worst Algorithm in the World</em>. It’s not just bad in the way that <a href="http://en.wikipedia.org/wiki/Bubble_sort">Bubble sort</a> is a bad sorting algorithm; it’s bad in the way that <a href="http://en.wikipedia.org/wiki/Bogosort">Bogosort</a> is a bad sorting algorithm.<br />
<span id="more-118"></span><br />
Why so bad? Well, mainly it is quite astonishingly slow. Computing <code>fib_rec(40)</code> takes about a minute and a half, on my computer. To see why it’s so slow, let’s calculate how many calls are made to the <code>fib_rec</code> routine. If we write <img src='http://s0.wp.com/latex.php?latex=c%28n%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='c(n)' title='c(n)' class='latex' /> for the number of calls made when calculating <code>fib_rec(n)</code>, then:</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Br%40%7B%5C%3B%7Dc%40%7B%5C%3B%7Dl%7Dc%280%29%26%3D%261%5C%5C+c%281%29%26%3D%261%5C%5C+c%28n%2B2%29%26%3D%261+%2B+c%28n%29+%2B+c%28n%2B1%29%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}c(0)&amp;=&amp;1&#92;&#92; c(1)&amp;=&amp;1&#92;&#92; c(n+2)&amp;=&amp;1 + c(n) + c(n+1)&#92;end{array}' title='&#92;begin{array}{r@{&#92;;}c@{&#92;;}l}c(0)&amp;=&amp;1&#92;&#92; c(1)&amp;=&amp;1&#92;&#92; c(n+2)&amp;=&amp;1 + c(n) + c(n+1)&#92;end{array}' class='latex' /></p>
<p>So <img src='http://s0.wp.com/latex.php?latex=c%28n%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='c(n)' title='c(n)' class='latex' /> are the <a href="http://en.wikipedia.org/wiki/Leonardo_number">Leonardo numbers</a>, <img src='http://s0.wp.com/latex.php?latex=c%28n%29+%3D+2%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%2B1%29-1&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='c(n) = 2&#92;mathop{&#92;mathrm{fib}}(n+1)-1' title='c(n) = 2&#92;mathop{&#92;mathrm{fib}}(n+1)-1' class='latex' />. In other words, computing <em>fib</em> using <code>fib_rec</code> takes time <em>O</em>(fib(<em>n</em>)).</p>
<p>So computing <code>fib_rec(40)</code> involves <em>c</em>(40) = 331,160,281 calls to the <code>fib_rec</code> routine, which is pretty crazy considering it’s only called with 40 different arguments. An obvious idea for improvement is to check whether it’s being called with an argument that we’ve seen before, and just return the result we got last time.</p>
<p><pre class="brush: python;">
cached_results = {}
def fib_improved(n):
  assert n &gt;= 0
  if n &lt; 2: return n
  if n not in cached_results:
   cached_results[n] = fib_improved(n-2) + fib_improved(n-1)
  return cached_results[n]
</pre></p>
<p>That’s a huge improvement, and we can compute <code>fib_improved(40)</code> in a fraction of a millisecond, which is much better than a minute and a half. What about larger values?</p>
<p><pre class="brush: python;">
&gt;&gt;&gt; fib_improved(1000)
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875L
</pre></p>
<p>That looks good, and it’s still apparently instant. How about 10,000?</p>
<p><pre class="brush: python;">
&gt;&gt;&gt; fib_improved(10000)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;&lt;stdin&gt;&quot;, line 6, in fib_improved
  File &quot;&lt;stdin&gt;&quot;, line 6, in fib_improved
  [...]
  File &quot;&lt;stdin&gt;&quot;, line 6, in fib_improved
RuntimeError: maximum recursion depth exceeded
</pre></p>
<p>Oh dear! We’ve blown the stack. You could blame Python here for having a hard (if configurable) limit on stack size, but that’s not the real point. The problem here is that this algorithm creates <code>n</code> stack frames when you call <code>fib_improved(n)</code>, so it uses at least <em>O</em>(<em>n</em>) space.</p>
<p>(Actually it uses <em>O</em>(<em>n</em><sup>2</sup>) space &#8212; later we’ll get into why that is. Thanks to <a href="http://www.reddit.com/r/programming/comments/h9x3r/the_worst_algorithm_in_the_world/c1ttxwv">CoderKyd on reddit</a> for pointing that out.)</p>
<p>It’s easy enough to write a version that only uses constant space &#8212; well, not really: but it only uses twice as much space as we need for the end result, so it’s within a small constant factor of optimal &#8212; as long as we’re willing to forgo recursion:</p>
<p><pre class="brush: python; wrap-lines: false;">
def fib_iter(n):
  assert n &gt;= 0
  i, fib_i, fib_i_minus_one = 0, 0, 1
  while i &lt; n:
    i = i + 1
    fib_i, fib_i_minus_one = fib_i_minus_one + fib_i, fib_i
  return fib_i
</pre></p>
<p>This is much better. We can compute <code>fib_iter(100,000)</code> in less than a second (on my computer, again), and <code>fib_iter(1,000,000)</code> in about 80 seconds. Asymptotically, this algorithm takes <em>O</em>(<em>n</em><sup>2</sup>) time to compute fib(<em>n</em>).</p>
<p>(Maybe you think it should be <em>O</em>(<em>n</em>) time, because the loop runs for <em>n</em> iterations. But the numbers we’re adding are getting bigger exponentially, and you can’t add two arbitrarily-large numbers in constant time. If you think that sounds a bit theoretical, you might enjoy <a href="http://www.catonmat.net/blog/on-the-linear-time-algorithm-for-finding-fibonacci-numbers/">the post where Peteris Krumins measures</a> the running time of <code>fib_iter</code>, and explains why it’s quadratic rather than linear.)</p>
<p>That’s not the end of the story. Since the Fibonacci numbers are defined by a linear recurrence, we can express the recurrence as a matrix, and it’s easy to verify that</p>
<p align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cleft%28%5Cbegin%7Barray%7D%7Bcc%7D0%261%5C%5C1%261%5Cend%7Barray%7D%5Cright%29%5En+%3D+%5Cleft%28%5Cbegin%7Barray%7D%7Bcc%7D%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n-1%29%26%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%29%5C%5C%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%29%26%5Cmathop%7B%5Cmathrm%7Bfib%7D%7D%28n%2B1%29%5Cend%7Barray%7D%5Cright%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^n = &#92;left(&#92;begin{array}{cc}&#92;mathop{&#92;mathrm{fib}}(n-1)&amp;&#92;mathop{&#92;mathrm{fib}}(n)&#92;&#92;&#92;mathop{&#92;mathrm{fib}}(n)&amp;&#92;mathop{&#92;mathrm{fib}}(n+1)&#92;end{array}&#92;right) ' title='&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^n = &#92;left(&#92;begin{array}{cc}&#92;mathop{&#92;mathrm{fib}}(n-1)&amp;&#92;mathop{&#92;mathrm{fib}}(n)&#92;&#92;&#92;mathop{&#92;mathrm{fib}}(n)&amp;&#92;mathop{&#92;mathrm{fib}}(n+1)&#92;end{array}&#92;right) ' class='latex' /></p>
<p>Since we can get from <img src='http://s0.wp.com/latex.php?latex=%5Ctiny%5Cleft%28%5Cbegin%7Barray%7D%7Bcc%7D0%261%5C%5C1%261%5Cend%7Barray%7D%5Cright%29%5En+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;tiny&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^n ' title='&#92;tiny&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^n ' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%5Ctiny%5Cleft%28%5Cbegin%7Barray%7D%7Bcc%7D0%261%5C%5C1%261%5Cend%7Barray%7D%5Cright%29%5E%7B2n%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;tiny&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^{2n} ' title='&#92;tiny&#92;left(&#92;begin{array}{cc}0&amp;1&#92;&#92;1&amp;1&#92;end{array}&#92;right)^{2n} ' class='latex' /> by squaring, this suggests we can compute fib(<em>n</em>) using just log<sub>2</sub>(<em>n</em>) iterations:</p>
<p><pre class="brush: python;">
def bits(n):
  &quot;&quot;&quot;Represent an integer as an array of binary digits.
  &quot;&quot;&quot;
  bits = []
  while n &gt; 0:
    n, bit = divmod(n, 2)
    bits.append(bit)
  bits.reverse()
  return bits

def fib_mat(n):
  assert n &gt;= 0
  a, b, c = 1, 0, 1
  for bit in bits(n):
    a, b, c = a*a + b*b, a*b + b*c, b*b + c*c
    if bit: a, b, c = b, c, b+c
  return b
</pre></p>
<p>This is certainly much faster in practice. We can compute <code>fib_mat(1,000,000)</code> in less than a second and a half. The asymptotic complexity depends on the multiplication algorithm used. If it’s conventional long multiplication then multiplying two <em>k</em>-bit numbers takes time <em>O</em>(<em>k</em><sup>2</sup>), in which case this algorithm is actually still quadratic! I believe Python uses the <a href="http://en.wikipedia.org/wiki/Karatsuba_algorithm">Karatsuba algorithm</a>, which makes it about <em>O</em>(<em>n</em><sup>1.6</sup>) in Python.</p>
<p>While we’re writing code, let’s improve the constant factor. Each step of <code>fib_mat</code> uses six multiplications, but we can halve that just by observing that <code>c</code> can always be computed as <code>a+b</code> and rearranging things a little:</p>
<p><pre class="brush: python;">
def fib_fast(n):
  assert n &gt;= 0
  a, b, c = 1, 0, 1
  for bit in bits(n):
    if bit: a, b = (a+c)*b, b*b + c*c
    else:   a, b = a*a + b*b, (a+c)*b
    c = a + b
  return b
</pre></p>
<p>And this does indeed run about twice as fast. Further improvement is possible, but I think the point has been made so let’s leave it there. If you want to see a super-efficient version, have a look at <a href="http://gmplib.org/manual/Fibonacci-Numbers-Algorithm.html">the algorithm in GMP</a>.</p>
<p>Another good article on the subject: <a href="http://www.ics.uci.edu/~eppstein/161/960109.html">David Eppstein’s lecture notes</a>, which cover similar ground to this.</p>
<p>If you’re thinking “This is silly! You can just compute Fibonacci numbers using the closed-form expression.” then you’ll probably enjoy the follow-up article on <a href="http://bosker.wordpress.com/2011/07/27/computing-fibonacci-numbers-using-binet’s-formula/">computing Fibonacci numbers using Binet’s formula</a>.</p>
<p><small>Are you wondering what this has to do with mazes? We’ll get to that eventually, I hope…</small></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=118&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>
	</item>
		<item>
		<title>I’m planning to write a few things about mazes.</title>
		<link>http://bosker.wordpress.com/2011/04/27/112/</link>
		<comments>http://bosker.wordpress.com/2011/04/27/112/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 22:11:59 +0000</pubDate>
		<dc:creator>Robin Houston</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[mazes]]></category>

		<guid isPermaLink="false">http://bosker.wordpress.com/?p=112</guid>
		<description><![CDATA[I had planned a short series of long, interactive essays, but it seems clear that I won&#8217;t get round to finishing them if I do it like that, so I’m going to try writing a few shorter more self-contained blog posts and see if anyone is interested. I started to think about mazes after reading [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=112&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had planned a short series of long, interactive essays, but it seems clear that I won&#8217;t get round to finishing them if I do it like that, so I’m going to try writing a few shorter more self-contained blog posts and see if anyone is interested.</p>
<p>I started to think about mazes after reading <a href="http://weblog.jamisbuck.org/">Jamis Buck’s</a> excellent series of posts, and found that mazes lead quite directly to some interesting mathematics and some remarkable algorithms. So I’d like to share some of that here.</p>
<p>To whet your appetite, here is an exhaustive list of the 3×3 <a href="http://weblog.jamisbuck.org/2011/3/4/maze-generation-weave-mazes">weave mazes</a>:</p>
<p><a href="http://bosker.files.wordpress.com/2011/04/all-the-3x3-weave-mazes.png"><img class="aligncenter size-full wp-image-113" title="All the 3x3 weave mazes" src="http://bosker.files.wordpress.com/2011/04/all-the-3x3-weave-mazes.png?w=480&#038;h=480" alt="" width="480" height="480" /></a>I may or may not do it all on this site. Ideally I would like to post some interactive demos, which isn’t possible here.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bosker.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bosker.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bosker.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bosker.wordpress.com&amp;blog=235722&amp;post=112&amp;subd=bosker&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bosker.wordpress.com/2011/04/27/112/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b7d4b678a3b4ff1b80f5c6e70d87d04e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bosker</media:title>
		</media:content>

		<media:content url="http://bosker.files.wordpress.com/2011/04/all-the-3x3-weave-mazes.png" medium="image">
			<media:title type="html">All the 3x3 weave mazes</media:title>
		</media:content>
	</item>
	</channel>
</rss>
