So, Let Me Begin .
So, Let Me Begin .
I would like to put before you with an explanation that would convince you all about recursively
solving the famous problem - “Towers of Hanoi”. Usually, we mug it up just because the code
length is too short. But as a teacher I never insist anybody to do so. Approach is very simple to
understand. One very important thing we need to grasp here is how recursion works and what the
role of STACK in that process is. Have a little patience to go through this article, I bet you would
teach someone how this problem can be addressed.
Below is the simple C program with recursive approach for Towers of Hanoi problem
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
In the above program, towers (…) is the function that shows its execution in a recursive manner.
OK, now let us see the problem constraints of Tower of Hanoi and let us try to understand how it
can be addressed non-programmatically….
Problem: Move all the disks over to the rightmost tower, one at a time, so that they end up in the
original order on that tower. You may use the middle tower as temporary storage, but at no time
during the transfer should a larger disk be on top of a smaller one.
3
2
1
3
2
1
What you are observing is the problem (in the upper part) and its solution (in the lower part). If
we manually try to work it out keeping all the constraints in mind, the sequence of steps that one
would perform for 3 disks:
A small note to you all, it would be better if you take a copy (print or handwritten) of the
program shown above in your hand for analysis purpose otherwise you need to scroll several
times.
3, A, C, B Pushed to
Stack TOP
n f t a
Once again, the condition is checked i.e., if(num==1) ? it’s not again.
One more time recursive call takes place:
towers(num - 1, frompeg, auxpeg, topeg); i.e., num-1 = 1; frompeg=A; auxpeg=C; topeg=B;
Now, these parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 1; frompeg = frompeg = A; topeg = auxpeg = C; auxpeg = topeg = B;
Now the stack content would look like this
Pushed to
1, A, C, B
Stack TOP
2, A, B, C
3, A, C, B
n f t a
This time the condition satisfies i.e., num =1 now. Top of the stack will be popped as recursion
gets a break. The statement inside will get executed and the following statement would get
printed
2, A, B, C Pushed to
Stack TOP
3, A, C, B
n f t a
Figure X
Here, the first recursive call gets totally a break.
Immediately, the next print statement gets executed by popping the stack contents.
Move disk 2 from peg A to peg B ; num=2, frompeg = A, topeg = B
Pushed to
3, A, C, B Stack TOP
n f t a
Now with the popped parameter values, second function call invokes the function.
towers(num - 1, auxpeg, topeg, frompeg); num-1 = 1; auxpeg = C; topeg = B; frompeg = A;
[referring to stack top of figure X ]
Now, the above parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 1; frompeg = auxpeg = C; topeg = topeg = B; auxpeg = frompeg = A;
1, C, B, A Pushed to
Stack TOP
3, A, C, B
n f t a
Since the condition if(num == 1) satisfies, the print statement would get executed by taking the
popped parameters.
Hence, we see
Move disk 1 from peg C to peg B; num=1, frompeg = C, topeg = B
n f t a
Figure Y
Finally, the stack top is popped and the parameters are used for executing the print statement
outside the condition. Stack is empty now.
n f t a
Now, the above parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 2; frompeg = auxpeg = B; topeg = topeg = C; auxpeg = frompeg = A;
Now the stack content would look like this
Pushed to
2, B, C, A Stack TOP
n f t a
The condition will not hold because num = 2. Now, the first recursive call invokes the function
by supplying parameters onto the stack.
towers(num - 1, frompeg, auxpeg, topeg); i.e., num-1 = 1; frompeg=B; auxpeg=C; topeg=A;
Now, the above parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 1; frompeg = frompeg = B; topeg = auxpeg = C; auxpeg = topeg = A;
Now the stack content would look like this
Gets
Popped 1, B, A, C
Pushed to
2, B, C, A Stack TOP
n f t a
Figure Z
The ‘if’ condition holds, hence print statement executes popping the stack.