1 / 60

Tower of Hanoi

Tower of Hanoi. AE1APS Algorithmic Problem Solving John Drake. Outline of the course. 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. A recap on induction.

pekelo
Download Presentation

Tower 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. Tower of Hanoi AE1APS Algorithmic Problem Solving John Drake

  2. Outline of the course • 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

  3. A recap on induction • 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)

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

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

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

  7. Tower of Hanoi

  8. Triangle representation • 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

  9. Triangle representation • 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.

  10. 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 • 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.

  11. 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 if the total is even, it cycles anticlockwise

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

  13. Iterative solution – 4 discs • Day 0

  14. Iterative solution – 4 discs • Day 1

  15. Iterative solution – 4 discs • Day 2

  16. Iterative solution – 4 discs • Day 3

  17. Iterative solution – 4 discs • Day 4

  18. Iterative solution – 4 discs • Day 5

  19. Iterative solution – 4 discs • Day 6

  20. Iterative solution – 4 discs • Day 7

  21. Iterative solution – 4 discs • Day 8

  22. Iterative solution – 4 discs • Day 9

  23. Iterative solution – 4 discs • Day 10

  24. Iterative solution – 4 discs • Day 11

  25. Iterative solution – 4 discs • Day 12

  26. Iterative solution – 4 discs • Day 13

  27. Iterative solution – 4 discs • Day 14

  28. Iterative solution – 4 discs • Day 15

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

  30. Inductive solution • 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

  31. Stuck • 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

  32. Example with n = 3 B C A

  33. Example with n = 3 B C A

  34. Still stuck • 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.

  35. Too 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

  36. Rotational symmetry • 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.

  37. An additional parameter • 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)

  38. 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 arbitrarily over the poles, but no disc is on top of a disc smaller than itself • This is a far more general definition

  39. 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. • Assume the discs are numbered 1 upwards (with the smallest disc numbered 1)

  40. Using the inductive hypothesis • There is little choice in exploiting the inductive hypothesiswe 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+1thdisc 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

  41. Example with n = 3

  42. Example with n = 3

  43. Example with n = 3

  44. Example with n = 3

  45. For n+1 (i.e. 3+1 = 4) • We can use the previous solution to get a solution for n+1 using the inductive step

  46. Example with n = 4

  47. Example with n = 4

  48. Example with n = 4

  49. Example with n = 4

  50. Psuedocode • 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.

More Related