Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted −
Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same.

Rules

The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are −

  • Only one disk can be moved among the towers at any given time.
  • Only the "top" disk can be removed.
  • No large disk can sit over a small disk.

Following is an animated representation of solving a Tower of Hanoi puzzle with three disks.
Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)

Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.

Algorithm

  • To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with lesser amount of disks, say → 1 or 2. We mark three towers with name, source, destination and aux (only to help moving the disks). If we have only one disk, then it can easily be moved from source to destination peg.
    we have 2 disks −
  • First, we move the smaller (top) disk to aux peg.
  • Then, we move the larger (bottom) disk to destination peg.
  • And finally, we move the smaller disk from aux to destination peg.

Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)

So now, we are in a position to design an algorithm for Tower of Hanoi with more than two disks. We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all other (n-1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto it. We can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are:
Step 1: Move n-1 disks from source to aux
Step 2: Move nth disk from source to dest
Step 3: Move n-1 disks from aux to dest

A recursive algorithm for Tower of Hanoi can be driven as follows:
START
Procedure Hanoi(disk, source, dest, aux)
   IF disk == 1, THEN
      move disk from source to dest  ELSE
      Hanoi(disk - 1, source, aux, dest)     // Step 1
      move disk from source to dest          // Step 2
      Hanoi(disk - 1, aux, dest, source)     // Step 3
   END IF
END Procedure
STOP

The document Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Towers of Hanoi - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is the Towers of Hanoi problem?
Ans. The Towers of Hanoi is a mathematical puzzle that involves three rods and a number of disks of different sizes. The objective is to move the entire stack of disks from one rod to another, following specific rules.
2. How many moves are required to solve the Towers of Hanoi problem with n disks?
Ans. The minimum number of moves required to solve the Towers of Hanoi problem with n disks can be calculated using the formula 2^n - 1. So, for example, if there are 3 disks, the minimum number of moves required is 2^3 - 1 = 7.
3. What are the rules for solving the Towers of Hanoi problem?
Ans. The rules for solving the Towers of Hanoi problem are as follows: 1. Only one disk can be moved at a time. 2. Each move consists of taking the uppermost disk from one of the rods and placing it on top of another rod. 3. No disk may be placed on top of a smaller disk.
4. What is the recursive algorithm to solve the Towers of Hanoi problem?
Ans. The recursive algorithm to solve the Towers of Hanoi problem is as follows: 1. If there is only one disk, move it from the source rod to the destination rod. 2. If there are more than one disks, recursively move n-1 disks from the source rod to an auxiliary rod. 3. Move the remaining disk from the source rod to the destination rod. 4. Recursively move the n-1 disks from the auxiliary rod to the destination rod.
5. Can the Towers of Hanoi problem be solved iteratively?
Ans. Yes, the Towers of Hanoi problem can be solved iteratively using a stack data structure. By simulating the recursive algorithm iteratively, the problem can be solved without using recursion. However, the iterative solution is more complex and less intuitive compared to the recursive solution.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

video lectures

,

Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

,

Previous Year Questions with Solutions

,

Exam

,

Summary

,

Extra Questions

,

mock tests for examination

,

Viva Questions

,

Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)

,

study material

,

Semester Notes

,

shortcuts and tricks

,

past year papers

,

Free

,

practice quizzes

,

ppt

,

Towers of Hanoi | Programming and Data Structures - Computer Science Engineering (CSE)

,

pdf

,

Sample Paper

,

MCQs

;