Download presentation
Presentation is loading. Please wait.
Published byRuby Nichols Modified over 9 years ago
1
Tower of Hanoi Puzzle Jianying Yu
2
I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks from first peg Goal: Move disks from first peg to third peg. to third peg. Limitations: Move one disk at a time. Limitations: Move one disk at a time. Forbidden larger disk on Forbidden larger disk on top of smaller ones. top of smaller ones.
3
Recursive Solutions: To move n>1 disks from peg1 to peg3, first move recursively n-1 disks from peg1 to peg2, then move the largest disk directly from peg1 to peg3, and finally move n-1 disks from peg2 to peg3. To move n>1 disks from peg1 to peg3, first move recursively n-1 disks from peg1 to peg2, then move the largest disk directly from peg1 to peg3, and finally move n-1 disks from peg2 to peg3.
4
Recursive Solution to the Tower of Hanoi Puzzle
5
II. Complexity Number of moves M(n) depends on n only: For n=1: M(1) = 1 For n>1: M(n) = 2 M(n-1) + 1 = 2 [2M(n-2) +1] + 1 = 2 [2M(n-2) +1] + 1 = 2 2 M(n-2) + 2 + 1 = 2 2 M(n-2) + 2 + 1 = 2 2 [2M(n-3) + 1] + 2 + 1 = 2 2 [2M(n-3) + 1] + 2 + 1 = 2 3 M(n-3) + 2 2 + 2 + 1 = 2 3 M(n-3) + 2 2 + 2 + 1………. After i substitutions, we get: M(n) = 2 i M (n-i) + 2 i-1 + 2 i-2 + … + 2 + 1 = 2 i M(n-i) + 2 i - 1 Since the initial condition is specified for n=1, which is achieved for i = n-1, we get: M(n) = 2 n-1 M(n-(n-1)) + 2 n-1 -1 = 2 n-1 M(1) + 2 n-1 -1 = 2 n - 1
6
An Exponential Algorithm: M (n) = 2 n -1 n M (n) 11 23 37 415 531 663 7127 8255 9511 101023
7
III. The Nature of Recursion Memory Use in Recursive Calls A recursive function calls itself. Using the recursive divide-and- conquer technique, we can solve a problem by dividing it into two or more simpler problems and applying the same technique again to these simpler tasks. Eventually, the sub-problems become simple enough to be solved directly and the recursive descent ends. The solution to the original problem then is composed from the solutions to the simpler parts. A storage area that can grow dynamically, such as the run-time stack, is necessary to implement recursion, because multiple activation records for the same function must exist simultaneously. If a recursive function calls itself five times, six stack frames will exist simultaneously for it, each holding the parameters for one of the active calls. Each time one of the calls returns, its stack frames is discarded and control goes back to the prior invocation. A recursive function calls itself. Using the recursive divide-and- conquer technique, we can solve a problem by dividing it into two or more simpler problems and applying the same technique again to these simpler tasks. Eventually, the sub-problems become simple enough to be solved directly and the recursive descent ends. The solution to the original problem then is composed from the solutions to the simpler parts. A storage area that can grow dynamically, such as the run-time stack, is necessary to implement recursion, because multiple activation records for the same function must exist simultaneously. If a recursive function calls itself five times, six stack frames will exist simultaneously for it, each holding the parameters for one of the active calls. Each time one of the calls returns, its stack frames is discarded and control goes back to the prior invocation.
8
Trace of Tower (A, C, B, 3)
9
IV. Java Code Highlights The program extends Java Applet class, implements ActionListenner and Runnable abstract classes. Used rectangle to represent disks and pegs. Used two-demission structured array to hold (x, y) values for each disk on each peg. values for each disk on each peg. Used two-demission structured array to hold colors for each disk on each peg. Used thread for animation. Sleeping time between each move is one second. Used recursive calls to move disks.
10
V. Conclusions Because the intrinsic difficulty of the problem, although this is an exponential algorithm, which will run for an unimaginably long time even for moderate n value, this still is a most efficient algorithm possible for this problem. It takes 17 min to move 10 disks at rate of 1 disk/sec. It takes 291 hours to move 20 disks at rate of 1 disk/sec. It takes 291 hours to move 20 disks at rate of 1 disk/sec. It takes 136 years to move 32 disks at rate of 1 disk/sec. It takes 136 years to move 32 disks at rate of 1 disk/sec. If you want to move 256 disks, you will end this world !!!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.