{"id":5362,"date":"2016-07-01T12:01:00","date_gmt":"2016-07-01T16:01:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=5362"},"modified":"2016-07-01T12:11:32","modified_gmt":"2016-07-01T16:11:32","slug":"knuths-first-lecture","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=5362","title":{"rendered":"Knuth&#8217;s First Lecture"},"content":{"rendered":"<p> Back in November of 1969 a young Don Knuth gave his first lecture as a Stanford professor. The lecture was on a new field in Computer Science called Analysis of Algorithms, a field that Knuth invented and named. Recently, Knuth <a href=\"http:\/\/www-cs-faculty.stanford.edu\/~knuth\/news.html\">recreated that lecture<\/a> so that it could be filmed for a university series of videos on important topics. The video is really excellent and everyone in our field should spend the hour to watch it. <\/p>\n<p> Because the Analysis of Algorithms field was new and mostly unknown, Knuth spent the lecture on an example of analyzing an algorithm. He choose a very simple problem. Given a sequence of numbers \\(x_1, x_{2},\\ldots,x_n\\), find the maximum number, \\(m\\), in the sequence. We can assume the the \\(x_i\\) are unique and that every ordering is equally likely. To solve the problem, Knuth chose the obvious algorithm captured by the flow chart below. <\/p>\n<div class=\"figure\">\n<p><img decoding=\"async\" src=\"http:\/\/irreal.org\/blog\/wp-content\/uploads\/2016\/07\/max-n-2.png\" alt=\"max-n.png\" \/> <\/p>\n<\/p><\/div>\n<p> At first glance, it&#8217;s hard to see what there is to analyze. The algorithm requires an array of size \\(n\\) and the two additional variables \\(m\\) and \\(k\\) for a total of \\(n+2\\) so it requires \\(O(n)\\) space. Similarly, it&#8217;s easy to see that we go through the loop \\(n-1\\) times before we terminate so its time is \\(O(n)\\) too. <\/p>\n<p> In fact, with one exception it&#8217;s trivially easy to specify how many times each node in the flow chart is executed. The <code>k == 0?<\/code> test is made \\(n\\) times and the <code>x[k] &gt; m<\/code> test is made \\(n-1\\) times for example. The one exception is the <code>m \u2190 x[k]<\/code> box. It is executed only when a new maximum is found. Call the number of times it&#8217;s executed \\(A\\). What can we say about it? <\/p>\n<p> Obviously, \\(A=0\\) when the maximum number is last in the sequence and it&#8217;s \\(n-1\\) when \\(x_1 > x_2 > \\ldots >x_n\\) but what is its average (or expected) value? If you don&#8217;t think about it too hard, \\(n\/2\\) seems like a good guess. <\/p>\n<p> It&#8217;s easy to calculate the \\(n=3\\) case and when you do, you find that the average value of \\(A\\) is \\(5\/6\\) so our \\(n\/2\\) guess is incorrect. Most of the lecture is spent finding the correct answer, which turns out to be \\(H_{n}-1\\) where \\(H_n\\) is the \\(n\\)<sup>th<\/sup> partial sum of the harmonic series. Therefore, \\(A \\approx \\ln(n)\\) and the average value of \\(A\\) for \\(n=1,000,000\\) is about 13.5; much different from what you might have expected. <\/p>\n<p> Knuth says that the techniques he uses in this analysis appear over and over again so the simple problem turns out to be an important and instructive example. If you&#8217;d like to explore the analysis at your own pace, you can find the same example in Section 1.2.10 of AOCP. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in November of 1969 a young Don Knuth gave his first lecture as a Stanford professor. The lecture was on a new field in Computer Science called Analysis of Algorithms, a field that Knuth invented and named. Recently, Knuth &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=5362\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[37],"class_list":["post-5362","post","type-post","status-publish","format-standard","hentry","category-programming","tag-tech"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5362"}],"version-history":[{"count":3,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5362\/revisions"}],"predecessor-version":[{"id":5367,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5362\/revisions\/5367"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}