Thursday, June 27, 2013

Explain the Towers Of Hanoi(TOH) problem.



The Towers Of Hanoi problem is abbreviated as TOH and the problem relates to shifting of the disks(or plates) from one peg(or pole) to another peg by using an auxiliary peg such that in every stage of the procedure the smaller disk is always mounted on the disk larger than that.
Let peg A be the source peg from where the disks have to be shifted, peg B is an auxiliary peg and peg C is the destination peg. The number of steps required for n number of disks is 2n-1 steps. Then the TOH algorithm is as follows:
1.   Start
2.   If n==1 move single disk from A to C and exit.
3.   move top n-1 disks from A to B using C as auxiliary peg.
4.   Move remaining nth disk from A to C.
5.   Move the n-1 disk from B to C using A as auxiliary.
6.   End

No comments:

Post a Comment