The Tower of Hanoi is a mathematical game. 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 rules:
• Only one disk may be moved at a time.
• Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
• No disk may be placed on top of a smaller
2. RMD SINHGAD COLLEGE OF COMPUTER
STUDIES,WARJE ,PUNE-58
Guide:
Balachandra Doddi
Submitted By:
Purva Nahar(2176)
Submitted To:
Department Of Computer Studies ,
RMD Sinhgad College of Computer
Studies
3. Historical Note
• The Tower of Hanoi was invented by the French mathematician Edouard Lucas and
sold as a toy in 1883. It originally bore the name of ”Prof.Claus” of the college of “Li-
Sou-Stain”, but these were soon discovered to be anagrams for “Prof.Lucas” of the
college of “Saint Loius”, the university where he worked in Paris.
• There is a history about an Indian temple in Kashi Vishwanath which contains a large
room with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests,
acting out the command of an ancient prophecy, have been moving these disks, in
accordance with the immutable rules of the Brahma, since that time. According to
the legend, when the last move of the puzzle will be completed, the world will end.
4. Objective
• The aim of the tower of Hanoi problem is to move the initial n different sized
disks from Tower A to Tower C using a temporary Tower B. The rule is that no
larger disk is to be placed above the smaller disk in any of the needle while
moving or at any time, and only the top of the disk is to be moved at a time
from any Tower to any Tower.
5. Rules of Puzzle
• The Tower of Hanoi 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 rules:
• 1) Only one disk must be moved at a time.
• 2) Each move consists of taking the upper disk from one of the rods and sliding it
onto another rod, on top of the other disks that may already be present on that rod.
• 3) No disk may be placed on top of a smaller disk.
6. Algorithm
Recursive Solution for the Tower of Hanoi with algorithm
Let’s 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.
7. Time Complexity of Algorithm
• Let the time required for n disks is T(n) .
• There are 2 recursive call for n-1 disks and one constant time operation to move a disk
from ‘from’ peg to ‘to’ peg . Let it be k1.
• Therefore,
• T(n) = 2 T(n-1) + k1
• T(0) = k2 , a constant.
• T(1) = 2 k2 + k1
• T(2) = 4 k2 + 2k1 + k1
• T(2) = 8 k2 + 4k1 + 2k1 + k1
• Coefficient of k1 =2n
• Coefficient of k2 =2n-1
• Time complexity is O(2n) or O(an) where a is a constant greater than 1.
• So it has exponential time complexity. For single increase in problem size the time
required is double the previous one. This is computationally very expensive. Most of the
recursive programs takes exponential time that is why it is very hard to write them
iteratively .
8. Space Complexity of Algorithm
• Space for parameter for each call is independent of n i.e., constant. Let it be k .
• When we do the 2nd recursive call 1st recursive call is over . So, we can reuse the
space of 1st call for 2nd call . Hence ,
• T(n) = T(n-1) + k
• T(0) = k
• T(1) = 2k
• T(2) = 3k
• T(3) = 4k
• So the space complexity is O(n).
• Here time complexity is exponential but space complexity is linear . Often there is a
trade off between time and space complexity .
9.
10. Table:
Showing Number of Disk
and Disk Moves.
Number of Disks Number of Moves
0 INVALID
1 INVALID
2 3
3 7
4 15
5 31
6 63
7 127
8 255
9 511
11. Applications
• The Tower of Hanoi is frequently used in psychological research
on problem solving. There also exists a variant of this task called Tower of
London for neuropsychological diagnosis and treatment of executive
functions.
• The Tower of Hanoi is also used as a Backup rotation scheme when
performing computer data Backups where multiple tapes/media are
involved.
• As mentioned above, the Tower of Hanoi is popular for teaching recursive
algorithms to beginning programming students. A pictorial version of this
puzzle is programmed into the emacs editor, accessed by typing M-x hanoi.
There is also a sample algorithm written in Prolog.
• The Tower of Hanoi is also used as a test by neuropsychologists trying to
evaluate frontal lobe deficits.