0% found this document useful (0 votes)
4 views3 pages

Tower of Hanoi

The document discusses recursive algorithms, defining them as functions that call themselves either directly or indirectly. It provides an example of the Towers of Hanoi problem, detailing the process of moving disks between towers under specific conditions and presenting the recursive algorithm for solving it. The time complexity of the Towers of Hanoi is established as O(2^n).

Uploaded by

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

Tower of Hanoi

The document discusses recursive algorithms, defining them as functions that call themselves either directly or indirectly. It provides an example of the Towers of Hanoi problem, detailing the process of moving disks between towers under specific conditions and presenting the recursive algorithm for solving it. The time complexity of the Towers of Hanoi is established as O(2^n).

Uploaded by

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

Design and Analysis of Algorithms

Recursive Algorithms:

 A Recursive function is a function that is defined in terms of itself.


 Similarly, an algorithm is said to be recursive if the same algorithm is
invoked in the body.
 An algorithm that calls itself is Direct Recursive.
 Algorithm „A‟ is said to be Indirect Recursive if it calls another
algorithm which in turns calls „A‟.
 The Recursive mechanism, are externally powerful, but even more
importantly, many times they can express an otherwise complex
process very clearly. Or these reasons we introduce recursion here.
 The following 2 examples show how to develop a recursive
algorithms.

 In the first, we consider the Towers of Hanoi problem, and in


the second, we generate all possible permutations of a list of
characters.

1. Towers of Hanoi:

.
.
.

Tower A Tower B Tower C

Towers of Hanoi is a problem in which there will be some disks


which of decreasing sizes and were stacked on the tower in decreasing order of
size bottom to top. Besides this there are two other towers (B and C) in which one
tower will be act as destination tower and other act as intermediate tower. In this
problem we have to move the disks from source tower to the destination tower. The
conditions included during this problem are:

GVP College of Engineering for Women -7-


Design and Analysis of Algorithms

1) Only one disk should be moved at a time.


2) No larger disks should be kept on the smaller disks.

Consider an example to explain more about towers of Hanoi:

Consider there are three towers A, B, C and there will be three disks present
in tower A. Consider C as destination tower and B as intermediate tower. The
steps involved during moving the disks from A to B are

Step 1: Move the smaller disk which is present at the top of the tower
A to C.
Step 2: Then move the next smallest disk present at the top of the tower A to
B.
Step 3: Now move the smallest disk present at tower C to tower B
Step 4: Now move the largest disk present at tower A to tower C
Step 5: Move the disk smallest disk present at the top of the tower B
to tower A.
Step 6: Move the disk present at tower B to tower C.
Step 7: Move the smallest disk present at tower A to tower C
In this way disks are moved from source tower to destination tower.

ALGORITHM FOR TOWERS OF HANOI:

Algorithm Towersofhanoi (n, X ,Y, Z)


{
if (n>=1) then
{
Towersofhanoi(n-1, X, Z, Y);
Write(“move top disk from tower “,X, “to top of tower”,Y);
Towersofhanoi (n-1, Z, Y, X);
}
}

TIME COMPLEXITY OF TOWERS OF HANOI:

The recursive relation is:

t(n)=1; if n=0
=2t(n-1)+2 if n>=1

GVP College of Engineering for Women -8-


Design and Analysis of Algorithms

Solve the above recurrence relation then the time complexity of towers of Hanoi is
O(2^n)

GVP College of Engineering for Women -9-

You might also like