You are on page 1of 4

EX 4:

Implement the problem of Towers of Hanoi – a critically acclaimed mathematical


implementation with the use of Stack data structure. Implement all of its operation.

Algorithm:
1. Stack Class:
• Initialize an empty list in the __init__ method to store stack items.
• Implement methods:
• is_empty: Returns True if the stack is empty, False otherwise.
• push(item): Appends an item to the stack.
• pop: Removes and returns the last item added to the stack (if not
empty).
• peek: Returns the last item added to the stack without removing it (if
not empty).
• size: Returns the number of items in the stack.
2. Towers of Hanoi Function (towers_of_hanoi):
• Initialize three stacks (source_stack, auxiliary_stack, and target_stack) to
represent the three pegs in the Towers of Hanoi.
• Fill the source_stack with n disks initially.
• Define a helper function move_disk(start, end, start_stack, end_stack) to
move a disk from one peg to another.
• Define a recursive function hanoi_recursive(n, source, auxiliary, target,
source_stack, auxiliary_stack, target_stack):
• Base Case: If n is 0, return.
• Recursive Steps:
1. Move n-1 disks from the source peg to the auxiliary peg using
the target peg as the auxiliary peg.
2. Move the nth disk from the source peg to the target peg.
3. Move the n-1 disks from the auxiliary peg to the target peg
using the source peg as the auxiliary peg.
3. Example Usage:
• Call hanoi_recursive with n disks, source peg 'A', auxiliary peg 'B', and target
peg 'C'.
• The movement of each disk is printed during the process.
Program:
class Stack:
def __init__(self):
self.items = []

def is_empty(self):
return len(self.items) == 0

def push(self, item):


self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()

def peek(self):
if not self.is_empty():
return self.items[-1]

def size(self):
return len(self.items)

def towers_of_hanoi(n, source, auxiliary, target):


source_stack = Stack()
auxiliary_stack = Stack()
target_stack = Stack()

# Initialize source stack


for i in range(n, 0, -1):
source_stack.push(i)

def move_disk(start, end, start_stack, end_stack):


disk = start_stack.pop()
end_stack.push(disk)
print(f"Move disk {disk} from {start} to {end}")

def hanoi_recursive(n, source, auxiliary, target, source_stack, auxiliary_stack,


target_stack):
if n > 0:
# Move n-1 disks from source to auxiliary using target as the auxiliary rod
hanoi_recursive(n-1, source, target, auxiliary, source_stack, target_stack,
auxiliary_stack)

# Move the nth disk from source to target


move_disk(source, target, source_stack, target_stack)

# Move the n-1 disks from auxiliary to target using source as the auxiliary rod
hanoi_recursive(n-1, auxiliary, source, target, auxiliary_stack, source_stack,
target_stack)

hanoi_recursive(n, source, auxiliary, target, source_stack, auxiliary_stack, target_stack)

# Example usage:
towers_of_hanoi(3, 'A', 'B', 'C')

Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
>

You might also like