Presentation on theme: "AE1APS Algorithmic Problem Solving John Drake"— Presentation transcript:
1AE1APS Algorithmic Problem Solving John Drake Tower of HanoiAE1APS Algorithmic Problem SolvingJohn Drake
2Outline of the course Invariants – Chapter 2 River Crossing – Chapter 3Logic Puzzles – Chapter 5Matchstick Games - Chapter 4Sum Games – Chapter 4Induction – Chapter 6Tower of Hanoi – Chapter 8
3A recap on induction Breaking larger problems down into sub- problems Using small instances of a problem to help us solve larger instancesFind a trivial base case where size = 0Show for an arbitrary number n how to solve n + 1 (induction step)
4Tower of Hanoi - Introduction This problem is discussed in many maths texts, and is used in computer science and AI as an illustration of recursion and problem solvingYou probably know the problem, but we will look at it slightly differentlyInduction gives a systematic way of finding a solutionHowever this solution is undesirableA better solution is obtained by observing the invariants of the inductive solution
5Begin with the solution We will start at the endWe begin with the solution (as you probably already know it) and you can see where we are goingAnother reason is to understand why a correct solution has been found, if no information about the solution method is provided
6 Problem specification 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 sizeThe Brahmin monks were given the task of moving the discs, one per day, from one pole to anotherHowever, no disc must be above a smaller disc, when on the same pole
8Triangle representation Almost every book (and webpage) I have seen draws the 3 poles in a lineWe can call the poles A, B, CHowever there is a simple symmetry which is being ignored here
9Triangle representation We draw them in different positionsA 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.
10Iterative solution (assumptions) There is a very easy solution to the Towers of Hanoi problemIt is easy to remember and executeWe assume the problem is to move the discs from one pole to the next in the clockwise directionThe 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.
11Iterative solution 1On every alternate day, the smallest disk is movedThe smallest disc should cycle around the poles.The direction of rotation depends on the number of discsIt the total number is odd, it cycles clockwise, otherwise if the total is even, it cycles anticlockwise
12Iterative solution 2On 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 discThe algorithm terminates when no further moves are possibleThat is, on a even numbered day when all the discs are on the same pole
29Why?Presenting the solution like this, provides us with no help in understanding how the solution was constructedHow would we give a formal mathematical verification of the correctness of the algorithmBy observing a number of invariants, we show how to derive the algorithm from the inductive solution.
30Inductive solutionSuppose 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 taskFor 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 BWe get stuck with this approach
31Stuck First move n top discs from A to B After doing this, all the discs are on BWe have no hypothesis about moving discs from this polei.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
34Still stuckAlternatively, we can move the smallest from pole A to pole CWe then move n discs from A to BOnce again we have exhausted all possibilities of using the inductive hypothesisBecause the n discs are on pole B and we have no hypothesis about moving discs from this pole.
35Too Specific 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
36Rotational symmetry We make a crucial decision Rather than name the poles A B C, we observe that the problem exhibits a rotational symmetryThis is obvious when the poles are arranged in a triangle, but obscured when the poles are placed in a line.
37An additional parameter One additional parameter needs to be introduced, the direction of movementWe only need to say if a particular disc needs to be moved in a clockwise or anti- clockwise directionThe 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)
38Inductive hypothesisThe 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 pointThat 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 itselfThis is a far more general definition
39Base and inductive case 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)
40Using the inductive hypothesis 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 dWe 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
50PsuedocodeThe following code summarises the inductive solution to the problemHn(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 directionsDirections are Boolean values, true representing clockwise, and false representing anti-clockwiseThe 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.
51Hn(d)Hn(d) means the sequence to move the n smallest discs in direction dTaking 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
52Psuedocode 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)
53PsuedocodeNotice 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)
54How to useThis inductive procedure gives us a way to generate the solution to the problem for any size nWe simply use the rules as rewrite rules, until all occurrences of H have been eliminatedi.e. we are left with just a sequence of actions(e.g. move disc 3 clockwise)
55H2(cw)Let us use cw and aw to mean clockwise and anti-clockwise, rather than true and false to improve readabilityCalculate H2(cw) i.e. how to move 2 discs, one pole in a clockwise directionEquation (1)H0(d) = Equation (2)Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d)
60H3(cw)? 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)