Download presentation

Presentation is loading. Please wait.

Published byPhilip Lavender Modified over 3 years ago

1
CSC 205 Programming II Lecture 10 Towers of Hanoi

2
Towers of Hanoi – the problem A number of disks, which are of different sizes and have a hole in the middle Three poles: A: the source B: the destination C: the spare A disk can be placed only on top of disks larger than itself How to move the disks from the source pole to the destination? ABC

3
Towers of Hanoi – the strategy The problem can be solved recursively The base case: if there is only one disk, move it from the source pole to destination Recursive solution Ignore the bottom disk and move the upper n-1 disks to the spare pole C Move the bottom disk to B, the real destination Move the upper n-1 disks from C to B, using the same strategy

4
The Strategy at Work ABCABC ABCABC The initial state Done! Move the upper n-1 disks Move the bottom disk

5
The Solution – an overview Divide a complicated problem into three smaller ones One of the three can be solved immediately Sizes of the other two may still need further diminishing Four arguments are needed to maintain the local environment Current size of the problem The source, destination, and spare

6
The Solution – a Java expression The problem statement Solve towers(n, 'A', 'B', 'C') The solution Move the upper n-1 disks to the spare pole towers(n-1, 'A', 'C', 'B') Move the bottom disk to destination towers(1, 'A', 'B', 'C') Move the upper n-1 disks back to destination towers(n-1, 'C', 'B', 'A')

7
The Solution – the sample code void solveTower(int count, char source, char destination, char spare) { if (count == 1) { //move a disk directly from source to destination System.out.println(Move top disk from pole + source + to pole + destination); } else { solveTower(count-1, source, spare, destination); solveTower(1, source, destination, spare); solveTower(count-1, spare, destination, source); }

8
Box Trace – towers of Hanoi

9
Is It TOP or BOTTOM? Class exercise Modify the source code given in your text (on slide 7) to display the disk moved each time

10
Efficiency Two factors contributing to inefficiency The overhead associated with method calls The inherent inefficiency of some recursive algorithms (such as the one for the Fibonacci sequence) Tail recursion Only one recursive call It is the last action in the recursive method Not as efficient as its iterative counterpart, and can easily be converted to iterative

11
Class Exercise – From Recursion to Iterative What does the following method do? Convert the following recursive method to its equivalent iterative counterpart void displayOctal(int n) { if (n > 0) { if (n/8 > 0) displayOctal(n/8); System.out.print(n%8); } else System.out.println(); }

Similar presentations

OK

Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google