0% found this document useful (0 votes)
45 views

Tower of Hanoi Using Recursion

Uploaded by

amutha d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Tower of Hanoi Using Recursion

Uploaded by

amutha d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Tower of Hanoi Using Recursion

Tower of Hanoi
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and
more than one rings is as depicted −

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.
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.

If 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.

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 (n th 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

Tower of Hanoi in Data Structures: An Overview


Tower of Hanoi is a mathematical game or puzzle that consists of three rods also called pegs and N number of
disks of various diameters. These disks are arranged in ascending order from the top, the smallest at the top,
thus approximating a conical shape. This puzzle is shifting all the given N number of disks from the source peg
to the destination peg with the help of an alternate or auxiliary peg.
This DSA tutorial will discuss in detail the methods to solve the Tower of Hanoi problem. To further enhance
your understanding and application of Tower of Hanoi concepts, consider enrolling in the Dsa Course Online,
to gain comprehensive insights into effective data structure utilization for improved problem-solving and time
management.

Problem Statement for Tower of Hanoi Problem


Move all the N number of disks from the given peg to the destination peg according to the simple rules
mentioned below.

Tower of Hanoi Rules

1. The game consists of three pegs and several disks of different sizes, which can slide onto any peg.
2. The disks are initially placed on one peg in order of decreasing size so that the smallest disk is at the
top and the largest disk is at the bottom.
3. The objective of the game is to move the entire stack to another peg, obeying the following rules:
o Only one disk can be moved at a time.
o Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack or an empty peg.
o No disk may be placed on top of a smaller disk.
4. The puzzle can be solved in the minimum number of moves, which is (2^n)-1, where n is the number
of disks.
5. These rules apply to any version of the Tower of Hanoi puzzle, regardless of the number of pegs or
disks used.

Constraints

1. Number of pegs = 3 (source, auxiliary, destination).


2. 1 <= N (Number of disks) < 100.

Example of working of Tower of Hanoi Algorithm

Let's assume the number of disks as N = 2. All three pegs are named A, B, and C. Our task is to shift the two
disks from A to C using B. The stack is represented as follows:
1. Move disk 1 from peg A to peg B. After this move, the arrangement looks like this, which is shown in
the below image.

2. Move disk 2 from peg A to peg C. After this move, the arrangement looks like this, which is shown in
the below image.

3. Move disk 1 from peg B to peg C. After this move, the arrangement looks like this which is shown in
the below image.
4. From the above image, we can conclude that every disk is been moved from A (source peg) to C
(destination peg) and this has been achieved by following the given rules which are mentioned
above.

Implementation of Tower of Hanoi Algorithm in Different


Programming Languages
There are mainly two ways to implement the Tower of Hanoi Algorithm in Data Structures,
using recursion and iteration. Let us look at each of them.

1. Using Recursion

This approach mainly discusses the recursive approach to be followed for moving the disks from the source
to the destination peg.

Recursive Tower of Hanoi Algorithm

Initially consider A as the source peg, B as the auxiliary peg, and C as the destination peg.

1. Firstly, shift the n-1 number of disks from A to B, using C.


2. Secondly, shift the last disk from A to C.
3. The third step is to shift the n-1 number of disks from B to C, using A.
Time Complexity Space Complexity

Recursive Method O(2^N) O(N)

Iterative Method O(N) O(N)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy