Professional Documents
Culture Documents
Dsa Assignment
Dsa Assignment
TOWER OF HANOI
EXPLANATION
This tower is an interesting game . In it we have three tower L ,M and N. And size of towers increases
while coming from top to bottom. we have to move all the disks from tower L to tower N using tower M
as middle such that at no point of time a larger disk comes on top of the smaller one and one more rule
is that we can only move one disk at a time so lets see an example.
EXAMPALE
1 DISK
imagine we have just one disk . this Is simple we can just move one disk from tower L to tower N directly
. we have named the tower and numbered the disks so we can track the movement .
2 DISKS
IF there are two disks we can move upper disk from tower L to tower M and then 2nd disk from tower L
to tower N and finally previous disk from tower M to tower N.
3 DISKS
If there are three disks we will move 1 disk from tower L to N and disk 2 from tower L to M . move disk 1
from N to M and move disk 3 from L to N . now we would move both of disk 1 and 2 to tower N .
As we can only move one disk at a time so we first need to move disk 1 from tower M to tower L and
then move disk 2 from tower M to N and then finally move disk 1 from L to N .
This was little more complicated then 2 disks .so when disks are greater in size .. this is where recursion
comes and it becomes quite easy with recursion.
N DISKS
For n number of disks we will repeat the same process of recursion.
manually it would take a lot of time but by recursion the following algorathim can help.
PSEUDOCODE
Move (N,’L’,’M’,’N’)
MOVE (N-1,’L’,’M’,’N’)
Move (N-1,’M’,’N’,’L’)