SlideShare a Scribd company logo
1 of 7
In 1883, Edouard Lucas invented, or perhaps reinvented, one of the most popular puzzles of all times - the Tower of Hanoi, as he called it - which is still used today in many computer science textbooks to demonstrate how to write a recursive algorithm or program. First of all, we will make a list of the rules of the puzzle: <br />There are three pegs: A, B and C. <br />There are n disks. The number n is constant while working the puzzle. <br />All disks are different in size. <br />The disks are initially stacked on peg A so that they increase in size from the top to the bottom. <br />The goal of the puzzle is to transfer the entire tower from the A peg to one of the others pegs. <br />One disk at a time can be moved from the top of a stack either to an empty peg or to a peg with a larger disk than itself on the top of its stack. <br />The Algorithm<br />It is well known and rather easy to prove that the minimum number of moves needed to complete the puzzle with n disks is . A simple algorithm which allows us to reach this optimum is as follows: for odd moves, take the smallest disk (number 1) from the peg where it lies to the next one in the circular sequence; for even moves, make the only possible move not involving disk 1. <br />Simple solution<br />The following solution is a simple solution for the toy puzzle.<br />Alternate moves between the smallest piece and a non-smallest piece. When moving the smallest piece, always move it in the same direction (to the right if the starting number of pieces is even, to the left if the starting number of pieces is odd). If there is no tower in the chosen direction, move the piece to the opposite end, but then continue to move in the correct direction. For example, if you started with three pieces, you would move the smallest piece to the opposite end, then continue in the left direction after that. When the turn is to move the non-smallest piece, there is only one legal move. Doing this should complete the puzzle using the least amount of moves to do so.<br />Recursive solution<br />A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. The following procedure demonstrates this approach.<br />label the pegs A, B, C—these labels may move at different steps <br />let n be the total number of discs <br />number the discs from 1 (smallest, topmost) to n (largest, bottommost) <br />To move n discs from peg A to peg C:<br />move n−1 discs from A to B. This leaves disc #n alone on peg A <br />move disc #n from A to C <br />move n−1 discs from B to C so they sit on disc #n <br />The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n−1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1. This step, moving a single disc from peg A to peg B, is trivial.<br />Non-recursive solution<br />The list of moves for a tower being carried from one peg onto another one, as produced by the recursive algorithm has many regularities. When counting the moves starting from 1, the ordinal of the disk to be moved during move m is the number of times m can be divided by 2. Hence every odd move involves the smallest disk. It can also be observed that the smallest disk traverses the pegs f, t, r, f, t, r, etc. for odd height of the tower and traverses the pegs f, r, t, f, r, t, etc. for even height of the tower. This provides the following algorithm, which is easier, carried out by hand, than the recursive algorithm.<br />In alternate moves:<br />move the smallest disk to the peg it has not recently come from. <br />move another disk legally (there will be one possibility only) <br />For the very first move, the smallest disk goes to peg t if h is odd and to peg r if h is even.<br />Also observe that:<br />Disks whose ordinals have even parity move in the same sense as the smallest disk. <br />Disks whose ordinals have odd parity move in opposite sense. <br />If h is even, the remaining third peg during successive moves is t, r, f, t, r, f, etc. <br />If h is odd, the remaining third peg during successive moves is r, t, f, r, t, f, etc. <br />With this knowledge, a set of disks in the middle of an optimal solution can be recovered with no more state information than the positions of each disk:<br />Call the moves detailed above a disk's 'natural' move. <br />Examine the smallest top disk that is not disk 0, and note what it's only (legal) move would be: (if there is no such disc, then we are either at the first or last move). <br />If that move is the disk's 'natural' move, then the disc has not been moved since the last disc 0 move, and that move should be taken. <br />If that move is not the disk's 'natural' move, then move disk 0. <br />Binary solutions<br />Disk positions may be determined more directly from the binary (base 2) representation of the move number (the initial state being move #0, with all digits 0, and the final state being #2n−1, with all digits 1), using the following rules:<br />There is one binary digit (bit) for each disk <br />The most significant (leftmost) bit represents the largest disk. A value of 0 indicates that the largest disk is on the initial peg, while a 1 indicates that it's on the final peg. <br />The bitstring is read from left to right, and each bit can be used to determine the location of the corresponding disk. <br />A bit with the same value as the previous one means that the corresponding disk is stacked on top the previous disk on the same peg. <br />(That is to say: a straight sequence of 1's or 0's means that the corresponding disks are all on the same peg). <br />A bit with a different value to the previous one means that the corresponding disk is one position to the left or right of the previous one. Whether it is left or right is determined by this rule: <br />Assume that the initial peg is on the left and the final peg is on the right. <br />Also assume quot;
wrappingquot;
 - so the right peg counts as one peg quot;
leftquot;
 of the left peg, and vice versa. <br />Let n be the number of greater disks that are located on the same peg as their first greater disk and add 1 if the largest disk is on the left peg. If n is even, the disk is located one peg to the left, if n is odd, the disk located one peg to the right. <br />For example, in an 8-disk Hanoi:<br />Move 0 <br />The largest disk is 0, so it is on the left (initial) peg. <br />All other disks are 0 as well, so they are stacked on top of it. Hence all disks are on the initial peg. <br />Move 28-1 <br />The largest disk is 1, so it is on the right (final) peg. <br />All other disks are 1 as well, so they are stacked on top of it. Hence all disks are on the final peg and the puzzle is complete. <br />Move 0b11011000 = 21610 <br />The largest disk is 1, so it is on the right (final) peg. <br />Disk two is also 1, so it is stacked on top of it, on the right peg. <br />Disk three is 0, so it is on another peg. Since n is odd, it is one peg to the right, i.e. on the left peg. <br />Disk four is 1, so it is on another peg. Since n is still even, it is one peg to the left, i.e. on the right peg. <br />Disk five is also 1, so it is stacked on top of it, on the right peg. <br />Disk six is 0, so it is on another peg. Since n is even, the disk is one peg to the left, i.e. on the middle peg. <br />Disks seven and eight are also 0, so they are stacked on top of it, on the middle peg. <br />Graphical representation<br />The game can be represented by an undirected graph, the nodes representing distributions of disks and the edges representing moves. For one disk, the graph is a triangle:<br />The graph for 2 disks is 3 triangles arranged in a larger triangle:<br />The nodes at the vertices of the outermost triangle represent distributions with all disks on the same peg.<br />For h+1 disks, take the graph of h disks and replace each small triangle with the graph for 2 disks.<br />For 3 disks the graph is:<br />call the pegs a, b and c <br />list disk positions from left to right in order of increasing size <br />The sides of the outermost triangle represent the shortest ways of moving a tower from one peg to another one. The edge in the middle of the sides of the largest triangle represents a move of the largest disk. The edge in the middle of the sides of each next smaller triangle represents a move of each next smaller disk. The sides of the smallest triangles represent moves of the smallest disk.<br />The game graph of level 7 shows the relatedness to the Sierpiński Triangle<br />In general, for a puzzle with n disks, there are 3n nodes in the graph; every node has three edges to other nodes, except the three corner nodes, which have two: it is always possible to move the smallest disk to the one of the two other pegs; and it is possible to move one disk between those two pegs except in the situation where all disks are stacked on one peg. The corner nodes represent the three cases where all the disks are stacked on one peg. The diagram for n+1 disks is obtained by taking three copies of the n-disk diagram—each one representing all the states and moves of the smaller disks for one particular position of the new largest disk—and joining them at the corners with three new edges, representing the only three opportunities to move the largest disk. The resulting figure thus has 3n+1 nodes and still has three corners remaining with only two edges.<br />As more disks are added, the graph representation of the game will resemble the Fractal figure, Sierpiński triangle. It is clear that the great majority of positions in the puzzle will never be reached when using the shortest possible solution; indeed, if the priests of the legend are using the longest possible solution (without re-visiting any position) it will take them 364-1 moves, or more than 1023 years.<br />The longest non-repetitive way for three disks can be visualized by erasing the unused edges:<br />The circular Hamiltonian path for three disks is:<br />The graphs clearly show that:<br />From every arbitrary distribution of disks, there is exactly one shortest way to move all disks onto one of the three pegs. <br />Between every pair of arbitrary distributions of disks there are one or two different shortest paths. <br />From every arbitrary distribution of disks, there are one or two different longest non selfcrossing paths to move all disks to one of the three pegs. <br />Between every pair of arbitrary distributions of disks there are one or two different longest non selfcrossing paths. <br />Let Nh be the number of non selfcrossing paths for moving a tower of h disks from one peg to another one. Then: <br />N1=2 <br />Nh+1=(Nh)2+(Nh)3. <br />For example: N8≈1.5456x10795 <br />Applications<br />The Tower of Hanoi is frequently used in psychological research on problem solving. There also exists a variant of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions.<br />The Tower of Hanoi is also used as Backup rotation scheme when performing computer data Backups where multiple tapes/media are involved.<br />As mentioned above, the Tower of Hanoi is popular for teaching recursive algorithms to beginning programming students. A pictorial version of this puzzle is programmed into the emacs editor, accessed by typing M-x hanoi. There is also a sample algorithm written in Prolog.<br />The Tower of Hanoi is also used as a test by neuropsychologists trying to evaluate frontal lobe deficits.<br /> <br />
Tower of hanoi
Tower of hanoi
Tower of hanoi
Tower of hanoi
Tower of hanoi
Tower of hanoi

More Related Content

Viewers also liked

Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi AlgorithmJorge Jasso
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoiNicholas Chen
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoistudent
 
Araling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleAraling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleJhing Pantaleon
 

Viewers also liked (9)

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi Algorithm
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoi
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoi
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
AP G8/G9 lm q1
AP G8/G9 lm q1AP G8/G9 lm q1
AP G8/G9 lm q1
 
Araling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter ModuleAraling Panlipunan Grade 8 - First Quarter Module
Araling Panlipunan Grade 8 - First Quarter Module
 

Similar to Tower of hanoi

Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithmWeaamRaed
 
Tower of Hanoi Investigation
Tower of Hanoi InvestigationTower of Hanoi Investigation
Tower of Hanoi InvestigationWriters Per Hour
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiManishPrajapati78
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentationFahadQaiser1
 
Newton's Second Law - The Effect of Force
Newton's Second Law - The Effect of ForceNewton's Second Law - The Effect of Force
Newton's Second Law - The Effect of ForceSarah Sue Calbio
 
AI-Lec3 State Search Space-Graph Theory.pptx
AI-Lec3 State Search Space-Graph Theory.pptxAI-Lec3 State Search Space-Graph Theory.pptx
AI-Lec3 State Search Space-Graph Theory.pptxSeharAli13
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
homework_chap_3_a_solutions.docx
homework_chap_3_a_solutions.docxhomework_chap_3_a_solutions.docx
homework_chap_3_a_solutions.docxtsrtgsdfg
 
Recurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxRecurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxgbikorno
 
100 Combinatorics Problems (With Solutions)
100 Combinatorics Problems (With Solutions)100 Combinatorics Problems (With Solutions)
100 Combinatorics Problems (With Solutions)Courtney Esco
 
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)Muhammad Faizan Musa
 
Recurrent problems: TOH, Pizza Cutting and Josephus Problems
Recurrent problems: TOH, Pizza Cutting and Josephus ProblemsRecurrent problems: TOH, Pizza Cutting and Josephus Problems
Recurrent problems: TOH, Pizza Cutting and Josephus ProblemsMenglinLiu1
 

Similar to Tower of hanoi (20)

Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithm
 
Tower of Hanoi Investigation
Tower of Hanoi InvestigationTower of Hanoi Investigation
Tower of Hanoi Investigation
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
 
Data structure lab
Data structure labData structure lab
Data structure lab
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentation
 
Newton's Second Law - The Effect of Force
Newton's Second Law - The Effect of ForceNewton's Second Law - The Effect of Force
Newton's Second Law - The Effect of Force
 
AI-Lec3 State Search Space-Graph Theory.pptx
AI-Lec3 State Search Space-Graph Theory.pptxAI-Lec3 State Search Space-Graph Theory.pptx
AI-Lec3 State Search Space-Graph Theory.pptx
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
 
homework_chap_3_a_solutions.docx
homework_chap_3_a_solutions.docxhomework_chap_3_a_solutions.docx
homework_chap_3_a_solutions.docx
 
Pascal Triangle
Pascal TrianglePascal Triangle
Pascal Triangle
 
Recurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxRecurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptx
 
100 Combinatorics Problems (With Solutions)
100 Combinatorics Problems (With Solutions)100 Combinatorics Problems (With Solutions)
100 Combinatorics Problems (With Solutions)
 
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
 
Blindfold
BlindfoldBlindfold
Blindfold
 
On reflection
On reflection On reflection
On reflection
 
Recurrent problems: TOH, Pizza Cutting and Josephus Problems
Recurrent problems: TOH, Pizza Cutting and Josephus ProblemsRecurrent problems: TOH, Pizza Cutting and Josephus Problems
Recurrent problems: TOH, Pizza Cutting and Josephus Problems
 
400 puzzlesnnn
400 puzzlesnnn400 puzzlesnnn
400 puzzlesnnn
 

Recently uploaded

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Tower of hanoi

  • 1. In 1883, Edouard Lucas invented, or perhaps reinvented, one of the most popular puzzles of all times - the Tower of Hanoi, as he called it - which is still used today in many computer science textbooks to demonstrate how to write a recursive algorithm or program. First of all, we will make a list of the rules of the puzzle: <br />There are three pegs: A, B and C. <br />There are n disks. The number n is constant while working the puzzle. <br />All disks are different in size. <br />The disks are initially stacked on peg A so that they increase in size from the top to the bottom. <br />The goal of the puzzle is to transfer the entire tower from the A peg to one of the others pegs. <br />One disk at a time can be moved from the top of a stack either to an empty peg or to a peg with a larger disk than itself on the top of its stack. <br />The Algorithm<br />It is well known and rather easy to prove that the minimum number of moves needed to complete the puzzle with n disks is . A simple algorithm which allows us to reach this optimum is as follows: for odd moves, take the smallest disk (number 1) from the peg where it lies to the next one in the circular sequence; for even moves, make the only possible move not involving disk 1. <br />Simple solution<br />The following solution is a simple solution for the toy puzzle.<br />Alternate moves between the smallest piece and a non-smallest piece. When moving the smallest piece, always move it in the same direction (to the right if the starting number of pieces is even, to the left if the starting number of pieces is odd). If there is no tower in the chosen direction, move the piece to the opposite end, but then continue to move in the correct direction. For example, if you started with three pieces, you would move the smallest piece to the opposite end, then continue in the left direction after that. When the turn is to move the non-smallest piece, there is only one legal move. Doing this should complete the puzzle using the least amount of moves to do so.<br />Recursive solution<br />A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. The following procedure demonstrates this approach.<br />label the pegs A, B, C—these labels may move at different steps <br />let n be the total number of discs <br />number the discs from 1 (smallest, topmost) to n (largest, bottommost) <br />To move n discs from peg A to peg C:<br />move n−1 discs from A to B. This leaves disc #n alone on peg A <br />move disc #n from A to C <br />move n−1 discs from B to C so they sit on disc #n <br />The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n−1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1. This step, moving a single disc from peg A to peg B, is trivial.<br />Non-recursive solution<br />The list of moves for a tower being carried from one peg onto another one, as produced by the recursive algorithm has many regularities. When counting the moves starting from 1, the ordinal of the disk to be moved during move m is the number of times m can be divided by 2. Hence every odd move involves the smallest disk. It can also be observed that the smallest disk traverses the pegs f, t, r, f, t, r, etc. for odd height of the tower and traverses the pegs f, r, t, f, r, t, etc. for even height of the tower. This provides the following algorithm, which is easier, carried out by hand, than the recursive algorithm.<br />In alternate moves:<br />move the smallest disk to the peg it has not recently come from. <br />move another disk legally (there will be one possibility only) <br />For the very first move, the smallest disk goes to peg t if h is odd and to peg r if h is even.<br />Also observe that:<br />Disks whose ordinals have even parity move in the same sense as the smallest disk. <br />Disks whose ordinals have odd parity move in opposite sense. <br />If h is even, the remaining third peg during successive moves is t, r, f, t, r, f, etc. <br />If h is odd, the remaining third peg during successive moves is r, t, f, r, t, f, etc. <br />With this knowledge, a set of disks in the middle of an optimal solution can be recovered with no more state information than the positions of each disk:<br />Call the moves detailed above a disk's 'natural' move. <br />Examine the smallest top disk that is not disk 0, and note what it's only (legal) move would be: (if there is no such disc, then we are either at the first or last move). <br />If that move is the disk's 'natural' move, then the disc has not been moved since the last disc 0 move, and that move should be taken. <br />If that move is not the disk's 'natural' move, then move disk 0. <br />Binary solutions<br />Disk positions may be determined more directly from the binary (base 2) representation of the move number (the initial state being move #0, with all digits 0, and the final state being #2n−1, with all digits 1), using the following rules:<br />There is one binary digit (bit) for each disk <br />The most significant (leftmost) bit represents the largest disk. A value of 0 indicates that the largest disk is on the initial peg, while a 1 indicates that it's on the final peg. <br />The bitstring is read from left to right, and each bit can be used to determine the location of the corresponding disk. <br />A bit with the same value as the previous one means that the corresponding disk is stacked on top the previous disk on the same peg. <br />(That is to say: a straight sequence of 1's or 0's means that the corresponding disks are all on the same peg). <br />A bit with a different value to the previous one means that the corresponding disk is one position to the left or right of the previous one. Whether it is left or right is determined by this rule: <br />Assume that the initial peg is on the left and the final peg is on the right. <br />Also assume quot; wrappingquot; - so the right peg counts as one peg quot; leftquot; of the left peg, and vice versa. <br />Let n be the number of greater disks that are located on the same peg as their first greater disk and add 1 if the largest disk is on the left peg. If n is even, the disk is located one peg to the left, if n is odd, the disk located one peg to the right. <br />For example, in an 8-disk Hanoi:<br />Move 0 <br />The largest disk is 0, so it is on the left (initial) peg. <br />All other disks are 0 as well, so they are stacked on top of it. Hence all disks are on the initial peg. <br />Move 28-1 <br />The largest disk is 1, so it is on the right (final) peg. <br />All other disks are 1 as well, so they are stacked on top of it. Hence all disks are on the final peg and the puzzle is complete. <br />Move 0b11011000 = 21610 <br />The largest disk is 1, so it is on the right (final) peg. <br />Disk two is also 1, so it is stacked on top of it, on the right peg. <br />Disk three is 0, so it is on another peg. Since n is odd, it is one peg to the right, i.e. on the left peg. <br />Disk four is 1, so it is on another peg. Since n is still even, it is one peg to the left, i.e. on the right peg. <br />Disk five is also 1, so it is stacked on top of it, on the right peg. <br />Disk six is 0, so it is on another peg. Since n is even, the disk is one peg to the left, i.e. on the middle peg. <br />Disks seven and eight are also 0, so they are stacked on top of it, on the middle peg. <br />Graphical representation<br />The game can be represented by an undirected graph, the nodes representing distributions of disks and the edges representing moves. For one disk, the graph is a triangle:<br />The graph for 2 disks is 3 triangles arranged in a larger triangle:<br />The nodes at the vertices of the outermost triangle represent distributions with all disks on the same peg.<br />For h+1 disks, take the graph of h disks and replace each small triangle with the graph for 2 disks.<br />For 3 disks the graph is:<br />call the pegs a, b and c <br />list disk positions from left to right in order of increasing size <br />The sides of the outermost triangle represent the shortest ways of moving a tower from one peg to another one. The edge in the middle of the sides of the largest triangle represents a move of the largest disk. The edge in the middle of the sides of each next smaller triangle represents a move of each next smaller disk. The sides of the smallest triangles represent moves of the smallest disk.<br />The game graph of level 7 shows the relatedness to the Sierpiński Triangle<br />In general, for a puzzle with n disks, there are 3n nodes in the graph; every node has three edges to other nodes, except the three corner nodes, which have two: it is always possible to move the smallest disk to the one of the two other pegs; and it is possible to move one disk between those two pegs except in the situation where all disks are stacked on one peg. The corner nodes represent the three cases where all the disks are stacked on one peg. The diagram for n+1 disks is obtained by taking three copies of the n-disk diagram—each one representing all the states and moves of the smaller disks for one particular position of the new largest disk—and joining them at the corners with three new edges, representing the only three opportunities to move the largest disk. The resulting figure thus has 3n+1 nodes and still has three corners remaining with only two edges.<br />As more disks are added, the graph representation of the game will resemble the Fractal figure, Sierpiński triangle. It is clear that the great majority of positions in the puzzle will never be reached when using the shortest possible solution; indeed, if the priests of the legend are using the longest possible solution (without re-visiting any position) it will take them 364-1 moves, or more than 1023 years.<br />The longest non-repetitive way for three disks can be visualized by erasing the unused edges:<br />The circular Hamiltonian path for three disks is:<br />The graphs clearly show that:<br />From every arbitrary distribution of disks, there is exactly one shortest way to move all disks onto one of the three pegs. <br />Between every pair of arbitrary distributions of disks there are one or two different shortest paths. <br />From every arbitrary distribution of disks, there are one or two different longest non selfcrossing paths to move all disks to one of the three pegs. <br />Between every pair of arbitrary distributions of disks there are one or two different longest non selfcrossing paths. <br />Let Nh be the number of non selfcrossing paths for moving a tower of h disks from one peg to another one. Then: <br />N1=2 <br />Nh+1=(Nh)2+(Nh)3. <br />For example: N8≈1.5456x10795 <br />Applications<br />The Tower of Hanoi is frequently used in psychological research on problem solving. There also exists a variant of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions.<br />The Tower of Hanoi is also used as Backup rotation scheme when performing computer data Backups where multiple tapes/media are involved.<br />As mentioned above, the Tower of Hanoi is popular for teaching recursive algorithms to beginning programming students. A pictorial version of this puzzle is programmed into the emacs editor, accessed by typing M-x hanoi. There is also a sample algorithm written in Prolog.<br />The Tower of Hanoi is also used as a test by neuropsychologists trying to evaluate frontal lobe deficits.<br /> <br />