AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Hanoi towers recursive function9/5/2023 ![]() The function moveDisk, shown in Listing 2, is very simple. The important thing to remember about handling the base case this way is that simply returning from moveTower is what finally allows the moveDisk function to be called. The base case is detected when the tower height is 0 in this case there is nothing to do, so the moveTower function simply Intermediate pole to the top of the largest disk. Then on line 5 we move the tower from the The next line simply moves the bottom disk to its final resting place. On line 3 we move all but the bottom disk on the initial tower to an intermediate pole. The key to the simplicity of the algorithm is that we make two different recursiveĬalls, one on line 3 and a second on line 5. Notice that the code in Listing 1 is almost identical to the English description. Int moveTower ( int height, char fromPole, char toPole, char withPole ) Listing 1 shows the Python code to solve the Tower of Hanoi puzzle. Move us toward the base case by reducing the height of the tower in steps 1 and 3. A tower of one disk will be our base case. In this case, we need move only a single disk to its final destination. The simplest Tower of Hanoi problem is a tower of one disk. The only thing missing from the outline above is Move the tower of height-1 from the intermediate pole to the final pole using the original pole.Īs long as we always obey the rule that the larger disks remain on the bottom of the stack, we can use the three steps above recursively, treating any larger disks as though they were not even there. Move the remaining disk to the final pole. Move a tower of height-1 to an intermediate pole, using the final pole. Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole: This sounds like a base case in the making. Is easy enough, trivial you might even say. ![]() But what if you do not know how to move a tower of three? How about movingĪ tower of two disks to peg two and then moving the third disk to peg three, and then moving the tower of height two on top of it? But what if you still do not know how to do this? Surely you would agree that moving a single disk to peg three That you knew how to move a tower of height three to peg three then it would be easy to move the fourth disk to peg two and move the three from peg three on top of it. But what if you do not know how to move a tower of height four? Suppose If youĪlready knew how to move a tower of four disks to peg two, you could then easily move the bottom disk to peg three, and then move the tower of four from peg two to peg three. Suppose you have a tower of five disks, originally on peg one. How do we go about solving this problem recursively? How would you go about solving this problem at all? What is our base case? Let's think about this problem from the bottom up. You do not need fancy disks and poles–a pile of books or pieces of paper will workįigure 1: An Example Arrangement of Disks for the Tower of Hanoi If you have not tried to solve this puzzle before, you should try it now. Notice that, as the rules specify, the disks onĮach peg are stacked so that smaller disks are always on top of the larger disks. At a rate of one move per second, that is 584, 942, 417, 355 584, 942, 417, 355 years! Clearly there is more to this puzzle than meets the eye.įigure 1 shows an example of a configuration of disks in the middle of a move from the first peg to the third. When they finished their work, the legend said, the temple would crumble into dust and the world wouldĪlthough the legend is interesting, you need not worry about the world ending any time soon. The priests worked very efficiently, day and night, moving one disk every second. They could only move one disk at a time,Īnd they could never place a larger disk on top of a smaller one. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. Poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. ![]() At the beginning of time, the priests were given three He was inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883.
0 Comments
Read More
Leave a Reply. |