How To Solve The Tower of Hanoi Problem - An Illustrated Algorithm Guide
How To Solve The Tower of Hanoi Problem - An Illustrated Algorithm Guide
2. Each move consists of taking the upper disk from one of the
stacks and placing it on top of another stack. In other words, a
disk can only be moved if it is the uppermost disk on a stack.
Three disks.
We can use B as a helper to finish this job. We are now ready to move
on. Let’s go through each of the steps:
You can see the animated image above for a better understanding.
Now, let’s try to build the algorithm to solve the problem. Wait, we
have a new word here: “Algorithm”. What is that? Any idea? No
problem, let’s see.
If you take a look at those steps you can see that we were doing the
same task multiple times — moving disks from one stack to another.
We can call these steps inside steps recursion.
Recursion
Forum Donate
Recursion — giphy
Recursion is calling the same action from that action. Just like the
above picture.
So there is one rule for doing any recursive work: there must be a
condition to stop that action executing. I hope you understand the
basics about recursion.
Now, let’s try to build a procedure which helps us to solve the Tower
of Hanoi problem. We are trying to build the solution using
pseudocode. Pseudocode is a method of writing out computer code
using the English language.
This is the skeleton of our solution. We take the total disks number as
an argument. Then we need to pass source, intermediate place, and
the destination so that we can understand the map which we will useDonate
Forum
to complete the job.
Learn to code — free 3,000-hour curriculum
Now we need to find a terminal state. The terminal state is the state
where we are not going to call this function anymore.
IF disk is equal 1
In our case, this would be our terminal state. Because when there will
be one disk in our stack then it is easy to just do that final step and
after that our task will be done. Don’t worry if it’s not clear to you.
When we reach the end, this concept will be clearer.
Alright, we have found our terminal state point where we move our
disk to the destination like this:
END
Output:
It took seven steps for three disks to reach the destination. We call
this a recursive method.
Forum Donate
Now, the time required to move n disks is T(n). There are two
recursive calls for (n-1). There is one constant time operation to move
a disk from source to the destination, let this be m1. Therefore:
And
From these patterns — eq(2) to the last one — we can say that the time
complexity of this algorithm is O(2^n) or O(a^n) where a is a constant
greater than 1. So it has exponential time complexity. For the single Donate
Forum
increase in problem size, the time required is double the previous one.
Learn to code — free 3,000-hour curriculum
This is computationally very expensive. Most of the recursive
programs take exponential time, and that is why it is very hard to write
them iteratively.
Space complexity
After the explanation of time complexity analysis, I think you can
guess now what this is…This is the calculation of space required in ram
for running a code or application.
In our case, the space for the parameter for each call is independent of
n, meaning it is constant. Let it be J. When we do the second recursive
call, the first one is over. That means that we can reuse the space after
finishing the first one. Hence:
Conclusion
From this article, I hope you can now understand the Tower of Hanoi
puzzle and how to solve it. Also, I tried to give you someForum
basic Donate
understanding about algorithms, their importance, recursion,
Learn to code — free 3,000-hour curriculum
pseudocode, time complexity, and space complexity. If you want to
learn these topics in detail, here are some well-known online courses
links:
1. Algorithms, Part I
2. Algorithms, Part II
You can visit my data structures and algorithms repo to see my other
problems solutions.
Dipto Karmakar
[ Full-stack software engineer | Backend Developer | Pythonista ] I love to
code in python. In my free time, I read books. I enjoy learning and
experiencing new skills.
If you read this far, tweet to the author to show them you care.
Tweet a thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Our Nonprofit
About Alumni Network Open Source Shop Support Sponsors Academic Honesty