In the previous article about the Tower of Hanoi, we explored the puzzle by examining its history and rules, and we developed an algorithm capable of solving the puzzle for any number of disks. If you haven't read it yet, click here.
This time, we'll discuss the Tower of Hanoi from a mathematical perspective, deriving a formula that represents the puzzle's solution and understanding how mathematics helps us determine the algorithm's complexity.
A solid understanding of the logic behind the puzzle is important, so ensure you're comfortable with that before proceeding.
Tower of Hanoi puzzle is much more than a simple puzzle used to exercise logic, it also has a significant relationship with math.
In the previous article, we couldn't determine the complexity of the algorithm we developed to solve the puzzle. This is where mathematics can help us.
To achieve this, the first step is to find a way to represent the resolution of the problem using a mathematical formula. It might seem daunting at first, but trust me, it's not that complicated. Patience is key here; my advice is to read and re-read each part of the explanation until every concept is clear.
The best way to solve any problem is by generalizing it. This means broadening the scope of a specific problem to a wider context, creating a more general version that can encompass various particular cases.
That's why we'll call the number of disks in the tower as .
The advantage of this approach is that we can increase or decrease the number of disks in the tower, and the formula will work without any problem.
The second step is to establish a nomenclature that will be used in the problem resolution. For this purpose we define that is the minimum number of moves needed to complete the tower.
So, to summarize:
The next step is to identify some statements that, although obvious, will help us find patterns in the problem:
We already know that to solve a tower with 3 disks, we first need to complete a tower with 2 disks, then move the third disk, and finally move the other 2 disks above the third disk.
Generalizing it, to solve a tower with disks we first need to solve a tower with disks, move the th disk and then move disks above the th disk.
Now bringing some math, to resolve a tower with disks we first need to resolve a tower with disks, which costs moves. Then we move the th disk, which takes move. Finally we move the tower on top of the th disk, which costs another moves.
We can express this logic using the following mathematical formula:
Note in the formula above that the solution of a depends of the solution of a tower, that is a recursion represented in math!
Like a recursion in coding, without a base case, it would continue indefinitely without reaching a solution. Hence, the statement T0=0T_0 = 0T0=0 serves as the base case that stops the recursion appropriately.
Understanding the logic behind the game and creating an appropriate nomenclature helped us to describe the problem's formula in a simple way.
So, we've found a formula that can solve the Tower of Hanoi puzzle for any number of disks, which is great. However, solving it directly using this formula can be challenging because it involves resolving each recursion step-by-step, which can be difficult. Furthermore, this recursive formula doesn't directly provide insights into the complexity of the Tower of Hanoi algorithm; it merely represents our code in a mathematical form.
What we really want is to derive a closed formula that is easy to compute and helps us determine the time complexity of solving the Tower of Hanoi.
To achieve this, let's manipulate the formula we've already established:
Firstly, let's add 1 to both sides of the equation:
Now, let's introduce a new notation:
There's nothing complicated here, is an arbitrary notation that we've created to represent .
Replacing the values using the new notation we get:
, for
The manipulations might seen difficult to grasp at first. I recommend going step by step and truly understand what's happening in each phase, don't skip steps!
Given the formulas we've established:
We can derive the following sequence:
Now, let's apply actual numbers:
From the statements above, we can assume that:
As last step we need to go back to the notation, so replacing we have:
Now we can find the number of steps needed to resolve the Tower of Hanoi puzzle using the closed formula and simply replacing with the actual number of disks in the tower.
Now, we have a closed formula that allows us to determine the number of steps needed to solve the Tower of Hanoi puzzle simply by substituting with the actual number of disks in the tower.
Now we're closer than ever to understanding the complexity of the Tower of Hanoi algorithm, as we have derived a formula to determine the number of steps required to solve the puzzle with any number of disks.
Applying the formula in Big O notation, we get:
Since in Big O notation we always drop the constants, we find that the complexity of the Tower of Hanoi algorithm is , also known as Exponential Time.
An exponential time algorithm is a truly slow one, and that's make a lot of sense considering the mythology behind the puzzle.
Below is a chart illustrating how the number of steps grows with the number of disks:
But here's the advantage of mathematical knowledge in this scenario:
If you ever need to create an algorithm that calculates the number of steps required to solve a Tower of Hanoi puzzle with any number of disks, you don't need to develop an entire solving algorithm. Instead, you can simply use the closed form that we discovered to determine the total number of moves needed. This reduces the algorithm's complexity from to .
Certainly, you probably won't ever need to solve the Tower of Hanoi puzzle. However, trust me, this logical approach can be applied to many other situations in your daily work.
In this article, we explored the Tower of Hanoi puzzle from a mathematical perspective and used mathematics to determine its time complexity.
If you have any questions or feedback, please feel free to send me a message on LinkedIn. If you enjoyed the article, consider sharing it on social media so that more people can benefit from it!