The Tower Of Hanoi problem is perhaps the most frequently asked recursion-based problem in interviews and examinations. Perhaps the most succinct explanation of this problem can be found in Wikipedia here .
PROBLEM STATEMENT :
A set of n disks are kept on a peg A such that each disk lies on top of a disk larger in size than it. This set of disks is to be moved to another peg B; always minding the consistency that a smaller disk lies on a larger disk. We can use an auxiliary peg C for our aid.
ALGORITHM :
We use recursion to solve the problem. Consider an auxiliary function moveTower that accepts the parameters sourceTower, destTower, auxTower and numberOfDisks. This function moves numberOfDisks disks (<= n) from the sourceTower to the destTower using the auxTower. We follow the recursive idea that in order to move n disks from A to B using C as auxiliary, we move the top n-1 disks from A to C using auxiliary and then move the remaining 1 disk (the largest disk) from A to B. The problem now shortens to moving n-1 disks from C to B using A as auxiliary. Thus we see that after each iteration, the problem gets smaller and thus there will be a terminating condition. The terminating condition of the recursion is that n becomes 1. 1 disk can be moved in 1 move.
Algorithm moveTower (sourceTower, destTower, auxTower, numberOfDisks)
ANALYSIS :
If T(n) is the time required to transfer n disks from sourceTower to destTower, then by the above algorithm, T(n) is distributed into 2 transfers of n-1 disks and a transfer of 1 disk. Thus,
T(n) = 2T(n-1) + T(1)
But how many moves do we require to transfer n disks from A to B? We can prove by mathematical induction that the minimum number of moves required are 2n - 1. The 3 steps of mathematical induction are :
PROBLEM STATEMENT :
A set of n disks are kept on a peg A such that each disk lies on top of a disk larger in size than it. This set of disks is to be moved to another peg B; always minding the consistency that a smaller disk lies on a larger disk. We can use an auxiliary peg C for our aid.
ALGORITHM :
We use recursion to solve the problem. Consider an auxiliary function moveTower that accepts the parameters sourceTower, destTower, auxTower and numberOfDisks. This function moves numberOfDisks disks (<= n) from the sourceTower to the destTower using the auxTower. We follow the recursive idea that in order to move n disks from A to B using C as auxiliary, we move the top n-1 disks from A to C using auxiliary and then move the remaining 1 disk (the largest disk) from A to B. The problem now shortens to moving n-1 disks from C to B using A as auxiliary. Thus we see that after each iteration, the problem gets smaller and thus there will be a terminating condition. The terminating condition of the recursion is that n becomes 1. 1 disk can be moved in 1 move.
Algorithm moveTower (sourceTower, destTower, auxTower, numberOfDisks)
- if (numberOfDisks == 1) then move disk from sourceTower to destTower and exit //terminating condition
- else{
- moveTower(sourceTower, auxTower, destTower, numberOfDisks - 1)
- moveTower(sourceTower, destTower, auxTower, 1)
- moveTower(auxTower, destTower, sourceTower, numberOfDisks-1)
- } if - else ends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TowerOfHanoi { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.println("Enter the number of disks to be moved from tower A to tower C"); | |
int numberOfDisks = input.nextInt(); | |
moveTower('A', 'C', 'B', numberOfDisks); | |
input.close(); | |
System.exit(0); | |
} | |
public static void moveTower(char sourceTower, char destTower, char auxTower, int numberOfDisks){ | |
if(numberOfDisks == 0){ | |
System.out.println("No more moves"); | |
System.exit(1); | |
} | |
if(numberOfDisks == 1){ | |
System.out.println(sourceTower + " --> " + destTower); | |
return; | |
} | |
else{ | |
moveTower(sourceTower, auxTower, destTower, numberOfDisks-1); | |
moveTower(sourceTower, destTower, auxTower, 1); | |
moveTower(auxTower, destTower, sourceTower, numberOfDisks-1); | |
} | |
} | |
} |
If T(n) is the time required to transfer n disks from sourceTower to destTower, then by the above algorithm, T(n) is distributed into 2 transfers of n-1 disks and a transfer of 1 disk. Thus,
T(n) = 2T(n-1) + T(1)
But how many moves do we require to transfer n disks from A to B? We can prove by mathematical induction that the minimum number of moves required are 2n - 1. The 3 steps of mathematical induction are :
- Basic Case : For n = 1 i.e if there is only 1 disk, then we directly transfer the disk from A to B in 1 move. 21 - 1 = 1. Hence, the equation holds true for the base case.
- Assumption : We now assume that the equation holds true for all integers 1 to n-1.
- Induction : Since the relation holds true for integers from 1 to n-1, we now check the relation for n number of disks. To transfer n disks from A to B, we first move n-1 disks from A to C in 2n-1 - 1 moves, then transfer 1 disk from A to B in 1 move and lastly move n-1 disks from C to B in 2n-1 - 1 moves. Thus, the total number of moves are 2n-1 - 1
+ 2n-1 - 1 + 1 = 2*2n-1 - 1 =
2n - 1. Thus, we have "induced" the relation to all integers.