This paper looks at the tower of hanoi problem and how to generate the closed formula from the recursive formula using linear algebra and matrix operations.
Micro-Scholarship, What it is, How can it help me.pdf
Tower of hanoi problem
1. Generating the Closed Formula for the Tower of Hanoi
Problem Using Matrix Algebra
Tyler Murphy
September 30, 2014
1 Background
The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower, and sometimes
pluralized) is a mathematical game or puzzle. It consists of three rods, and a number of
disks of dierent sizes which can slide onto any rod. The puzzle starts with the disks in
a neat stack in ascending order of size on one rod, the smallest at the top, thus making a
conical shape.
Figure 1: Tower of Hanoi Game
The objective of the puzzle is to move the entire stack to another rod, obeying the
following simple rules:
Only one disk can be moved at a time.
Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a
stack.
No disk may be placed on top of a smaller disk.
We are interested in
2. nding the smallest number of moves it takes to move a tower
with n discs to another location. First, we generate a table of experimentation values
1
3. Number of Discs (n) Minimum Number of Moves (Mn)
1 1
2 3
3 7
4 15
5 31
6 63
We see that after this point, experimentation begins to take a long time, so we stop
here and try to devise a pattern.
2 Recurrence Form
We notice that each value for Mn is twice the value of Mn1 plus 1. So we need to know
the value for Mn1, which we can see would be dicult to generate for any large n. So
calculating the value for Mn becomes no small task when we use the recurrence formula to
4. nd it. We need a simpler way.
It turns out that if can design a matrix that models this recurrence relation, we will be
able to use this matrix to generate a closed formula to
5. nd Mn without having to know
Mn1.
3 Designing the Matrix
Recall that a matrix is simply another way to write a system of equations.
For example, the equations
2x + 3y = 4 (1)
and
x + y = 1: (2)
These can be written in matrix multiplication form as:
2 3
1 1
x
y
=
4
1
So we can write our recursive formula in this manner.
We will eventually end up with something of the form
Anb = c
Where A is our matrix, b is our initial value vector and c is our solution vector.
2
6. Because of the constant term in the recursion relation and because Mn+1 only depends
on Mn but not Mn1 (like the Fibonnaci sequence), we will use vectors of the form
Mn
1
We also know our matrix will be a 2x2 matrix because we only have two terms in our
recurrence formula. So:
A =
a11 a12
a21 a22
We want a matrix A such that
Mn+1
1
= A
Mn
1
The equations represented by this are:
Mn+1 = a11Mn + a12
and
1 = a21Mn + a22:
To make the
7. rst equation true, we use the recursion relation and choose a11 = 2; a12 =
1. The second equation becomes true if we choose a21 = 0 and a22 = 1. Thus our matrix
is
A =
2 1
0 1
So, using this matrix, we can
8. nd that
Mn+1
1
=
2 1
0 1
Mn
1
This makes sense when we put it back into equation form and obtain: Mn+1 = 2Mn+1
and 1 = 0Mn+1.
The problem now is that we still need an input vector that requires Mn in order to give
us Mn+1.
But consider that
Mn+1
1
= A
Mn
1
So for n = 1,
M2
1
= A
M1
1
3
9. M3
1
= A
A
M1
1
= A2
M1
1
M4
1
= A
A2
M1
1
= A3
M1
1
So we see the pattern that:
Mn+1
1
= An
M1
1
4 Quickly Computing An
Since computing An for any large n would be extremely cumberson and dicult, we
must
10. nd a better way to computer these large powers of A.
Linear Algebra teaches us that if a matrix can be diagonalized, then this process of
computing powers becomes very simple. When a matrix is diagonalized, it becomes of the
form A = SDS1 where S is the matrix composed of the eigenvectors of the matrix, and
D is the diagonal matrix composed of the eigenvalues () of A. That is, D = I, where I
is the Identity Matrix.
4.1 Finding the Eigenvalues of A
So
13. nding the values for
that satisfy the equation:
det(A I) = 0
First, we
14. nd (A I) =
2 1
0 1
Recall that the determinant of a 2x2 matrix A =
a b
c d
is ad bc
So , Det(A = I) = (2 )(1 ) (0)(1).
Setting this equal to zero and solving we
15. nd = 1; 2.
So we can plug these values into I to
16. nd D =
1 0
0 2
4.2 Finding the Eigenvectors of A
Now we need to
18. nd our matrix S.
This is done by solving the equation
(A I)x = 0
4
19. for each .
First, we choose = 1.
2 1 1
0 1 1
x1
x2
=
1 1
0 0
x1
x2
=
0
0
Converting this to equations, we gets that
x1 + x2 = 0 ) x1 = x2
and
0x1 + 0x2 = 0
Since the second equation tells us nothing, we use the
21. nd that the
basis vector x=1 =
1
1
.
Doing the same thing for = 2, we
22. nd that x=2 =
1
0
.
Now we put these two eigenvectors into Matrix S, remembering that the order we put
them into the matrix should match the order we put the eigenvalues into D. So,
S =
1 1
1 0
.
4.3 Computing S1
Recall that the inverse of a 2x2 matrix A =
a b
c d
= A1 =
1
Det(A)
d b
c a
So
S1 =
1
(1)(0) (1)(1)
0 1
1 1
= 1
0 1
1 1
=
0 1
1 1
4.4 Dealing with A = SDS1
So all put together, A = SDS1 =
1 1
1 0
1 0
0 2
0 1
1 1
It is a simple matter of matrix multiplication to verify the truth of this.
Now recall that
Mn+1
1
= An
M1
1
5
23. 5 Computing the Closed Formula
The reason we diagonalize matrices is because An = SDnS1 and Dn is easily computed.
So
Mn+1
1
=
1 1
1 0
1 0
0 2
n
0 1
1 1
M1
1
Now realize that
1 0
0 2
n
=
1n 0
0 2n
=
1 0
0 2n
So we have
Mn+1
1
=
1 1
1 0
1 0
0 2n
0 1
1 1
M1
1
Now we can do some matrix multiplication to simplify this.
We get
Mn+1
1
=
1 1
1 0
1 0
0 2n
0 1
1 1
M1
1
Mn+1
1
=
1 2n
0 2n
0 1
1 1
M1
1
Mn+1
1
=
2n 2n 1
2n 2n
M1
1
Recall now that M1 = 1.
So we have
Mn+1
1
=
2n 2n 1
2n 2n
1
1
Now we can convert this back into equational format and we
24. nd that
Mn+1 = 2n(1)+(2n 1)(1) = 2n +2n 1 = 2 2n 1 = 2n+1 1 (1)
This is ok, but we want a formula that gives is Mn. We can get this by using our
original recursive formula.
Mn+1 = 2Mn + 1 (2)
Substituting (2) into equation (1) we get:
2Mn + 1 = 2 2n 1
Now we simplify this.
2Mn + 1 = 2 2n 1
2Mn = 2 2n 2
Mn = 2n 1
So we
25. nally have our closed form.
The minimum number of moves for a tower of n discs is 2n 1.
6