Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] - - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 15 Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Prelim 1 Graded and released Mean : 81 out of 104 (78%) Can pick up your exam in homework


slide-1
SLIDE 1

Recursion

Lecture 15

CS 1110:

Introduction to Computing Using Python

[Andersen, Gries, Lee, Marschner, Van Loan, White]

slide-2
SLIDE 2

Announcements: Prelim 1

  • Graded and released
  • Mean: 81 out of 104 (78%)
  • Can pick up your exam in homework handback room
  • Need Cornell ID
  • Suggest printing your netid on paper
  • Do not discuss exam with people taking makeups.
  • Regrade requests: we will send email to you

10/13/16 Recursion 2

slide-3
SLIDE 3

Announcements: Assignment 3

  • Released.
  • Due: Thursday, March 30th, 11:59pm
  • Recommendation: follow milestone deadlines.
  • You MUST acknowledge help from others
  • We run software analyzers to detect similar programs
  • Have had some academic integrity violations so far
  • Not a recursion assignment!

10/13/16 Recursion 3

slide-4
SLIDE 4

Announcement: Lab 8

  • Out.
  • Not a recursion lab!

10/13/16 Recursion 4

slide-5
SLIDE 5

Recursion

  • Recursive Definition:

A definition that is defined in terms of itself

10/13/16 Recursion 5

slide-6
SLIDE 6

A Mathematical Example: Factorial

  • Non-recursive definition:

n! = n × n-1 × … × 2 × 1 = n (n-1 × … × 2 × 1)

  • Recursive definition:

n! = n (n-1)! 0! = 1

10/13/16 Recursion 6

for n ≥ 0 Recursive case Base case What happens if there is no base case?

slide-7
SLIDE 7

Recursion

  • Recursive Definition:

A definition that is defined in terms of itself

  • Recursive Function:

A function that calls itself (directly or indirectly)

10/13/16 Recursion 7

slide-8
SLIDE 8

Recursive Call Frames

10/13/16 Recursion 8

factorial 1

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

1 2 3

slide-9
SLIDE 9

Recursive Call Frames

10/13/16 Recursion 9

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

1 2 3

slide-10
SLIDE 10

Recursion

10/13/16 Recursion 10

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

1 2 3

Now what? Each call is a new frame.

slide-11
SLIDE 11

A:

What happens next?

10/13/16 Recursion 11

factorial 1, 3

n

3 factorial 1

n

2 factorial 1, 3, 1

n

3 2

 B:

factorial 1

n

3

C:

factorial 1, 3, 1

n

3 2

 D:

factorial 1

n

2 ERASE FRAME factorial 1, 3

n

3

CORRECT

slide-12
SLIDE 12

Recursive Call Frames

10/13/16 Recursion 12

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1

slide-13
SLIDE 13

Recursive Call Frames

10/13/16 Recursion 13

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3

slide-14
SLIDE 14

Recursive Call Frames

10/13/16 Recursion 14

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1

slide-15
SLIDE 15

Recursive Call Frames

10/13/16 Recursion 15

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1, 3

slide-16
SLIDE 16

Recursive Call Frames

10/13/16 Recursion 16

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1, 3 factorial

n

1

slide-17
SLIDE 17

Recursive Call Frames

10/13/16 Recursion 17

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1, 3 factorial

n

1, 2

slide-18
SLIDE 18

Recursive Call Frames

10/13/16 Recursion 18

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1, 3 factorial

n

RETURN 1 1, 2

slide-19
SLIDE 19

Recursive Call Frames

10/13/16 Recursion 19

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 1, 3 factorial

n

RETURN 1 1, 2

slide-20
SLIDE 20

Recursive Call Frames

10/13/16 Recursion 20

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2

slide-21
SLIDE 21

Recursive Call Frames

10/13/16 Recursion 21

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2

1 2 3

1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2

slide-22
SLIDE 22

Recursive Call Frames

10/13/16 Recursion 22

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2 RETURN

1 2 3

2 1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2

slide-23
SLIDE 23

Recursive Call Frames

10/13/16 Recursion 23

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

factorial

n

2 RETURN

1 2 3

2 1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2

slide-24
SLIDE 24

Recursive Call Frames

10/13/16 Recursion 24

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

RETURN factorial

n

2 RETURN

1 2 3

2 1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2 6

slide-25
SLIDE 25

Recursive Call Frames

10/13/16 Recursion 25

factorial 1, 3

n

3

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1) Call: factorial(3)

RETURN factorial

n

2 RETURN

1 2 3

2 1, 3 factorial

n

1 RETURN 1 1, 3 factorial

n

RETURN 1 1, 2 6

slide-26
SLIDE 26

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

  • Get the next number by adding previous two
  • What is a8?

10/13/16 Recursion 26

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these.

slide-27
SLIDE 27

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

  • Get the next number by adding previous two
  • What is a8?

10/13/16 Recursion 27

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these. correct

slide-28
SLIDE 28

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

  • Get the next number by adding previous two
  • What is a8?
  • Recursive definition:
  • an = an-1 + an-2

Recursive Case

  • a0 = 1

Base Case

  • a1 = 1

(another) Base Case

10/13/16 Recursion 28

Why did we need two base cases this time?

slide-29
SLIDE 29

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

10/13/16 Recursion 29

Recursive case Base case(s) Handles both base cases in one conditional.

slide-30
SLIDE 30

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

10/13/16 Recursion 30

n fibonacci 3 5 n fibonacci 1 4 n fibonacci 1 3

slide-31
SLIDE 31

Recursion vs Iteration

  • Recursion is provably equivalent to iteration
  • Iteration includes for-loop and while-loop (later)
  • Anything can do in one, can do in the other
  • But some things are easier with recursion
  • And some things are easier with iteration
  • Will not teach you when to choose recursion
  • We just want you to understand the technique

10/13/16 Recursion 31

slide-32
SLIDE 32

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/13/16 Recursion 32

data

slide-33
SLIDE 33

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/13/16 Recursion 33

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P

slide-34
SLIDE 34

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/13/16 Recursion 34

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P Combine Answer!

slide-35
SLIDE 35

Divide and Conquer Example

Count the number of 'e's in a string:

10/13/16 Recursion 35

p e n n e

Two 'e's

p e n n e

One 'e' One 'e'

slide-36
SLIDE 36

Divide and Conquer Example

Count the number of 'e's in a string:

10/13/16 Recursion 36

p e n n e

Two 'e's

p e n n e

Zero 'e's Two 'e's

slide-37
SLIDE 37

Three Steps for Divide and Conquer

  • 1. Decide what to do on “small” data
  • Some data cannot be broken up
  • Have to compute this answer directly
  • 2. Decide how to break up your data
  • Both “halves” should be smaller than whole
  • Often no wrong way to do this (next lecture)
  • 3. Decide how to combine your answers
  • Assume the smaller answers are correct
  • Combining them should give bigger answer

10/13/16 Recursion 37

slide-38
SLIDE 38

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/13/16 Recursion 38

p e n n e

2 +

s[0] s[1:]

slide-39
SLIDE 39

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/13/16 Recursion 39

p e n n e

2 +

s[0] s[1:]

slide-40
SLIDE 40

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/13/16 Recursion 40

p e n n e

2 +

s[0] s[1:]

slide-41
SLIDE 41

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/13/16 Recursion 41

p e n n e

2 +

s[0] s[1:]

slide-42
SLIDE 42

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

10/13/16 Recursion 42

Base Case Recursive Case

slide-43
SLIDE 43

Exercise: Remove Blanks from a String

def deblank(s): """Returns: s but with its blanks removed"""

  • 1. Decide what to do on “small” data
  • If it is the empty string, nothing to do

if s == '': return s

  • If it is a single character, delete it if a blank

if s == ' ': # There is a space here return '' # Empty string else: return s

10/13/16 Recursion 43

slide-44
SLIDE 44

Exercise: Remove Blanks from a String

def deblank(s): """Returns: s but with its blanks removed"""

  • 2. Decide how to break it up

left = deblank(s[0]) # A string with no blanks right = deblank(s[1:]) # A string with no blanks

  • 3. Decide how to combine the answer

return left+right # String concatenation

10/13/16 Recursion 44

slide-45
SLIDE 45

Putting it All Together

def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: return '' if s[0] == ' ' else s left = deblank(s[0]) right = deblank(s[1:]) return left+right

10/13/16 Recursion 45

Handle small data Break up the data Combine answers

slide-46
SLIDE 46

Putting it All Together

def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: return '' if s[0] == ' ' else s left = deblank(s[0]) right = deblank(s[1:]) return left+right

10/13/16 Recursion 46

Base Case Recursive Case

slide-47
SLIDE 47

Following the Recursion

a b c deblank

10/13/16 Recursion 47

slide-48
SLIDE 48

Following the Recursion

a b c deblank

10/13/16 Recursion 48

deblank a b c deblank stops (base case) a deblank stops (base case) b c deblank …

slide-49
SLIDE 49

Following the Recursion

a b c deblank a b c deblank

10/13/16 Recursion 49

slide-50
SLIDE 50

Following the Recursion

a b c a deblank a b c deblank b c deblank

10/13/16 Recursion 50

slide-51
SLIDE 51

Following the Recursion

a b c a deblank a b c deblank b c deblank b c deblank

10/13/16 Recursion 51

slide-52
SLIDE 52

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank

10/13/16 Recursion 52

slide-53
SLIDE 53

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/13/16 Recursion 53

slide-54
SLIDE 54

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c

slide-55
SLIDE 55

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c

slide-56
SLIDE 56

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c

slide-57
SLIDE 57

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b

slide-58
SLIDE 58

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b

 

slide-59
SLIDE 59

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a

 

slide-60
SLIDE 60

Following the Recursion

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a c b a

  

slide-61
SLIDE 61

Following the Recursion

a b c a b c c c c b c b c b a c b a c b a deblank a b c deblank b c deblank b c deblank c deblank c deblank

  

slide-62
SLIDE 62

Tower of Hanoi

  • Three towers: left, middle, and right
  • n disks of unique sizes on left
  • Goal: move all disks from left to right
  • Cannot put a larger disk on top of a smaller disk

10/13/16 Recursion 62

4

left middle right

3 2 1

slide-63
SLIDE 63

1 Disc

  • 1. Move from left to right

10/13/16 Recursion 63

1

left middle right

slide-64
SLIDE 64

1 Disc

  • 1. Move from left to right

10/13/16 Recursion 64

1

left middle right

slide-65
SLIDE 65

2 Discs

  • 1. Move from left to middle

10/13/16 Recursion 65

left middle right

2 1

slide-66
SLIDE 66

2 Discs

10/13/16 Recursion 66

left middle right

2 1

  • 1. Move from left to middle
  • 2. Move from left to right
slide-67
SLIDE 67

2 Discs

10/13/16 Recursion 67

left middle right

2 1

  • 1. Move from left to middle
  • 2. Move from left to right
  • 3. Move from middle to right
slide-68
SLIDE 68

2 Discs

10/13/16 Recursion 68

left middle right

2 1

  • 1. Move from left to middle
  • 2. Move from left to right
  • 3. Move from middle to right
slide-69
SLIDE 69

3 Discs

  • 1. Move from left to right

10/13/16 Recursion 70

left middle right

3 2 1

slide-70
SLIDE 70

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle

10/13/16 Recursion 71

left middle right

3 2 1

slide-71
SLIDE 71

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle

10/13/16 Recursion 72

left middle right

3 2 1

slide-72
SLIDE 72

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle
  • 4. Move from left to right

10/13/16 Recursion 73

left middle right

3 2 1

slide-73
SLIDE 73

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle
  • 4. Move from left to right
  • 5. Move from middle to left

10/13/16 Recursion 74

left middle right

3 2 1

slide-74
SLIDE 74

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle
  • 4. Move from left to right
  • 5. Move from middle to left
  • 6. Move from middle to right

10/13/16 Recursion 75

left middle right

3 2 1

slide-75
SLIDE 75

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle
  • 4. Move from left to right
  • 5. Move from middle to left
  • 6. Move from middle to right
  • 7. Move from left to right

10/13/16 Recursion 76

left middle right

3 2 1

slide-76
SLIDE 76

3 Discs

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle
  • 4. Move from left to right
  • 5. Move from middle to left
  • 6. Move from middle to right
  • 7. Move from left to right

10/13/16 Recursion 77

left middle right

3 2 1

slide-77
SLIDE 77

4

4 Discs: High-level Idea

10/13/16 Recursion 78

left middle right

3 2 1

slide-78
SLIDE 78

4

4 Discs: High-level Idea

10/13/16 Recursion 79

left middle right

3 2 1

  • Plan: move top three disks

from left to middle

slide-79
SLIDE 79

4

4 Discs: High-level Idea

10/13/16 Recursion 80

left middle right

3 2 1

  • Plan: move top three disks

from left to middle

  • Move: largest disk from left

to right

slide-80
SLIDE 80

4

4 Discs: High-level Idea

10/13/16 Recursion 81

left middle right

3 2 1

  • Plan: move top three disks

from left to middle

  • Move: largest disk from left

to right

  • Plan: move top three disks

from middle to right

slide-81
SLIDE 81

4

4 Discs: High-level Idea

10/13/16 Recursion 82

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

slide-82
SLIDE 82

4

4 Discs: High-level Idea

10/13/16 Recursion 83

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

slide-83
SLIDE 83

4

4 Discs: High-level Idea

10/13/16 Recursion 84

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
slide-84
SLIDE 84

4

4 Discs: High-level Idea

10/13/16 Recursion 85

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

slide-85
SLIDE 85

4

4 Discs: High-level Idea

10/13/16 Recursion 86

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

slide-86
SLIDE 86

4

4 Discs: High-level Idea

10/13/16 87

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

  • Move: disk 4 from left to

right

Recursion

slide-87
SLIDE 87

4

4 Discs: High-level Idea

10/13/16 88

left middle right

3 2 1

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

  • Move: disk 4 from left to

right

  • Plan: move disks 1, 2, and 3

from middle to right

slide-88
SLIDE 88

4 Discs: High-level Idea

10/13/16 89

left middle

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

  • Move: disk 4 from left to

right

  • Plan: move disks 1, 2, and 3

from middle to right

4

right

3 2 1

slide-89
SLIDE 89

Observation: Plans within a Plan

10/13/16 90

  • Plan: move disks 1, 2, and 3

from left to middle

  • Plan: move disks 1 and 2 from

left to right

  • Move: disk 3 from left to right
  • Plan: move disks 1 and 2 from

right to middle

  • Move: disk 4 from left to

right

  • Plan: move disks 1, 2, and 3

from middle to right High-level plan Low-level plan

slide-90
SLIDE 90

General Pattern

To move n disks from source to target:

(source, other, and target can be any permutation of left, middle and right)

1. Plan: move disks 1, …, n-1 from source to other 2. Move: disk n to from source to target 3. Plan: move disks 1, …, n-1 from other to target

10/13/16 Recursion 91

4 3 2 1

disks 1, …, n-1 disk n source

  • ther

target

  • ther disks?