ae1aps algorithmic problem solving john drake. invariants – chapter 2 river crossing – chapter...
TRANSCRIPT
![Page 1: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/1.jpg)
Tower of Hanoi
AE1APS Algorithmic Problem SolvingJohn Drake
![Page 2: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/2.jpg)
Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games - Chapter 4
◦ Sum Games – Chapter 4 Induction – Chapter 6
◦ Tower of Hanoi – Chapter 8
Outline of the course
![Page 3: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/3.jpg)
Breaking larger problems down into sub-problems
Using small instances of a problem to help us solve larger instances
Find a trivial base case where size = 0
Show for an arbitrary number n how to solve n + 1 (induction step)
A recap on induction
![Page 4: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/4.jpg)
This problem is discussed in many maths texts, and is used in computer science and AI as an illustration of recursion and problem solving
You probably know the problem, but we will look at it slightly differently
Induction gives a systematic way of finding a solution
However this solution is undesirable A better solution is obtained by observing the
invariants of the inductive solution
Tower of Hanoi - Introduction
![Page 5: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/5.jpg)
We will start at the end We begin with the solution (as you probably
already know it) and you can see where we are going
Another reason is to understand why a correct solution has been found, if no information about the solution method is provided
Begin with the solution
![Page 6: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/6.jpg)
There is a temple in Bramah, where there are 3 giant poles fixed in the ground.
On the first pole, God placed 64 discs, each of different size in decreasing order of size
The Brahmin monks were given the task of moving the discs, one per day, from one pole to another
However, no disc must be above a smaller disc, when on the same pole
Problem specification
![Page 7: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/7.jpg)
Tower of Hanoi
![Page 8: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/8.jpg)
Almost every book (and webpage) I have seen draws the 3 poles in a line
We can call the poles A, B, C
However there is a simple symmetry which is being ignored here
Triangle representation
![Page 9: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/9.jpg)
We draw them in different positions A better way is to draw and equilateral
triangle, and the symmetry is obvious. Now the moves of the discs can easily be
described as clockwise or anti-clockwise.
Triangle representation
![Page 10: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/10.jpg)
There is a very easy solution to the Towers of Hanoi problem
It is easy to remember and execute We assume the problem is to move the
discs from one pole to the next in the clockwise direction
The days are numbered from 0, 1, 2… On day 0, the discs are placed in their initial
position, and the monks begin moving the discs on day 1.
Iterative solution (assumptions)
![Page 11: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/11.jpg)
On every alternate day, the smallest disk is moved
The smallest disc should cycle around the poles.
The direction of rotation depends on the number of discs
It the total number is odd, it cycles clockwise, otherwise if the total is even, it cycles anticlockwise
Iterative solution 1
![Page 12: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/12.jpg)
On every other day, another disc is moved (other than the smallest disc)
As no disc can be on a smaller disc, there is only one possible move for the disc
The algorithm terminates when no further moves are possible
That is, on a even numbered day when all the discs are on the same pole
Iterative solution 2
![Page 13: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/13.jpg)
Day 0
Iterative solution – 4 discs
![Page 14: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/14.jpg)
Day 1
Iterative solution – 4 discs
![Page 15: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/15.jpg)
Day 2
Iterative solution – 4 discs
![Page 16: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/16.jpg)
Day 3
Iterative solution – 4 discs
![Page 17: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/17.jpg)
Day 4
Iterative solution – 4 discs
![Page 18: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/18.jpg)
Day 5
Iterative solution – 4 discs
![Page 19: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/19.jpg)
Day 6
Iterative solution – 4 discs
![Page 20: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/20.jpg)
Day 7
Iterative solution – 4 discs
![Page 21: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/21.jpg)
Day 8
Iterative solution – 4 discs
![Page 22: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/22.jpg)
Day 9
Iterative solution – 4 discs
![Page 23: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/23.jpg)
Day 10
Iterative solution – 4 discs
![Page 24: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/24.jpg)
Day 11
Iterative solution – 4 discs
![Page 25: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/25.jpg)
Day 12
Iterative solution – 4 discs
![Page 26: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/26.jpg)
Day 13
Iterative solution – 4 discs
![Page 27: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/27.jpg)
Day 14
Iterative solution – 4 discs
![Page 28: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/28.jpg)
Day 15
Iterative solution – 4 discs
![Page 29: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/29.jpg)
Presenting the solution like this, provides us with no help in understanding how the solution was constructed
How would we give a formal mathematical verification of the correctness of the algorithm
By observing a number of invariants, we show how to derive the algorithm from the inductive solution.
Why?
![Page 30: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/30.jpg)
Suppose the task is to move M discs from one pole to another specific pole.
Base case. When there are 0 discs, no steps are needed to complete the task
For the inductive step, we assume we can move n discs from A to B, and the problem is to show how to move n+1 discs from A to B
We get stuck with this approach
Inductive solution
![Page 31: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/31.jpg)
First move n top discs from A to B
After doing this, all the discs are on B
We have no hypothesis about moving discs from this pole
i.e. The information that we can move n discs from A to B does not help us in trying to move n+1 from A to B
Stuck
![Page 32: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/32.jpg)
Example with n = 3
A B C
![Page 33: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/33.jpg)
Example with n = 3
A B C
![Page 34: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/34.jpg)
Alternatively, we can move the smallest from pole A to pole C
We then move n discs from A to B
Once again we have exhausted all possibilities of using the inductive hypothesis
Because the n discs are on pole B and we have no hypothesis about moving discs from this pole.
Still stuck
![Page 35: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/35.jpg)
We have been too specific about the inductive hypothesis
The way out of this is to introduce some parameters which model the start and final positions of the discs
Too Specific
![Page 36: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/36.jpg)
We make a crucial decision
Rather than name the poles A B C, we observe that the problem exhibits a rotational symmetry
This is obvious when the poles are arranged in a triangle, but obscured when the poles are placed in a line.
Rotational symmetry
![Page 37: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/37.jpg)
One additional parameter needs to be introduced, the direction of movement
We only need to say if a particular disc needs to be moved in a clockwise or anti- clockwise direction
The generalisation of the problem becomes, how to move n discs from one pole to the next in the direction d◦ (where d is either clockwise or anti-clockwise)
An additional parameter
![Page 38: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/38.jpg)
The inductive hypothesis we use is that it is possible to move the n smallest discs from one pole to another in direction d beginning from any valid starting point
That is, a starting position in which the discs are distributed arbitrarily over the poles, but no disc is on top of a disc smaller than itself
This is a far more general definition
Inductive hypothesis
![Page 39: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/39.jpg)
For n = 0, the sequence of moves is empty. In the case of n+1 discs, we assume we
have a method of moving the n smallest discs from one pole to one of the other two poles.
We must show how to move n+1 discs from one pole to another pole in direction d, where d is either clockwise or anticlockwise.
Assume the discs are numbered 1 upwards (with the smallest disc numbered 1)
Base and inductive case
![Page 40: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/40.jpg)
There is little choice in exploiting the inductive hypothesis we can begin by moving the n smallest discs in the direction d, or direction ¬d.
The solution is to move the n discs in direction ¬d, then the n+1th disc can be moved in direction d
We can then use the inductive hypothesis again to move the n smallest discs in the direction ¬d.
This places the n discs above the n+1th disc, and all of the n+1 smallest discs have now been moved from their original position to a new pole in direction d
Using the inductive hypothesis
![Page 41: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/41.jpg)
Example with n = 3
![Page 42: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/42.jpg)
Example with n = 3
![Page 43: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/43.jpg)
Example with n = 3
![Page 44: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/44.jpg)
Example with n = 3
![Page 45: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/45.jpg)
We can use the previous solution to get a solution for n+1 using the inductive step
For n+1 (i.e. 3+1 = 4)
![Page 46: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/46.jpg)
Example with n = 4
![Page 47: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/47.jpg)
Example with n = 4
![Page 48: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/48.jpg)
Example with n = 4
![Page 49: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/49.jpg)
Example with n = 4
![Page 50: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/50.jpg)
The following code summarises the inductive solution to the problem
Hn(d) is the sequence of pairs <k,d’> where n in the number of discs, k is a disc number and d and d’ are directions
Directions are Boolean values, true representing clockwise, and false representing anti-clockwise
The pair <k, d’> means, move disc number k from its current position in the direction d’
The pair is therefore an action. We use the ; to separate movements.
Psuedocode
![Page 51: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/51.jpg)
Hn(d) means the sequence to move the n smallest discs in direction d
Taking the pairs in order from left to right, the complete sequence Hn(d) prescribes how to move the n smallest discs, one by one, from one pole to another in direction d
Hn(d)
![Page 52: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/52.jpg)
Hn(d)
H0(d) = []
– base case (i.e. 0 moves needed)
Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d)
– induction step (move n discs in a given direction, move the n+1th disc in the opposite direction then move n discs again in the same direction)
Psuedocode
![Page 53: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/53.jpg)
Notice that the procedure name, H, occurs on both sides of the equation for Hn+1(d)◦ Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d)
For this reason we call this a recursive solution (i.e. the procedure calls itself, think of Java)
Psuedocode
![Page 54: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/54.jpg)
This inductive procedure gives us a way to generate the solution to the problem for any size n
We simply use the rules as rewrite rules, until all occurrences of H have been eliminated◦ i.e. we are left with just a sequence of actions
(e.g. move disc 3 clockwise)
How to use
![Page 55: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/55.jpg)
Let us use cw and aw to mean clockwise and anti-clockwise, rather than true and false to improve readability
Calculate H2(cw) i.e. how to move 2 discs, one pole in a clockwise direction
Equation (1)◦ H0(d) = []
Equation (2)◦ Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d)
H2(cw)
![Page 56: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/56.jpg)
<1,aw>
Example with n = 2
![Page 57: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/57.jpg)
<2,cw>
Example with n = 2
![Page 58: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/58.jpg)
<1,aw>
Example with n = 2
![Page 59: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/59.jpg)
Example with n = 2
![Page 60: AE1APS Algorithmic Problem Solving John Drake. Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games -](https://reader035.vdocuments.net/reader035/viewer/2022062417/551bebf0550346b9588b6353/html5/thumbnails/60.jpg)
Exercise – Can you do H3(aw)?
Can you see a pattern in the number of moves required for n discs? i.e. H0, H1, H2, H3…
Show this as a formula in terms of n?
If this is easy, try exercise 8.3 a) (without cheating and looking at the answer first)
H3(cw)?