Towers of Hanoi
From Math Images
|Towers of Hanoi|
Basic DescriptionThe standard game begins with 3 poles, one of which (the starting pole) has some natural number, k, disks placed around it in increasing size from top to bottom, while the other poles start out empty.
- Goal: Move all k disks from the starting pole (typically the leftmost) to the specified end pole (typically the rightmost), following these rules:
- Only one disk may be moved at one time.
- Each move consists of moving the top disk of one pole onto the top of another pole.
- Each disk must be placed on a larger disk.
The game is solvable and has a shortest solution (fewest moves) for any number of disks. To fully understand the game's legend, one must understand the relationship between the number of disks and the number of moves. As it turns out, this relationship is exponential (see A More Mathematical Section for details).
We can see the effects of this exponential relationship in the legend about the priests, who had to move 64 disks from one pole to the other. If they were to move one disk per second, then they would need 18,446,744,073,709,551,615 seconds, or 584.54 billion years, to complete the game. Given that this is about 58.5 times the life span of the Sun, the legend probably overestimates the amount of time we have until the world ends (unless we can survive without the Sun and the Earth).
Play the Game
You can play the game Towers of Hanoi right here! This applet will help you understand how the game works.
Example of Solving the Game
Solving Towers of Hanoi with 4 disks.
A More Mathematical Explanation
The Recursive SolutionThis game becomes more interesting when we try to figure out the shortest [...]
The Recursive Solution
This game becomes more interesting when we try to figure out the shortest solution for any number of disks. We will first state an algorithm that solves the game, then show that this algorithm is the shortest solution. We will refer to the start pole as Pole A, the end pole as Pole C, and the remaining pole as Pole B. Define Tk to be the number of moves our algorithm needs to solve the game for k disks. To solve the game with 1 disk, simply move the disk from Pole A to Pole C, so
Assume that we know how to move k -1 disks (so we also know the value of Tk-1). To solve the game with k disks, follow these steps:
- 1. Move the top k -1 disks on Pole A to Pole B using our algorithm, taking Tk-1 moves.
- 2. Move the kth disk to Pole C, taking 1 move.
- 3. Move the k -1 disks on Pole B on top of the kth disk on Pole C using our algorithm, taking Tk-1 moves.
Hence we can describe the number of moves in our algorithm by the recursive formula:
We will now show by induction that our algorithm uses the smallest number of moves. The base case (T1 = 1) is satisfied because there is no way to solve the game without moving any disks. Assume that our algorithm takes the least number of moves to solve for k -1 disks, so Tk-1 is minimal.
To solve the game with k disks, the kth disk must be moved at some point, and moving it requires the top k -1 disks to be on the pole that is not the starting or ending pole of the kth disk. Since our algorithm needs the least number of moves to solve for k -1 disks, we will assume that moving the top k -1 disks takes Tk-1 moves. Note that there are two possible ways to move the kth disk: onto Pole B or onto Pole C. We will reason that moving it onto Pole C is the correct choice.
If we wish to move the kth disk onto Pole B, we must first move the top k -1 disks onto Pole C. We then need at least one additional move to move the kth disk onto Pole B. However, the end pole is Pole C, so the kth disk must be on Pole C before the remaining k -1 disks can be stacked on top of it. In order to move the kth disk to Pole C, we move the k -1 disks onto Pole A. Once again, we need at least one additional move to move the kth disk onto Pole C. Finally, we complete the game by moving the top k -1 disks onto Pole C. Using this approach, we had to move the kth disk at least twice and the k -1 disks three times.
If we wish to move the kth disk directly to Pole C, we must first move the top k -1 disks onto Pole B (see Image 1). We then need at least one additional move to move the kth disk onto Pole C. Finally, we complete the game by moving the top k -1 disks onto Pole C (see Image 2). Using this approach, we had to move the kth disk once and the k -1 disks twice, which is less than the previous approach. Since the rules imply that all moves are independent, we can conclude that this approach takes the least number of moves to solve for k disks.
Thus the minimum number of moves required to solve for k disks assuming Tk-1 is minimized is
The Explicit Formula
We will show by induction that the explicit formula for the number of moves in this minimal algorithm is
The base case is satisfied:
Assume the (k -1)th case holds. Then
Thus we have shown the minimal algorithm for any Towers of Hanoi game with k disks requires Tk = 2k – 1 steps.
Back to the Legend
Now that we have the equation to determine the number of moves needed for any number of disks, we can verify the time we said was needed to complete the game in the legend. A Towers of Hanoi game with 64 disks will take:
If the priests move 1 disk per second, then they will need:
Assuming that the Sun has a life span of 10 billion years, the time predicted by the legend is times longer than the Sun's lifespan.
Why It's InterestingTowers of Hanoi is a popular example in introductory computer science courses because programming it is instructional but relatively simple. The mathematical nature of the game teaches recursion, while the simple interface of the game introduces basic graphics processing. In addition, the game's exponential solution is an example of why computer scientists dislike exponential time; it takes far too long to compute. Assuming the average computer can do 100 million operations per minute, then solving the game with 64 disks would take the computer 5845.42 years to complete.
Towers of Hanoi is also a popular tool in psychological research; it can test problem solving capabilities. Consequently, the game is often used to test intelligence. In the 2011 film Rise of the Planet of the Apes, scientists test the intelligence of apes with Towers of Hanoi. The faster the apes solve the game, the more intelligent they are perceived to be.
- There are currently no teaching materials for this page. Add teaching materials.
About the Creator of this Image
- ↑ http://www.lawrencehallofscience.org/java/tower/
- ↑ http://helios.gsfc.nasa.gov/qa_sun.html
- ↑ http://apcentral.collegeboard.com/apc/members/courses/teachers_corner/45418.html
- ↑ http://cognitivepsychology.wikidot.com/problem-solving:tower-of-hanoi
- ↑ http://www.imdb.com/title/tt1318514/synopsis
Leave a message on the discussion page by clicking the 'discussion' tab at the top of this image page.