{"id":450,"date":"2021-03-30T19:00:00","date_gmt":"2021-03-30T18:00:00","guid":{"rendered":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/?p=450"},"modified":"2022-01-24T13:34:03","modified_gmt":"2022-01-24T13:34:03","slug":"tower-hanoi","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/2021\/03\/30\/tower-hanoi\/","title":{"rendered":"Tower of Hanoi"},"content":{"rendered":"\n<p>I remember my parents taking my brother and I to Pavilh\u00e3o do Conhecimento, a science museum in Lisbon, to spend a day there at least once a year when we were little. And I remember that I loved those days. There were a lot of things we could do there, from playing &#8220;grown-ups&#8221; to solving &#8220;little&#8221; mathematical problems (and here I say little because they were easy enough for a kid to solve without getting frustrated). One of the problems available for us to solve was the Tower of Hanoi consisting of only 3 discs.  In this post I will talk briefly about this problem and why it may be hard to solve.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Problem<\/h3>\n\n\n\n<p>The Tower of Hanoi problem consists of 3 rods and <span class=\"wp-katex-eq\" data-display=\"false\"> n <\/span> discs of different sizes. Roughly, the goal of the problem is to move the stack of discs from the leftmost rod to the rightmost rod. However, as all problems, some rules have to be followed:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>We can only move one disc at a time<\/li><li>We can also only move a disc if it is the uppermost disc on a stack<\/li><li>Finally, we cannot place a larger disc on top of a smaller one. In other words, we will always need to have cone-shape towers<\/li><\/ol>\n\n\n\n<p>So, the simple example I had in my visits to Pavilh\u00e3o do Conhecimento can be solved as shown in the figure below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-content\/uploads\/sites\/22\/2021\/04\/towershanoialg.jpg\" alt=\"\" class=\"wp-image-482\" width=\"513\" height=\"381\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-content\/uploads\/sites\/22\/2021\/04\/towershanoialg.jpg 620w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-content\/uploads\/sites\/22\/2021\/04\/towershanoialg-300x223.jpg 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><figcaption>Image taken from <a href=\"https:\/\/craftofcoding.wordpress.com\/2020\/06\/23\/recursion-the-towers-of-hanoi-iii\/\" data-type=\"URL\" data-id=\"https:\/\/craftofcoding.wordpress.com\/2020\/06\/23\/recursion-the-towers-of-hanoi-iii\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<p>We can see that <span class=\"wp-katex-eq\" data-display=\"false\"> 7 <\/span> steps were required to solve this simple example. This corresponds to <span class=\"wp-katex-eq\" data-display=\"false\"> 2^n-1 <\/span>. You can start to see why this may be difficult to solve in some situations. Imagine if you had <span class=\"wp-katex-eq\" data-display=\"false\"> 10 <\/span> discs, you would solve the problem in <span class=\"wp-katex-eq\" data-display=\"false\"> 1023 <\/span> steps. You may argue it&#8217;s doable but you would need a lot of patience to spend time moving pieces <span class=\"wp-katex-eq\" data-display=\"false\"> 1023 <\/span> times. <\/p>\n\n\n\n<p>So, what can be done to efficiently determine the steps to take with a fairly large number of discs? Well, we can construct an algorithm. If we think about it, we can solve the Tower of Hanoi problem for <span class=\"wp-katex-eq\" data-display=\"false\"> n <\/span> discs by solving it for <span class=\"wp-katex-eq\" data-display=\"false\"> n-1 <\/span> discs. And we can solve the problem with <span class=\"wp-katex-eq\" data-display=\"false\"> n-1 <\/span> discs by solving it for <span class=\"wp-katex-eq\" data-display=\"false\"> n-2 <\/span> disks. And you get the idea: each smaller stack is a sub-problem of the original. For this motive, we can solve the problem by using a<span style=\"color:#b7a99a\" class=\"has-inline-color\"> recursive algorithm<\/span>. So, to move <span class=\"wp-katex-eq\" data-display=\"false\"> n <\/span> discs from the leftmost rod, say <span class=\"wp-katex-eq\" data-display=\"false\"> A <\/span>, to the rightmost rod, say <span class=\"wp-katex-eq\" data-display=\"false\"> C <\/span>, the algorithm can be <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Move <span class=\"wp-katex-eq\" data-display=\"false\"> n-1 <\/span> discs from <span class=\"wp-katex-eq\" data-display=\"false\"> A <\/span> to <span class=\"wp-katex-eq\" data-display=\"false\"> B <\/span> (steps 1 to 3 on the image)<\/li><li>Move the last, and larger, disc from <span class=\"wp-katex-eq\" data-display=\"false\"> A <\/span> to <span class=\"wp-katex-eq\" data-display=\"false\"> C <\/span> (step 4 on the image)<\/li><li>Move <span class=\"wp-katex-eq\" data-display=\"false\"> n-1 <\/span> discs from <span class=\"wp-katex-eq\" data-display=\"false\"> B <\/span> to <span class=\"wp-katex-eq\" data-display=\"false\"> C <\/span> (steps 5 to 7 on the image)<\/li><\/ol>\n\n\n\n<p>Note that step 1 can be solved taking the exact three steps, and so on.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>It may seem a simple enough problem to solve but, in reality, as the complexity of the algorithm is proportional to <span class=\"wp-katex-eq\" data-display=\"false\"> 2^n <\/span>, it will take a long time to solve when the number of discs is large. <\/p>\n\n\n\n<p>I hope you found this post interesting! If so, you can read more about the Tower of Hanoi problem here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.freecodecamp.org\/news\/analyzing-the-algorithm-to-solve-the-tower-of-hanoi-problem-686685f032e3\/\" data-type=\"URL\" data-id=\"https:\/\/www.freecodecamp.org\/news\/analyzing-the-algorithm-to-solve-the-tower-of-hanoi-problem-686685f032e3\/\" target=\"_blank\">https:\/\/www.freecodecamp.org\/news\/analyzing-the-algorithm-to-solve-the-tower-of-hanoi-problem-686685f032e3\/<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/craftofcoding.wordpress.com\/2020\/06\/23\/recursion-the-towers-of-hanoi-iii\/\" data-type=\"URL\" data-id=\"https:\/\/craftofcoding.wordpress.com\/2020\/06\/23\/recursion-the-towers-of-hanoi-iii\/\" target=\"_blank\">https:\/\/craftofcoding.wordpress.com\/2020\/06\/23\/recursion-the-towers-of-hanoi-iii\/<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/csce.ucmss.com\/cr\/books\/2017\/LFS\/CSREA2017\/FEC3153.pdf\" target=\"_blank\">https:\/\/csce.ucmss.com\/cr\/books\/2017\/LFS\/CSREA2017\/FEC3153.pdf<\/a><\/li><\/ul>\n\n\n\n<p>See you on my next post!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I remember my parents taking my brother and I to Pavilh\u00e3o do Conhecimento, a science museum in Lisbon, to spend a day there at least once&#46;&#46;&#46;<\/p>\n","protected":false},"author":21,"featured_media":506,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[18,8,17],"class_list":["post-450","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-algorithm","tag-operational-research","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":9,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/posts\/450\/revisions\/505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/media\/506"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/lidia-andre\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}