Professional Documents
Culture Documents
GE8151 U1 15 Towers of Hanoi
GE8151 U1 15 Towers of Hanoi
GE8151 U1 15 Towers of Hanoi
Towers of Hanoi
The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower[1] and sometimes
pluralized) is a mathematical game or puzzle. It consists of three rods and a number of disks of
different sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in
ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following
simple rules:
Rule 2: Each move consists of taking the upper disk from one of the stacks and placing it on top
of another stack or on an empty rod.
Disk 1
Disk 2
Disk 3
Source Destination Aux
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Towers of
Hanoi Playing
with 1 Disks
Disk 1
Total number of
moves is 1
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Towers of
Hanoi Playing
with 2 Disks
Disk 1
Disk 2
Move 1
Disk 1 Move S to
A
Move 2
Disk 2 Move S to
D
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Towers of
Hanoi
Playing with
3 Disks
Disk 1
Disk 2
Disk 3
Source Destination Aux
Move 1
Disk 1 Move S
to D
Move 2
Disk 2 Move S
to A
Move 3
Disk 1 Move D
to A
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Towers of
Hanoi Playing
Move 4 with 3 Disks
Disk 3 Move S to
D
Move 5
Disk 1 Move A to
S
Move 7
Disk 1 Move S to
D
Total number of
moves is 7
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Algorithm
Move Disks
Move Disks
Step 1 Start Process
Step 2 Receive number of disks and store in n
Step 3 If n is equal to 1 then Move disk from source to destination and goto Step 16
Step 4 Else goto Step 5
Step 5 Assign disks as n-1
Step 6 Assign source as source
Step 7 Assign destination as aux
Step 8 Assign aux as destination
Step 9 Call Move Disks with disks, source, destination, aux
Step 10 Move disk n from source to destination
Step 11 Assign disks as n – 1
Step 12 Assign source as aux
Step 13 Assign destination as destination
Step 14 Assign aux as source
Step 15 Call Move Disks with disks, source, destination, aux
Step 16 Stop Process
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Flow Chart
start
read no of disks
Move Disks
stop
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
start
recive disks
disks = 1 False
?
True
move disks from source to destination
Move Disks
Move Disks
stop
Rajasekaran S AP/IT
Problem Solving and Python Programming GE8151
Pseudo Code
START
READ number of disks
CALL move_disks(no of disks, source, destination, aux)
STOP
START
PROCEDURE: move_disks(disks, source, destination, aux)
IF disks equal to 1
Move Disks from source to destination
ELSE
CALL move_disks(disks - 1, source, aux, destination)
Move Disks from source to destination
CALL move_disks(disks - 1, aux, destination, source)
END IF
END PROCEDURE
STOP
Rajasekaran S AP/IT