1 / 15

CMPF144 FUNDAMENTALS OF COMPUTING THEORY

CMPF144 FUNDAMENTALS OF COMPUTING THEORY. Module 9: The Tower of Hanoi. Objectives. Learning Objectives: Present one classic example of problem that apply recursive algorithm to solve Understand the method to solve a problem using recursive algorithm. Introduction. The Tower of Hanoi

fay
Download Presentation

CMPF144 FUNDAMENTALS OF COMPUTING THEORY

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. CMPF144FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi

  2. Objectives • Learning Objectives: • Present one classic example of problem that apply recursive algorithm to solve • Understand the method to solve a problem using recursive algorithm CMPF144 Fundamentals of Computing Theory

  3. Introduction • The Tower of Hanoi • The Tower of Hanoi (also called Towers of Hanoi) is a mathematical game or puzzle. • It consists of three pegs, and a number of discs of different sizes which can slot onto any peg. CMPF144 Fundamentals of Computing Theory

  4. Application • The Tower of Hanoi is frequently used in psychological research on problem solving . There also exist variants of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions. CMPF144 Fundamentals of Computing Theory

  5. Introduction (cont.) • The puzzle starts with the discs neatly stacked in order of size on one peg, smallest at the top, thus making a conical shape CMPF144 Fundamentals of Computing Theory

  6. 1 2 3 The Flow CMPF144 Fundamentals of Computing Theory

  7. Tower of Hanoi • The object of the game is to move the entire stack to another peg, obeying the following rules: • only one disc may be moved at a time • a disc can only be placed onto a larger disc (it doesn't have to be the adjacent size, though: the smallest disc may sit directly on the largest disc) CMPF144 Fundamentals of Computing Theory

  8. How to Solve • Initialization • ·Label the pegs A, B, C • ·Let n be the total number of discs • ·Number the discs from 0 (smallest, topmost) to n (largest, bottommost) CMPF144 Fundamentals of Computing Theory

  9. Recursive Algorithm • To move n discs from peg A to peg C: 1.Move n-1 discs from A to B. This leaves disc #n alone on peg A 2.Move disc #n from A to C 3. Move n-1 discs from B to C so that they sit on disc #n CMPF144 Fundamentals of Computing Theory

  10. Recursive Algorithm (cont.) • The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n-1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1. This step, moving a single disc from peg A to peg B is trivial. CMPF144 Fundamentals of Computing Theory

  11. Explanation of the Algorithm A more human-readable version of the same algorithm follows: • 1.move disc 1 to peg B • 2.move disc 2 to peg C • 3.move disc 1 from B to C, so it sits on 2 You now have 2 discs stacked correctly on peg C, peg B is empty again • 1.move disc 3 to peg B • 2.repeat the first 3 steps above to move 1 & 2 to sit on top of 3 CMPF144 Fundamentals of Computing Theory

  12. Explanation • Each time you re-build the tower from disc i up to 1, move disc i+1 from peg A, the starting stack, and move the working tower again. CMPF144 Fundamentals of Computing Theory

  13. Pseudocode • The pseudocode of the function for Tower of Hanoi that will be called at the top level is as the following : FUNCTION MoveTower(disk, source, dest, spare): IF disk == 0, THEN: move disk from source to dest ELSE: MoveTower(disk - 1, source, spare, dest) // Step 1 above Move disk from source to dest // Step 2 above MoveTower(disk - 1, spare, dest, source) // Step 3 above END IF disk : disc’s number source: the source peg dest: the destination peg spare: the spare peg CMPF144 Fundamentals of Computing Theory

  14. Note that the pseudocode adds a base case: When disk is 0, the smallest disc. In this case we don't need to worry about smaller disks, so we can just move the disc directly. In the other cases, we follow the three-step recursive procedure we already describe earlier. CMPF144 Fundamentals of Computing Theory

  15. Analysis • In spite of the simplicity of the algorithms, the shortest way to solve the problem with n discs takes 2n-1 moves. It is not known in general how many moves are required to solve the puzzle when there are more than 3 pegs, although it should take less or equal steps than 2n-1 moves. CMPF144 Fundamentals of Computing Theory

More Related