0% found this document useful (0 votes)
25 views7 pages

So, Let Me Begin .

The document explains how the Towers of Hanoi problem can be recursively solved using a stack. It details the recursive function calls, parameters passed, and stack contents at each step to move 3 disks according to the problem rules.

Uploaded by

study material
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)
25 views7 pages

So, Let Me Begin .

The document explains how the Towers of Hanoi problem can be recursively solved using a stack. It details the recursive function calls, parameters passed, and stack contents at each step to move 3 disks according to the problem rules.

Uploaded by

study material
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/ 7

Dear Users,

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.

So, Let me begin…..

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;

printf("Enter the number of disks : ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

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

A: FROM B:Auxiliary C:TO

3
2
1

A: FROM B:Auxiliary C:TO

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:

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
O K….. now that you got an idea of how this problem can be solved manually. But, we being
programmers should find out solution with computer as a tool. Let’s do that…..

For simplicity purpose, I have annotated disks as 1, 2, and 3, and stands as


A->From, B->Auxiliary and C->To

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.

Yup… I am starting my explanation now… Please don’t get exhausted  


Firstly, the function call invokes the called function upon supplying the parameters (as shown
below)
towers(num, ‘A’,’C’,’B’);
void towers(int num, char frompeg, char topeg, char auxpeg) -------------------------------(1)
So, recursion makes use of stack, the stack content would be:

3, A, C, B Pushed to
Stack TOP
n f t a

Let us interpret it as follows:


num=3; frompeg=A; topeg=C; auxpeg=B;
After this, a condition is checked i.e., if(num==1) ? As of now, it’s not.
A recursive call takes place after that;
towers(num - 1, frompeg, auxpeg, topeg); i.e., num-1 = 2; frompeg=A; auxpeg=B; topeg=C;
(From the figure)
Now, these parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 2; frompeg = frompeg = A; topeg = auxpeg = B; auxpeg = topeg = C;
Now the stack content would look like this
Pushed to
2, A, B, C
Stack TOP
3, A, C, B
Not stack
contents 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

Move disk 1 from peg A to peg C ; num=1, frompeg = A, topeg = C

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

Stack contents now,

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;

Now the stack content would look like this

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

Stack contents now,


Pushed to
3, A, C, B Stack TOP

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

Move disk 3 from peg A to peg C; num=3, frompeg = A, topeg = C


Now, the second recursive function is processed with the popped values and the called function
is executed by pushing the contents onto the stack.
towers(num - 1, auxpeg, topeg, frompeg); num-1 = 2; auxpeg = B; topeg = C; frompeg = A;
[referring to stack top of figure Y ]

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

Move disk 1 from peg B to peg A; num=1, frompeg = B, topeg = A


Next, Using the stack top contents, print statement will be executed by popping out stack top.
[Refer to the above figure; minus popped contents].
Move disk 2 from peg B to peg C; num=2, frompeg = B, topeg = C
Stack becomes empty.
Now, the second recursive call will be invoked by supplying the popped contents as input.
towers(num - 1, auxpeg, topeg, frompeg); num-1 = 1; auxpeg = A; topeg = C; frompeg = B;
[referring to stack top of figure Z ]
Now, the above parameter values will be mapped to the parameters in (1)
Resulting into as below,
num = num-1 = 1; frompeg = auxpeg = A; topeg = topeg = C; auxpeg = frompeg = B;

The ‘if’ condition holds, hence print statement executes popping the stack.

Move disk 1 from peg A to peg C; num=1, frompeg = A, topeg = C

Finally, Stack goes empty. … I hope mind is not   


Hope my explanation has reached you and clarified few of your doubts in this regard.
Thank you….. C U Soon

You might also like