0% found this document useful (0 votes)
4 views

Chapter_6_Recursion_Example

Chapter 6 discusses the Tower of Hanoi, a mathematical game involving three rods and n disks that must be moved from one rod to another under specific constraints. The chapter outlines recursive strategies to solve the problem for varying numbers of disks, illustrating the base case and recursive steps necessary for solving the puzzle. It concludes with a code example demonstrating the recursive function to move the disks effectively.

Uploaded by

Angel Wong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter_6_Recursion_Example

Chapter 6 discusses the Tower of Hanoi, a mathematical game involving three rods and n disks that must be moved from one rod to another under specific constraints. The chapter outlines recursive strategies to solve the problem for varying numbers of disks, illustrating the base case and recursive steps necessary for solving the puzzle. It concludes with a code example demonstrating the recursive function to move the disks effectively.

Uploaded by

Angel Wong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 6.

Recursion
(Additional Example)

2023-2024
COMP1117A Computer Programming
Dr. T.W. Chim ([email protected]) & Dr. H.F. Ting ([email protected])
Department of Computer Science, The University of Hong Kong
Example 5. Tower of Hanoi

n=4

A B C
The Tower of Hanoi is a mathematical game.
It consists of three rods (A,B,C) and n disks of different sizes which can slide
onto any rod.
Constraint: At any stage, no larger disk can be placed on top of smaller disk.
The objective of the puzzle is to move the entire stack from rod A to rod C.
Only one disk is allowed to move at a time. 2
Example 5. Tower of Hanoi

n=1

A B C

Base case: Can you solve


the above problem
instance n=1?

3
Example 5. Tower of Hanoi

n=2

A B C

Can you solve the


above problem
instance n=2?

4
Example 5. Tower of Hanoi

n=2

A B C

Strategy to move a tower of 2 disks:


Move the top 1 disk to B. (Problem of n=1)

5
Example 5. Tower of Hanoi

n=2

A B C

Strategy to move a tower of 2 disks:


Move the top 1 disk to B. (Problem of n=1)
Move the largest disk to C. (Problem of n=1)

6
Example 5. Tower of Hanoi

n=2

A B C

Strategy to move a tower of 2 disks:


Move the top 1 disk to B. (Problem of n=1)
Move the largest disk to C. (Problem of n=1)
Move the top 1 disk back to C. (Problem of n=1)

7
Example 5. Tower of Hanoi

n=3

A B C

(Important problem solving technique)


We know how to move a tower of 2 disks
from A to C using B as buffer.
Can we reuse the solution for n=2 to help to
solve this problem where n=3?
8
Example 5. Tower of Hanoi
Problem
for n = 2
n=2

A B C

Strategy to move a tower of 3 disks:


Step1. Move the top 2 disks from A to B, using C as buffer. (Problem of n=2)

9
Example 5. Tower of Hanoi
Problem
for n = 2
n=2

A B C

Strategy to move a tower of 3 disks:


Step1. Move the top 2 disks from A to B, using C as buffer. (Problem of n=2)
Step2. Move the largest disk from A to C. (Problem of n=1)

10
Example 5. Tower of Hanoi
Problem
for n = 2
n=2

A B C

Strategy to move a tower of 3 disks:


Step1. Move the top 2 disks from A to B, using C as buffer. (Problem of n=2)
Step2. Move the largest disk from A to C. (Problem of n=1)
Step3. Move the top 2 disks from B to C, using A as buffer. (Problem of n=2)

11
Example 5. Tower of Hanoi
Problem
n=3 for n = 3

A B C

Strategy to move a tower of 4 disks:


Step1. Move the top 3 disks from A to B, using C as buffer. (Problem of n=3)

12
Example 5. Tower of Hanoi
Problem
n=3 for n = 3

A B C

Strategy to move a tower of 4 disks:


Step1. Move the top 3 disks from A to B, using C as buffer. (Problem of n=3)
Step2. Move the largest disk from A to C. (Problem of n=1)

13
Example 5. Tower of Hanoi
Problem
n=3 for n = 3

A B C

Strategy to move a tower of 4 disks:


Step1. Move the top 3 disks from A to B, using C as buffer. (Problem of n=3)
Step2. Move the largest disk from A to C. (Problem of n=1)
Step3. Move the top 3 disks from B to C, using A as buffer. (Problem of n=3)

14
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1,from, buffer, to)
move(1,from, to, buffer)
moveTower(n-1, buffer, to, from)

Base case:
Just move the disk from
the fromPole to the toPole
when height==1.
15
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end)
move(1, start, end, buffer)
move(n-1, buffer, end, start)

Progress:
Make a n-1 problem to move the tower from start to buffer.
Make a 1 problem to move the tower from start to end.
Make a n-1 problem to move the tower from buffer to end.

16
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end)
move(1, start, end, buffer)
move(n-1, buffer, end, start)

Now, let’s try to simulate what will happen


when we run move(3,"A","C","B").
move(3,"A","C","B")
To move the tower of 3 disks from A to C,
using B as buffer.
17
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case
Now we recursively call
print("Moving disk from",start,"to",end) move(2,"A","B","C") to
else: # Progress
move(n-1, start, buffer, end) move the tower with 2
move(1, start, end, buffer) disks from A to B first.
move(n-1, buffer, end, start)

move(2,"A","B","C")

move(3,"A","C","B")

18
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case move(1,"A","C","B")
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end) move(1,"A","B","C")
move(1, start, end, buffer)
move(n-1, buffer, end, start)
move(1,"C","B","A")

move(2,"A","B","C")

move(3,"A","C","B")

19
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case move(1,"A","C","B")
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end) move(1,"A","B","C")
move(1, start, end, buffer)
move(n-1, buffer, end, start)
move(1,"C","B","A")

move(2,"A","B","C")

move(1,"A","C","B")
move(3,"A","C","B")

20
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case move(1,"A","C","B")
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end) move(1,"A","B","C")
move(1, start, end, buffer)
move(n-1, buffer, end, start)
move(1,"C","B","A")

move(2,"A","B","C")

move(1,"A","C","B")
move(3,"A","C","B")

move(2,"B","C","A")
21
Example 5. Tower of Hanoi
def move(n, start, end, buffer):
if n == 1: # Base case move(1,"A","C","B")
print("Moving disk from",start,"to",end)
else: # Progress
move(n-1, start, buffer, end) move(1,"A","B","C")
move(1, start, end, buffer)
move(n-1, buffer, end, start)
move(1,"C","B","A")

move(2,"A","B","C")

move(1,"B","A","C")
move(1,"A","C","B")
move(3,"A","C","B")
move(1,"B","C","A")

move(2,"B","C","A")
move(1,"A","C","B")
22
Example 5. Tower of Hanoi
1
Now, let’s try to use these 7 steps to see if move(1,"A","C","B")
you can solve the Tower of Hanoi problem 2
with n=3? move(1,"A","B","C")
Let’s try move(4,"A","C","B") as
3
well!
move(1,"C","B","A")

move(2,"A","B","C")
5
4 move(1,"B","A","C")
move(1,"A","C","B")
move(3,"A","C","B") 6
move(1,"B","C","A")

7
move(2,"B","C","A")
move(1,"A","C","B")
23
Example 5. Tower of Hanoi

A B C

24
Chapter 6.

END
Acknowledgement:
This set of slides is partially adopted from
ENGG1330. Thanks to Dr. C.K. Chui!

2023-2024
COMP1117A Computer Programming
Dr. T.W. Chim ([email protected]) & Dr. H.F. Ting ([email protected])
Department of Computer Science, The University of Hong Kong

You might also like