0% found this document useful (0 votes)
139 views3 pages

Tower of Hanoi

The Tower of Hanoi puzzle involves moving disks of different sizes stacked on three pegs, obeying rules where only the top disk can be moved, and a larger disk can never be placed on top of a smaller one. The algorithm uses recursion to solve the puzzle, by breaking it down into moving all but the largest disk to an intermediate peg, moving the largest disk, and then moving the remaining disks to the destination peg. The number of moves required to solve the puzzle for N disks is 2^N - 1.

Uploaded by

Niazi
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)
139 views3 pages

Tower of Hanoi

The Tower of Hanoi puzzle involves moving disks of different sizes stacked on three pegs, obeying rules where only the top disk can be moved, and a larger disk can never be placed on top of a smaller one. The algorithm uses recursion to solve the puzzle, by breaking it down into moving all but the largest disk to an intermediate peg, moving the largest disk, and then moving the remaining disks to the destination peg. The number of moves required to solve the puzzle for N disks is 2^N - 1.

Uploaded by

Niazi
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/ 3

Tower of Hanoi

Introduction:
Tower of Hanoi is a mathematical puzzle in which concept of Recursion is
applied to solve the problem. This game is invented by French mathematician Edouard
Lucas in 1983. The game consists of three pegs named A, B, C and N number of Disks of
Different sizes; from top to bottom size of disks increase. The bottom disks have
maximum size and the Top disk has minimum size. The objective of the Game is to
Transfer the entire stack of disks from one peg to another obeying the Following Rules:
❖ Only one disk can be Transfer at a time.
❖ We take upper disk from one the stack and place it on another stack top. A
disk can be transfer only if it is upper disk of the stack.
❖ No large disk can be placed on top of small disk.
Algorithm:
This algorithm is applied for N number of Disks using Recursion procedure.
Tower (N, Beg, Aux, End)
1)If N=1 then
Write Beg→End and Return.
2)if N>1 then
Transfer N-1 disks from Beg→Aux peg using End as an intermediate.
Call Tower (Beg, End, Aux)
3) Tower (1, Beg, Aux, End)
or
Write Beg→End.
4) Transfer N-1 disks from peg Aux to Peg End.
Call Tower (N-1, Aux, Beg, End).
5) Return.
Explanation of Algorithm for N=4 Disks.

1. Transfer disk from A →B.


2. Transfer disk from A→C.
3. Transfer disk from B→C.
4. Transfer disk from A→B.
5. Transfer disk from C→A.
6. Transfer disk from C→B.
7. Transfer disk from A→B.
8. Transfer disk from A→C.
9. Transfer disk from B→C.
10. Transfer disk from B→A.
11. Transfer disk from C→A.
12. Transfer disk from B→C.
13. Transfer disk from A→B.
14. Transfer disk from A→C.
15. Transfer disk from B→C.

By this theorem we have identified that to transfer N disks from one peg to another
we need 2n-1 steps.
Pseudocode for Explanation.

Tower (disks, source, intermediate, destination)


{
If disk is equal to 1 then
Transfer disk from source to designation.
Else
Call Tower (disks-1, source, designation, intermediate) //step 1
Transfer disk from source to designation. //step 2
Call Tower (disks-1, intermediate, source, designation) //step 3
End if
End.

}
This is structure of our solution. we take number of disks as an argument
from the user and provide source, intermediate, destination so that we
understand overall map to complete solution.
We find a terminate state where we don’t need to call this function.
Terminate State:
If disk is equal to 1
In this case this would be our terminate state. When there will be only
one disk it is easy to go to the final step after that our task will done.
In this case final step is:
Transfer the disk from source to designation.
Now we call our function again by passing these arguments in this case
we divide our algorithm in two parts. The largest disk (Nth disk) in one
Part and other (N-1th disk) in another part. Then we call the function two
times for N-1 disks.
First time:
Tower (disks-1, source, designation, intermediate).
We pass total N-1 disks as an argument then we:
Transfer the disks from Source to designation.
In this case designation peg helps to transfer disks from source to intermediate peg.
After that we again call our function to Transfer disks from intermediate to
designation stage.
Call function:
Tower (disks-1, intermediate, source, designation).
End.

You might also like