Download presentation
Presentation is loading. Please wait.
Published byLeona Holt Modified over 8 years ago
1
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi
2
Module 9CMPF144 Fundamentals of Computing TheorySlide 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
3
Module 9CMPF144 Fundamentals of Computing TheorySlide 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.
4
Module 9CMPF144 Fundamentals of Computing TheorySlide 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.
5
Module 9CMPF144 Fundamentals of Computing TheorySlide 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
6
Module 9CMPF144 Fundamentals of Computing TheorySlide 6 The Flow 1 2 3
7
Module 9CMPF144 Fundamentals of Computing TheorySlide 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)
8
Module 9CMPF144 Fundamentals of Computing TheorySlide 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)
9
Module 9CMPF144 Fundamentals of Computing TheorySlide 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
10
Module 9CMPF144 Fundamentals of Computing TheorySlide 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.
11
Module 9CMPF144 Fundamentals of Computing TheorySlide 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
12
Module 9CMPF144 Fundamentals of Computing TheorySlide 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.
13
Module 9CMPF144 Fundamentals of Computing TheorySlide 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
14
Module 9CMPF144 Fundamentals of Computing TheorySlide 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.
15
Module 9CMPF144 Fundamentals of Computing TheorySlide 15 Analysis In spite of the simplicity of the algorithms, the shortest way to solve the problem with n discs takes 2 n -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 2 n -1 moves.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.