Open In App

Time Complexity Analysis | Tower Of Hanoi (Recursion)

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 
1) Only one disk can be moved at a time. 
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 
3) No disk may be placed on top of a smaller disk. 

Algorithm

• Move the top n – 1 disks from Source to Auxiliary tower,
• Move the nth disk from Source to Destination tower,
• 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. Once we solve Towers of Hanoi with three disks, we can solve it with any number of disks with the above algorithm. 

Pseudo Code 

TOH(n, x, y, z)
{
   if (n >= 1)
   {
      // put (n-1) disk to z by using y
      TOH((n-1), x, z, y)
   
       // move larger disk to right place
       move:x-->y
     
      // put (n-1) disk to right place 
      TOH((n-1), z, y, x)
   }
}



Analysis of Recursion 

Recursive Equation : T(n) = 2T(n-1) + 1     ——-equation-1 

Solving it by Backsubstitution : 
T(n-1) = 2T(n-2) + 1     ———–equation-2 
T(n-2) = 2T(n-3) + 1     ———–equation-3 

Put the value of T(n-2) in the equation–2 with help of equation-3 
T(n-1)= 2( 2T(n-3) + 1 ) + 1     ——equation-4 

Put the value of T(n-1) in equation-1 with help of equation-4 
T(n)= 2( 2( 2T(n-3) + 1 ) + 1 ) + 1
T(n) = 2^3 T(n-3) + 2^2 + 2^1 + 1

After Generalization : 
T(n)= 2^k T(n-k) + 2^{(k-1)} + 2^{(k-2)} + ............ +2^2 + 2^1 + 1

Base condition T(1) =1 
n – k = 1 
k = n-1
put, k = n-1
T(n) =2^{(n-1)}T(1) + + 2^{(n-2)} + ............ +2^2 +2^1 + 1

It is a GP series, and the sum is 2^n - 1

T(n)= O( 2^n - 1)     , or you can say O(2^n)     which is exponential

for 5 disks i.e. n=5 It will take 2^5-1=31 moves.


Previous Article
Next Article

Similar Reads

Cost Based Tower of Hanoi
The standard Tower of Hanoi problem is explained here . In the standard problem, all the disc transactions are considered identical. Given a 3x3 matrix costs[][] containing the costs of transfer of disc between the rods where costs[i][j] stores the cost of transferring a disc from rod i to rod j. Cost of transfer between the same rod is 0. Hence th
10 min read
Twisted Tower of Hanoi Problem
The basic version of the Tower of Hanoi can be found here. It is a twisted Tower of Hanoi problem. In which, all rules are the same with an addition of a rule: You can not move any disk directly from the first rod to last rod i.e., If you want to move a disk from the first rod to the last rod then you have to move the first rod to the middle rod fi
7 min read
Tower of Hanoi | Set 2
Given a positive integer N representing the number of disks in the Tower of Hanoi, the task is to solve the Tower of Hanoi puzzle using Binary representations. Examples: Input: N = 3Output:Move the 1 disk to next circular right rodMove the 2 disk to next circular right rodMove the 1 disk to next circular right rodMove the 3 disk to next circular ri
12 min read
Iterative Tower of Hanoi
The Tower of Hanoi is a mathematical puzzle. It consists of three poles and a number of disks of different sizes which can slide onto any pole. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. The objective of the puzzle is to move all the disks from one pol
16 min read
Recursive Tower of Hanoi using 4 pegs / rods
Tower of Hanoi is a mathematical puzzle. Traditionally, It consists of three poles and a number of disks of different sizes which can slide onto any poles. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. The objective of the puzzle is to move all the disks
8 min read
Program for Tower of Hanoi Algorithm
Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to move the entire stack to another rod (here considered C), obeying the following simple
6 min read
Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
Asymptotic Notation and Analysis (Based on input size) in Complexity Analysis of Algorithms
Asymptotic Analysis is defined as the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, how the time (or space) taken by an algorithm increases with the input size. Asymptotic notation is
8 min read
How to solve time complexity Recurrence Relations using Recursion Tree method?
The Recursion Tree Method is a way of solving recurrence relations. In this method, a recurrence relation is converted into recursive trees. Each node represents the cost incurred at various levels of recursion. To find the total cost, costs of all levels are summed up. Steps to solve recurrence relation using recursion tree method: Draw a recursiv
4 min read
Step Count Method for Time Complexity Analysis
What is Time Complexity? Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm. Time Complexity can be calculated by using Two types of methods. They are: Step Count MethodAsymptotic Notation. Here, we will discuss the Step Count Method. What is Step Count M
4 min read
Practice Tags :