<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Ashutosh Mehra's Blog</title>
	
	<link>http://ashutoshmehra.net/blog</link>
	<description>Notes on Math, Computer Science &amp; Programming</description>
	<lastBuildDate>Sat, 27 Feb 2010 14:22:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AshutoshMehraBlog" /><feedburner:info uri="ashutoshmehrablog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>RtlWriteDecodedUcsDataIntoSmartLBlobUcsWritingContext and Other Long Function Names</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/xpmZpYO4pcg/</link>
		<comments>http://ashutoshmehra.net/blog/2010/02/long-function-names/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 14:21:27 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Humor]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=633</guid>
		<description><![CDATA[Every reader should ask himself periodically &#8220;Toward what end, toward what end?&#8221; &#8212; but do not ask it too often lest you pass up the fun of programming for the constipation of bittersweet philosophy.
&#8211; Alan J. Perlis [foreword to SICP]


I find long functions names charming &#8212; I think they impart a certain personality to the [...]]]></description>
			<content:encoded><![CDATA[<p><i>Every reader should ask himself periodically &#8220;Toward what end, toward what end?&#8221; &#8212; but do not ask it too often lest you pass up the fun of programming for the constipation of bittersweet philosophy.<br/>
<p style="text-align:right">&#8211; Alan J. Perlis [<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-5.html">foreword to SICP</a>]</p>
<p></i></p>
<div class="alignright" style="margin-left: 5px"><img border="0" src="http://ashutoshmehra.net/images/posts/funcnames/RtlWriteDecodedUcsDataIntoSmartLBlobUcsWritingContext.png" width="250px" alt="RtlWriteDecodedUcsDataIntoSmartLBlobUcsWritingContext (53 characters)"></div>
<p>I find long functions names charming &#8212; I think they impart a certain personality to the API and add to the <i>fun</i> of programming. And while I try to avoid excessively long names in my own code at work (unless they add to clarity), I never fail to get a good kick seeing such names used by other programmers! <span id="more-633"></span></p>
<p>I&#8217;ve often wondered what the longest names would look like. So I decided to explore a large system &#8212; the system DLLs in <tt>C:\Windows</tt> directory &#8212; and dig up some of the biggies. My plan was to write a script that walked through the installed DLLs and looked at their exported functions list for interesting candidates.</p>
<h4>The Winners</h4>
<p>Here are the longer ones I could find:</p>
<ul>
<li><tt>RtlWriteDecodedUcsDataIntoSmartLBlobUcsWritingContext</tt> [wcp.dll], with 53-characters, was the longest one (but this function is not documented)</li>
<li>(52) <a href="http://msdn.microsoft.com/en-us/library/aa376397%28VS.85%29.aspx"><tt>ConvertSecurityDescriptorToStringSecurityDescriptor{A,W}</tt> [advapi32.dll]</a>, the function that <a href="http://en.wikipedia.org/wiki/Raymond_Chen">Raymond Chen</a> admitted was the reason for <a href="http://blogs.msdn.com/oldnewthing/archive/2004/03/12/88572.aspx">his post on security descriptors</a>.
<li>(52) <a href="http://msdn.microsoft.com/en-us/library/aa376397%28VS.85%29.aspx"><tt>ConvertStringSecurityDescriptorToSecurityDescriptor{A,W}</tt> [advapi32.dll]</a>, the complement of the above.</li>
<li>(50) <a href="http://msdn.microsoft.com/en-us/library/aa446582%28VS.85%29.aspx"><tt>CreatePrivateObjectSecurityWithMultipleInheritance</tt> [advapi32.dll]</a>, another security function.</li>
<li>(50) <a href="http://msdn.microsoft.com/en-us/library/aa376038%28VS.85%29.aspx"><tt>CertCreateCTLEntryFromCertificateContextProperties</tt> [crypt32.dll]</a>.</li>
<li>(50) <a href="http://msdn.microsoft.com/en-us/library/bb204685%28VS.85%29.aspx"><tt>EapHostPeerQueryUIBlobFromInteractiveUIInputFields</tt> [eappcfg.dll]</a>.</li>
<li>(49) <a href="http://msdn.microsoft.com/en-us/library/aa374843%28VS.85%29.aspx"><tt>AccessCheckByTypeResultListAndAuditAlarmByHandle{A,W}</tt> [advapi32.dll]</a>, yet another security API &#8212; I guess the security API programmers get a good kick from long names, just like me!</li>
<li>(47) <a href="http://msdn.microsoft.com/en-us/library/dd692949%28VS.85%29.aspx"><tt>GetNumberOfPhysicalMonitorsFromIDirect3DDevice9</tt> [Dxva2.lib]</a>.</li>
<li>(43) <a href="http://msdn.microsoft.com/en-us/library/aa377432%28VS.85%29.aspx"><tt>SetupRemoveInstallSectionFromDiskSpaceList{A,W}</tt> [Setupapi.dll]</a>.</li>
</ul>
<p>And just so that no one thinks I&#8217;m making these up, I&#8217;ve linked to their documentation!</p>
<p>Curious on seeing these names, I dug through the other &#8220;operating system&#8221; I had access to &#8212; GNU EMACS. The longest interactive command there is <tt>slime-compiler-notes-default-action-or-show-details/mouse</tt>: Counting punctuations, this beats the longest Windows export by 4 characters.</p>
<h4>The Method</h4>
<p>I used the <a href="http://code.google.com/p/pefile/">pefile Python library</a> to parse all DLLs in my <tt>windows</tt> directory. I only considered &#8220;system&#8221; DLLs (ignoring any third-party drivers etc.) by checking for &#8220;Microsoft&#8221; in the DLL copyright-string. In addition, I discarded any C++-ish exports, because the <a href="http://en.wikipedia.org/wiki/Name_mangling">C++ name-mangling</a> skewed the results too much and I was too lazy to hook in a &#8220;undecoratify&#8221; procedure.</p>
<p>Finally, on my 64-bit windows installation, there are both 32-bit and 64-bit versions of many core DLLs (in <tt>System32</tt> and <tt>SysWOW64</tt> directories respectively), and I encountered duplicates. A similar thing happened with <a href="http://msdn.microsoft.com/en-us/library/aa376307%28VS.85%29.aspx">SxS</a> DLLs.</p>
<p>Using <a href="https://gist.github.com/230c3f53261a20340118">this Python script I coded</a>, I generated a delimitered text-file with around 1500 entries that I manually scanned (I had some time to waste!) for &#8220;interesting&#8221; names. Below is a histogram of the function-name lengths. The rough bell shape gives me confidence that script wasn&#8217;t totally off the mark.</p>
<p><a href="http://ashutoshmehra.net/images/posts/funcnames/histogram_large.png"><img border="0" src="http://ashutoshmehra.net/images/posts/funcnames/histogram_large.png" alt="A histogram of the length of functions names exported by system DLLs in the Windows directory"></a></p>
<h4>Functions Taking Lots of Parameters</h4>
<p>A second axis to dig &#8220;interesting&#8221; functions would be to count the <i>number of function params</i>. </p>
<p>This one is a bit of fuzzy due to questions like &#8220;Do you count <i>deep</i>&#8220;? In that when a function accepts a pointer to a struct, do you count the struct members as inputs too? For instance, <a href="http://msdn.microsoft.com/en-us/library/dd183501%28VS.85%29.aspx"><tt>CreateFontIndirectEx</tt> [gdi32.dll]</a> takes a pointer to <a href="http://msdn.microsoft.com/en-us/library/dd162628%28VS.85%29.aspx"><tt>ENUMLOGFONTEXDV</tt></a> structure that holds about two dozen items (all things considered).</p>
<p>Counting params is also more work, at least for DLL exports (where you need to cross-reference with documentation/headers if the export is, at all, public). Otherwise, the problem can be approached differently by forgetting DLL exports entirely and instead using a crawler that walks the the locally installed MSDN and parses the &#8220;Syntax&#8221; section to count the number of params &#8212; just assuming <tt>num_params = num_commas + 1</tt> might be good enough.</p>
<p>Anyway, manually browsing through some of MSDN, I chanced upon a few gems:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/aa374843%28VS.85%29.aspx"><tt>AccessCheckByTypeResultListAndAuditAlarmByHandle</tt></a>, our friend from above, true to its character, takes 17 parameters!</li>
<li><a href="http://msdn.microsoft.com/en-us/library/ms690529%28VS.85%29.aspx"><tt>OleCreateFromFileEx</tt></a> takes in a filename, interface ID, flags, sink, connection, site&#8230; 13 in all.</li>
</ul>
<h4>On a Serious Note</h4>
<p>Well named functions (just like well named variables) are good instant documentation. <i>Descriptive</i> function are important when:</p>
<ul>
<li>Such functions all belong to a flat namespace (DLL exports or C code)</li>
<li>Several of them have very similar purpose (like the six or so <tt>AccessCheck*</tt> APIs)</li>
</ul>
<p>Such names become even more relevant when they define the public API of your library/system.</p>
<p>Some people (&#8220;constipated by bittersweet philosophy&#8221;?) dislike long function names, and I wonder why. The argument that longer functions take longer to type seems mostly dud: Visual Studio with the awesome <a href="http://www.wholetomato.com/">Visual Assist X</a> addon does Intellisense beautifully; Emacs with <tt><a href="http://www.emacswiki.org/emacs/HippieExpand">hippie-expand</a></tt> does some good magic (the brave also have <a href="http://cedet.sourceforge.net/intellisense.shtml">CEDET</a>); Eclipse, BBEdit, and Vim surely have their own thing. So typing isn&#8217;t a problem. And with machines so fast and powerful, the (JIT)compilation/linking-time for longer function names should be irrelevant in most cases.</p>
<h4>Links</h4>
<p>While researching for this entry, I came across the post <a href="http://blogs.msdn.com/brada/archive/2005/01/09/349678.aspx">&#8220;Best&#8221; method names ever</a> by <a href="http://blogs.msdn.com/brada/default.aspx">Brad Abrams</a> that has some interesting content/comments.</p>
<p><i>[Note: Lest someone should give a wicked twist to the whole point of this post, I should add that I have most sincere respect for Microsoft engineers, enjoy using their programs every day and working with their APIs.]</i></p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/xpmZpYO4pcg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2010/02/long-function-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2010/02/long-function-names/</feedburner:origLink></item>
		<item>
		<title>The Wire Identification Problem</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/gOoAriuoHRo/</link>
		<comments>http://ashutoshmehra.net/blog/2009/09/wire-identification-problem/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 10:10:01 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=598</guid>
		<description><![CDATA[
A bunch of n wires have been labeled at one end with alphabetic codes A, B&#8230; The wire identification problem asks for an efficient procedure to mark the other end of the bunch with the corresponding labels. The wires run underground so you can&#8217;t track them individually and any wire is visibly indistinguishable from any [...]]]></description>
			<content:encoded><![CDATA[<div class="alignleft" style="margin-right: 5px"><img border="0" src="http://ashutoshmehra.net/images/posts/wireident/wire_ident.png" width="250px" alt="The Wire Identification Problem: Try to label the wires on the right with labels corresponding to those on the left"></div>
<p>A bunch of <it>n</it> wires have been labeled at one end with alphabetic codes <tt>A</tt>, <tt>B</tt>&#8230; The <i>wire identification problem</i> asks for an efficient procedure to mark the other end of the bunch with the corresponding labels. The wires run underground so you can&#8217;t track them individually and any wire is visibly indistinguishable from any other (except for the labeling). <span id="more-598"></span></p>
<p>As you might have guessed, the only way to gain information in this setup is to connect some set of wires at one end, walk up to the other end, and test for conductivity &#8212; this would give some partial labeling information. The process may have to be repeated many times before a complete labeling can be constructed. And since each such step involves walking across the distance of the wires, we wish to solve the problem <i>with the least number of trips</i>.</p>
<h4>Small Cases</h4>
<p>It should be easy to convince oneself that this problem cannot be solved when <tt>n = 2</tt> &#8212; there&#8217;s really nothing we can use to break the symmetry of the situation.</p>
<div class="alignright" style="margin-left: 5px"><img border="0" src="http://ashutoshmehra.net/images/posts/wireident/three_wires.png" width="200px" alt="Solution to the wire identification problem of 3 wires with just two trips"></div>
<p>For <tt>n = 3</tt>, we can use the following simple procedure (see the illustration on the right):</p>
<ul>
<li><i>Step 1</i>: Connect the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed6558df25ce694119274d78624a4df9.png" title="B" style="vertical-align:-20%;" class="tex" alt="B" /> wires at the labeled end, walk up to the unlabeled end, and test for conductivity. The pair that conducts electricity would be <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, although we cannot determine exactly which is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> or <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> at this point, and the remaining wire is uniquely identified as <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8390854cea1afc6c349c4c26b8bebb3c.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />. For notation, we use lowercase letters for the labelings that we discover, versus uppercase letters for the given labeling.</li>
<p><br/></p>
<li><i>Step 2</i>: Connect the newly identified wire <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8390854cea1afc6c349c4c26b8bebb3c.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" /> with any one of the wires <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a507101e260cbc3dfcd3d8748fcbf821.png" title="a/b" style="vertical-align:-20%;" class="tex" alt="a/b" /> (recall, we don&#8217;t know which is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> or <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> individually, only that one is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and the other <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />). Now walk back to the labeled side and test for conductivity of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7f4c3f605551dd79f35a2b26c504b279.png" title="C" style="vertical-align:-20%;" class="tex" alt="C" /> with the remaining wires. If we find that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7f4c3f605551dd79f35a2b26c504b279.png" title="C" style="vertical-align:-20%;" class="tex" alt="C" />&#8211;<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed6558df25ce694119274d78624a4df9.png" title="B" style="vertical-align:-20%;" class="tex" alt="B" /> is conducting, then the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a507101e260cbc3dfcd3d8748fcbf821.png" title="a/b" style="vertical-align:-20%;" class="tex" alt="a/b" /> wire we hooked up on the other side was really <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, otherwise it was <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.
</ul>
<p>The case <tt>n = 4</tt> can be solved similarly with just two trip: Hook up <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" />&#8211;<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed6558df25ce694119274d78624a4df9.png" title="B" style="vertical-align:-20%;" class="tex" alt="B" /> on one side and then run conductivity tests on the other side to partition the wires into two groups <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />&#8211;<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8390854cea1afc6c349c4c26b8bebb3c.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />&#8211;<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_497cff51c95d215717abe330faa6c838.png" title="d" style="vertical-align:-20%;" class="tex" alt="d" />; then hook up one from each of the two groups together etc.</p>
<p>So the question is: Can the problem be solved for all <tt>n</tt> with just two trips?</p>
<h4>The General Case</h4>
<p>The wire identification problem is one of those puzzles that is deceptively simple in its description, whose small cases are easy enough to solve in the head, and yet a fair amount of math goes into proving the general result.</p>
<p>I first read about this fascinating problem in the Don Knuth&#8217;s <a href="http://www-cs-faculty.stanford.edu/~knuth/dm.html">Selected Papers on Discrete Mathematics</a> book &#8212; in a short 5-page paper titled <a href="http://portal.acm.org/citation.cfm?id=229834.229849">The Knowlton&#8211;Graham partition problem</a> (<a href="http://arxiv.org/abs/math/9502237">arXiv link</a>).</p>
<p>Knuth states the following idea, attributed to <a href="http://en.wikipedia.org/wiki/Ken_Knowlton">Kenneth Knowlton</a>, to solve the general problem:</p>
<blockquote style="background-color: #ffffff"><p>Partition <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_37db547b4d62a40a25f7582fd757155f.png" title="\{1,\ldots,n\}" style="vertical-align:-20%;" class="tex" alt="\{1,\ldots,n\}" /> into disjoint sets in two ways <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a9d5bb33753d3bce556e4872680864d7.png" title="A_1,\ldots,A_p" style="vertical-align:-20%;" class="tex" alt="A_1,\ldots,A_p" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_49ce03efedd641691e0db4bf08b75cc2.png" title="B_1,\ldots,B_q" style="vertical-align:-20%;" class="tex" alt="B_1,\ldots,B_q" />, subject to the condition that at most one element appears in <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" />-set of cardinality <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> and a <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed6558df25ce694119274d78624a4df9.png" title="B" style="vertical-align:-20%;" class="tex" alt="B" />-set of cardinality <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" />, for each <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" />. We can then use the coordinates <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_361a29e906ad728b0b44b89f8f4f02a2.png" title="(j,k)" style="vertical-align:-20%;" class="tex" alt="(j,k)" /> to identify each element.</p></blockquote>
<p>By a theorem of <a href="http://math.ucsd.edu/~fan/ron/">Ronald Graham</a>, a solution involving just two trip always exists for <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> wires unless <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> is 2, 5, or 9 (<a href="http://www.math.ucsd.edu/~sbutler/ron/66_01_finite_set.pdf">On partitions of a finite set [PDF]</a>).</p>
<p>Knuth in <a href="http://arxiv.org/abs/math/9502237">his paper</a> translates the problem of Knowlton-Graham partitions in terms of <a href="http://mathworld.wolfram.com/01-Matrix.html">0/1-matrices</a>. Lemma 1 in that paper states: <i>Knowlton-Graham partitions exist for <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> <u>iff</u> there&#8217;s a 0/1-matrix having row sums <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1adb696d2fc8c83e04fd21d967222530.png" title="(r_1,\ldots,r_m)" style="vertical-align:-20%;" class="tex" alt="(r_1,\ldots,r_m)" /> and column sums <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c2748e393a6f1496187a0ca941f98d8c.png" title="(c_1,\ldots,c_m)" style="vertical-align:-20%;" class="tex" alt="(c_1,\ldots,c_m)" /> such that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1cb553dc51f902cad549c8919e078958.png" title="r_j" style="vertical-align:-20%;" class="tex" alt="r_j" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b47b4b1a76f49711d2704abe2f6125fc.png" title="c_j" style="vertical-align:-20%;" class="tex" alt="c_j" /> are multiples of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5a98011eff00f5af57a04e0e13e6d265.png" title="\sum r_j = \sum c_j = n" style="vertical-align:-20%;" class="tex" alt="\sum r_j = \sum c_j = n" /></i>.</p>
<p>Let&#8217;s use this lemma to label a set of 6 wires (<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a1b754dce3ce7b8bcc6fe139145a78b5.png" title="A\ldots F" style="vertical-align:-20%;" class="tex" alt="A\ldots F" />). Since we can write 6 = 1 + 2 + 3, a rather obvious matrix satisfying the conditions of the lemma is: <br/><br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c249fe859c8806537d69ee529c481613.png" title="\left(\begin{array}{ccc}0 &#038; 0 &#038; 1\\ 0 &#038; 1 &#038; 1\\ 1 &#038; 1 &#038; 1\end{array}\right)\quad" style="vertical-align:-20%;" class="tex" alt="\left(\begin{array}{ccc}0 &#038; 0 &#038; 1\\ 0 &#038; 1 &#038; 1\\ 1 &#038; 1 &#038; 1\end{array}\right)\quad" />, corresponding to the assignment <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e547ae3187532f0182c3897f5580279b.png" title="\left(\begin{array}{ccc}0 &#038; 0 &#038; A\\ 0 &#038; B &#038; C\\ D &#038; E &#038; F\end{array}\right)" style="vertical-align:-20%;" class="tex" alt="\left(\begin{array}{ccc}0 &#038; 0 &#038; A\\ 0 &#038; B &#038; C\\ D &#038; E &#038; F\end{array}\right)" /></center></p>
<p>Guided by this matrix, here&#8217;s our two-trip identification procedure:</p>
<ul>
<li>On the labeled side, hook up wires in the following groups: <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_dd131ab893e404d89956b6c7c8ea6058.png" title="\{A\}" style="vertical-align:-20%;" class="tex" alt="\{A\}" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9d26d3f7e33560299fc1e1c2c4b808e8.png" title="\{B,C\}" style="vertical-align:-20%;" class="tex" alt="\{B,C\}" />, and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_935f387dca435507c4d94acbd0e69328.png" title="\{D,E,F\}" style="vertical-align:-20%;" class="tex" alt="\{D,E,F\}" />.</li>
<li>Walk over to the unlabeled side, run the conductivity tests, and make the corresponding groups <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8089cdf57099db6f489f74687a927e6f.png" title="\{a\}" style="vertical-align:-20%;" class="tex" alt="\{a\}" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_078e413ea9156a97c7716bd7ead529b2.png" title="\{b,c\}" style="vertical-align:-20%;" class="tex" alt="\{b,c\}" />, and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c9056f32dd1902dcbb401ccb2837bc13.png" title="\{d,e,f\}" style="vertical-align:-20%;" class="tex" alt="\{d,e,f\}" />.</li>
<li>Still on the unlabeled side, hook up the wires in the groups <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_dcae3eaf383d2d66eabcb6b231a33dd3.png" title="\{d/e/f\}" style="vertical-align:-20%;" class="tex" alt="\{d/e/f\}" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c2b281670bf0764b3d420099860d2971.png" title="\{b/c, d/e/f\}" style="vertical-align:-20%;" class="tex" alt="\{b/c, d/e/f\}" />, and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_88cf1c09415f6fdfb949394783147ed1.png" title="\{a, b/c, e/f/g\}" style="vertical-align:-20%;" class="tex" alt="\{a, b/c, e/f/g\}" />. Note that we still don&#8217;t know exactly which wire is, say, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />; but we do know the two wires of which one if <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_55490d5b9084c964ed789b39be71bbad.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> and the other <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8390854cea1afc6c349c4c26b8bebb3c.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />.</li>
<li>Walk over to the labeled side, unhook the connection we had made in the first step, and check for conductivity. The sole wire here, say, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_4bdee45bb8394ab19c11eac5997c8153.png" title="F" style="vertical-align:-20%;" class="tex" alt="F" /> would correspond to the the sole wire from the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_f9918e717de7e50dffda0ec33b48cf5b.png" title="e/f/g" style="vertical-align:-20%;" class="tex" alt="e/f/g" /> group on the other side. Similarly, the connected-pair of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_70855464e1a033461d96eee013edaced.png" title="B/C" style="vertical-align:-20%;" class="tex" alt="B/C" /> &#8212; <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_18744d201a45f8f04592f7cfb03ea92d.png" title="E/F/G" style="vertical-align:-20%;" class="tex" alt="E/F/G" /> corresponds to the pair <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c2b281670bf0764b3d420099860d2971.png" title="\{b/c, d/e/f\}" style="vertical-align:-20%;" class="tex" alt="\{b/c, d/e/f\}" /> on the other side. etc.</li>
</ul>
<h4>Extensions</h4>
<p><a href="http://research.microsoft.com/en-us/people/navingo/default.aspx">Navin Goyal</a>, Sachin Lodha, and <a href="http://www.cs.rutgers.edu/~muthu/">S. Muthukrishnan</a> consider some variants &#038; generalizations of the wire identification problem in their paper <a href="http://portal.acm.org/citation.cfm?id=1132710">The Graham-Knowlton Problem Revisited</a>. In particular, they consider the restrictions to just hooking up pairs in the identification process (in our examples above, we were free to hook, say, 3 wires).</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/gOoAriuoHRo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/09/wire-identification-problem/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/09/wire-identification-problem/</feedburner:origLink></item>
		<item>
		<title>Kernighan &amp; Plauger on Programming Style</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/gpQwEt1ZAhg/</link>
		<comments>http://ashutoshmehra.net/blog/2009/06/kernighan-plauger-programming-style/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 17:20:43 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=556</guid>
		<description><![CDATA[

  h4 {
  margin-top: 15px;
  }
  span.tipitem span {
  font-style: italic;
  font-weight: normal;
  color: #000000;
  while-space: nowrap;
  margin: 0 5px 0 5px;
  }
  ol.importantlist li {
  font-weight: bold;
  color: #60743A;
  margin-bottom: 10px;
  }

Last week I finished reading Kernighan &#038; Plauger&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div class="alignright" style="margin-left: 5px"><a href="http://www.amazon.com/gp/product/0070342075?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0070342075"><img border="0" src="http://ashutoshmehra.net/images/posts/teops/the_elements_of_programming_style_small_front.jpg" width="100px"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=0070342075" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<style type="text/css">
  h4 {
  margin-top: 15px;
  }
  span.tipitem span {
  font-style: italic;
  font-weight: normal;
  color: #000000;
  while-space: nowrap;
  margin: 0 5px 0 5px;
  }
  ol.importantlist li {
  font-weight: bold;
  color: #60743A;
  margin-bottom: 10px;
  }
</style>
<p>Last week I finished reading <a href="http://www.cs.bell-labs.com/who/bwk/index.html">Kernighan</a> &#038; <a href="http://www.plauger.com/index.html">Plauger</a>&#8217;s beautiful book <a href="http://en.wikipedia.org/wiki/The_Elements_of_Programming_Style">The Elements of Programming Style</a>, the classic that <a href="http://www.plauger.com/books.html">pioneered</a> the term <i><a href="http://en.wikipedia.org/wiki/Programming_style">programming style</a></i>. I&#8217;ve excerpted below some rules of style from that book. I hope these get you excited to reading the book too!</p>
<p><span id="more-556"></span></p>
<h4>How the Book Works</h4>
<p>Here&#8217;s how the book works: The authors pick up a simple &#8220;real-world&#8221; program (mostly from other programming texts) and comment critically on its style. For analyzing the style, they look at the expressions and statements (at the lowest level), program and control structures, I/O-handling, program efficiency and documentation. In addition to suggesting what&#8217;s wrong with the code, the authors fix it, sometimes rewriting the whole thing, to produce a simpler, cleaner, sometimes more efficient, and always a more obviously correct program. </p>
<p>The experience is that of watching two master programmers doing a code-review!</p>
<p>You may read the text in one run. Or, you may challenge yourself and use it as a &#8220;problem-book&#8221; by studying the programs and analyzing them for style and fixing them before reading what the masters have to comment.</p>
<p>In all, it is a very pragmatic book &#8212; full of useful, practical advice.</p>
<p>A word of caution is due: The program fragments are in <a href="http://en.wikipedia.org/wiki/Fortran">Fortran</a> and <a href="http://en.wikipedia.org/wiki/PL/I">PL/I</a>. And while they use only the basic features of the language, it is still somewhat of a quest to figure out the meaning of the longer Fortran programs infested with GOTOs and arithmetic-IFs. PL/I is much easier to read.</p>
<h4>Reaffirm Your Own Beliefs of Good Style</h4>
<p>There&#8217;s probably <i>not much new stuff</i> you&#8217;ll find in this book. Most things the authors say are perhaps ones you already knew, believed, and agreed with (if only subconsciously). And yet it is a delight to read the whole thing if only because it reaffirms those beliefs. This is close to how I felt when reading <i><a href="http://www.pragprog.com/the-pragmatic-programmer">The Pragmatic Programmer</a></i> &#8212; that book hardly had anything <i>new</i>, and yet it was a sheer pleasure to read.</p>
<div class="alignleft" style="margin-right: 5px"><a href="http://en.wikipedia.org/wiki/Brian_Kernighan"><img border="0" src="http://ashutoshmehra.net/images/posts/teops/kernighan_small.jpg" width="75px"></a></div>
<div class="alignleft" style="margin-right: 10px"><a href="http://en.wikipedia.org/wiki/P._J._Plauger"><img border="0" src="http://ashutoshmehra.net/images/posts/teops/plauger_small.jpg" width="75px"></a></div>
<p>Programming is far from being a exact science &#8212; perhaps it is an <a href="http://awards.acm.org/images/awards/140/articles/7143252.pdf">art</a> (or engineering?). As we, programmers, work day after day doing what we do, we build many notions (or guiding principles) about our craft. From reading books to blogging, from studying other people&#8217;s code to writing our own to fixing bugs, from discussions with colleagues &#8212; the entire software development experience helps us build various concepts of what constitutes good practices of programming. And once in a while, it is reassuring to have all these notions validated by the masters &#8212; expert programmers, like Kernighan and Plauger, who we can safely trust to know about the art.</p>
<p>Reading this book will give such a reaffirmation to your own principles of programming. And if (alas!) you had been harboring some truly deviant ideas about programming, reading it would hopefully help set them right.</p>
<h4>What has Changed in These Three Decades?</h4>
<p>It&#8217;s been more than three decades since the second edition of the book came out in 1978. So it is natural to wonder what, if anything, has changed about how we program and what we consider good programs. To what extent have our programming languages and programming principles advanced through the years? Is this book still relevant?</p>
<p>To be honest, I wasn&#8217;t even born until many years after the book came out, so I&#8217;m naturally not qualified to comment. But studying how the programs in the text, perhaps typical of their age, were written, I couldn&#8217;t help but notice the following.</p>
<p>There has been definite improvement in the following respects:</p>
<ul>
<li><i>Structured programming</i> and <i>structured control statements</i> (like <tt>if/then/else</tt>, <tt>for</tt>, <tt>while</tt>, <tt>break</tt>, <tt>continue</tt>, <tt>yield</tt>) have made some of the points (mainly about <tt>goto</tt>, but also some others) in this book less relevant.</li>
<li>Support for <i>recursion</i> appears to be univerally available in all &#8220;modern&#8221; languages I can think of. (To contrast, the original Fortran didn&#8217;t allow recursion.)</li>
<li>Python/Haskell-style <i>layout</i> has solved many problems related to block-indentation.</li>
<li><i>Functional languages</i> like Haskell have made certain class of problems, like forgetting initialization, close to impossible. (And while functional languages definitely existed many decades ago, I think they are much more widely known &#8212; I may be wrong in this.)</li>
<li><i>Profilers and other instrumentation tools</i> for measuring timing performance and &#8220;hotspots&#8221; are available in plenty, though perhaps not used enough.</li>
<li><i>Module/Package/Namespace systems</i> are available in many mainstream languages.</li>
</ul>
<p>But we still have to routinely solve the same old problems and we still make the same old mistakes when solving them: What is the best way to design/structure correct and reliable programs? How to best validate input? How to correctly program with floating point numbers? &#8230; The book would perhaps provide some guidance with respect to these questions.</p>
<h4>The Rest of the Post&#8230;</h4>
<p>I&#8217;ve tried to collect some of the timeless wisdom of Kernighan and Plauger&#8217;s words as quotations in the remainder of the post. </p>
<p>Each chapter in their book is sprinkled with several terse lines summarizing the essence of the discussion. I&#8217;ve also collected some of those towards the end. If you enjoy and appreciate these, <i>you would definitely want to read the whole book</i>. As far as programming books go, this is quite thin (the size of K&amp;R), so you have a good chance of actually finishing it!</p>
<h4>On Obscure Code</h4>
<p>The introductory chapter gives an example of how a Fortran program used the expression <tt>(I/J)*(J/I)</tt> to initialize <tt>V(I,J)</tt> to an identity matrix! Notice that with integer arithmetic, assume <tt>i</tt> and <tt>j</tt> are nonzero, <tt>(I/J)*(J/I)</tt> is same as <tt>I == J ? 1 : 0</tt>.</p>
<p>The author&#8217;s point out why such code is wrong:</p>
<blockquote><p>The problem with obscure code is that debugging and modification become much more difficult, and these are already the hardest aspects of computer programming. Besides, there is the added danger that a too-clever program may not say what you thought it said. <i>(Page 2)</i></p></blockquote>
<h4>On Healthy Skepticism</h4>
<p>The first chapter ends with an advice on healthy skepticism:</p>
<blockquote><p>Nevertheless, mistakes can occur. We encourage you to view with suspicion anything we say that looks peculiar. Test it, try it out. Don&#8217;t treat computer output as gospel. If you learn to be wary of everyone else&#8217;s programs, you will be better able to check your own. <i>(Page 7)</i></p></blockquote>
<p>This is reminiscent of Feynman&#8217;s <a href="http://www.feynmanlectures.info/flp_errata.html">words</a> <i>&#8220;You should, in science, believe logic and arguments, carefully drawn, and not authorities. &#8230; I am not sure how I did it, but I goofed. And you goofed, too, for believing me.&#8221;</i> (Page x, <i><a href="http://www.amazon.com/gp/product/0805390456?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0805390456">Feynman Lectures in Physics</a>; Vol I</i>).</p>
<h4>On Clever Programming</h4>
<p>The chapter on expressions has the famous words on <i>clever programming</i> that are often quoted:</p>
<blockquote><p>Everyone knows that debugging is twice as hard as writing a program in the first place. So if you&#8217;re as clever as you can be when you write it, how will you ever debug it? <i>(Page 10)</i></p></blockquote>
<p>Simplicity and clarity trump stray microseconds:</p>
<blockquote><p>Simplicity and clarity are often of more value than the microseconds possibly saved by clever coding&#8230; Trivia rarely affect efficiency. Are all the machinations worth it, when their primary effect is to make the code less readable? <i>(Page 127)</i></p></blockquote>
<h4>On Temporary Variables</h4>
<p>Here&#8217;s some advice on the demerits of arbitrary temporary variables:</p>
<blockquote><p>The fewer temporary variables in a program, the less chance there is that one will not be properly initialized, or that one will be altered unexpectedly before it is used. &#8220;Temporary&#8221; is a dirty word in programming &#8212; it suggests that a variable can be used with less thought than a &#8220;normal&#8221; (permanent?) one, and it encourages the use of one variable for several unrelated calculations. Both are dangerous practices. <i>(Page 11)</i></p></blockquote>
<h4>The Telephone Test</h4>
<p>The authors present a peculiar test, to which <i><a href="http://www.codinghorror.com/blog/archives/000962.html">Elevator Test</a></i> bears some resemblance, to assess code readability:</p>
<blockquote><p>A useful way to decide if some piece of code is clear or not is the &#8220;telephone test.&#8221; If someone could understand your code when read aloud over the telephone, it&#8217;s clear enough. If not, then it needs rewriting. Use the &#8220;telephone test&#8221; for readability. <i>(Page 21)</i></p></blockquote>
<h4>On the Shape of Programs</h4>
<p>The text of the program should be close to the process it evokes:</p>
<blockquote><p>It is a good rule of thumb that a program should read from top to bottom in the order that it will be executed; if this is not true, watch out for the bugs that often accompany poor structure. <i>(Page 37)</i></p></blockquote>
<h4>On Program Structuring</h4>
<p>Write short functions/classes with well defined purpose:</p>
<blockquote><p>When a program is not broken up into small enough pieces, the larger modules often fail to deliver on these promises. They try to do too much, or too many different things, and hence are difficult to maintain and are too specialized for general use. <i>(Page 59)</i> &#8230; Combining too many functions in one module is a sure way to limit its usefulness, while at the same time making it more complex and harder to maintain. <i>(Page 64)</i></p></blockquote>
<h4>On Premature Optimization</h4>
<blockquote><p>&#8220;Optimizing&#8221; too early in the life of a program can kill its chances for growth. <i>(Page 61)</i></p></blockquote>
<h4>On Loosely Coupled Modules</h4>
<blockquote><p>It must be possible to describe the function performed by a module in the briefest of terms and it is necessary to minimize whatever relationships exist with other modules, and display those that remain as explicitly as possible. This is how we obtain the minimum &#8220;coupling&#8221;, and hence maximum independence, between modules. <i>(Page 62)</i></p></blockquote>
<blockquote><p>As we have said several times, the hard part of programming is controlling complexity &#8212; keeping the pieces decoupled so they can be dealt with separately instead of all at once. And the need to separate into pieces is not some academically interesting point, but a practical necessity, to keep things from interacting with each other in unexpected ways. <i>(Page 95)</i></p></blockquote>
<h4>On Encapsulation</h4>
<blockquote><p>One good test of the worth of a module, in fact, is how good a job it does of hiding some aspect of the problem from the rest of the code. <i>(Page 65)</i></p></blockquote>
<h4>On Functions as Black-boxes</h4>
<blockquote><p>&#8230; break the job into five small functions, each one of which can be assimilated separately, then treated as a black box that does some part of the job. Once it works, we need no longer concern ourselves with how it does something, only with the fact that it does. We thus have some assurance that we can deal with the program a small section at a time without much concern for the rest of the code. There is no other way to retain control of a large program. <i>(Page 77)</i></p></blockquote>
<h4>On Top-down design</h4>
<blockquote><p>One of the better ways of [planning program structure] is what is often called &#8220;top-down design.&#8221; In a top-down design, we start with a very general pseudo-code statement of the program &#8230; and then elaborate this statement in stages, filling in details until we ultimately reach executable code. Not only does this help to keep the structure fairly well organized, and avoid getting bogged down in coding too early, but it also means that we can back up and alter bad decisions without losing too much investment. <i>(Page 71)</i></p></blockquote>
<h4>On Recursion</h4>
<blockquote><p>Learning to think recursively takes some effort, but it is repaid with smaller and simpler programs. Not every problem benefits from a recursive approach, but those that deal with data that is recursively defined often lead to very complicated programs unless the code is also recursive. <i>(Page 77)</i></p></blockquote>
<h4>I/O Programming &#8212; Never Trust Any Data &amp; Remember the User</h4>
<blockquote><p>Input/output is the interface between a program and its environment. Two rules govern all I/O programming: <b>NEVER TRUST ANY DATA</b>, and <b>REMEMBER THE USER</b>. This requires that a program be as foolproof as is reasonably possible, so that it behaves intelligently even when used incorrectly, and that it be easy to use correctly. Ask yourself: Will it defend itself against the stupidity and ignorance of its users (including myself)? Would I want to have to use it myself? <i>(Page 97)</i></p></blockquote>
<h4>On Bounds-checking</h4>
<blockquote><p>Some compilers allow a check during execution that subscripts do not exceed array dimensions. This is a help &#8230; many programmers do not use such compilers because &#8220;They&#8217;re not efficient.&#8221; (Presumably this means that it is vital to get the wrong answers quickly.) <i>(Page 85)</i></p></blockquote>
<h4>On Bug Infestation</h4>
<blockquote><p>Where there are two bugs, there is likely to be a third. <i>(Page 102)</i></p></blockquote>
<h4>Floating Point Numbers Are Like Sandpiles</h4>
<blockquote><p>Floating point arithmetic adds a new spectrum of errors, all based on the fact that the machine can represent numbers only to a finite precision. <i>(Page 115)</i></p></blockquote>
<blockquote><p>As a wise programmer once said, &#8220;Floating point numbers are like sandpiles: every time you move one, you lose a little sand and you pick up a little dirt.&#8221; And after a few computations, things can get pretty dirty. <i>(Page 117)</i></p></blockquote>
<h4>On Efficiency</h4>
<p>Concerns of efficiency must strike a balance with those of overall cost.</p>
<blockquote><p>Machines have become increasingly cheap compared to people; any discussion of computer efficiency that fails to take this into account is shortsighted. &#8220;Efficiency&#8221; involves the reduction of overall cost &#8212; not just machine time over the life of the program, but also time spent by the programmer and by the users of the program.</p>
<p>A clean design is more easily modified as requirements change or as more is learned about what parts of the code consume significant amounts of execution time. A &#8220;clever&#8221; design that fails to work or to run fast enough can often be salvaged only at great cost. Efficiency does not have to be sacrificed in the interest of writing readable code &#8212; rather, writing readable code is often the only way to ensure efficient programs that are also easy to maintain and modify. </p>
<p>To begin, let us state the obvious. If a program doesn&#8217;t work, it doesn&#8217;t matter how fast it runs. <i>(Page 123)</i></p></blockquote>
<h4>Algorithmic Improvements versus Tuning</h4>
<blockquote><p>How can we really speed it up? Fundamental improvements in performance are most often made by algorithm changes, not by tuning &#8230; There are two lessons. First, time spent selecting a good algorithm is certain to pay larger dividends than time spent polishing an implementation of a poor method. Second, for any given algorithm, polishing is not likely to significantly improve a fundamentally sound, clean implementation. It may even make things worse. <i>(Page 133&#8211;134)</i></p></blockquote>
<h4>On Profiling</h4>
<p>Profile and measure your code before making performance improvements.</p>
<blockquote><p>Beware of preconceptions about where a program spends its time. This avoids the error of looking in the wrong place for improvements. Of course, you have to have some working idea of which part of a program has the most effect on overall speed, but changes designed to improve efficiency should be based on solid measurement, not intuition. </p>
<p>A useful and cheap way to measure how a program spends its time is to count how many times each statement is executed. The resulting set of counts is called the program&#8217;s &#8220;profile&#8221; (a term first used by <a href="http://www-cs-faculty.stanford.edu/~knuth/">D. E. Knuth</a> in an <a href="http://oai.dtic.mil/oai/oai?verb=getRecord&#038;metadataPrefix=html&#038;identifier=AD0715513">article in Software Practice and Experience, April, 1971</a>). Some enlightened computer centers make available a &#8220;profiler&#8221; &#8230; <i>(Page 136)</i></p></blockquote>
<h4>On Documentation</h4>
<p>The sole truth about a program is its text.</p>
<blockquote><p>The only reliable documentation of a computer program is the code itself. The reason is simple &#8212; whenever there are multiple representations of a program, the chance for discrepancy exists. If the code is in error, artistic flowcharts and detailed comments are to no avail. Only by reading the code can the programmer know for sure what the program does. <i>(Page 141)</i></p></blockquote>
<h4>On What Documentation Should Comprise</i></h4>
<blockquote><p>In a project of any size it is vital to maintain readable descriptions of what each program is supposed to do, how it is used, how it interacts with other parts of the system, and on what principles it is based. These form useful guides to the code. What is not useful is a narrative description of what a given routine actually does on a line-by-line basis. Anything that contributes no new information, but merely echoes the code, is superfluous. <i>(Page 141)</i></p></blockquote>
<h4>On Following the Rules</h4>
<p>The book ends with the following paragraph on following the rules of programming style:</p>
<blockquote><p>To paraphrase an observation in <i><a href="http://en.wikipedia.org/wiki/The_Elements_of_Style">The Elements of Style</a></i>, rules of programming style, like those of English, are sometimes broken, even by the best writers. When a rule is broken, however, you will usually find in the program some compensating merit, attained at the cost of the violation. Unless you are certain of doing as well, you will probably do best to follow the rules. <i>(Page 159)</i></p></blockquote>
<h4>A Treasure Trove of Pithy Rules</h4>
<p>You would surely have heard of programming maxims like &#8220;make it right before you make it faster&#8221; or &#8220;don&#8217;t comment bad code &#8212; rewrite it&#8221;. Well, this book is generously sprinkled with such short witty one-lines capturing the essence of the section. Below are some of those words of wisdom.<br/></p>
<ol class="importantlist">
<li>From the Introduction: <span class="tipitem"><span>Write clearly &#8211; don&#8217;t be too clever. </span></span>
  </li>
<li>On Expressions: <span class="tipitem"><span>Say what you mean, simply and directly. </span><span>Use library functions. </span><span>Avoid temporary variables. </span><span>Trying to outsmart a compiler defeats much of the purpose of using one. </span><span>Write clearly &#8211; don&#8217;t sacrifice clarity for &#8220;efficiency&#8221;. </span><span>Let the machine do the dirty work. </span><span>Replace repetitive expressions by calls to a common function. </span><span>Parenthesize to avoid ambiguity. </span><span>Choose variable names that won&#8217;t be confused. </span><span>Use the good features of a language; avoid the bad ones. </span></span>
  </li>
<li>On Control Structures: <span class="tipitem"><span>Use <tt>DO-END</tt> and indenting to delimit groups of statements. </span><span>Use <tt>IF-ELSE</tt> to emphasize that only one of two actions is to be performed. </span><span>Use <tt>DO</tt> and <tt>DO-WHILE</tt> to emphasize the presence of loops. </span><span>Make your programs read from top to bottom. </span><span>Use <tt>IF ... ELSE IF ... ELSE IF ... ELSE ...</tt> to implement multi-way branches. </span><span>Use the fundamental control flow constructs. </span><span>Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use. </span><span>Avoid <tt>THEN-IF</tt> and null-<tt>ELSE</tt>. </span><span>Avoid <tt>ELSE GOTO</tt> and <tt>ELSE RETURN</tt>. </span><span>Follow each decision as closely as possible with its associated action. </span><span>Use data arrays to avoid repetitive control sequences. </span><span>Choose a data representation that makes the program simple. </span><span>Don&#8217;t stop with your first draft. </span></span>
  </li>
<li>On Program Structures: <span class="tipitem"><span>Modularize. Use subroutines. </span><span>Make the coupling between modules visible. </span><span>Each module should do one thing well. </span><span>Make sure every module hides something. </span><span>Let the data structure the program. </span><span>Don&#8217;t patch bad code &#8211; rewrite it. </span><span>Write and test a big program in small pieces. </span><span>Use recursive procedures for recursively-defined data structures. </span></span>
  </li>
<li>On Input/Output: <span class="tipitem"><span>Test input for validity and plausibility. </span><span>Make sure input cannot violate the limits of the program. </span><span>Terminate input by end-of-file or marker, not by count. </span><span>Identify bad input; recover if possible. </span><span>Treat end-of-file conditions in a uniform manner. </span><span>Make input easy to prepare and output self-explanatory. </span><span>Use uniform input formats. </span><span>Make input easy to proofread. </span><span>Use free-form input when possible. </span><span>Use self-identifying input. Allow defaults. Echo both on output. </span><span>Localize input and output in subroutines. </span></span>
  </li>
<li>On Common Blunders: <span class="tipitem"><span>Make sure all variables are initialized before use. </span><span>Don&#8217;t stop at one bug. </span><span>Use debugging compilers. </span><span>Watch out for off-by-one errors. </span><span>Take care to branch the right way on equality. </span><span>Avoid multiple exits from loops. </span><span>Make sure your code &#8220;does nothing&#8221; gracefully. </span><span>Test programs at their boundary values. </span><span>Program defensively. </span><span>10.0 times 0.1 is hardly ever 1.0. </span><span>Don&#8217;t compare floating point numbers just for equality. </span></span>
  </li>
<li>On Efficiency and Instrumentation: <span class="tipitem"><span>Make it right before you make it faster. </span><span>Keep it right when you make it faster. </span><span>Make it clear before you make it faster. </span><span>Don&#8217;t sacrifice clarity for small gains in &#8220;efficiency.&#8221;</span><span>Let your compiler do the simple optimizations. </span><span>Don&#8217;t strain to re-use code; reorganize instead. </span><span>Make sure special cases are truly special. </span><span>Keep it simple to make it faster. </span><span>Don&#8217;t diddle code to make it faster &#8212; find a better algorithm. </span><span>Instrument your programs. Measure before making &#8220;efficiency&#8221; changes. </span></span>
  </li>
<li>On Documentation: <span class="tipitem"><span>Make sure comments and code agree. </span><span>Don&#8217;t just echo the code with comments &#8212; make every comment count. </span><span>Don&#8217;t comment bad code &#8212; rewrite it. </span><span>Use variable names that mean something. </span><span>Format a program to help the reader understand it. </span><span>Indent to show the logical structure of a program. </span><span>Document your data layouts. </span><span>Don&#8217;t over-comment. </span></span>
  </li>
</ol>
<p><br/></p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/gpQwEt1ZAhg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/06/kernighan-plauger-programming-style/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/06/kernighan-plauger-programming-style/</feedburner:origLink></item>
		<item>
		<title>On Asymptotic Methods in Analysis: Prof. de Bruijn’s beautiful little book</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/Itjw6agJT_8/</link>
		<comments>http://ashutoshmehra.net/blog/2009/05/asymptotic-methods-in-analysis/#comments</comments>
		<pubDate>Fri, 08 May 2009 23:44:33 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[TAOCP]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=441</guid>
		<description><![CDATA[

For the past several days, I&#8217;ve been working my way through Prof. N.G. de Bruijn&#8217;s book Asymptotic Methods in Analysis; and I want to share some of the fun I&#8217;ve had reading it.
This post is not a review or anything (here&#8217;s an image of the back cover with some reviews). Below are just fragments of [...]]]></description>
			<content:encoded><![CDATA[<div class="alignright" style="margin-left: 5px"><a href="http://www.amazon.com/gp/product/0486642216?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0486642216"><img border="0" src="http://ashutoshmehra.net/images/books/amia/de_Bruijn_asymptotic_methods_in_analysis_back_small.png" width="75px"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=0486642216" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<div class="alignright" style="margin-left: 5px"><a href="http://www.amazon.com/gp/product/0486642216?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0486642216"><img border="0" src="http://ashutoshmehra.net/images/books/amia/de_Bruijn_asymptotic_methods_in_analysis_front_small.jpg" width="75px"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=0486642216" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<p>For the past several days, I&#8217;ve been working my way through <a href="http://www.win.tue.nl/~wsdwnb/">Prof. N.G. de Bruijn&#8217;s</a> book <a href="http://www.amazon.com/gp/product/0486642216?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0486642216">Asymptotic Methods in Analysis</a>; and I want to share some of the fun I&#8217;ve had reading it.</p>
<p>This post is not a review or anything (here&#8217;s an <a href="http://ashutoshmehra.net/images/books/amia/de_Bruijn_asymptotic_methods_in_analysis_back_big.png">image of the back cover</a> with some reviews). Below are just fragments of what I&#8217;ve read so far and found fascinating. <span id="more-441"></span> You could also consider this my attempt at trying to persuade you to get a copy of this beautiful little book! Sadly, new copies appear <i>not</i> to be available at most online bookstores, so it might be hard to get one.</p>
<h4>Prof. de Bruijn, his cycles, and average height of trees</h4>
<div class="alignleft" style="margin-right: 5px"><img src="http://ashutoshmehra.net/images/books/amia/de_bruijn_cycle_color.png" width="150px" alt="A de Bruijn cycle"></div>
<p>This book is by the same Prof. de Bruijn well-known, among other things, for his <a href="http://en.wikipedia.org/wiki/De_Bruijn_sequence">cycles</a>, which are defined as following: An <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" />-ary <i>de Bruijn cycle</i> is a sequence of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_90223c6cfe2c22939b91052bf5096d05.png" title="m^n" style="vertical-align:-20%;" class="tex" alt="m^n" /> radix-<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> digits such that every combinations of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> digits occurs consecutively in the cycle. For example, one possible binary de Bruijn cycle of length <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2eb0518083e46aef997ecb32a917459c.png" title="2^4" style="vertical-align:-20%;" class="tex" alt="2^4" /> is:<br />
<center><tt>0 0 0 1 1 1 1 0 1 0 1 1 0 0 1 0</tt>.</center></p>
<p>Treating this sequence as a cycle, notice that each of the 16 possible 4-bit patterns (<tt>0000</tt> &#8230; <tt>1111</tt>) appear exactly once in it. Knuth describes at least <i>three</i> interesting algorithms for generating such cycles in TAOCP Section 7.2.1.1 (printed in <a href="http://www.amazon.com/gp/product/0201853930?ie=UTF8&#038;tag=ashmehblo-20&#038;link_code=as3&#038;camp=211189&#038;creative=373489&#038;creativeASIN=0201853930">Volume 4, Fascicle 2</a> and downloadable as <a href="http://www-cs-faculty.stanford.edu/~knuth/fasc2a.ps.gz">pre-fascicle 2a</a>).</p>
<div class="alignright" style="margin-left: 5px"><a href="http://owpdb.mfo.de/person_detail?id=539"><img src="http://ashutoshmehra.net/images/books/amia/de_Bruijn_(196x)_1_Oberwolfach_Collection.jpg" width="100px" alt="Prof. de Bruijn; From the Oberwolfach Collection"></a></div>
<p>There&#8217;s surely lots to say about Prof. de Bruijn&#8217;s work (including his cycles), but I&#8217;ll reserve them for future posts. Take a look, for example, at the index of TAOCP Vol I (perhaps also in other volumes) and you&#8217;ll see references to many of his results, sometimes framed as exercises. And if you enjoy asymptotic calculations and analyses of algorithms, you would find it interesting to read his co-authored paper (with Knuth and Rice) where it is shown that <i>the average height of planted plane trees</i> is:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c99c19546a80f8e17954618ea271dcac.png" title="\sqrt{\pi n} - {1\over2} + O(n^{-1/2})" style="vertical-align:-20%;" class="tex" alt="\sqrt{\pi n} - {1\over2} + O(n^{-1/2})" />.</center><br />
This is also TAOCP Ex. 2.3.1&#8211;11, where the question is posed in terms of the average value of the maximum stack size consumed while running the usual in-order binary tree traversal, assuming all binary trees with n nodes are equally probable.</p>
<p>As an interesting note, Knuth in his <a href="http://scpd.stanford.edu/knuth/index.jsp">musings</a> on <i>The Joy of Asymptotics</i> (30 May 2000) said:</p>
<blockquote>
<div class="alignleft" style="margin-right: 5px"><a href="http://www.amazon.com/gp/product/1575862123?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1575862123"><img border="0" src="http://ashutoshmehra.net/images/books/amia/knuth_papers_on_analysis_of_algorithms_front_small.jpg" width="75px"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=1575862123" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<p>I&#8217;ve dedicated this book [<a href="http://www-cs-faculty.stanford.edu/~knuth/aa.html">Selected Papers on Analysis of Algorithms</a>] to Prof. de Bruijn &#8230; in the Netherlands because he has been my <i>guru</i> &#8230; Ever since I got my PhD, I considered him my post-graduate doctoral advisor. Every time I had a problem that I got stuck on, I would write to him, and he would write me back a letter that would help me get unstuck. And that&#8217;s been going on for over fourty years now. So I dedicated this book to him. And what I want to talk to you about today is one of the things <i>he</i> has spent most of <i>his</i> life on &#8212; asymptotic methods.</p></blockquote>
<p>By the way, that paper deriving the cool formula for the height of trees is included in these <i>Selected Papers</i> (Ch. 15).</p>
<h4>The Book: A Pragmatic Exposition</h4>
<p>I had got myself a copy of this book several years ago after first reading about it in the section on asymptotic calculations in TAOCP (Sec 1.2.11.3). But unfortunately, the book suffered the same fate as countless others &#8212; it just kept resting on my bookshelf. But a few days ago, I decided to give it a try.</p>
<p><i>A brief history</i>. As described in its preface, this book grew out of lectures given in 1954&#8211;55 and was first published in 1958. The Dover edition I have is a reprint of the third edition from 1970.</p>
<p>Most computer programmers are familiar with the <a href="http://en.wikipedia.org/wiki/Big_O_notation">Big-O notation</a>, or the Bachmann-Landau notation, for analyzing space/time-complexity of an algorithm. Among other things, this book talks of the tricks involved in getting precise asymptotic bounds once we have reduced the problem at hand to, say, a sum or an integral.</p>
<p>This is a <i>pragmatic</i> book &#8212; its focus is on demonstrating general techniques by working out several examples in detail (sometimes in more than one way) rather than expounding highly generalized theories of analysis. In this context, I found these lines from the preface reassuring:</p>
<blockquote><p>Usually in mathematics one has to choose between saying more and more about less and less on the one hand, and saying less and less about more and more on the other. In a practical subject like this, however, the proper attitude should be to say as much as possible in a given situation.</p></blockquote>
<p>From my reading, at certain places it, of necessity, assumes a basic knowledge of analysis and complex variable theory. Other than that, it is a fairly elementary treatment. (Note: as Feynman made a quip in <a href="http://www.amazon.com/gp/product/0393039188?ie=UTF8&#038;tag=ashmehblo-20&#038;link_code=as3&#038;camp=211189&#038;creative=373489&#038;creativeASIN=0393039188">one of his lectures</a>, <i>&#8220;Elementary&#8221; means that very little is required to know ahead of time in order to understand it, except to have an infinite amount of intelligence.&#8221;</i>)</p>
<p>It&#8217;s a small book (200 pages) with very few exercises and so can be read in about two weeks, if you read an hour a day. And since there are lots of cliff-hanging moments (well, to the extent a book on analysis can have!) and the reader is left wondering how a tricky sum/integral would be tamed, your might be compelled to finish it even sooner!</p>
<p>de Bruijn has a good sense of humor too. At the start of the first chapter, when trying to answer the question <i>&#8220;What is Asymptotics?&#8221;</i>, he says:</p>
<blockquote><p>The safest and not the vaguest definition is the following one: Asymptotics is that part of analysis which considers problems of the type dealt with in this book.</p></blockquote>
<p>Didn&#8217;t see that one coming! </p>
<p>And while the book is tiny, it covers lots of tricks &amp; techniques. So, to give my interested readers a taste, I&#8217;ve picked a couple of my favorite from the first chapter. All examples are de Bruijn&#8217;s. </p>
<h4>Uniform Estimates</h4>
<p><i>Uniformity of estimates</i> is this little thing that keeps popping up every so often when analyzing the asymptotics of some characteristic of an algorithm. But it is rarely explained what it means or why uniformity is crucial to the application at hand. Grown up boys and girls are supposed to know what uniformity is, I suppose.</p>
<p>Before looking at uniformity, let&#8217;s recap the definition of the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-notation. We say:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_62bc6f0b98d5560f041b8bc6c43f9135.png" title="f(x)=O(\phi(x))\qquad(x\rightarrow\infty)" style="vertical-align:-20%;" class="tex" alt="f(x)=O(\phi(x))\qquad(x\rightarrow\infty)" /></center><br />
to mean the existence of numbers <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" /> such that:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c85fe500fc2a4584b5cbc6b4167a79d2.png" title="|f(x)|\leq A|\phi(x)|\qquad\text{whenever}\quad x\,\textgreater\,a" style="vertical-align:-20%;" class="tex" alt="|f(x)|\leq A|\phi(x)|\qquad\text{whenever}\quad x\,\textgreater\,a" />.</center><br />
The thing to note in this definition is that <i>the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-notation implies two implicit constants</i>, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_51271a2c86027496c0237a713770db43.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.</p>
<p>Returning to the question of uniformity, suppose <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> is a positive integer and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ce53f5d0df72577c4cd72dad85303234.png" title="g(x)" style="vertical-align:-20%;" class="tex" alt="g(x)" /> are arbitrary functions. Then:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5a545d3ddd3d049250ff49fe425f31c4.png" title="( f(x) + g(x) )^k = O( ( f(x) )^k + ( g(x) )^k)" style="vertical-align:-20%;" class="tex" alt="( f(x) + g(x) )^k = O( ( f(x) )^k + ( g(x) )^k)" />, because,</center><br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e575fac57bcde5edb2e9993ba6e89396.png" title="|(f+g)^k|\leq (|f|+|g|)^k\leq (2\max(|f|, |g|))^k\leq 2^k(|f|^k + |g|^k)" style="vertical-align:-20%;" class="tex" alt="|(f+g)^k|\leq (|f|+|g|)^k\leq (2\max(|f|, |g|))^k\leq 2^k(|f|^k + |g|^k)" /></center><br />
Rewriting the last line as: <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_017c3150b4a36dc7f99f888efbd23ac1.png" title="(f(x) + g(x))^k\leq A|f(x)|^k + B|g(x)|^k" style="vertical-align:-20%;" class="tex" alt="(f(x) + g(x))^k\leq A|f(x)|^k + B|g(x)|^k" />, we see that <i>the implicit &#8220;constants&#8221; <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed6558df25ce694119274d78624a4df9.png" title="B" style="vertical-align:-20%;" class="tex" alt="B" /> depend on the parameter <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /></i>. We call such an estimate <i>non-uniform</i>.</p>
<p></p>
<p>On the other hand, in the asymptotic relation (with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> again being a positive integer):<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_93f8c949fcda792fe4e9cbb767d13b41.png" title="({k\over{x^2+k^2}})^k=O({1\over{x^k}})\qquad (x \,\textgreater\, 1)" style="vertical-align:-20%;" class="tex" alt="({k\over{x^2+k^2}})^k=O({1\over{x^k}})\qquad (x \,\textgreater\, 1)" /></center><br />
the implicit constant in the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-notation is independent of k, since we have<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_18a183079c6b8cf7e8f05501e347d5cf.png" title="({k\over{x^2+k^2}})^k\leq ({k\over{2kx}})^k\leq {1\over{x^k}}" style="vertical-align:-20%;" class="tex" alt="({k\over{x^2+k^2}})^k\leq ({k\over{2kx}})^k\leq {1\over{x^k}}" />.</center></p>
<p>Hence if we write <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_150fb51734afc97fdbf930e24a5972a1.png" title="({k\over{x^2+k^2}})^k\leq {A\over{x^k}}" style="vertical-align:-20%;" class="tex" alt="({k\over{x^2+k^2}})^k\leq {A\over{x^k}}" />, with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_521d1468fd87987f5107a61b41c12251.png" title="x \,\textgreater\,1" style="vertical-align:-20%;" class="tex" alt="x \,\textgreater\,1" />, we can choose <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abdfb1ee5ca8eaf34770b230fed37ffd.png" title="A" style="vertical-align:-20%;" class="tex" alt="A" /> to be 1. In such a case &#8212; when the hidden constants in the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-notation do not depend on the parameter <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1536fd5a256e8ae0bf1cd5a9c828392e.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> &#8212; the estimate is called <i>uniform</i>. This requirement is much like that of <a href="http://en.wikipedia.org/wiki/Uniform_convergence">uniform convergence</a> or <a href="http://en.wikipedia.org/wiki/Uniform_continuity">uniform continuity</a> in analysis.</p>
<p>So why trouble oneself with uniformity? Here&#8217;s one case where uniform estimates prove helpful: In &#8220;balancing&#8221; the asymptotic terms of a sum evenly to achieve a tight bound.</p>
<p>For instance, if we have somehow shown that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_d73c4c3b4ed0a9491e5a5db2e61e7611.png" title="f(x) = O(x^2t) + O(x^4t^{-2})" style="vertical-align:-20%;" class="tex" alt="f(x) = O(x^2t) + O(x^4t^{-2})" /> with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6424a0e9bc3f3fce99cca400465275c5.png" title="x, t \,\textgreater\, 1" style="vertical-align:-20%;" class="tex" alt="x, t \,\textgreater\, 1" /> and where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> is a parameter. If the formula holds uniformly, we can get a tight bound by reasoning as follows: When <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> is small, the first part, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_845054021567cb9b8967fc193af6ee05.png" title="O(x^2t)" style="vertical-align:-20%;" class="tex" alt="O(x^2t)" />, is small, but the second part, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_40f19fc474d7c6d7d6dc67fbb530880e.png" title="O(x^4t^{-2})" style="vertical-align:-20%;" class="tex" alt="O(x^4t^{-2})" />, is large; the reverse holds if <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" /> is large. Hence we wish to find a &#8220;balance&#8221; that minimizes the sum of the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23cf3a439d9492818690c93067ded298.png" title="O(\cdot)" style="vertical-align:-20%;" class="tex" alt="O(\cdot)" /> terms. And this happens when both the terms are asymptotically equivalent. So to achieve such a balance, we simply equate the two expressions <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_f3d3544bb21c11888c1b9f9236600434.png" title="x^2t = x^4t^{-2}" style="vertical-align:-20%;" class="tex" alt="x^2t = x^4t^{-2}" />, giving <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6ded7e8506cf9c320a56fc69e8d5f04e.png" title="t=x^{2/3}" style="vertical-align:-20%;" class="tex" alt="t=x^{2/3}" />, and we finally get <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_59516dcbc0d883e23593df6c74bb6a58.png" title="f(x) = O(x^{8/3})" style="vertical-align:-20%;" class="tex" alt="f(x) = O(x^{8/3})" />.</p>
<p>Notice that because of uniformity, the four hidden constants (<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_91d90b51ee8342c289278378741b884b.png" title="A_1" style="vertical-align:-20%;" class="tex" alt="A_1" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5b9b5c199983fd0b6465db77e6cda44a.png" title="a_1" style="vertical-align:-20%;" class="tex" alt="a_1" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_d01f353275520948daf9d74cf8e70dbf.png" title="A_2" style="vertical-align:-20%;" class="tex" alt="A_2" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_3da18de36bc90bc95a74c746ac13e605.png" title="a_2" style="vertical-align:-20%;" class="tex" alt="a_2" />) of the two <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23cf3a439d9492818690c93067ded298.png" title="O(\cdot)" style="vertical-align:-20%;" class="tex" alt="O(\cdot)" /> do not vary when we vary the parameter <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />. If the two original <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23cf3a439d9492818690c93067ded298.png" title="O(\cdot)" style="vertical-align:-20%;" class="tex" alt="O(\cdot)" /> estimates had not been uniform; that is, had their hidden constants (implied by the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-notation) varied with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />, we wouldn&#8217;t have been able to apply this simplistic &#8220;balancing&#8221; idea. For instance, while for small <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7719e254cdcddde73868082c03747973.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />, the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_085cbcc5243900a13a760673f18bc0e0.png" title="x^2t" style="vertical-align:-20%;" class="tex" alt="x^2t" /> term might have been small, the hidden constant <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_91d90b51ee8342c289278378741b884b.png" title="A_1" style="vertical-align:-20%;" class="tex" alt="A_1" /> in <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_845054021567cb9b8967fc193af6ee05.png" title="O(x^2t)" style="vertical-align:-20%;" class="tex" alt="O(x^2t)" /> might have grown, leading to unpredictable overall behavior.</p>
<p>The good thing about uniform estimates is that they are sometimes more useful; but proving uniformity might require a more careful analysis. </p>
<h4>Asymptotic Series and Convergence: Some Curious Facts</h4>
<p>An <i>asymptotic series</i> or <a href="http://en.wikipedia.org/wiki/Asymptotic_expansion"><i>asymptotic expansion</i></a> for a function <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> is a formula like:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ad7f5a1fbecc853bd4486d8a6bca0780.png" title="f(x) \approx c_0\phi_0(x) + c_1\phi_1(x) + c_2\phi_2(x) + \ldots \qquad (x\rightarrow\infty)" style="vertical-align:-20%;" class="tex" alt="f(x) \approx c_0\phi_0(x) + c_1\phi_1(x) + c_2\phi_2(x) + \ldots \qquad (x\rightarrow\infty)" /></center><br />
where, as <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0f1aa60bf517a5bbf13780fa564b5e69.png" title="x\rightarrow\infty" style="vertical-align:-20%;" class="tex" alt="x\rightarrow\infty" />,<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e68d7586867d3ea3ba4723b3ee3f264e.png" title="\phi_{j+1}(x)=o(\phi_j(x))\qquad j\geq 0" style="vertical-align:-20%;" class="tex" alt="\phi_{j+1}(x)=o(\phi_j(x))\qquad j\geq 0" />, and,</center><br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_dbb4ba230b58aefd99ec2256eedc3218.png" title="f(x) = c_0\phi_0(x) + c_1\phi_1(x) + \cdots + c_{n-1}\phi_{n-1}(x) + O(\phi_n(x))\qquad (n\geq 0)" style="vertical-align:-20%;" class="tex" alt="f(x) = c_0\phi_0(x) + c_1\phi_1(x) + \cdots + c_{n-1}\phi_{n-1}(x) + O(\phi_n(x))\qquad (n\geq 0)" />.</center><br />
That is, the asymptotic expansion for <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> is a series of functions such that if we truncate the series at the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /><sup>th</sup> term, the partial series provides an approximation to <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> (as <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0f1aa60bf517a5bbf13780fa564b5e69.png" title="x\rightarrow\infty" style="vertical-align:-20%;" class="tex" alt="x\rightarrow\infty" />) with the error term asymptotically bound by the first truncated function. The <a href="http://en.wikipedia.org/wiki/Euler%27s_summation_formula#Asymptotic_expansion_of_sums">Euler-Maclaurin formula</a> is one way to get such an expansion. de Bruijn mentions other techniques in the book.</p>
<p>A large class of examples of asymptotic expansions are convergent power series, like:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe74febed2281589c514d7e406215d4b.png" title="\exp(z) = 1 + z + {z^2\over2!} + {z^3\over3!} + \cdots\qquad (|z|\rightarrow0)" style="vertical-align:-20%;" class="tex" alt="\exp(z) = 1 + z + {z^2\over2!} + {z^3\over3!} + \cdots\qquad (|z|\rightarrow0)" />.</center></p>
<p>But there could be asymptotic expansions that are not convergent power series. And with such asymptotic expansions, some curious things can happen:</p>
<ul>
<li>The series of the asymptotic expansion need not be convergent.</li>
<li>If the series does converge, its sum need not be equal to <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /></li>
<li>It is even possible to have <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_696b622b47435feb569472b2e73b8e19.png" title="\phi_j(x)" style="vertical-align:-20%;" class="tex" alt="\phi_j(x)" />&#8217;s such that the sum of the series does not have the series as its own asymptotic expansion!</li>
</ul>
<p>The essential reason for these seemingly strange facts, as de Bruijn explains, is:</p>
<blockquote style="background-color: #ffffff"><p>&#8230; that convergence means something for some fixed <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_84a8809ce4927565dc0a056edf52ea49.png" title="x_0" style="vertical-align:-20%;" class="tex" alt="x_0" /> whereas the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2f55e2f764c899f5c8dabb45cd9338d8.png" title="O" style="vertical-align:-20%;" class="tex" alt="O" />-formulas are not concerned with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_f1fdb243b0d5a9002b1f871e10aae976.png" title="x=x_0" style="vertical-align:-20%;" class="tex" alt="x=x_0" />, but with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0f1aa60bf517a5bbf13780fa564b5e69.png" title="x\rightarrow\infty" style="vertical-align:-20%;" class="tex" alt="x\rightarrow\infty" />. Convergence of the series, for all <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_32394b39aa8100258d9cd1b74aa703e2.png" title="x \,\textgreater\, 0" style="vertical-align:-20%;" class="tex" alt="x \,\textgreater\, 0" />, say, means that for every individual <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6f2ba5df4f650911e133231e13279a3a.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> there is a statement about the case <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_cf518ba638f8d0c256339ca4a611de68.png" title="n\rightarrow\infty" style="vertical-align:-20%;" class="tex" alt="n\rightarrow\infty" />. On the other hand, the statement that the series is the asymptotic expansion of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> means that for every individual <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> there is a statement about the case <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0f1aa60bf517a5bbf13780fa564b5e69.png" title="x\rightarrow\infty" style="vertical-align:-20%;" class="tex" alt="x\rightarrow\infty" />.</p></blockquote>
<p>For example, when <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_04313d2ebdcb2accb5180a12e91c213c.png" title="f(x) = \int_1^x{e^t\over t}dt" style="vertical-align:-20%;" class="tex" alt="f(x) = \int_1^x{e^t\over t}dt" />, the asymptotic expansion of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_154897c44af75233feafb0566b226f74.png" title="e^{-x}f(x)" style="vertical-align:-20%;" class="tex" alt="e^{-x}f(x)" /> is:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c0e321dbedda903bc13a33f6cefd4323.png" title="{1\over x} + {1!\over x^2}+ {2!\over x^3} + {3!\over x^4} + \ldots \qquad (x\rightarrow\infty)" style="vertical-align:-20%;" class="tex" alt="{1\over x} + {1!\over x^2}+ {2!\over x^3} + {3!\over x^4} + \ldots \qquad (x\rightarrow\infty)" />,</center><br />
but the series converges for no value of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6f2ba5df4f650911e133231e13279a3a.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" />.</p>
<h4>So&#8230;</h4>
<p>The book covers lots of useful stuff. In particular, It talks about <a href="http://en.wikipedia.org/wiki/Lagrange_inversion_theorem">Lagrange&#8217;s inversion formula</a> (using which we can solve for the <a href="http://en.wikipedia.org/wiki/Lambert_W_function">&#8220;tree function&#8221;</a>), it has a especially thorough treatment of <a href="http://en.wikipedia.org/wiki/Method_of_steepest_descent">the saddle-point method</a>. And much much more.</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/Itjw6agJT_8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/05/asymptotic-methods-in-analysis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/05/asymptotic-methods-in-analysis/</feedburner:origLink></item>
		<item>
		<title>Aha! Emacs 23!</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/F4Ed-ZOSKx4/</link>
		<comments>http://ashutoshmehra.net/blog/2009/04/aha-emacs-23/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 00:23:20 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=327</guid>
		<description><![CDATA[
Some days ago I migrated to the emacs 23 pretest (23.0.92 Windows binaries and 23.0.91 Cocoa Mac binaries). In this post I&#8217;ll give brief notes of several of the cool features I found in emacs 23. 
First, the good news: My zillions of customizations, installed packages and hooks all worked without a glitch and my [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/emacs128x128icon.png" alt="Emacs Logo" title="Emacs Logo" width="128" height="128" class="alignleft size-full wp-image-360" /><br />
Some days ago I migrated to the <a href="http://alpha.gnu.org/gnu/emacs/pretest/">emacs 23 pretest</a> (<a href="http://alpha.gnu.org/gnu/emacs/pretest/windows/"><tt>23.0.92</tt> Windows binaries</a> and <a href="http://www.porkrind.org/emacs/"><tt>23.0.91</tt> Cocoa Mac binaries</a>). In this post I&#8217;ll give brief notes of several of the cool features I found in emacs 23. </p>
<p>First, the good news: My zillions of customizations, installed packages and hooks all worked without a glitch and my most frequently used modes have been working great <span id="more-327"></span> &#8212; <tt>c++</tt>, <tt>emacs-lisp</tt>, <tt>haskell</tt> (with <tt>ghci</tt> interaction), <tt>dired</tt>, <tt>eshell</tt> and the dear old <tt>text</tt>. Also there have been no crashes or freezes or (I shudder!) data corruptions. The pretest looks rock-solid. The only major thing that didn&#8217;t work for me was <tt>w3m</tt> (it asked me to try the development version; that might likely have worked).</p>
<p>So, what&#8217;s new?</p>
<h4>The <tt>C-n</tt> and <tt>C-p</tt> line-motion commands now move by screen lines</h4>
<p>Now <tt>next-line</tt>/<tt>previous-line</tt> go to the next/previous <i>visual</i> line as opposed to the <i>logical</i> line. In the words of StackOverflow poster Chris R, emacs navigation now <a href="http://stackoverflow.com/questions/515946/emacs-navigation-in-new-versions-acts-like-notepad">acts like notepad</a>.</p>
<p>While it&#8217;s just a change of defaults and one could revert to the earlier behavior by setting <tt>line-move-visual</tt> to <tt>nil</tt>, I still think, this is, by far, the biggest change in behavior of emacs 23. And I can safely bet that it would break many keyboard macros. In less than a week of use, this behavior bit me twice while calling some keyboard macros. Note that our friends <tt>C-a</tt> and <tt>C-e</tt> (<tt>move-beginning-of-line</tt> and <tt>move-end-of-line</tt>) still act on logical lines.</p>
<p>With this new default, the usual technique of jumping to the next/previous visual line (of a long logical line) via a quick isearch is no longer needed. But more keystrokes have to be pressed to go next/previous logical line.</p>
<p>There seems to have been a fair bit of discussion around <a href="http://www.opensubscriber.com/message/emacs-devel@gnu.org/11355250.html">the behavior of <tt>C-n</tt> and <tt>C-p</tt></a>.</p>
<h4>Emacs can start as a daemon (the <i>multi-tty</i> feature)</h4>
<p>With the <tt>--daemon</tt> command-line argument, emacs starts running in background (with no visible terminal session or <a href="http://emacsworld.blogspot.com/2008/08/emacs-frames-and-windows.html">frame</a>).  New terminal sessions or frames can be created using <tt>emacsclient</tt>: specify <tt>-c</tt> to <b>c</b>reate frames or <tt>-t</tt> for <b>t</b>erminal sessions; and optionally <tt>-n</tt> if you don&#8217;t wish <tt>emacsclient</tt> to wait for the editing to finish. And if you&#8217;re feeling all hackerish, there&#8217;s even a <tt>-e</tt> option to supply an expression to be eval&#8217;ed!</p>
<p><tt>emacsclient</tt> also worked way back with emacs 21, but only after a <tt>server-start</tt> had been done from a running emacs session. Then <tt>emacsclient</tt> would just create a new buffer in that existing session/frame.</p>
<p>With the new daemon support, distinct sessions/frames can be created, all connected with the same server. One thing I find cool with this setup is that different sessions opened simultaneously share buffers and other state. So if you make some changes (add text, delete paragraphs whatever) in a buffer in one session, you can see those changes get reflected in the view of that buffer in another session <i>in real time</i>. Finally, even if all sessions are closed, the buffers remain open.</p>
<p><center><div id="attachment_364" class="wp-caption alignnone" style="width: 410px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/emacs23_server_edit.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/emacs23_server_edit_ss.png" alt="Two sessions (terminal and frame) working on the same buffer" title="Two sessions (terminal and frame) working on the same buffer" width="400" height="305" class="size-full wp-image-364" /></a><p class="wp-caption-text">Two sessions (terminal and frame) working on the same buffer.</p></div></center></p>
<p>In emacs 21, <tt>emacsclient</tt> was already instantaneous because it didn&#8217;t have to load up a new copy of emacs and do all the startup initialization. Hence it could be safely set as the <tt>$EDITOR</tt> on Mac or Linux (See Jared&#8217;s post <a href="http://curiousprogrammer.wordpress.com/2009/01/29/what-is-your-dollar-editor/">What is your $EDITOR?</a>). The same is true with emacs 23 running in <tt>--daemon</tt> mode, and now we have the added flexibility to create new sessions/frames.</p>
<p><tt>--daemon</tt> worked great for me on the Mac but on Windows, emacs refused to launch in the daemon mode, giving the error &#8220;This platform does not support the -daemon flag.&#8221; I wonder if daemon support for Windows would be included in the final emacs 23.</p>
<p>Emacs sessions created via <tt>emacsclient</tt> have a special indicator <tt>@</tt> in the mode-line and the minor-mode indicator <tt>Server</tt>. <tt>C-x C-c</tt> is now mapped to <tt>save-buffers-kill-<i>terminal</i></tt> rather than <tt>save-buffers-kill-<i>emacs</i></tt>.</p>
<p>See <tt>djcb</tt>&#8217;s entries at <a href="http://emacs-fu.blogspot.com/">emacs-fu</a> (a good regular source of emacs tricks) on <a href="http://emacs-fu.blogspot.com/2009/02/emacs-daemon.html">emacs &#8211;daemon</a> and <a href="http://emacs-fu.blogspot.com/2009/03/windows-and-daemons.html">windows and daemons</a>.</p>
<h4>Directory-local Variables</h4>
<p>Directory-local variables are a great way to associate certain mode-specific variables to every file contained in a particular directory (or in its subdirectories).</p>
<p>As an example, say, you&#8217;re working on a <tt>C++</tt> project that has peculiar code indentation requirements (indents are to be real tab character worth 8-space) while your usual indentation style is quite different (always 2-space indents, never tabs). Using directory-local variables, you can emacs pick the project&#8217;s indent style for files in the project&#8217;s directory and otherwise use your regular style. Something like the following setup would do the trick.</p>
<p>First, as usual, in your <tt>.emacs</tt> define your own personal style, via, say:<br />
<script src="http://gist.github.com/93143.js"></script></p>
<p>Next, define variables like <tt>c-basic-offset</tt>, <tt>tab-width</tt>, <tt>indent-tabs-mode</tt> to match your project&#8217;s style and associate them to the root of your source repository. So if your projects root is at &#8220;d:/work/BeautifulCode/&#8221;, you do this in your <tt>.emacs</tt>:<br />
<script src="http://gist.github.com/93142.js"></script></p>
<p>Now, whenever you open any <tt>C++</tt> file under your project&#8217;s root, the project-specific formatting would get applied. You can work this way with many different projects, each having different formatting requirements, without having to write specific customizations via a magic first line at the top of each file like:</p>
<pre>
/* -*- c-basic-offset: 8; tab-width: 8; indent-tabs-mode: t; -*- */
</pre>
<p>Using directory-local variables, one could also define scm parameters (server/port, repository root, &#8230;) on a per root-directory basis. I think it&#8217;s a very powerful feature.</p>
<p>Similar to the above configuration via <tt>dir-locals-set-directory-class</tt> and <tt>dir-locals-set-class-variables</tt>, it is possible to define directory-local variables via a <tt>.dir-locals.el</tt> file (actually, the file defined by the <tt>dir-locals-file</tt> variable).</p>
<p>The idea is to place all project-related customizations in a <tt>.dir-locals.el</tt> file placed at the root of the project source tree. Then, when emacs opens any file under it, it applies these customizations. This way, uniform configurations can be shared across multiple developers using emacs.</p>
<p>Also see <a href="http://atomized.org/2009/01/emacs-23-directory-local-variables/">atomized&#8217;s post on directory local variables</a> where he brings up some problems he&#8217;s faced with the directory-local variables feature.</p>
<h4>Emacs can have transparent frames</h4>
<p>The transparency of the emacs frame can be now controlled via the <tt>alpha</tt> frame parameter. Try something like: <tt>(set-frame-parameter (selected-frame) 'alpha 70)</tt></p>
<p><center><div id="attachment_366" class="wp-caption alignnone" style="width: 410px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/emacs23_transparency_support.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/emacs23_transparency_support_sm.png" alt="A Transparent emacs frame" title="A Transparent emacs frame" width="400" height="284" class="size-full wp-image-366" /></a><p class="wp-caption-text">A Transparent emacs frame</p></div></center></p>
<p>This is undoubtedly cool (I bet XCode and Visual Studio can&#8217;t do that!) and maybe useful in one-off cases. But I don&#8217;t think anyone would have long emacs sessions with a transparent frame &#8212; it hinders visibility and is distracting.</p>
<h4>D-Bus and ZeroConf</h4>
<p><a href="http://en.wikipedia.org/wiki/D-Bus">D-Bus</a> is a Unix IPC system for applications to talk to each other. I&#8217;ve not had a chance to play with it, but for an example of using <tt>dbus</tt> in emacs, see <tt>djcb</tt>&#8217;s <a href="http://emacs-fu.blogspot.com/2009/01/using-d-bus-example.html">using D-Bus example</a>.</p>
<h4>Anti-aliased font on X11</h4>
<p>This will be a welcome enhancement for linux programmers. Being a Mac/Win user, I&#8217;ve enjoyed anti-aliased fonts for some time now. For details and screenshots on Linux, see <a href="http://www.emacswiki.org/cgi-bin/wiki/XftGnuEmacs">XftGnuEmacs</a> on EmacsWiki. Alexandre Vassalotti has a <a href="http://peadrop.com/blog/2007/09/17/pretty-emacs-reloaded/">Pretty Emacs Reloaded</a> package for Ubuntu. <a href="http://dagobart.wordpress.com/2009/04/08/for-purpose-of-reference-anti-alias-emacs/">Another page</a> describing how to set this up on Linux.</p>
<p>By the way, with anti-aliasing, I love the <a href="http://www.gnome.org/fonts/">Bitstream Vera Sans Mono</a> and <a href="http://www.webdevkungfu.com/textmate-envy-aka-monaco-font-for-windows/">Monaco</a> fonts for programming (in particular because they have nice distinct shapes for <tt>l</tt>/<tt>1</tt>).</p>
<h4>Recycle Bin/Trash can be used for file deletion</h4>
<p>If <tt>delete-by-moving-to-trash</tt> is non-nil, then deleted files are moved to the Recycle Bin/Trash. It&#8217;s surprising that this feature wasn&#8217;t already available.</p>
<h4>Search Enhancements</h4>
<p>Some neat search and highlight related enhancements I found:</p>
<ul>
<li><tt>M-s a C-s</tt> and <tt>M-s a M-C-s</tt> in dired runs multi-file isearch on the marked files. Should come in handy.</li>
<li>For incremental <i>word</i> search, there&#8217;s a new command <tt>isearch-forward-word</tt> globally bound to <tt>M-s w</tt>. While doing an isearch, the same key <tt>M-s w</tt> (now bound to another new command <tt>isearch-toggle-word</tt>) toggles word searching on/off.</li>
<li><tt>M-s h r</tt> (bound globally to the command <tt>highlight-regexp</tt>) can be used to highlight all occurances of a regexp in a buffer. The same key can be used when doing isearch to highlight all occurrances of the current search string (it calls <tt>isearch-highlight-regexp</tt> internally).</li>
<li><tt>occur</tt> can now be invoked using <tt>M-s o</tt>. The same key runs an <tt>isearch-occur</tt> when doing an isearch.</li>
<li>If an isearch is started in the minibuffer, it searches <i>in the minibuffer</i>!</li>
<li>You can now specify group numbers explicitly in the regexp via an extended form of &#8220;shy&#8221; groups: <tt>\(?<i>number</i>:<i>regexp</i>\)</tt>. Again a handy thing.</li>
</ul>
<h4>Some New Default Keybindings</h4>
<ul>
<li><tt>C-x C-+</tt> or <tt>C-x C-=</tt> increase the text face size (font size), while <tt>C-x C--</tt> decreases it (that is, zooming in/out). <tt>C-x C-0</tt> restores to the default size. These are mapped to <tt>text-scale-adjust</tt>. You can also do stuff like <tt>C-x C-+ C-+ C-+</tt>, that is, omit the <tt>C-x</tt> on repeated commands.</li>
<li><tt>C-l</tt> now does something more useful. On the first invocation, it moves the current line to center of the window (as it did earlier). But on the second successive invocation, it moves the current line to the top of the window; and then to the bottom on a third invocation. Subsequent invocations cycle through these three placements. <tt>C-l</tt> is now bound to the <tt>recenter-top-bottom</tt> (in previous versions, it was bound to <tt>recenter</tt>).</li>
</ul>
<h4>New Modes</h4>
<ul>
<li><tt>linum-mode</tt> is a minor-mode for line numbers in the left margin. This is like vi&#8217;s <tt>:set nu</tt> command. I have already had <a href="http://stud4.tuwien.ac.at/~e0225855/linum/linum.html">the linum package</a> installed for some years now, and had used it occasionally. It is nice to see it finally become a part of emacs.</li>
<li>There&#8217;s a new game called <tt>bubbles</tt> (somewhat like &#8220;bejeweled&#8221;)</li>
<li><tt>proced</tt> is a &#8220;process editor&#8221; and creates a buffer with a list of processes running on the system (much like <tt>ibuffer</tt> does for buffers, or <tt>dired</tt> for files). You can use commands like <tt>k</tt> to kill a process etc. <tt>proced</tt> was not available on the Mac, while on Windows, it didn&#8217;t allow me to kill processes.</li>
</ul>
<h4>New Behaviors</h4>
<ul>
<li>The <tt>shift-select-mode</tt> variable (<tt>t</tt> by default) enables Win/Mac style selection by pressing the shift key first, and then pressing the right/left arrow keys to grow/shrink selection.</li>
<li>Pressing <tt>TAB</tt> when transient mark mode is now on (the default now) causes certain commands like <tt>M-q</tt> (<tt>fill-paragraph</tt>), <tt>M-$</tt> (<tt>ispell-word</tt>) and <tt>TAB</tt> to operate on the region instead.</li>
<li>Completion now takes text after the point in minibuffer into account for filtering the remaining alternatives (that were generated by the text before the point). For instance, if I&#8217;m trying to open the emacs PROBLEMS file, and the minibuffer input is <tt>d:/progs/emacs/etc/</tt>, and I type <tt>oo</tt> and bring the point back to before the <tt>oo</tt>, so that the input now looks like:<br/>
<p><center><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/completion_pre.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/completion_pre.png" alt="Minibuffer input before completion" title="Minibuffer input before completion" width="513" height="32" class="size-full wp-image-362" /></a></center></p>
<p>If I then press <tt>TAB</tt>, emacs not only restricts the choices to the two files that have <tt>oo</tt> in them (<tt>COOKIES</tt> and <tt>spook.lines</tt>) but also updates the minibuffer input <tt>oo</tt> to <tt>ook</tt> (since both of these have a <tt>k</tt> following <tt>oo</tt>):<br/></p>
<p><center><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/completion_post.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/completion_post.png" alt="Updated minibuffer input and suggestion completion changes" title="Updated minibuffer input and suggestion completion changes" width="499" height="302" class="size-full wp-image-361" /></a></center></p>
</li>
<li>Minibuffer input for shell commands (<tt>M-!</tt>) also allows completion.</li>
<li><tt>M-x butterfly</tt> flips the <a href="http://xkcd.com/378/">desired bit on the disk platter</a>. It is almost a cliché now &#8212; but somehow it&#8217;s still funny!</li>
</ul>
<p>That&#8217;s about it! More pleasant surprises will turn up as I play more with it.</p>
<p><i>A word on the emacs <tt>etc/NEWS</tt> file</i>. I would never have discovered some of the features mentioned above without digging through the <tt>NEWS</tt> file. It lists several other improvements and additions that I&#8217;ve not even had time to try! I couldn&#8217;t find a copy online, so here&#8217;s a link to a local copy of the <a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/04/news-230921.txt">emacs 23 NEWS file (build 23.0.92.1)</a>.</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/F4Ed-ZOSKx4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/04/aha-emacs-23/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/04/aha-emacs-23/</feedburner:origLink></item>
		<item>
		<title>Read wrote Wright… Graph Theorists playing with words</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/reNsUhl8HPw/</link>
		<comments>http://ashutoshmehra.net/blog/2009/03/read-wrote-wright/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 01:38:31 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Humor]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=271</guid>
		<description><![CDATA[
I was going through the first chapter of the book Graphical Enumeration by Frank Harary and Edgar M. Palmer when I chanced upon this perplexing footnote:
Read wrote Wright that both Read [R2]1 and Wright [W3]2 were wrong. So Read and Wright wrote a joint erratum [RW1]3 to set things right. This may be wrong since [...]]]></description>
			<content:encoded><![CDATA[<div class="alignleft" style="margin-right: 5px;"><a href="http://owpdb.mfo.de/detail?photo_id=4390"><img src="http://ashutoshmehra.net/images/posts/readwrotewright/frank_harary_oberwolfach_1972.jpg" alt="Frank Harary, Oberwolfach (1972)" width="130px" /></a></div>
<p>I was going through the first chapter of the book <i>Graphical Enumeration</i> by <a href="http://www.cs.nmsu.edu/fnh/">Frank Harary</a> and <a href="http://genealogy.math.ndsu.nodak.edu/id.php?id=5534">Edgar M. Palmer</a> when I chanced upon this perplexing footnote:</p>
<blockquote><p>Read wrote Wright that both Read [R2]<sup>1</sup> and Wright [W3]<sup>2</sup> were wrong. So Read and Wright wrote a joint erratum [RW1]<sup>3</sup> to set things right. This may be wrong since Wright asserts that Wright wrote Read first.</p></blockquote>
<p><span id="more-271"></span><br />
It appears on Pg. 17 in reference to k-colored graphs. Read and Wright are, of course, the mathematicians <a href="http://en.wikipedia.org/wiki/Ronald_Read">Ronald C. Read</a> and <a href="http://en.wikipedia.org/wiki/E._M._Wright">Edward M. Wright</a>!</p>
<p>A quick glance at the bibliography confirms that the above footnote describes, even if humorously, a true incident. However, I can&#8217;t say what the last sentence means: <q>This may be wrong since Wright asserts that Wright wrote Read first</q>. Does it mean:</p>
<ul>
<li>There is a genuine priority dispute as to who figured the error out [I don't think this is the case]. Or,
<li>Since [RW1] paper has Read as the first author (so, in that sense, &#8220;Read wrote first&#8221;), Wright couldn&#8217;t have written first. Or,</li>
<li>Since &#8220;write&#8221; must happen before &#8220;read&#8221;, Wright wrote first. Or,</li>
<li>It doesn&#8217;t mean anything &#8212; the authors are just playing around with words!</li>
</ul>
<p>I could not find anything relevant by searching the phrase &#8220;Read wrote Wright&#8221; on Google.com. I&#8217;ll be eager to learn any other clues the reader might have as as to the meaning of the last sentence of the footnote.</p>
<p><i>Aside</i>: It is sad that the <i>Graphical Enumeration</i> book, which was published by Academic Press, New York (1973), has apparently gone <a href="http://www.amazon.com/Graphical-Enumeration-Frank-Harary/dp/0123242452">out of print</a>. I&#8217;ve added it to the <a href="http://outofprintmath.blogspot.com/">out of print math blog</a>.
<ol class="footnotes">
<li id="footnote_0_271" class="footnote">[R2] R.C. Read, The number of k-colored graphs on labelled nodes, Canad. J. Math. <b>12</b>, 409&#8211;413 (1960).</li>
<li id="footnote_1_271" class="footnote">[W3] E.M. Wright, Counting coloured graphs, Canad. J. Math. <b>13</b>, 683&#8211;693 (1961).</li>
<li id="footnote_2_271" class="footnote">[RW1] R.C. Read and E.M. Wright, Coloured graphs: A correction and extension, Canad. J. Math. <b>22</b>, 594&#8211;596 (1970).</li>
</ol>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/reNsUhl8HPw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/03/read-wrote-wright/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/03/read-wrote-wright/</feedburner:origLink></item>
		<item>
		<title>Solving Project Euler Problems 161 (Trominoes Tiling) and 185 (Number Mind) with ZDDs</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/duWunVP_FZw/</link>
		<comments>http://ashutoshmehra.net/blog/2009/03/solving-project-euler-problems-with-zdds/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 14:16:47 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[TAOCP]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=157</guid>
		<description><![CDATA[Some months ago, I was re-introduced to Project Euler (PE) through the blogosphere. I had visited that site before, but had been left unmotivated by the first dozen or so problem statements that I read &#8212; neither the programming nor the math involved anything new or challenging. Only in the past few months, when I [...]]]></description>
			<content:encoded><![CDATA[<p>Some months ago, I was re-introduced to <a href="http://projecteuler.net/">Project Euler</a> (PE) through the blogosphere. I had visited that site before, but had been left unmotivated by the first dozen or so problem statements that I read &#8212; neither the programming nor the math involved anything new or challenging. Only in the past few months, when I attempted to crack some of the harder nuts, did I realize how interesting many PE problems could be &#8212; requiring a neat algorithm (or invoking a crucial theorem) for an efficient solution. <span id="more-157"></span></p>
<h4>My experience with Project Euler Problems</h4>
<p>As I attempted to solve more and more problems, I found a disproportionately large number of them centered around the theory of numbers. These involved important ideas of number sieves, congruences, continued fractions, the Farey series, solving <a href="http://en.wikipedia.org/wiki/Pell%27s_equation">Pell&#8217;s equation</a>, implementing <a href="http://en.wikipedia.org/wiki/Shanks-Tonelli_algorithm">Shanks Tonelli algorithm</a> etc., and had me sifting through Hardy and Wright&#8217;s book every so often. But after a while they weren&#8217;t so much fun. I did, however, find enough problems of my interest to get me hooked &#8212; enumeration and dynamic programming (<a href="http://projecteuler.net/index.php?section=problems&#038;id=208">208</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=161">161</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=209">209</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=172">172</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=169">169</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=215">215</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=219">219</a>), probability (<a href="http://projecteuler.net/index.php?section=problems&#038;id=227">227</a>, <a href="http://projecteuler.net/index.php?section=problems&#038;id=213">213</a>) and those occasionally unique ones like <a href="http://projecteuler.net/index.php?section=problems&#038;id=197">197</a> (concerning the &#8220;steady-state&#8221; behavior of a certain sequence). Working through these problems made me realize what a treasure PE was. Kudos to Colin &#8220;Euler&#8221; Hughes and the PE-team for their effort in running this great site!</p>
<p>In addition to having the fun of solving the problems myself, I could study the solutions worked out by other members in the forum. Seeing the elegance, efficiency and analyses of some of their solutions was a rewarding (even if a bit humbling) experience. A case in point is the solution to <a href="http://projecteuler.net/index.php?section=problems&#038;id=208">Robot Walks (208)</a> by <tt>sajninredoc</tt> and <tt>stijn263</tt> (among others), where they reduce the enumeration problem to a single summation. And then there were those APL/J programmers with their cute one-liners!</p>
<p>In this entry, I shall outline my solutions (and their performance characteristics) to the <a href="http://projecteuler.net/index.php?section=problems&#038;id=161">Trominoes Tiling (161)</a> and <a href="http://projecteuler.net/index.php?section=problems&#038;id=185">Number Mind (185)</a> problems. To solve these problems, I used the <a href="http://en.wikipedia.org/wiki/Zero_suppressed_decision_diagram">ZDD</a> techniques I had just studied in Knuth&#8217;s <a href="http://www-cs-faculty.stanford.edu/~uno/fasc1b.ps.gz">pre-fascicle 1B</a> (now in print as <a href="http://www.amazon.com/Art-Computer-Programming-Fascicle-Techniques/dp/0321580508">Vol 4 Fascicle 1</a>). I had <a href="http://ashutoshmehra.net/blog/2008/12/notes-on-zdds/trackback/">blogged earlier</a> on <a href="http://www-cs-faculty.stanford.edu/~knuth/musings.html">Knuth&#8217;s Fun with ZDDs musing</a>.</p>
<h4>Trominoes Tiling (161): Enumerating Exact Covers using ZDDs</h4>
<p>Trominoes Tiling (<a href="http://projecteuler.net/index.php?section=problems&#038;id=161">161</a>) is almost the tiling problem of TAOCP 7.1.4 &#8212; (130) with the difference that only trominoes are allowed (no monominoes or dominoes) and the grid-size is slightly larger (<tt>9x12</tt> instead of <tt>8x8</tt>). I reuseed, with the obvious changes, the ZDD routines that I had coded while working out that section. Since Knuth has already explained the ideas involved so beautifully (see the text around 7.1.4 &#8212; (129)), I shall only briefly sketch the ZDD construction before giving some performance statistics.</p>
<p>To begin, we first model the tiling problem as an <a href="http://en.wikipedia.org/wiki/Exact_cover">Exact Cover</a>. This involves creating a boolean matrix <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_dc7ce529b32ce1c71aa499962a871b74.png" title="(a_{ij})" style="vertical-align:-20%;" class="tex" alt="(a_{ij})" /> of <tt>9x12</tt> = <tt>108</tt> columns (corresponding to cells of the board) and <tt>526</tt> rows (corresponding to the ways of placing the <tt>L</tt> and <tt>I</tt> trominoes in all possible orientations on a <tt>9x12</tt> grid). We have <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_1c9980b7224f0d30f9fca908a5d28d32.png" title="a_{ij} = 1" style="vertical-align:-20%;" class="tex" alt="a_{ij} = 1" /> iff tromino placement <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> occupies cell <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" />. The strategy for placement numbering I used is shown below for the simpler <tt>4x4</tt> case (the cells are numbered in the usual row-major order). Since the placement strategy decides the variable-ordering of the ZDD and hence its size (unless, of course, we choose to sift/reorder variables later on), it is important to pick a placement strategy that is not too inefficient.<br/></p>
<p><center><div id="attachment_210" class="wp-caption aligncenter" style="width: 498px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/03/tiling.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/03/tiling.png" alt="Tromino Placement Numbering" title="Tromino Placement Numbering" width="488" height="394" class="size-full wp-image-210" /></a><p class="wp-caption-text">Tromino Placement Numbering for a 4x4 grid (also used for the rows of the exact cover matrix and ZDD variable ordering)</p></div></center><br/></p>
<p><center><div id="attachment_235" class="wp-caption aligncenter" style="width: 371px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2009/03/exactcovermatrix.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2009/03/exactcovermatrix.png" alt="Exact Cover Matrix (for the simplified 4x4 case)" title="Exact Cover Matrix" width="361" height="180" class="size-full wp-image-235" /></a><p class="wp-caption-text">Exact Cover Matrix (for the simplified 4x4 case)</p></div></center><br/></p>
<p>Having constructed the boolean matrix, to enumerate the tilings, we find the number of ways to select some rows of the matrix such that if we inspect any column, precisely one of the selected rows contains a <tt>1</tt> in that column. Hence the name &#8220;exact&#8221; cover &#8212; we neither want to leave any cell uncovered, nor do we want parts of two or more trominoes to overlap.</p>
<p>Exact covers can be enumerated using Knuth&#8217;s <a href="http://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X">Algorithm X</a> &#8212; an efficient backtracking technique implemented using an idea Knuth calls <a href="http://en.wikipedia.org/wiki/Dancing_Links">Dancing Links</a> (<a href="http://www-cs-faculty.stanford.edu/~knuth/programs/dance.w">DANCE program</a>, <a href="http://lanl.arxiv.org/pdf/cs/0011047">paper at arXiv</a>, <a href="http://stanford-online.stanford.edu/seminars/knuth/000222-knuth-100.asx">Computer Musing video</a>, ASL Implementations for <a href="http://stlab.adobe.com:8080/@md=d&#038;cd=//adobe_source_libraries/adobe/&#038;cdf=//adobe_source_libraries/adobe/dancing_links.hpp&#038;c=unH@//adobe_source_libraries/adobe/dancing_links.hpp">dancing_links</a> and <a href="http://stlab.adobe.com:8080/@md=d&#038;cd=//adobe_source_libraries/adobe/&#038;cdf=//adobe_source_libraries/adobe/dancing_links.hpp&#038;c=unH@//adobe_source_libraries/adobe/implementation/toroid.hpp">toroid_node_t</a>). Algorithm X not only <i>enumerates</i> the solution, it in fact <i>generates</i> them all!</p>
<p>To enumerate exact covers using ZDDs, using the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe9bb6d8a0edbab4f541d976501129da.png" title="m\times n" style="vertical-align:-20%;" class="tex" alt="m\times n" /> matrix <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_dc7ce529b32ce1c71aa499962a871b74.png" title="(a_{ij})" style="vertical-align:-20%;" class="tex" alt="(a_{ij})" /> produced in the step above, we construct the boolean function (Eq. 7.1.4 &#8212; (129)):<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_25f8b1b2356d5a5530d3bf454b1bc8ef.png" title="f(x_1,\ldots,x_m) = \bigwedge_{j=1}^n S_1(X_j)" style="vertical-align:-20%;" class="tex" alt="f(x_1,\ldots,x_m) = \bigwedge_{j=1}^n S_1(X_j)" /></center><br />
where boolean variable <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6934f2728bd2faf498c0e5f163f0a6b5.png" title="x_i" style="vertical-align:-20%;" class="tex" alt="x_i" /> indicates selection of row <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> of the matrix (that is, placement a tromino in the orientation <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />), <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_861ea1f62f4123c9fd82e1e3352ac180.png" title="X_j" style="vertical-align:-20%;" class="tex" alt="X_j" /> = <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_272031c949db9f3a7a6bc656df4314a2.png" title="\{x_i | a_{ij} = 1\}" style="vertical-align:-20%;" class="tex" alt="\{x_i | a_{ij} = 1\}" /> is the set of rows <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> that have a <tt>1</tt> in column <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" />, and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_569d1d4fd0a693fbd6651ca3238a06ce.png" title="S_1" style="vertical-align:-20%;" class="tex" alt="S_1" /> is the <i>Symmetric Boolean Function</i> that is true if exactly one of its inputs are true. The function <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> will be true iff for each column <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" />, exactly one of the selected row <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> has <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_02ad58c6e982b858ab5d2594c9363554.png" title="a_{ij}" style="vertical-align:-20%;" class="tex" alt="a_{ij}" /> = 1 &#8212; this is just the condition for exact cover!<br/></p>
<p>For various ways to efficiently construct the above ZDD, see Exercise 7.1.4 &#8212; 212. The function <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_569d1d4fd0a693fbd6651ca3238a06ce.png" title="S_1" style="vertical-align:-20%;" class="tex" alt="S_1" /> can itself be implemented using Exercise 207&#8217;s &#8220;Symmetrizing&#8221; operation.<br/></p>
<p>Once we have the ZDD for the boolean function <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> above, the <i>number of solutions</i>, i.e., the number of vectors <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_48928d13ced42d8a72b7bc5b9c4d194a.png" title="(x_1,\ldots,x_m)" style="vertical-align:-20%;" class="tex" alt="(x_1,\ldots,x_m)" /> that make <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> true (which are precisely the vectors representing exact covers) can be readily found using the ZDD-analog of Algorithm 7.1.4C.<br/></p>
<p><i>Runtime performance of the solution</i>: The resulting ZDD involved 526 variables and had a size of <tt>7893743</tt> zeads. Without invoking any garbage collection, the peak memory usage was <tt>~600MB</tt>  (where each zead in my implementation was a <tt>20</tt>-byte node); time taken was <tt>34s</tt> (user) and <tt>85s</tt> (elapsed). Given that my memos weren&#8217;t very optimized (I had used GCC&#8217;s <tt>std::map</tt>) and neither had I tried doing any variable reordering, the performance seemed reasonable.<br/></p>
<p>A dynamic programming approach (like one I later used for <a href="http://projecteuler.net/index.php?section=problems&#038;id=215">Crack-free Walls (215)</a>),  should have been able to give the results in about a second (this was confirmed by posts on the forum). Nevertheless, the ZDD solution was fast enough to keep my conscience clear of any violations of <a href="http://projecteuler.net/index.php?section=about">Project Euler&#8217;s one-minute-rule</a>.<br/></p>
<p><i>Aside</i>: The <a href="http://projecteuler.net/index.php?section=problems&#038;id=215">Crack-free Walls (215)</a> problem is kind of like TAOCP Exercise 7.1.4 &#8212; 214 (Knuth calls it &#8220;faultfree&#8221;) and should be amenable to the ZDD attack. There, however, appears to be a danger of hitting space-out since the grid-size <tt>10x32</tt> is somewhat large. I&#8217;ve not tried this approach.<br/></p>
<h4>Number Mind (185): Using ZDDs to satisfy an ad-hoc set of constraints</h4>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=185">Number Mind (185)</a> was the other PE problem that I solved with ZDDs. In this problem we&#8217;re to uncover a 16-digit number given a set of &#8220;guesses&#8221; of the form:<br />
<tt><br />
5616185650518293 ;2 correct<br />
3847439647293047 ;1 correct<br />
5855462940810587 ;3 correct<br />
. . .<br />
</tt></p>
<p>The guesses along with the &#8220;hit-rates&#8221; provide partial information about the secret number. Our aim is to find the &#8220;secret&#8221; number corresponding to the set of guesses.</p>
<p>To solve this problem, we create variables <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6880c20b2ca196156a42d330b95f3113.png" title="x_{i,j}" style="vertical-align:-20%;" class="tex" alt="x_{i,j}" /> representing the condition that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /><sup>th</sup> digit (<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2afff0691791fbd2606dc4117a512d56.png" title="1\leq i\leq 16" style="vertical-align:-20%;" class="tex" alt="1\leq i\leq 16" />) is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> (<img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_731d15f2e46cf9802cf5a7d84f821fe4.png" title="0\leq j\leq 9" style="vertical-align:-20%;" class="tex" alt="0\leq j\leq 9" />). We then have the following constraints: </p>
<ul>
<li>Since each position <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> can hold just one digit, for each <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_b2d5fbf99790e9a8f89f51d876bf7d45.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> exactly one of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6880c20b2ca196156a42d330b95f3113.png" title="x_{i,j}" style="vertical-align:-20%;" class="tex" alt="x_{i,j}" /> can be true. Constraints of this kind correspond to terms like <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_33048833db55c02df3be7c605eb7c54f.png" title="S_1(x_{i0},\ldots,x_{i9})" style="vertical-align:-20%;" class="tex" alt="S_1(x_{i0},\ldots,x_{i9})" />, where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_569d1d4fd0a693fbd6651ca3238a06ce.png" title="S_1" style="vertical-align:-20%;" class="tex" alt="S_1" /> is again our friend, the symmetric boolean function.</li>
<li>Each of the given guess &#8220;hit-rates&#8221; must be satisfied. As an example, the third constraint &#8220;<tt>5855462940810587 (3 correct)</tt>&#8221; is represented as:<br />
<center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a60bfd11aef715abf28cbc57cace0534.png" title="S_3(x_{15},x_{28},x_{35},x_{45},x_{54},x_{66},x_{72},x_{89}, x_{94},x_{A0},x_{B8},x_{C1},x_{D0},x_{E5},x_{F8},x_{G7})" style="vertical-align:-20%;" class="tex" alt="S_3(x_{15},x_{28},x_{35},x_{45},x_{54},x_{66},x_{72},x_{89}, x_{94},x_{A0},x_{B8},x_{C1},x_{D0},x_{E5},x_{F8},x_{G7})" /></center>
</li>
</ul>
<p>Using the symmetrizing operator from Exercise 7.1.4 &#8212; 207, both the above kinds of constraints are easily represented. Finally, we compute the <tt>AND</tt> (or, <tt>INTERSECT</tt>, if one prefers family-of-subsets point-of-view) of the individual constraints &#8212; and we&#8217;re left with the final ZDD representing the family of feasible solutions (in our case, the solution in fact turns out to be unique).</p>
<p><i>Runtime performance of the solution</i>: The ZDD had <tt>160</tt> variables <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6880c20b2ca196156a42d330b95f3113.png" title="x_{i,j}" style="vertical-align:-20%;" class="tex" alt="x_{i,j}" />, program execution had a peak memory usage of <tt>~1GB</tt> without any garbage collection or reordering (zead-size <tt>20</tt>-bytes), size of the largest partial function was <tt>4665450</tt> zeads. The running time was <tt>~16s</tt> (both user and elapsed).</p>
<h4>Conclusion</h4>
<p>Comparing the ZDD solutions to the two PE problems, I think it was the kind of &#8220;unstructured&#8221; problem like Number Mind where the ZDD technique really shined.</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/duWunVP_FZw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2009/03/solving-project-euler-problems-with-zdds/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>

		<feedburner:origLink>http://ashutoshmehra.net/blog/2009/03/solving-project-euler-problems-with-zdds/</feedburner:origLink><enclosure url="http://feedproxy.google.com/~r/AshutoshMehraBlog/~5/kkWcNm4KUvU/000222-knuth-100.asx" length="75" type="video/x-ms-asf" /><feedburner:origEnclosureLink>http://stanford-online.stanford.edu/seminars/knuth/000222-knuth-100.asx</feedburner:origEnclosureLink></item>
		<item>
		<title>Fun with ZDDs: Notes from Knuth’s 14th Annual Christmas Tree Lecture</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/DYcIA2Z-kgc/</link>
		<comments>http://ashutoshmehra.net/blog/2008/12/notes-on-zdds/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 09:29:05 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[TAOCP]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=51</guid>
		<description><![CDATA[
In this entry, I&#8217;ll attempt to record the important ideas Knuth presented in his 14th Annual Christmas Tree Lecture, part of his regular Computer Musings. 
While I wasn&#8217;t fortunate enough to attend the lecture in person, I did go through the recording that, thankfully, was quite well done. Like all other &#8220;musings&#8221;, this one included [...]]]></description>
			<content:encoded><![CDATA[<div class="alignright" style="margin-left: 5px"><a href="http://scpd.stanford.edu/knuth/index.jsp"><img src="http://ashutoshmehra.net/images/posts/funzdd/don_knuth_scpd_small.jpg" alt="Don Knuth"></a></div>
<p>In this entry, I&#8217;ll attempt to record the important ideas Knuth presented in his <a href="http://scpd.stanford.edu/knuth/index.jsp">14<sup>th</sup> Annual Christmas Tree Lecture</a>, part of his regular <a href="http://www-cs-faculty.stanford.edu/~knuth/musings.html">Computer Musings</a>. </p>
<p>While I wasn&#8217;t fortunate enough to attend the lecture in person, I did go through the <a href="http://scpd.stanford.edu/knuth/index.jsp">recording</a> that, thankfully, was quite well done. Like all other &#8220;musings&#8221;, this one included some fascinating anecdotal bits (no pun intended) and Knuth&#8217;s good sense of humor was sprinkled throughout; but most noticeable was his infectious enthusiasm for the topics he spoke about. <span id="more-51"></span> The preface to Volume 4 (printed in Fascicle 0) begins with:</p>
<blockquote><p>The title of Volume 4 is <i>Combinatorial Algorithms</i>, and when I proposed it, I was strongly inclined to add a subtitle: <i>The Kind of Programming I Like Best</i>.</p></blockquote>
<p>His excitement for these topics clearly showed in the lecture. It is a must-watch for anyone interested in The Art of Computer Programming (especially Volume 4 on Combinatorial Algorithms), or interested in learning a very useful data structure for combinatorial applications &#8212; ZDDs. </p>
<p><a href="http://en.wikipedia.org/wiki/Zero_Suppressed_Decision_Diagram">ZDDs</a> (Zero-suppressed Binary Decision Diagrams) were introduced by <a href="http://www-alg.ist.hokudai.ac.jp/~minato/">Shin-Ichi Minato</a> in <a href="http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=1600231">1993</a>, about 7 years after <a href="http://en.wikipedia.org/wiki/Randal_Bryant">Randal Bryant</a> introduced <a href="http://www.cs.cmu.edu/~bryant/pubdir/ieeetc86.ps">his key ideas</a> of the <a href="http://en.wikipedia.org/wiki/Binary_decision_diagram">BDD data structure</a>; one of which was that keeping the BDD both <i>reduced</i> and <i>ordered</i> is important in practice. In his &#8220;Fun with BDDs&#8221; musing (this June), Knuth mentioned that this 1986 paper of Bryant&#8217;s took computer science almost by storm; remaining the most-cited paper in the field for many years (see <a href="http://citeseer.ist.psu.edu/source.html">here</a>).</p>
<p>Minato&#8217;s idea (of zero-suppression in BDDs) was motivated by his observation that when working with combinatorial objects, very often tests in BDDs have to be made just to ensure a potentially large number of variables are FALSE. So, in ZDDs, we just &#8220;skip over&#8221; any node whose <tt>HI</tt> pointer leads to <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9d9a6507e830e8b593d1ed70e98f833e.png" title="\bot" style="vertical-align:-20%;" class="tex" alt="\bot" /> (the FALSE sink) &#8212; if having <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a24b216b266c2b3073a06a61ed351cfb.png" title="x_j" style="vertical-align:-20%;" class="tex" alt="x_j" /> TRUE results in the subfunction rooted at <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a24b216b266c2b3073a06a61ed351cfb.png" title="x_j" style="vertical-align:-20%;" class="tex" alt="x_j" /> to become FALSE, then we skip testing <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a24b216b266c2b3073a06a61ed351cfb.png" title="x_j" style="vertical-align:-20%;" class="tex" alt="x_j" /> altogether. That&#8217;s the basic difference between BDDs and ZDDs. And while ZDD theory is much like the BDD theory, it differs sufficiently in the sense of requiring a different &#8220;mind-set&#8221;.</p>
<p>The <i>pre-fascicle 1B</i> that discusses BDDs and ZDDs is available from <a href="http://www-cs-faculty.stanford.edu/~knuth/news.html">Knuth&#8217;s News page</a> (<a href="http://www-cs-faculty.stanford.edu/~knuth/fasc1b.ps.gz">direct link</a>). Interested readers can download it (but Knuth cautions against printing it out just yet, since he&#8217;ll be putting up an updated version in a couple of weeks). That pre-fascicle is a 140-odd page booklet with more than 260 exciting exercises! It will be published as part of fascicle 1 (Knuth said it&#8217;ll go to printing around the end of January 2009); fascicle 1 would in turn form part of Volume 4A (along with the already published fascicles 0, 2, 3 and 4).</p>
<hr />
<b><i>Update (May 2009): Volume 4, Fascicle 1, is here!</i></b><br/></p>
<div class="alignleft" style="margin-right: 5px;"><a href="http://www.amazon.com/gp/product/0321580508?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321580508"><img border="0" src="/images/books/zdd/knuth_vol4_fascicle_1_small.jpg"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=0321580508" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<div class="alignright"> <a href="http://www.amazon.com/gp/product/0321637135?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321637135"><img border="0" src="/images/books/bundle0_4/taocp_vol4_fascicles0to4_bundle_small.jpg"></a><img src="http://www.assoc-amazon.com/e/ir?t=ashmehblo-20&#038;l=as2&#038;o=1&#038;a=0321637135" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></div>
<p>The Fascicle 1 of Volume 4 of The Art of Computer Programming is <a href="http://www.amazon.com/gp/product/0321580508?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321580508">now in print</a>. In addition to the material on BDD and ZDD techniques (described in this post), it includes a subsection on bitwise tricks and techniques, including broadword computing (formerly known as &#8220;Platologic computing&#8221;). In <a href="http://www-cs-faculty.stanford.edu/~knuth/news09.html">Knuth&#8217;s own words</a>, it is <i>&#8220;cram-packed with lots of new stuff among nearly 500 exercises and answers to exercises.&#8221;</i> <br/></p>
<p>There&#8217;s also a bundle of <a href="http://www.amazon.com/gp/product/0321637135?ie=UTF8&#038;tag=ashmehblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321637135">Volume 4, Fascicles 0-4</a> available at a discounted price.</p>
<p><i>Get your copies today!</i><br />
<hr /><br/></p>
<h4>Families of subsets and their ZDD representation</h4>
<p>BDD is a data structure for (representing, manipulating, working with) boolean functions. ZDD, on the other hand, is a data-structure for what Knuth calls <i>Families of Sets</i> &#8212; a large number of combinatorial problems are based on studying families of sets (which are just <a href="http://en.wikipedia.org/wiki/Hypergraph">Hypergraphs</a>). Families of sets are another way of looking at boolean functions since there&#8217;s a natural one-to-one correspondence between the two &#8212; the <i>solutions of a boolean function</i> (the set of n-tuples of boolean values <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_362c33a0c2a82633a46a79521a7c0376.png" title="x=(x_1,\ldots,x_n)" style="vertical-align:-20%;" class="tex" alt="x=(x_1,\ldots,x_n)" /> such that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_57a3db9b14bded9c06fc0922f156537d.png" title="f(x)" style="vertical-align:-20%;" class="tex" alt="f(x)" /> is TRUE) correspond to family of subsets, with each solution corresponding to a subset where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_17205bd7356afc72c6e1cad22749dbb0.png" title="j" style="vertical-align:-20%;" class="tex" alt="j" /> belong to the subset iff <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7cc29b9fb1d9d77e9b6c8839cc745eeb.png" title="x_j=1" style="vertical-align:-20%;" class="tex" alt="x_j=1" /> [see TAOCP Exercise 7.1.4 -- 203(b)]. But even though the families of sets can be encoded as boolean functions, it is sometimes better to think of them directly as families of sets, rather than in boolean sense. This is the thinking cap we&#8217;ll put on when working with ZDDs.</p>
<p>Families of subsets are taken from a well-ordered universe <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9ad2a0bf52569b8d8ebfdaa076d52a7d.png" title="U" style="vertical-align:-20%;" class="tex" alt="U" />. We use the following rules to represent those using ZDDs [again, see TAOCP Exercise 7.1.4 -- 203]:</p>
<ul>
<li><i>The Empty Family</i>, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7fc8c0e997f25c275cd61a6add8b0517.png" title="\phi" style="vertical-align:-20%;" class="tex" alt="\phi" />, is represented by <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9d9a6507e830e8b593d1ed70e98f833e.png" title="\bot" style="vertical-align:-20%;" class="tex" alt="\bot" /> (the FALSE sink node).</li>
<li><i>The Unit Family</i> (the set comprising just the empty set), <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_06d58e07d22069e3966ded0c097c13ea.png" title="\{\phi\} = \epsilon" style="vertical-align:-20%;" class="tex" alt="\{\phi\} = \epsilon" />, is represented by <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c13311766e7f63959b9ccbeef9c93407.png" title="\top" style="vertical-align:-20%;" class="tex" alt="\top" /> (the TRUE sink node).
<li>If <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> is any other family (besides empty and unit), we know that it is nonempty, and that at least one of its members is in turn nonempty. In that case, let <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0eef60d6c8cf58fd2d6a7f5b43e675fb.png" title="v" style="vertical-align:-20%;" class="tex" alt="v" /> be the <i>least</i> element of universe <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9ad2a0bf52569b8d8ebfdaa076d52a7d.png" title="U" style="vertical-align:-20%;" class="tex" alt="U" /> that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> <i>supports</i> &#8212; that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0eef60d6c8cf58fd2d6a7f5b43e675fb.png" title="v" style="vertical-align:-20%;" class="tex" alt="v" /> appears in at least one set in <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" />. [see answer to TAOCP Exercise 7.1.4 -- 197 for the definition of supports]. Then define sub-families <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ea593d4a16d608ae79b67db74d6e6618.png" title="f_0" style="vertical-align:-20%;" class="tex" alt="f_0" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed9375129f95fe69105a13aea771bdbe.png" title="f_1" style="vertical-align:-20%;" class="tex" alt="f_1" /> of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> with <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0cfbfa49a3294a7219790ecb80763deb.png" title="f_0=\{\alpha | \alpha\in f\ \mbox{and}\ v\notin\alpha\}" style="vertical-align:-20%;" class="tex" alt="f_0=\{\alpha | \alpha\in f\ \mbox{and}\ v\notin\alpha\}" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5e39bc3e7d60eea465611135c0ca1fd7.png" title="f_1=\{\alpha | \alpha\cup\{v\}\in f\ \mbox{and}\ v\notin\alpha\}" style="vertical-align:-20%;" class="tex" alt="f_1=\{\alpha | \alpha\cup\{v\}\in f\ \mbox{and}\ v\notin\alpha\}" />. These sub-families correspond to those sets that don&#8217;t and do contain <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0eef60d6c8cf58fd2d6a7f5b43e675fb.png" title="v" style="vertical-align:-20%;" class="tex" alt="v" />. Then, inside the computer, the family <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8cb65257e3377e4035d06ddf990b6657.png" title="f" style="vertical-align:-20%;" class="tex" alt="f" /> corresponds to a <i>zead</i> (ZDD-node) labeled <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_0eef60d6c8cf58fd2d6a7f5b43e675fb.png" title="v" style="vertical-align:-20%;" class="tex" alt="v" /> with <tt>LO</tt> and <tt>HI</tt> branches going to the ZDD representations of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ea593d4a16d608ae79b67db74d6e6618.png" title="f_0" style="vertical-align:-20%;" class="tex" alt="f_0" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ed9375129f95fe69105a13aea771bdbe.png" title="f_1" style="vertical-align:-20%;" class="tex" alt="f_1" /> respectively. Notice the recursive definition (see Knuth&#8217;s diagram below).</li>
</ul>
<div id="attachment_59" class="wp-caption alignleft" style="width: 310px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/zeads-in-zdd-attime-19-00.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/zeads-in-zdd-attime-19-00-300x225.png" alt="Defining a ZDD recursively (@19m)" width="300" height="225" class="size-medium wp-image-59"/></a><p class="wp-caption-text">Defining a ZDD recursively (@19m)</p></div>
<div id="attachment_56" class="wp-caption alignright" style="width: 310px"><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/example-1-zdd-of-symmetric-2-function-attime-24-00.png"><img src="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/example-1-zdd-of-symmetric-2-function-attime-24-00-300x216.png" alt="Ex. 1: ZDD of a function (@24m)" width="300" height="225" class="size-medium wp-image-56" /></a><p class="wp-caption-text">Ex. 1: ZDD of a function (@24m)</p></div>
<p>Throughout the talk, Knuth took up various problems, and worked them out in real-time for everyone to follow, rather than putting up a bunch of slides with everything already worked out. This is one of the beauties of his musing lectures &#8212; it&#8217;s interactive. While Knuth works his way through the problems, the audience can work with him or with their own pencil and paper. And it&#8217;s even more convenient when watching the recorded session over net, where one can pause, work the problem out, and then play to see if they got it right, and go back etc.). Knuth also joked about how he sometimes had to sit through presentations where the speaker doing PowerPoint would whiz through the slides a bit too fast. So, anyway, the most instructive (and most enjoyable) way to understand these examples, I think, would be to just watch the video and work them out yourself (possibly after going through the relevant material in the pre-fascicle).</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/DYcIA2Z-kgc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2008/12/notes-on-zdds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2008/12/notes-on-zdds/</feedburner:origLink></item>
		<item>
		<title>Analytically evaluating a limiting probability (June 2007 Ponder This)</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/EuxcPzHe_A8/</link>
		<comments>http://ashutoshmehra.net/blog/2008/12/a-limiting-probability/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 06:20:40 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Ponder]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=23</guid>
		<description><![CDATA[In this post, I&#8217;ll describe my solution to June 2007&#8217;s Ponder This. I had felt that my solution was kind of nifty, and different from the one that was published.  It had actually taken me several days to work the whole thing out.
Here&#8217;s part (2), the tougher part, of the problem: Values for a [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I&#8217;ll describe my solution to <a href="http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/Challenges/June2007.html">June 2007&#8217;s Ponder This</a>. I had felt that my solution was kind of nifty, and different from the one that was <a href="http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/solutions/June2007.html">published</a>.  It had actually taken me several days to work the whole thing out.</p>
<p>Here&#8217;s part (2), the tougher part, of the problem: Values for a random variable are generated independently and uniformly over <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_8c23b3d397583438b90a390f0adf1147.png" title="\left[0,1\right)" style="vertical-align:-20%;" class="tex" alt="\left[0,1\right)" />. By accumulating these values, your job is to reach a sum between <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e9be79e62f04cc4b913b199c5644142b.png" title="n+x" style="vertical-align:-20%;" class="tex" alt="n+x" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6dd5ce1bb087fe9aa816105476a2e764.png" title="n+1" style="vertical-align:-20%;" class="tex" alt="n+1" />, where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> is a positive integer, and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_2ad1d29b70558316aa7d42e94c3d6749.png" title="0\,\textless\,x\,\textless\,1" style="vertical-align:-20%;" class="tex" alt="0\,\textless\,x\,\textless\,1" />. <span id="more-23"></span> You can ask for successive values of this variate as many times as you wish, but you must add each such value to the running total you maintain. If the accumulating sum exceeds <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6dd5ce1bb087fe9aa816105476a2e764.png" title="n+1" style="vertical-align:-20%;" class="tex" alt="n+1" />, you loose; if you manage to get a sum between <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e9be79e62f04cc4b913b199c5644142b.png" title="n+x" style="vertical-align:-20%;" class="tex" alt="n+x" /> and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6dd5ce1bb087fe9aa816105476a2e764.png" title="n+1" style="vertical-align:-20%;" class="tex" alt="n+1" />, you win. What is the probability of winning as <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe3500a8a2c425af0aea75cd0856ec8f.png" title="n\to\infty" style="vertical-align:-20%;" class="tex" alt="n\to\infty" />?</p>
<p>Earlier that year, I had finished studying Knuth&#8217;s <a href="http://www-cs-faculty.stanford.edu/~knuth/dm.html">Selected Papers on Discrete Mathematics</a>. That is a precious collection for anyone interested in discrete structures, combinatorics, asymptotics, random structures etc. I had especially enjoyed one of the papers, <a href="http://portal.acm.org/citation.cfm?id=67827">&#8220;The first cycles in an evolving graph&#8221;</a> (with <a href="http://algo.inria.fr/flajolet/">Flajolet</a> &#038; <a href="http://www.math.ohio-state.edu/~bgp/">Pittel</a> as co-authors), not only for the neat results but also for the techniques used to get asymptotic values/bounds of certain integrals. That paper helped introduce me to, and drive home the importance of, the fascinating field I later came to know as <a href="http://en.wikipedia.org/wiki/Analytic_combinatorics">Analytic combinatorics</a> (also <a href="http://algo.inria.fr/flajolet/Publications/books.html">this</a>). That paper analyzes the evolution of a random graph, where edges are successively added to a set of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_23e517569a067209bf264db2132df9e1.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> vertices(under different <a href="http://en.wikipedia.org/wiki/Random_graph">random graph models</a>). Among other results, it shows that expected length of the first cycle to appear is proportional to <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_abedac5cf09d0ec5cb99f0e6d7deb619.png" title="n^{1/6}" style="vertical-align:-20%;" class="tex" alt="n^{1/6}" /> and the size of the component having this cycle is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_f2895a79ad21921b9db265027724fe02.png" title="\sim\sqrt{n}" style="vertical-align:-20%;" class="tex" alt="\sim\sqrt{n}" /> &#8212; quite fascinating stuff. At any rate, with these analytic techniques still fresh in my mind, I attacked the problem by trying to find a bound on the probability integral. Details follow (with a I-to-we switch).</p>
<p>To begin with, we use the classical <a href="http://en.wikipedia.org/wiki/Central_limit_theorem">Central Limit Theorem</a> to replace the sum of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> independently and uniformly distributed variates (each of which had mean <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_84fd05d8198f7e5eda6fd5ce77c37d9b.png" title="1\over{2}" style="vertical-align:-20%;" class="tex" alt="1\over{2}" /> and variance <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9a8228f8f96e9f778861f7f7c9a143e1.png" title="1\over{12}" style="vertical-align:-20%;" class="tex" alt="1\over{12}" />) with a Normal variate with mean <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_30756ac17007304d2c29512e77046d3a.png" title="m\over{2}" style="vertical-align:-20%;" class="tex" alt="m\over{2}" /> and variance <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c157181c901c839c9d1a17371e2f7a20.png" title="m\over{12}" style="vertical-align:-20%;" class="tex" alt="m\over{12}" />.</p>
<p>Now, the probability of &#8220;hitting&#8221; (i.e., achieving a sum lying within the interval) <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_06c1b7d438c5568abfe5d1d88ad0bbb3.png" title="\left[n+x,n+1\right)" style="vertical-align:-20%;" class="tex" alt="\left[n+x,n+1\right)" /> in <i>exactly</i> <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> &#8220;moves&#8221; is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_4a108dee71f7e0bf54957e31c157f6df.png" title="\int_{n-1+x}^{n+x}P_a(m,y) P_b(y)\,dy" style="vertical-align:-20%;" class="tex" alt="\int_{n-1+x}^{n+x}P_a(m,y) P_b(y)\,dy" />, where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_24f3db5ede6bbc2dc5e0f081c78a8143.png" title="P_a(m,y)\,dy" style="vertical-align:-20%;" class="tex" alt="P_a(m,y)\,dy" /> is the probability of hitting <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fb2e6bd1da6560741cb2f389adf66fb0.png" title="\left[y, y+dy\right)" style="vertical-align:-20%;" class="tex" alt="\left[y, y+dy\right)" /> in exactly <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_53892837d5d40b86ad3cfd16b2cab553.png" title="m-1" style="vertical-align:-20%;" class="tex" alt="m-1" /> moves and <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5c33891a4360d41ccc1394cd607ca387.png" title="P_b(y)" style="vertical-align:-20%;" class="tex" alt="P_b(y)" /> is the probability of jumping from <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fb2e6bd1da6560741cb2f389adf66fb0.png" title="\left[y, y+dy\right)" style="vertical-align:-20%;" class="tex" alt="\left[y, y+dy\right)" /> to the desired interval <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_7fc6ba65c10c3ed9de130f889e4d876f.png" title="\left[n+x, n+1\right)" style="vertical-align:-20%;" class="tex" alt="\left[n+x, n+1\right)" /> in the m<sup>th</sup>-move.</p>
<p>When <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_24b66d7843d3fa9936b8815ae9da1a72.png" title="y\in\left[n-1+x, n\right)" style="vertical-align:-20%;" class="tex" alt="y\in\left[n-1+x, n\right)" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_cad0e32f603525b16b55df5d28410538.png" title="P_b(y)=y-(n-1+x)" style="vertical-align:-20%;" class="tex" alt="P_b(y)=y-(n-1+x)" />; and when <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ba098d18f6fa034de80e594a4d754438.png" title="y\in\left[n, n+x\right)" style="vertical-align:-20%;" class="tex" alt="y\in\left[n, n+x\right)" />, <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_6f1091abfcbed7571656718c0f05a0d0.png" title="P_b(y)=1-x" style="vertical-align:-20%;" class="tex" alt="P_b(y)=1-x" />. <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_088923d77af492404f106ac65999d128.png" title="P_a(m,y)" style="vertical-align:-20%;" class="tex" alt="P_a(m,y)" /> is the <a href="http://en.wikipedia.org/wiki/Probability_density_function">probability density function</a> of the aforementioned normal variate; so</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5e3c5ae9dbe51c52a040bf93d4d02198.png" title="\displaystyle P_a(m,y) = e^{-{{6(y-{m\over{}2})^2}\over m}}\sqrt{6\over{m\pi}}" style="vertical-align:-20%;" class="tex" alt="\displaystyle P_a(m,y) = e^{-{{6(y-{m\over{}2})^2}\over m}}\sqrt{6\over{m\pi}}" />.</center></p>
<p>Thus the probability of success in precisely <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> moves is</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e366e51097b0706e13e1d0522f2348cb.png" title="\displaystyle P(m)=\int_{n-1+x}^{n} P_a(m,y) (y-(n-1+x))\,dy + \int_n^{n+x} P_a(m,y) (1-x)\,dy" style="vertical-align:-20%;" class="tex" alt="\displaystyle P(m)=\int_{n-1+x}^{n} P_a(m,y) (y-(n-1+x))\,dy + \int_n^{n+x} P_a(m,y) (1-x)\,dy" /></center></p>
<p>which simplifies to</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_4ce44a215685239bfe1b3c7b23a4e962.png" title="\displaystyle (1-x)\int_{n-1+x}^{n+x} P_a(m,y)\,dy + \int_{n-1+x}^{n} P_a(m,y) (y-n)\,dy" style="vertical-align:-20%;" class="tex" alt="\displaystyle (1-x)\int_{n-1+x}^{n+x} P_a(m,y)\,dy + \int_{n-1+x}^{n} P_a(m,y) (y-n)\,dy" />.</center></p>
<p>Since we&#8217;re interested in a success in any number of moves, we must evaulate <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_c41276c5d941f22c181113ae7a46d213.png" title="\sum_{m=n+1}^{\infty}P(m)" style="vertical-align:-20%;" class="tex" alt="\sum_{m=n+1}^{\infty}P(m)" />, and finish by taking the desired limit <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe3500a8a2c425af0aea75cd0856ec8f.png" title="n\to\infty" style="vertical-align:-20%;" class="tex" alt="n\to\infty" />.</p>
<p>To evalutate the sum over <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" />, we replace the summation by integration (see caveat below) and substitute the variable of integration from <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_39b54fb569800b4de76f3a22735c0ec5.png" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> to <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_4f4a2b18cb439a6759bdc45633eb2277.png" title="2n + r\sqrt{n}" style="vertical-align:-20%;" class="tex" alt="2n + r\sqrt{n}" /> (resulting in an extra factor of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fc601043caad9475490f58f47045a6c0.png" title="\sqrt{n}" style="vertical-align:-20%;" class="tex" alt="\sqrt{n}" />). So our final integral turs out to:</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_ef3a3a8387a16dc4dbd26137f16d71ee.png" title="\displaystyle \int\limits_{-\sqrt{n}}^{\infty} \sqrt{n}\Big((1 - x)\int\limits_{-(1-x)}^{x}P_a(2n+r\sqrt{n},n+t)\,dt + \int\limits_{-(1-x)}^{0}P_a(2n+r\sqrt{n},n+t)t\,dt\Big)\,dr" style="vertical-align:-20%;" class="tex" alt="\displaystyle \int\limits_{-\sqrt{n}}^{\infty} \sqrt{n}\Big((1 - x)\int\limits_{-(1-x)}^{x}P_a(2n+r\sqrt{n},n+t)\,dt + \int\limits_{-(1-x)}^{0}P_a(2n+r\sqrt{n},n+t)t\,dt\Big)\,dr" /></center><br/></p>
<p>Expanding the integrand (of the first integral) in powers of <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_082902c6620229bf1a530f37b45cb739.png" title="1\over n" style="vertical-align:-20%;" class="tex" alt="1\over n" />, the integrand becomes:</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_9f70994514936eb1d003782a8beec9d1.png" title="\displaystyle \sqrt{3\over{4\pi}}e^{-3r^2/4}(1-x^2) + O(e^{-3r^2/4}r^3n^{-1/2})" style="vertical-align:-20%;" class="tex" alt="\displaystyle \sqrt{3\over{4\pi}}e^{-3r^2/4}(1-x^2) + O(e^{-3r^2/4}r^3n^{-1/2})" /></center></p>
<p>When we integrate over <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_f95e4a9e37d3f8fee71fee03216426c8.png" title="r: [-\sqrt{n},\infty)" style="vertical-align:-20%;" class="tex" alt="r: [-\sqrt{n},\infty)" /> and take limit <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe3500a8a2c425af0aea75cd0856ec8f.png" title="n\to\infty" style="vertical-align:-20%;" class="tex" alt="n\to\infty" />, all terms other than the first go away. As for the first term, it yields <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a1a2683706081112a94e400e4250251c.png" title="1-x^2" style="vertical-align:-20%;" class="tex" alt="1-x^2" /> since</p>
<p><center><img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_94d58cea0aa1badbea66a788dfd0b16b.png" title="\displaystyle \int_{-\sqrt{n}}^{\infty}\sqrt{3\over{4\pi}}e^{-3r^2/4}\,dr = {1\over{2}}(1+\mbox{erf}({1\over{2}}\sqrt{3n}))\to 1\quad \mbox{as}\,n\to\infty" style="vertical-align:-20%;" class="tex" alt="\displaystyle \int_{-\sqrt{n}}^{\infty}\sqrt{3\over{4\pi}}e^{-3r^2/4}\,dr = {1\over{2}}(1+\mbox{erf}({1\over{2}}\sqrt{3n}))\to 1\quad \mbox{as}\,n\to\infty" /></center></p>
<p>(where <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_db8f8cda989eee3d5f243afd8c0bef0d.png" title="\mbox{erf}()" style="vertical-align:-20%;" class="tex" alt="\mbox{erf}()" /> is the <a href="http://en.wikipedia.org/wiki/Error_function">Error function</a>).</p>
<p>Whew! That was heavy going. Let us catch our breath. Okay. So the answer to the original problem turns out to be <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_a1a2683706081112a94e400e4250251c.png" title="1-x^2" style="vertical-align:-20%;" class="tex" alt="1-x^2" />. As a sanity check, it&#8217;s value turns out to be 0 at <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_5b00f5ad5f9a199f675af2fb2d0ccf70.png" title="x=1" style="vertical-align:-20%;" class="tex" alt="x=1" /> and 1 at <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_cb11ae6764404172995f1dac8d1eac62.png" title="x=0" style="vertical-align:-20%;" class="tex" alt="x=0" />. Good.</p>
<p><i>Caveat</i>: The above derivation is <i>not rigorous</i> (in fact, it is somewhat sloppy) for at least the following reasons:<br/></p>
<ul>
<li>Convergence of many integrals/summations is implictly assumed</li>
<li>The error term has not been calculated when changing a summation to an integration. <a href="http://mathworld.wolfram.com/Euler-MaclaurinIntegrationFormulas.html">Euler Summation Formula</a> (also TAOCP 1.2.11.2) should have been used to bound the error terms.</li>
<li>The Central Limit approximation has been used without being demonstrated that it is being applied only close to the peak of the distribution, and not far into the tails (unless justified)</li>
</ul>
<p><i>A question:</i> If you read the <a href="http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/solutions/June2007.html">publisher solution on Ponder This</a>, it says, &#8220;Values between 0 and 1 are equally likely but larger values are more likely to cause the sum to exceed <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_e9be79e62f04cc4b913b199c5644142b.png" title="n+x" style="vertical-align:-20%;" class="tex" alt="n+x" />. So as <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_fe3500a8a2c425af0aea75cd0856ec8f.png" title="n\to\infty" style="vertical-align:-20%;" class="tex" alt="n\to\infty" /> the differential probability that <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_109361a77d0b092159482d8224960bce.png" title="x_k=t" style="vertical-align:-20%;" class="tex" alt="x_k=t" /> is <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_41997461bba1857b03e9f3d45d7e5561.png" title="2t\,dt" style="vertical-align:-20%;" class="tex" alt="2t\,dt" />.&#8221; <i>Can the reader explain the reasoning behind this differential probability?</i></p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/EuxcPzHe_A8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2008/12/a-limiting-probability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2008/12/a-limiting-probability/</feedburner:origLink></item>
		<item>
		<title>Certain autorickshaw phenomena</title>
		<link>http://feedproxy.google.com/~r/AshutoshMehraBlog/~3/XsbCkHuGO_4/</link>
		<comments>http://ashutoshmehra.net/blog/2008/12/autorickshaw-phenomena/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 22:20:29 +0000</pubDate>
		<dc:creator>Ashutosh</dc:creator>
				<category><![CDATA[Humor]]></category>

		<guid isPermaLink="false">http://ashutoshmehra.net/blog/?p=17</guid>
		<description><![CDATA[

Some time back, I had read a hilarious piece by Shashank, a friend of mine from the days at BITS, Pilani. The auto-podal-tow phenomena that he describes has to be seen to be believed: an autorickshaw towing another with one of the drivers using his leg as a connection! A good soul captured the moment [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/auto-podal-tow.jpg"><br />
<img src="http://ashutoshmehra.net/blog/wp-content/uploads/2008/12/auto-podal-tow-300x225.jpg" alt="The auto-podal-tow" title="auto-podal-tow" width="200" height="150" class="size-medium wp-image-20" style="float:left;padding:8px"/></a></p>
<p>Some time back, I had read <a href="http://shankaholic.blogspot.com/2008/08/auto-podal-tow-and-other-phenomena-on.html">a hilarious piece</a> by Shashank, a friend of mine from the days at <a href="http://www.bits-pilani.ac.in/">BITS, Pilani</a>. The <i>auto-podal-tow</i> phenomena that he describes has to be seen to be believed: an autorickshaw towing another with one of the drivers using his leg as a connection! <a href="http://www.mattlogelin.com/">A good soul</a> captured <a href="http://www.flickr.com/photos/mattlogelin/121647831/">the moment</a> and licensed it under <a href="http://creativecommons.org/licenses/by-nc/2.0/">Creative Commons Attribution-Noncommercial 2.0 Generic</a>.<span id="more-17"></span></p>
<p>It would have been unfortunate to have left a phenomenon as recurrent as the auto-podal-tow without a name. Shashank did his part, and I&#8217;ll do mine by documenting two other phenomena that deserve to be &#8220;named and conquered&#8221;. People who haven&#8217;t been on Indian roads will have to get their imaginations working (since I couldn&#8217;t find images for them).</p>
<p>First there is, what we&#8217;ll call, the <i>autorickshaw-big-v-formation</i>: In a 2-lane road, a bunch of auto-rickshaws (usually three to five in number) do a <a href="http://en.wikipedia.org/wiki/V_formation">V-formation</a> that&#8217;s impossible for anything but a motor-bike/scooter to breach. While the V-formation is the most commonly sighted one, other <a href="http://www.snowbirds.forces.gc.ca/site/airshows/formations_e.asp">tactical formations</a> are also deployed to advantage.</p>
<p>Second, there&#8217;s a phenomenon known as <i>differential-velocity-autorickshaw-overtake-maneuver</i>. To a casual observer it would appear that two or three autorickshaws are engaging in a line-abreast formation; but in all probability, something more subtle is under way. What&#8217;s really happening is that one of the autorickshaws is running just a little bit faster than the one alongside it (any non-zero <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_264c27cddc8e1cffd6e8ccb80f01011c.png" title="\delta" style="vertical-align:-20%;" class="tex" alt="\delta" />, no matter how small, would be sufficient for our purpose); and the <img src="http://ashutoshmehra.net/blog/wp-content/plugins/easy-latex/cache/tex_264c27cddc8e1cffd6e8ccb80f01011c.png" title="\delta" style="vertical-align:-20%;" class="tex" alt="\delta" />-faster autorickshaw is doing the only reasonable thing to under such circumstances &#8212; overtake the slower one. The phenomenon usually lasts for a good one-two minutes before the &#8220;formation&#8221; is broken.</p>
<img src="http://feeds.feedburner.com/~r/AshutoshMehraBlog/~4/XsbCkHuGO_4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ashutoshmehra.net/blog/2008/12/autorickshaw-phenomena/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://ashutoshmehra.net/blog/2008/12/autorickshaw-phenomena/</feedburner:origLink></item>
	</channel>
</rss>
