Sunteți pe pagina 1din 20

Data Structure and Algorithm Analysis

Slide Set 4
Towers of Hanoi
13 CS- I & II
Engr. Maria Shaikh
maria.sheikh@faculty.muet.edu.pk

STACK
A Stack is a list of elements in which an element may
be inserted or deleted at one end which is known as
TOP of the stack.
Last-in first-out data structure (LIFO)
Supports the following operations
push(item) inserts an item at the end
Pop( ) deletes the end item
Peek( ) returns the last element
Engr. Maria Shaikh

STACK
Stack() creates a new stack that is empty. It needs
no parameters and returns an empty stack.isEmpty()
tests to see whether the stack is empty. It needs no
parameters and returns a boolean value.
size() returns the number of items on the stack. It
needs no parameters and returns an integer.
For example, if s is a stack that has been created and
starts out empty.
Engr. Maria Shaikh

Representation of Stack
EEE

TOP

DDD
CCC
BBB
AAA

Engr. Maria Shaikh

Stack Representation

Engr. Maria Shaikh

RECURSION
A programming technique in which a function calls itself.
One of the most effective techniques in programming.
Consider the numbers 1, 3, 6, 10, 15.
What is so peculiar about them?
The nth term in the series is obtained by adding n to the
previous number.
Recursion can be used to find the nth term

Engr. Maria Shaikh

TOWERS OF HANOI
Hanoi is the capital of Vietnam. It is also a logical puzzle game that can be solved very
effectively with recursion.
The setup of Hanoi is simple. We have three pegs. On each of these pegs is a series of
disks decreasing in size from the bottom of the peg to the top of the peg.
Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard
Lucas in 1883.
He was inspired by a legend that tells of a Hindu temple where the puzzle was
presented to young priests. At the beginning of time, the priests were given three
poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it.
Their assignment was to transfer all 64 disks from one of the three poles to another,
with two important constraints. They could only move one disk at a time, and they
could never place a larger disk on top of a smaller one. The priests worked very
efficiently, day and night, moving one disk every second. When they finished their
work, the legend said, the temple would crumble into dust and the world would vanish.
Engr. Maria Shaikh

TOWERS OF HANOI
The game starts by having few discs stacked in increasing order of size.
The number of discs can vary, but there are only three pegs.
The goal is to move all these disks to another peg following these rules:

-Only one disk can be moved at a time.

-Only the topmost disk of any given peg can be moved to another peg.

Engr. Maria Shaikh

The Towers of Hanoi

A larger disk cannot be placed on top a smaller disk.


Only one disk could be moved at a time.
A larger disk must never be stacked above a smaller one.
One and only one auxillary needle could be used for the intermediate
storage of disks.

Engr. Maria Shaikh

The Towers of Hanoi


Case 1

Figure 6-11

Engr. Maria Shaikh

The Towers of Hanoi

Move one disk to auxiliary needle.


Move one disk to destination needle.
Move one disk from auxiliary to destination needle.

Figure 6-11

Move two disks from source to auxiliary needle. (Step-3)


Move one disk from source to destination needle. (Step-4)
Move two disks from auxiliary to destination needle. (Step-7)

The Towers of Hanoi


1.
2.
3.

Move n-1 disks from source to auxiliary. General Case


Move one disk from source to destination. Base Case
Move n-1 disks form auxiliary to destination. General Case

Call Towers( n-1, source, auxiliary, destination)


Move one disk from source to destination
Call Towers(n-1, auxilary, destination, source).

Engr. Maria Shaikh

Tower of Hanoi
How to solve the 4 pegs

Engr. Maria Shaikh

Algorithm
TOWER (N, BEG, AUX, END)
This procedures gives a recursive solution to the Tower of Hanoi problem for N disks.
1.

If N = 1, then:
(a) Write: BEG
END
(b) Return
[End of If Structure]
2. [ Move N-1 disks from peg BEG to peg AUX]
Call TOWERS (N -1, BEG, END, AUX)
3. Write: BEG
END
4. [Move N -1 disks from peg AUX to peg END]
Call TOWER (N -1, AUX, BEG, END)
5. Return
Engr. Maria Shaikh

15

Tower of Hanoi
Solution
To get a better understanding for the general algorithm
used to solve the Tower of Hanoi, try to solve the
puzzle with a small amount of disks, 3 or 4, and once you
master that , you can solve the same puzzle with more
discs with the following algorithm.
No of steps can be find out by a formula
Where n are number of disks.
Engr. Maria Shaikh

2n 1

Tower of Hanoi
Recursive Solution for the Tower of Hanoi with algorithm
Lets call the three peg Src(Source), Aux(Auxiliary) and st(Destination).
1) Move the top N 1 disks from the Source to Auxiliary tower
2) Move the Nth disk from Source to Destination tower
3) Move the N 1 disks from Auxiliary tower to Destination tower. Transferring the top
N 1 disks from Source to Auxiliary tower can again be thought of as a fresh
problem and can be solved in the same manner. So once you master solving Tower
of Hanoi with three disks, you can solve it with any number of disks with the above
algorithm.

Engr. Maria Shaikh

Tower of Hanoi
This actually serves as the definition of the function Solve. The function is recursive in that it calls
itself repeatedly with decreasing values of N until a terminating condition (in our case N = 0) has been
met. To me the sheer simplicity of the solution is breathtaking. For N =3 it translates into
1. Move from Src to Dst
2. Move from Src to Aux
3. Move from Dst to Aux
4. Move from Src to Dst
5. Move from Aux to Src
6. Move from Aux to Dst
7. Move from Src to Dst
Of course "Move" means moving the top most disk.

Engr. Maria Shaikh

Tower of Hanoi
For N = 4 we get the following sequence
1. Move from Src to Aux
2. Move from Src to Dst
3. Move from Aux to Dst
4. Move from Src to Aux
5. Move from Dst to Src
6. Move from Dst to Aux
7. Move from Src to Aux
8. Move from Src to Dst
9. Move from Aux to Dst
10. Move from Aux to Src
11. Move from Dst to Src
12. Move from Aux to Dst
13. Move from Src to Aux

14. Move from Src to Dst


15. Move from Aux to Dst

Engr. Maria Shaikh

END OF SLIDE SET 4

Engr. Maria Shaikh

20

S-ar putea să vă placă și