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

BCAC302

Report on BCAC302.

Uploaded by

Sulagna Dutta
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)
17 views

BCAC302

Report on BCAC302.

Uploaded by

Sulagna Dutta
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/ 16

SYAMAPRASAD INSTITUTE OF TECHNOLOGY & MANAGEMENT

Continuous Assessment - CA2

Subject name - Data Structure Through C

Subject code - BCAC302

Student name - Sulagna Dutta

University roll no. - 15342723045

University Registration Number - 23151010045 of

2023-24

Semester - 3rd

Year - 2nd

Session - 2024-25
Table of Content

1. Introduction..................................................1
2. History of the Tower of Hanoi......................2
3. Problem Definition.......................................3
4. Recursive Approach to Tower of Hanoi.........4
5. Mathematical Explanation............................5
6. Algorithm for Tower of Hanoi.......................6
7. Explanation of Code.....................................7
8. Time and Space Complexity.........................8
9. Applications of Tower of Hanoi....................9
10. Conclusion.............................................10-12
11. Bibliography...............................................13
12. Acknowledgment.........................................14
Introduction
The Tower of Hanoi is a well-known mathematical and computational puzzle

that illustrates the power and elegance of recursion in algorithmic design.

Originally formulated by the French mathematician Édouard Lucas in 1883,

it remains a central example in computer science courses due to its simplicity

and the depth of its recursive structure.

In the problem, there are three pegs and a number of disks of different sizes.

The objective is to move all the disks from the source peg to the destination

peg, using an auxiliary peg, under specific rules. The puzzle poses a challenge

because it seems simple at first glance, but as the number of disks increases,

the complexity of the solution grows exponentially. This puzzle helps to

explain recursion, a fundamental concept in programming, as well as divide-

and-conquer strategies, which are used in various algorithms.

Understanding the Tower of Hanoi involves grasping the recursive

decomposition of problems, solving them step-by-step, and combining those

steps to form a complete solution. This report will explore the Tower of

Hanoi problem from its historical roots to its implementation in modern

programming using recursion. It will also discuss the mathematical properties

and computational complexity of the solution.

1
History of the Tower of Hanoi

The Tower of Hanoi, or "Towers of Brahma," as it is sometimes called,

has a fascinating origin rooted in both mathematics and legend. The

problem was invented by French mathematician Édouard Lucas in 1883.

According to an ancient legend associated with the puzzle, in a temple

far away, monks are tasked with transferring 64 golden disks from one

peg to another. The belief is that when the monks complete the task, the

world will come to an end.

While the legend adds mystique to the problem, the mathematical

significance of the Tower of Hanoi cannot be overstated. It became an

ideal puzzle to demonstrate the recursive approach to problem-solving.

Over time, the Tower of Hanoi has been used to teach programming

concepts, mathematical induction, and cognitive science, particularly in

relation to human problem-solving.

2
Problem Definition
In the Tower of Hanoi problem, the goal is to move n disks from

one rod (the source) to another rod (the destination) with the help of

an auxiliary rod. The rules for solving the problem are as follows:

1. You can only move one disk at a time.

2. A disk can only be moved if it is the uppermost disk on a stack.

3. A larger disk may never be placed on top of a smaller disk.

The challenge intensifies as the number of disks increases. For

example, for 3 disks, the minimum number of moves required is 7.

For 4 disks, it is 15, and for n disks, it is 2^n - 1 moves. The

simplicity of the rules contrasts sharply with the rapid increase in the

number of moves required, making it a perfect problem to

demonstrate recursion.

3
Recursive Approach to Tower of Hanoi

Recursion is a process where a function calls itself with a smaller

input in each call. The Tower of Hanoi problem can be elegantly

solved using a recursive approach, which works by breaking down

the problem into smaller sub-problems.

For example, to solve the Tower of Hanoi with n disks:

First, move n-1 disks from the source rod to the auxiliary rod

using the destination rod as a helper.

Then, move the largest disk directly from the source rod to the

destination rod.

Finally, move the n-1 disks from the auxiliary rod to the

destination rod using the source rod as a helper.

This recursive breakdown continues until the simplest case, where

only one disk needs to be moved. This recursive approach minimizes

the complexity of the problem by dividing it into smaller, more

manageable tasks. The key to solving the problem efficiently lies in

understanding the recursion's base case (where n = 1) and its

recursive relation (where n > 1).

4
Mathematical Explanation

The Tower of Hanoi problem has a strong mathematical

foundation. For n disks, the minimum number of moves required is

given by the formula:

T(n)=2n−1T(n) = 2^n - 1T(n)=2n−1

This means that for each additional disk, the number of moves

doubles and then one extra move is added. Mathematically, the

Tower of Hanoi is an example of exponential growth, which is a key

concept in both mathematics and computer science. As the number

of disks increases, the number of moves grows exponentially,

demonstrating the inefficiency of solving the problem manually for

large n.

Let’s analyze the recursive relation of the problem:

For n = 1, the minimum number of moves is 1.

For n = 2, the minimum number of moves is 3.

For n = 3, the minimum number of moves is 7.

The recurrence relation for the problem can be expressed as:

T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1T(n)=2T(n−1)+1

5
Algorithm for Tower of Hanoi
The Tower of Hanoi algorithm is simple yet powerful. The recursive

nature of the problem allows it to be broken down into the

following steps:

1. Move n-1 disks from the source rod to the auxiliary rod.

2. Move the nth disk (the largest) from the source rod to the

destination rod.

3. Move the n-1 disks from the auxiliary rod to the destination rod.

The steps are repeated until the base case is reached, which occurs

when only one disk remains. Here is the algorithm in general terms:

Steps:

1. Move n-1 disks from the source peg to the auxiliary peg.

2. Move the largest disk from the source peg to the destination

peg.

3. Move the n-1 disks from the auxiliary peg to the destination peg.

This recursive algorithm can be easily implemented in various

programming languages, including C.


6
Explanation of Code
The provided C code implements the Tower of Hanoi problem using
recursion. Let's break down the code and explain each part:
#include <stdio.h>

void towerOfHanoi(int n, char source, char destination, char auxiliary)


{
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}

int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}

The towerOfHanoi() function is a recursive function that takes four arguments:


n: the number of disks.
source: the peg where the disks are currently stacked.
destination: the peg where the disks need to be moved.
auxiliary: the peg used as a temporary storage during the moves.
The base case is when n == 1, meaning there is only one disk to move. In this case,
the function directly moves the disk and returns. For n > 1, the function first
moves the n-1 disks recursively, then moves the nth disk, and finally moves the n-1
disks again from the auxiliary peg to the destination peg.

7
Time and Space Complexity

The time complexity of the Tower of Hanoi problem is exponential due

to the recursive nature of the solution. The minimum number of moves

required is 2^n - 1, meaning the time complexity is O(2^n). This

exponential time complexity means that for every additional disk, the

time taken to solve the problem doubles.

Time Complexity:

Base Case: For n = 1, only one move is required.

Recursive Case: For n disks, the total number of moves is 2^n - 1.

Therefore, the time complexity is O(2^n).

Space Complexity:

The space complexity of the Tower of Hanoi problem is determined by

the depth of the recursion. Since the recursion depth is proportional to

the number of disks, the space complexity is O(n).

8
Applications of Tower of Hanoi

Though it is primarily a theoretical puzzle, the Tower of Hanoi has several

practical applications in computing and other fields. Some of its key

applications include:

1. Recursive Algorithm Design: The Tower of Hanoi is used to teach

recursion in computer science courses. It helps students understand how

large problems can be broken down into smaller sub-problems.

2. Data Movement in Computer Systems: The principles of the Tower of

Hanoi are applied in real-life scenarios, such as data storage and retrieval

systems where data needs to be moved efficiently.

3. Psychological Research: The problem is used in psychological studies to

explore problem-solving strategies and cognitive processes in humans.

4. Game Design: Many puzzles and video games incorporate the Tower of

Hanoi principles to increase complexity and challenge players.

5. Disk Storage Management: In systems that involve disk-based storage,

the algorithm is applied to rearrange and optimize disk usage.

9
Conclusion

The Tower of Hanoi is not just a simple mathematical puzzle but a

profound example of how recursion can elegantly solve complex

problems. Through this puzzle, we witness the power of breaking down

large problems into smaller, more manageable sub-problems, which is

the core principle behind recursive algorithms. The recursive approach

demonstrated in solving the Tower of Hanoi emphasizes the importance

of recognizing patterns that can be reused, a fundamental skill in

programming and algorithmic design.

From its humble origins as a theoretical puzzle conceived by

mathematician Édouard Lucas in the 19th century, the Tower of Hanoi

has grown to become an essential teaching tool in computer science. The

problem highlights the recursive thought process and teaches how to

manage the limitations and constraints that often arise in computational

problems, such as memory constraints or the need for efficient solutions.

10
Beyond its use in education, the Tower of Hanoi also finds practical

applications in various fields. Its recursive structure is utilized in areas such as

disk storage management, where data must be moved efficiently between

different storage locations, and in psychological research, where it is used to

study problem-solving strategies and cognitive behavior in humans. In this

way, the Tower of Hanoi has transcended its role as a theoretical exercise and

has become a versatile tool in both computing and cognitive science.

Mathematically, the Tower of Hanoi problem serves as a perfect example of

exponential growth. The time complexity of the solution, O(2^n), reflects how

even a small increase in the number of disks results in a dramatic increase in

the number of moves required. This exponential relationship is significant in

understanding the limitations of recursive algorithms and serves as a

reminder that not all problems are efficiently solvable as their scale increases

In terms of algorithmic design, the Tower of Hanoi showcases the beauty and

simplicity of recursion. Despite its seemingly simple rules, the problem

becomes increasingly complex as the number of disks grows. This complexity

is managed through recursion, which allows for a systematic and structured

approach to solving the problem. Recursive solutions, as seen in the Tower of

Hanoi, are widely applicable in many areas of computer science, from sorting

algorithms to tree traversal methods.

11
In conclusion, the Tower of Hanoi is more than just an academic

exercise—it is a valuable pedagogical tool and a gateway to deeper

understanding of recursion, problem-solving, and algorithm

efficiency. Its rich history, elegant mathematical properties, and

real-world applications make it a timeless and significant problem in

the study of computer science and mathematics. The insights gained

from solving the Tower of Hanoi can be applied to a wide range of

computational problems, fostering a deeper understanding of

recursion and helping to develop problem-solving techniques that

can be applied in real-world scenarios.

As students and programmers, mastering the Tower of Hanoi and

its recursive solution provides a solid foundation for tackling more

complex algorithms and systems in the future. By understanding its

principles, one can gain not only an appreciation for the elegance of

recursion but also the knowledge necessary to design efficient

algorithms in a world where solving problems quickly and

effectively is more critical than ever.

12
Bibliography

1. Thomas H. Cormen, Introduction to Algorithms, MIT Press,

2009.

2. Robert L. Kruse, Data Structures and Program Design in C,

Prentice Hall, 1997.

3. Online resources like GeeksforGeeks and StackOverflow for

code implementation ideas.

13
Acknowledgment

I would like to express my deepest gratitude to Prof. Birabrata

Ganguly for his invaluable guidance and support throughout the

completion of this report. His insightful teaching of both the

theoretical and practical aspects of the "Data Structure through C"

subject has greatly contributed to my understanding of this topic.

I am truly grateful for his encouragement, patience, and expertise,

which helped me navigate the complexities of recursion and the

Tower of Hanoi problem. His constant feedback and willingness to

help have made this learning experience incredibly enriching.

Thank you, Professor, for your mentorship and for helping me grow

both academically and personally through this journey.

14

You might also like