Chapter 9.1.2 Towers of Hanoi | Introduction to Programming Using Java

## 9.1.2 Towers of Hanoi

Binary search can be implemented with a while loop, instead of with recursion, as was done in Subsection 7.4.1. Next, we turn to a problem that is easy to solve with recursion but difficult to solve without it.

This is a standard example known as “The Towers of Hanoi.” The problem involves a stack of various-sized disks, piled up on a base in order of decreasing size. The object is to move the stack from one base to another, subject to two rules: Only one disk can be moved at a time, and no disk can ever be placed on top of a smaller disk. There is a third base that can be used as a “spare.” The starting situation for a stack of ten disks is shown in the top half of the following picture.

The situation after a number of moves have been made is shown in the bottom half of the picture. These pictures are from the applet at the end of Section 9.5, which displays an animation of the step-by-step solution of the problem.

The problem is to move ten disks from Stack 0 to Stack 1, subject to certain rules. Stack 2 can be used as a spare location. Can we reduce this to smaller problems of the same type, possibly generalizing the problem a bit to make this possible? It seems natural to consider the size of the problem to be the number of disks to be moved.

If there are N disks in Stack 0, we know that we will eventually have to move the bottom disk from Stack 0 to Stack 1. But before we can do that, according to the rules, the first N-1 disks must be on Stack 2. Once we’ve moved the N-th disk to Stack 1, we must move the other N-1 disks from Stack 2 to Stack 1 to complete the solution.

But moving N-1 disks is the same type of problem as moving N disks, except that it’s a smaller version of the problem. This is exactly what we need to do recursion! The problem has to be generalized a bit, because the smaller problems involve moving disks from Stack 0 to Stack 2 or from Stack 2 to Stack 1, instead of from Stack 0 to Stack 1. In the recursive subroutine that solves the problem,

the stacks that serve as the source and destination of the disks have to be specified. It’s also convenient to specify the stack that is to be used as a spare, even though we could figure that out from the other two parameters. The base case is when there is only one disk to be moved. The solution in this case is trivial: Just move the disk in one step. Here is a version of the subroutine that will print out step-by-step instructions for solving the problem:

This subroutine just expresses the natural recursive solution. The recursion works because each recursive call involves a smaller number of disks, and the problem is trivial to solve in the base case, when there is only one disk. To solve the “top level” problem of moving N disks from Stack 0 to Stack 1, it should be called with the command TowersOfHanoi(N,0,1,2). The subroutine is demonstrated by the sample program TowersOfHanoi*.*java.

Here, for example, is the output from the program when it is run with the number of disks set equal to 3:

The output of this program shows you a mass of detail that you don’t really want to think about! The difficulty of following the details contrasts sharply with the simplicity and elegance of the recursive solution. Of course, you really want to leave the details to the computer. It’s much more interesting to watch the applet from Section 9.5, which shows the solution graphically.

That applet uses the same recursive subroutine, except that the System.out.println statements are replaced by commands that show the image of the disk being moved from one stack to another.

There is, by the way, a story that explains the name of this problem. According to this story, on the first day of creation, a group of monks in an isolated tower near Hanoi were given a stack of 64 disks and were assigned the task of moving one disk every day, according to the rules of the Towers of Hanoi problem.

On the day that they complete their task of moving all the disks from one stack to another, the universe will come to an end. But don’t worry. The number of steps required to solve the problem for N disks is 2* ^{N }*– 1, and 2

^{64 }– 1 days is over 50,000,000,000,000 years. We have a long way to go.

(In the terminology of Section 8.6, the Towers of Hanoi algorithm has a run time that is Θ(2* ^{n}*), where n is the number of disks that have to be moved. Since the exponential function 2

*grows so quickly, the Towers of Hanoi problem can be solved in practice only for a small number of disks.)*

^{n }By the way, in addtion to the graphical Towers of Hanoi applet at the end of this chapter, there are two other end-of-chapter applets in the on-line version of this text that use recursion. One is a maze-solving applet from the end of Section 11.5, and the other is a pentominos applet from the end of Section 10.5.

The Maze applet first builds a random maze. It then tries to solve the maze by finding a path through the maze from the upper left corner to the lower right corner. This problem is actually very similar to a “blob-counting” problem that is considered later in this section.

The recursive maze-solving routine starts from a given square, and it visits each neighboring square and calls itself recursively from there. The recursion ends if the routine finds itself at the lower right corner of the maze.

The Pentominos applet is an implementation of a classic puzzle. A pentomino is a connected figure made up of five equal-sized squares. There are exactly twelve figures that can be made in this way, not counting all the possible rotations and reflections of the basic figures.

The problem is to place the twelve pentominos on an 8-by-8 board in which four of the squares have already been marked as filled. The recursive solution looks at a board that has already been partially filled with pentominos. The subroutine looks at each remaining piece in turn.

It tries to place that piece in the next available place on the board. If the piece fits, it calls itself recursively to try to fill in the rest of the solution. If that fails, then the subroutine goes on to the next piece. A generalized version of the pentominos applet with many more features can be found at http://math.hws.edu/xJava/PentominosSolver/.

The Maze applet and the Pentominos applet are fun to watch, and they give nice visual representations of recursion.

### SEE MORE:

- Chapter 9.3.1 Stacks | Introduction to Programming Using Java
- Chapter 9.4.1 Tree Traversal | Introduction to Program,ming Using Java
- Chapter 10.1.1 Generic Programming in Smalltalk | Introduc’tion to Programming Using Java
- Chapter 10.1.2 Generic Programming in C++ | Introduction to Program’ming Using Java
- Chapter 10.1.3 Generic Programming in Java | Introduction to Program,ming Using Java
- Chapter 10.1.4 The Java Collection Framework | Introduc’tion to Program’ming Using Java
- Chapter 10.1.5 Iterators and for-each Loops | Introduction to Programming Using Java
- Chapter 11.4.2 TCP/IP and Client/Server | Introduc’tion to Program ming ,Using Java
- Chapter 12 Advanced GUI Programming | Images and Resources | Introduction to Program ming Using Java

## 11 thoughts on “Chapter 9.1.2 Towers of Hanoi | Introduction to Programming Using Java”