 # Active Learning with Ian Parberry's Towers of Hanoi

## Introduction

The Towers of Hanoi puzzle is a fun way to get students thinking logically and algorithmically, which is an important first step to becoming a programmer. It is also a gateway to attaining some of the more difficult skills that separate the professional programmer from the novice: theoretical skills such as algorithm design, algorithm analysis, and theory of algorithms; and practical skills such as programming and experimental algorithmics. Specific topics touched upon include divide-and-conquer, recursion, stacks, recurrence relations, and mathematical induction.

We begin with some relatively simple activities that, while they are accessible to grade school or high school students, can be profitable to even the most jaded of graduate students. Competitive Activities gets students to compete against each other to see who can solve the Towers of Hanoi with the largest number of discs. Looking and Thinking Activities engage students in experimenting with the Towers of Hanoi app, and asks them to think about and report their observations. Acting Out Activities asks students to act out an algorithm in pseudocode using the Towers of Hanoi app.

The remaining activities are intended for undergraduate and graduate students. The main theme is enabling students to generalize from the small examples in the Towers of Hanoi app. Algorithm Design Activities has students compose algorithms for interesting variants of the Towers of Hanoi puzzle. Algorithm Analysis Activities involve the construction of mathematical formulae for the number of moves made by the standard algorithm and the algorithms from the Algorithm Design Activities. Theory of Algorithms Activities has students investigate some of the properties of their algorithms. Programming Activities invites students to code up their algorithms. Experimental Algorithmics Activities engages students in measuring the run-time of their programs in order to identify which algorithmic techniques are faster in practice.

## Dependencies

The following diagram shows the dependencies between the activities. Click for a larger image. ## Competitive Activities Competitive activities help engage student interest. They can be used first thing to get students motivated, as breaks from the more rigorous activities, or after they have done some of the activities so that they can apply what they have learned, if anything. Have students try to solve the Towers of Hanoi with as many discs as they can and report the result to the class. They will get a humorous rank for each puzzle solved. The ability to follow instructions and to drag and drop with the mouse are all that is necessary to participate in this activity. Graduate students may get as much out of it as 6 year-olds.

## Looking and Thinking Activities

The activities in this section are best done in groups, although it can also be profitable for students to do them individually. They are accessible even to young students who have not yet learned to program. The ability to follow instructions, drag and drop with the mouse, and some logical, observational, and reporting skills are all that is really necessary.

1. Watch the solution for the Towers of Hanoi with 4 discs. Reset it, and press the Step button 7 times. What do you think the algorithm was doing up to this point? What do you think it will do next? Watch the solution for the Towers of Hanoi with 5 discs. Reset it, and press the Step button 15 times. Can you see anything interesting? How many steps do you think it will take to get to this point with 6 discs? Try it out and see if you are right.
2. Watch the solutions for the Towers of Hanoi with 3, 4, 5, and 6 discs with the Count checkbox checked. Write down the number of moves that it takes each time. Can you see a pattern to these numbers? Why do you think this pattern exists?
3. Watch the solution for the Towers of Hanoi with 6 discs. When does the red disc move? How does it move? What about the green disc? The blue one? Is there any pattern here? 1. Uncheck the Colors checkbox and watch the solution for the Towers of Hanoi with 6 discs. Do you notice anything special about the stacked discs?
2. Watch the solution for the Towers of Hanoi for 3 or 4 discs with each of the 5 Restrictions on Motion. what is similar about them? what is different about them? watch the smallest disc in particular.
3. Construct a table that shows, for $1\le n\le 8$$1 \leq n \leq 8$, the number of moves used by autosolve to solve the Towers of Hanoi for $n$$n$ discs with each of the 5 Restrictions on Motion. Your table should look something like the following. Examine the entries for the "move through a peg" restrictions. Can you see any patterns? Similarly, can you see any patterns for the "rotate" restrictions? From here on, the activities require knowledge of things that are normally taught in an advanced high school or introductory undergraduate programming class.

## Acting Out Activities

The standard algorithm (that is, the recursive one that's always taught in intro programming classes) for the Towers of Hanoi puzzle with $n$$n$ discs looks like the following. Number the pegs 0, 1, and 2 from left to right. Our aim is to move $n$$n$ discs from peg 0 to peg 2. However, we need to come up with a general method for moving $n$$n$ discs from peg $i$$i$ to peg $j$$j$ that works for all $i$$i$ and $j$$j$ between 0 and 2. 1. Does the above algorithm work correctly for $n=1$$n=1$? For $n=2$$n=2$? How many recursive calls are made?
2. Act out the above algorithm for $n=3,$$n=3,$ and $n=4$$n=4$. How many recursive calls are made?

## Algorithm Design Activities

The activities in this section require knowledge of algorithm design (preferably using pseudocode), particularly recursive algorithms.

1. Design a non-recursive algorithm for the Towers of Hanoi using a stack data structure. You can probably find one online, but you will learn more if you design it yourself.
2. Design a non-recursive algorithm for the Towers of Hanoi that doesn't use a stack. (Hint: See Theory of Algorithms Activity 3).
3. The standard algorithm only solves the Towers of Hanoi when all discs start on the leftmost peg. Design a simple recursive algorithm that will solve it from any legal start configuration (that is, the discs may be on any pegs but they must be in ascending order of size from bottom to top on each peg). If you have trouble getting started, try observing autosolve in action.
4. Following up on Looking and Thinking Activity 1, devise an algebraic formula that gives you the peg that disc $i$$i$ will be on after $m$$m$ moves on $n$$n$ discs. (hint the answer will depend mostly on $i$$i$ and $m$$m$. the direction will also depend on whether $n$$n$ is odd or even.) this formula lets you can jump very quickly to any point in the solution without having to go through all of the intervening steps, which is how we are able to show progress on the towers of hanoi with 64 discs since the big bang in The Tower of Brahma and the End of the Universe.
5. Devise recursive algorithms for the Towers of Hanoi with each of the 5 Restrictions on Motion. Surprisingly, you should find that the 5 algorithms all fit the following template: ## Algorithm Analysis Activities

The activities in this section require knowledge of algorithm analysis techniques, particularly the construction and solution of recurrence relations.

1. Following up on Looking and Thinking Activity 2, show that the standard algorithm for the Towers of Hanoi puzzle with $n$$n$ discs uses ${2}^{n}-1$$2^n-1$ moves. Hint: Devise a recurrence relation for the number of moves, then solve it either by induction or by repeated substitution.
2. Show that any solution to the Towers of Hanoi puzzle with $n$$n$ discs must make at least ${2}^{n}-1$$2^n-1$ moves. This shows that autosolve uses the minimal number of moves.

The next set of activities are about the analysis of the number of moves made by the Towers of Hanoi algorithms with Restrictions on Motion. They follow up on Looking and Thinking Activity 6 and Algorithm Design Activity 5, which should be attempted first.

1. Let ${T}_{0}\left(n\right)$$T_0(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must be through peg 0. Show that ${T}_{0}\left(1\right)=1$$T_0(1)=1$ and for all $n\ge 2$$n \geq 2$, ${T}_{0}\left(n\right)=3{T}_{0}\left(n-1\right)+1.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{0}\left(n\right)=\left({3}^{n}-1\right)/2$$T_0(n) = (3^n - 1)/2$.
2. Let ${T}_{1}\left(n\right)$$T_1(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must be through peg 1. Show that ${T}_{1}\left(1\right)=2$$T_1(1)=2$ and for all $n\ge 2$$n \geq 2$, ${T}_{1}\left(n\right)=3{T}_{1}\left(n-1\right)+2.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{1}\left(n\right)={3}^{n}-1$$T_1(n) = 3^n - 1$.
3. Let ${T}_{2}\left(n\right)$$T_2(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must be through peg 2. Show that ${T}_{2}\left(1\right)=1$$T_2(1)=1$ and for all $n\ge 2$$n \geq 2$, ${T}_{2}\left(n\right)=3{T}_{2}\left(n-1\right)+1.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{2}\left(n\right)=\left({3}^{n}-1\right)/2$$T_2(n) = (3^n - 1)/2$.
4. Let ${T}_{R}\left(n\right)$$T_R(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must rotate to the right. Show that ${T}_{R}\left(1\right)=2$$T_R(1)=2$ and for all $n\ge 2$$n \geq 2$, ${T}_{R}\left(n\right)\le 3{T}_{R}\left(n-1\right)+2.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{R}\left(n\right)\le {3}^{n}-1$$T_R(n) \leq 3^n - 1$.
5. Let ${T}_{L}\left(n\right)$$T_L(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must rotate to the left. Show that ${T}_{L}\left(1\right)=1$$T_L(1)=1$ and for all $n\ge 2$$n \geq 2$, ${T}_{L}\left(n\right)\le 3{T}_{L}\left(n-1\right)+1.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{L}\left(n\right)\le \left({3}^{n}-1\right)/2$$T_L(n) \leq (3^n - 1)/2$.

The following two activities are advanced, perhaps at the difficulty level of a graduate student in computer science who is specializing in the mathematics of computing.

1. Let ${T}_{R}\left(n\right)$$T_R(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must rotate right. Show that ${T}_{R}\left(1\right)=2$$T_R(1)=2$, ${T}_{R}\left(2\right)=7$$T_R(2)=7$ and for all $n\ge 3$$n \geq 3$, ${T}_{R}\left(n\right)=2\left({T}_{R}\left(n-1\right)+{T}_{R}\left(n-2\right)\right)+3.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{R}\left(n\right)=\frac{\left(1-\sqrt{3}{\right)}^{n+1}+\left(3\sqrt{3}+5\right)\left(1+\sqrt{3}{\right)}^{n}}{2\sqrt{3}\left(1+\sqrt{3}\right)}-1.$
2. Let ${T}_{L}\left(n\right)$$T_L(n)$ be the number of moves used by the Towers of Hanoi algorithm with $n$$n$ discs when all moves must rotate left. Show that ${T}_{L}\left(1\right)=1$$T_L(1)=1$, ${T}_{L}\left(2\right)=5$$T_L(2)=5$ and for all $n\ge 3$$n \geq 3$, ${T}_{L}\left(n\right)=2\left({T}_{L}\left(n-1\right)+{T}_{L}\left(n-2\right)\right)+3.$ Prove that for all $n\ge 1$$n \geq 1$, ${T}_{L}\left(n\right)=\frac{\left(1-\sqrt{3}{\right)}^{n}+\left(\sqrt{3}+2\right)\left(1+\sqrt{3}{\right)}^{n}}{\sqrt{3}\left(1+\sqrt{3}\right)}-1.$

## Theory of Algorithms Activities

The activities in this section require knowledge of techniques used in the theory of algorithms, particularly mathematical induction. 1. Following up on Acting Out Activity 1 and Acting Out Activity 2, prove that the standard algorithm for the Towers of Hanoi is correct, that is, it moves $n$$n$ discs from peg $i$$i$ to peg $j$$j$ without placing a disc on a smaller one.
2. Following up on Looking and Thinking Activity 4, prove by induction on the number of discs that when the discs are colored alternately using two colors, the standard algorithm never places two discs of the same color on top of each other.
3. Following up on Looking and Thinking Activity 3, Prove the following by induction on the number of discs, $n$$n$. If $n$$n$ is even, the solution alternates between moving the smallest disc one peg to the right (wrapping around to the leftmost peg) and making the only other legal move. If $n$$n$ is odd, the solution alternates between moving the smallest disc one peg to the left (wrapping around to the rightmost peg) and making the only other legal move. Verify this claim experimentally by observing the animation carefully. It might help to set the animation speed set on Slow. This gives you a nonrecursive algorithm for the Towers of Hanoi that doesn't use a stack.
4. Following up on Looking and Thinking Activity 3, show that in the standard algorithm for the Towers of Hanoi puzzle with $n$$n$ discs, the ${i}^{th}$$i^{th}$ smallest disc makes ${2}^{n-i-1}$$2^{n-i-1}$ moves for all $0\le i\le n$$0 \leq i \leq n$.
5. Following up on Looking and Thinking Activity 5, modify the technique from Theory of Algorithms Activity 3 for each of the 5 Restrictions on Motion.

## Programming Activities

The activities in this section require programming skills, a computer, and a compiler. However, they do require at least the solutions to the more theoretically oriented activities above. Students can probably find code online, but they will learn more if they program it themselves.

1. Write programs that implement the algorithms from Algorithm Design Activities 1, 2, 3, 5, and 6. The input to your program should be the number of discs $n$$n$, and the output should be the moves that solve it, one per line, as follows:
0->1
0->2
1->2
etc.
where, for example, 0->1 means a move of a single disc from peg 0 to peg 1 when the pegs are numbered 0, 1, and 2 from left to right.
2. Write a program that implements the algorithm from Algorithm Design Activities 4. The input should also include a file containing the initial configuration of the puzzle. The design of this file format is left up to you.
3. Write programs that implement the algorithms from Algorithm Design Activities 5.
4. Modify and use your programs from Programming Activities 3 to extend the table from Looking and Thinking Activities 6 to $n=9,10$$n=9, 10$ and (if you have the patience) beyond. Do your results match the expectations of Algorithm Analysis Activities 3-9?

## Experimental Algorithmics Activities

The activities in this section require solid theoretical, programming, and analytic skills. They depend on code from the Programming Activities.

1. Which algorithm is faster in practice, the recursive one or the nonrecursive one? Begin with the programs for these algorithms that you wrote in the Programming Activities. disable all of the output code, and add code to measure the amount of cpu time used when solving the puzzle. this code will vary from one programming language to another, and from one operating system to another. the amount of cpu time used will typically be very, very, very small, so you will probably need to solve the puzzle from scratch somewhere from 1,000 to 10,000 times to get cpu times that register at all. Have your program measure this for $10\le n\le 20$$10 \leq n \leq 20$ discs and output the resulting times. Compare the recursive algorithm from the Acting Out Activities to the nonrecursive one that uses a stack from Algorithm Design Activity 1. Plot the ratio of the nonrecursive time over the recursive time against the value of $n$$n$. Your graph should look similar to the one shown here, but the exact numbers will vary with programming language, compiler, operating system, computing hardware, and your competence as an algorithm designer and a programmer. Which is faster? Why do you think this is so? Explain the shape of your graph.
2. Repeat the above with the nonrecursive algorithm from Algorithm Design Activities 3. Which is faster? Why do you think this is so? Explain the shape of your graph. Created March 23, 2016 by Ian Parberry. Written in HTML 4.01 and CSS 3. Last updated March 8, 2016.