tower of hanoi puzzle jianying yu. i. introduction the puzzle: conditions: n disks and three pegs....

10
Tower of Hanoi Puzzle Tower of Hanoi Puzzle Jianying Yu Jianying Yu

Upload: ruby-nichols

Post on 20-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

Tower of Hanoi PuzzleTower of Hanoi Puzzle

Jianying YuJianying Yu

Page 2: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

I. IntroductionI. Introduction

The Puzzle:The Puzzle:

Conditions: n disks and three pegs.Conditions: n disks and three pegs.

Goal: Goal: Move disks from first peg 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.

Page 3: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

Recursive Solutions:Recursive Solutions:

To move n>1 disks from peg1 to peg3, first To move n>1 disks from peg1 to peg3, first move recursively n-1 disks from peg1 to move recursively n-1 disks from peg1 to peg2, then move the largest disk directly peg2, then move the largest disk directly from peg1 to peg3, and finally move n-1 from peg1 to peg3, and finally move n-1 disks from peg2 to peg3.disks from peg2 to peg3.

Page 4: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

Recursive Solution to the Tower of Hanoi PuzzleRecursive Solution to the Tower of Hanoi Puzzle

A B C

1st: move (n-1) to B 3rd: move (n-1) to C

2nd: move nth to C

Page 5: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

II. ComplexityII. ComplexityNumber of moves M(n) depends on n only:Number of moves M(n) depends on n only:

For n=1: M(1) = 1For n=1: M(1) = 1For n>1: M(n) = 2 M(n-1) + 1For n>1: M(n) = 2 M(n-1) + 1 = 2 [2M(n-2) +1] + 1= 2 [2M(n-2) +1] + 1 = 2= 22 2 M(n-2) + 2 + 1M(n-2) + 2 + 1 = 2= 222 [2M(n-3) + 1] + 2 + 1 [2M(n-3) + 1] + 2 + 1 = 2= 23 3 M(n-3) + 2M(n-3) + 222 + 2 + 1 + 2 + 1………………. . After i substitutions, we get:After i substitutions, we get:M(n) = 2M(n) = 2i i M (n-i) + 2M (n-i) + 2i-1i-1 + 2 + 2i-2 i-2 + … + 2 + 1 = 2+ … + 2 + 1 = 2i i M(n-i) + 2M(n-i) + 2ii - 1 - 1 Since the initial condition is specified for n=1, which isSince the initial condition is specified for n=1, which isachieved for i = n-1, we get: achieved for i = n-1, we get: M(n) = 2M(n) = 2n-1n-1 M(n-(n-1)) + 2 M(n-(n-1)) + 2n-1n-1 -1 = 2 -1 = 2n-1n-1 M(1) + 2 M(1) + 2n-1n-1 -1 = 2 -1 = 2nn - 1 - 1

Page 6: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

An Exponential Algorithm: M (n) = 2An Exponential Algorithm: M (n) = 2nn-1-1

nn M (n)M (n)

11 11

22 33

33 77

44 1515

55 3131

66 6363

77 127127

88 255255

99 511511

1010 10231023

Number of Disk vs. Number of Move

0

200

400

600

800

1000

1200

0 2 4 6 8 10 12

Number of Disk

Nu

mb

er o

f M

ove

Page 7: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

III. The Nature of RecursionIII. The Nature of Recursion

Memory Use in Recursive CallsMemory Use in Recursive Calls A recursive function calls itself. Using the recursive divide-and-A recursive function calls itself. Using the recursive divide-and-

conquer technique, we can solve a problem by dividing it into two or conquer technique, we can solve a problem by dividing it into two or more simpler problems and applying the same technique again to more simpler problems and applying the same technique again to these simpler tasks. Eventually, the sub-problems become simple these simpler tasks. Eventually, the sub-problems become simple enough to be solved directly and the recursive descent ends. The enough to be solved directly and the recursive descent ends. The solution to the original problem then is composed from the solutions solution to the original problem then is composed from the solutions to the simpler parts. A storage area that can grow dynamically, such to the simpler parts. A storage area that can grow dynamically, such as the run-time stack, is necessary to implement recursion, because as the run-time stack, is necessary to implement recursion, because multiple activation records for the same function must exist multiple activation records for the same function must exist simultaneously. If a recursive function calls itself five times, six stack simultaneously. If a recursive function calls itself five times, six stack frames will exist simultaneously for it, each holding the parameters frames will exist simultaneously for it, each holding the parameters for one of the active calls. Each time one of the calls returns, its 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 stack frames is discarded and control goes back to the prior invocation.invocation.

Page 8: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

Trace of Tower (A, C, B, 3)Trace of Tower (A, C, B, 3)

Page 9: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

IV. Java Code HighlightsIV. Java Code Highlights

The program extends Java Applet class, implements The program extends Java Applet class, implements ActionListenner and Runnable abstract classes.ActionListenner and Runnable abstract classes.

Used rectangle to represent disks and pegs.Used rectangle to represent disks and pegs.

Used two-demission structured array to hold (x, y) 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 Used two-demission structured array to hold colors for each disk on each peg.each disk on each peg.

Used thread for animation. Sleeping time between each Used thread for animation. Sleeping time between each move is one second.move is one second.

Used recursive calls to move disks.Used recursive calls to move disks.

Page 10: Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks

V. ConclusionsV. Conclusions

Because the intrinsic difficulty of the problem, although Because the intrinsic difficulty of the problem, although this is an exponential algorithm, which will run for an this is an exponential algorithm, which will run for an unimaginably long time even for moderate n value, this unimaginably long time even for moderate n value, this still is a most efficient algorithm possible for this problem.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 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 !!! If you want to move 256 disks, you will end this world !!!