The Tower of Hanoi is a classic mathematical puzzle consisting of three pegs and a set of disks of different sizes. The objective of the puzzle is to move the entire stack of disks from one peg (the source peg) to another peg (the target peg), while following these rules:
The initial configuration has all the disks stacked in decreasing order of size on one peg. The challenge is to move this entire stack to another peg by using the destination peg as an intermediate step if necessary.
There are two methods to solve this problem which are as follows:
The iterative approach to solving the Tower of Hanoi problem involves simulating the recursive steps of the algorithm using an iterative loop and a stack data structure.
Here’s a step-by-step explanation of the iterative approach:
Create an empty stack to store the disk movement states.
Push the initial movement state onto the stack. This state represents the movement of all disks from the source peg to the target peg using the auxiliary peg.
While the stack is not empty, do the following:
Pop a movement state from the stack.
If the number of disks in the movement state is 1, directly move the disk from the source peg to the target peg.
If the number of disks is greater than 1, push three subproblems onto the stack in reverse order:
n-1
disks from the auxiliary peg to the target peg using the source peg as the auxiliary.
Moving the largest disk from the source peg to the target peg.n-1
disks from the source peg to the auxiliary peg using the target peg as the auxiliary.Repeat steps 3 until the stack is empty.
The iterative approach essentially emulates the recursive steps of the Tower of Hanoi algorithm by using a stack to keep track of the states of each movement. Instead of making recursive function calls, the algorithm pushes subproblems onto the stack and processes them iteratively.
By following this iterative approach, the Tower of Hanoi problem can be solved efficiently without relying on explicit recursion.
The following code will help us better understand this iterative method.
class DiskMove:def __init__(self, n, source, target, auxiliary):self.n = n # Number of disks to be movedself.source = source # Source pegself.target = target # Target pegself.auxiliary = auxiliary # Auxiliary pegdef iterativeHanoi(n, source, target, auxiliary):stack = [] # Stack to store disk movement statesstack.append(DiskMove(n, source, target, auxiliary))while stack:move = stack.pop()if move.n == 1:print(f"Move disk 1 from {move.source} to {move.target}")else:# Push the subproblems onto the stack in reverse orderstack.append(DiskMove(move.n - 1, move.auxiliary, move.target, move.source))stack.append(DiskMove(1, move.source, move.target, move.auxiliary))stack.append(DiskMove(move.n - 1, move.source, move.auxiliary, move.target))# Example usage:n = 3 # Number of diskssource = "A" # Source pegtarget = "C" # Target pegauxiliary = "B" # Auxiliary pegiterativeHanoi(n, source, target, auxiliary)
DiskMove
with an initializer method that sets the number of disks (n
) to be moved and the source, target, and auxiliary pegs (source
, target
, auxiliary
) as instance variables.(n)
and the source, target, and auxiliary pegs. It pushes subproblems onto the stack in reverse order to ensure the correct order of movements.The recursive approach to solving the Tower of Hanoi problem involves breaking down the problem into smaller subproblems. Here’s a step-by-step explanation of the recursive algorithm:
n-1
disks from the source peg to the auxiliary peg, using the target peg as the temporary peg.n-1
disks from the auxiliary peg to the target peg, using the source peg as the temporary peg.
This recursive process continues until all the disks are moved from the source peg to the target peg, following the rules of the Tower of Hanoi puzzle.The recursive solution is concise and elegant as it relies on the recursive nature of the problem.
def towerOfHanoi(n, source, target, auxiliary):if n == 1:print(f"Move disk 1 from {source} to {target}")else:towerOfHanoi(n - 1, source, auxiliary, target)print(f"Move disk {n} from {source} to {target}")towerOfHanoi(n - 1, auxiliary, target, source)# Example usagen = 3 # Number of diskssource = "A" # Source pegtarget = "C" # Target pegauxiliary = "B" # Auxiliary pegtowerOfHanoi(n, source, target, auxiliary)
Free Resources