1 / 33

Towers of Hanoi

Towers of Hanoi. Introduction. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem solving. You probably know the problem, but we will look at it slightly differently.

nonnie
Download Presentation

Towers of Hanoi

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Towers of Hanoi

  2. Introduction • Introduction • This problem is discussed in many maths texts, • And in computer science an 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.

  3. Begin with the solution • Begin with the solution • 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.

  4.  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 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.

  5. Triangle representation. • Almost every book (and webpage) I have see 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. • There is no difference between B and C (assuming all the discs start on pole A) • But 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.

  6. Iterative solution (assumptions) • 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. • We assume 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.

  7. Iterative solution 1 • 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, anticlockwise.

  8. Iterative solution 2 • 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. • Try executing this algorithm yourself on 4 discs.

  9. WHY? • 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.

  10. Inductive solution • Obviously, induction on the number of discs. • Let us call the poles A B C • 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.

  11. Stuck 1 • 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. • (illustrate this on the board)

  12. Stuck 2 • 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.

  13. Too Specific • We have been too specific about the inductive hypothesis. • The way out of this to recovery, is to introduce some parameters, • Which model the start and final positions of the discs.

  14. Rotational Symmetry • We make a crucial decision. • Rather than name the poles A B C, • We observer 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.

  15. Additional Parameter • One additional parameter needs to be introduced. • This is the direction of movement. • i.e. we only need to say if a particular disc needs to be moved in a clockwise or counter clockwise direction. • Also, the generalization 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 anticlockwise)

  16. Inductive hypothesis • 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 arbitrarly over the poles, • But no disc is on top of a disc smaller than itself).

  17. Base 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. • Lets assume the discs are numbered 1 upwards • (the smallest disc being numbered 1)

  18. Using the inductive hypothesis • There is little choice in exploiting the inductive hypothesis. • We begin by moving the n smallest discs in the direction d, or direction (not)d. • The solution is to move the n discs in direction (not)d, • Then the n+1 th disc can be moved in direction d. • Finally we can use the inductive hypothesis again for a second time, • To move the n smallest discs in the direction (not) d. • This places the n discs above the n+1 th disc, • And all of the n+1 smallest discs have now been moved from there original position to a new pole in direction d. (draw a picture)

  19. Code • 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 counter 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.

  20. Hn.d • Hn.dmeans 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 • Write out the rules

  21. Hn.d • H0.d = [] • Hn+1.d = Hn.(not)d ; [<n+1, d>] ; Hn.(not)d • Notice that the procedure name, H, occurs on both sides of the equation for Hn+1.d • For this reason we call this a recursive solution (i.e. the procedure calls itself, think of Java).

  22. How to use • This inductive procedure gives us a way to generate the solution to the problem, • for any 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)

  23. H2,cw • Let us use cw and aw to mean clockwise and anticlockwise, • Rather than true false, to improve readability. • Show working on board p112 • Exercise – you do H3.aw • Can you see a pattern in the number of moves required.

  24. Recursion vs Iteration • The recursive solution is not the solution the monks were using. • There solution was iteratively (repeatedly) executing a simple procedure depenent only on the current state. • Conversely, The implementation of the inductive solution involves maintaining a stack of the sequence of moves yet to be executed. • In java this can lead to “out of memory” runtime errors. • There are interesting memory and speed issues here, • Which you can address in a future course on Complexity.

  25. The iterative solution • Remember that the iterative solution had two main elements. • First, that the smallest disc cycles around the poles • (i.e. keeps going around in one direction) • Second, that the disc to be moved alternates between the smallest disc and some other disc. • We now show how these two properties can be derived from the inductive solution.

  26. Cyclic moves • In this section we show that the smallest disc cycles around the poles. • In fact we can show that all discs cycle around the poles !!! • And we can show which direction it is

  27. The key - invariant • For all pairs <k, d’> in the sequence Hn+1.d, • The Boolean value even.k=d’ is invariant. • Applying the formula, Hn+1.d, • The parameter “n+1” is replaced by “n” • And “d” is replaced by “not d” • (n:=n-1; and d:= not d; ) • Since even.(n+1) = not.even.n • The value of even(n+1) = d remains constant.

  28. Initial conditions • Initial conditions • Even.k = d’ is true or false, • And this depends on the initial values of n and d. • Suppose these are N and D, then • Even.k = d = even.n = D

  29. In words • In words • If we require to move an even number of discs in a clockwise direction, • All even numbered discs should cycle in a clockwise direction, • And all odd numbers discs cycle in an anticlockwise direction. • The converse is true. • In particular, the smallest disc (number 1), • Cycles in a direction opposite to D, • If N is even, • And in the same direction as D if N is odd.

  30. Alternate discs • The second fact was that the disc to be moved alternates between • The smallest disc and some other disc. • This is not difficult to see this. • Two consecutive moves of the smallest disc can be done in one move. • Two consecutive moves of any other disc do not change the state of the puzzle. • We want to give a formal proof that the sequence Hn.d has this property.

  31. Alternating • Let us call a sequence of numbers alternating if it has two properties. • First, consecutive elements alternate between 1 and a value greater than one. • Second, when the sequence is not empty, it begins and ends with the value 1. • We write alt.ks if the sequence ks has these properties.

  32. Formally • discn.k is the sequence of discs moved on successive days. • Obtained by taking the first component of pairs in Hn.d, • And ignoring the second directional component. • By definition of H we get, • disc0.d = [] • discn+1.d = discn.not.d ; [n+1] ; discn.not.d

More Related