Announcements (#1)
Come to office hours! (Not just for this class but your other classes, too)
Announcements (#2)
The algorithmic complexity lecture was pretty intense!
Announcements (#3)
Road map of the coming two weeks:
Tricky lectures.
Hard lab.
Hard project.
Tricky lecture.
Hard lab.
Hard lab.
P.S. Let’s do a website tour.
CS10: The Beauty and Joy of Computing - Fall 2021
Lecture 9: Recursion I
Puzzle
What does the ulgometh command do?
If you’re not sure where to start consider:
Puzzle #2
How would we modify ulgometh so that it stops at 0?
Recursion
Recursive (adjective): A procedure is called recursive if the body of the procedure calls the procedure itself.
Recursion is just the noun version of recursive.
Recursion
Alternate approach on the right.
Recursion
In its most boring form, recursion is simply an alternative to looping (a.k.a. iteration).
However, it is also a profoundly powerful way of thinking!
List of algorithmic elements: https://docs.google.com/presentation/d/1Xmm6iYL7MtIQIJ1JXHqDy8kdMVAejViRfH5swfLbNJ4/edit#slide=id.gebdf3f01da_0_196
We’ve Used Recursion Before
Used this idea implicitly in the way we described selection sort.
7 | 3 | 1 | 8 | 4 | 2 |
To selection sort this list...
1 | 7 | 3 | 8 | 4 | 2 |
Move 1 to the front:
7 | 3 | 1 | 8 | 4 | 2 |
Then selection sort this list...
Solving Problems Using Recursion
List Concatenation
Consider a command “add everything in ___ to ___”:
Would map, combine, or keep be useful for this task?�
List Concatenation
Consider a command “add everything in ___ to ___”:
Would map, combine, or keep be useful for this task?
We could do this with a loop, but let’s use recursion.�
Examples of Recursion
Implement the “add everything in __ to __” command using the 8 blocks below.
Solution Debrief
Deconstructing Josh’s Solution
Like almost all recursive code, solution is divided into two pieces:
Treating a recursive call as a usable abstraction is often called a “Recursive Leap of Faith”
Alternate Implementation
An arguably simpler version is shown below.
Acts like a “report” in the sense that code stops running.
If you use this approach make sure to use “stop this block”, not “stop this script”.
Another Example: Downup (time permitting)
Let’s implement a recursive reporter.
Another Example: Downup (Solution)
We’ll get a lot more practice with recursive reporters during next week’s Monday lecture and the recursive reporter lab.
This way of programming is very challenging to learn.
The Towers of Hanoi
One Last Example: The Towers of Hanoi
Sometimes the recursive leap of faith feels like magic.
“In the great temple at Benares beneath the dome which marks the center of the world, rests a brass plate in which are fixed three diamond needles, each a cubit high and as thick as the body of a bee. On one of these needles, at the creation, God placed sixty-four disks of pure gold, the largest disk resting on the brass plate and the others getting smaller and smaller up to the top one. This is the Tower of Brahma. Day and night unceasingly, the priests transfer the disks from one diamond needle to another according to the fixed and immutable laws of Brahma, which require that the priest on duty must not move more than one disk at a time and that he must place this disk on a needle so that there is no smaller disk below it. When all the sixty-four disks shall have been thus transferred from the needle on which at the creation God placed them to one of the other needles, tower, temple and Brahmins alike will crumble into dust, and with a thunderclap the world will vanish. “ (Henri de Parville, 1883 (see Orientalism))
Problem and Animated Solution (watch later if you want)
Basic idea:
Let’s try this out interactively: http://towersofhanoi.info/Play.aspx
Solving the Towers of Hanoi
There is a very simple recursive solution.
Solving the Towers of Hanoi (Recursive Solution)
To Move N Disks from source to destination:
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
Key tricky point: For moving 4 disks, we’ll use the ‘source’ as the temp, and the ‘temp’ as the source.
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
See extra slides for a slightly more thorough animation.
Test Your Understanding
Suppose we had a block called:
If we called this block with N = 3, s = LEFT, d = MIDDLE, temp = RIGHT, what would happen?
Another Way of Putting It...
Suppose we had a block called:
If we called this block with N = 3, s = LEFT, d = MIDDLE, temp = RIGHT, what would happen?
Another Way of Putting It...
Suppose we had a block called:
If we called this block with N = 2, s = MIDDLE, d = LEFT, temp = RIGHT, what would happen?
Another Way of Putting It...
Suppose we had a block called:
If we called this block with N = 2, s = MIDDLE, d = LEFT, temp = RIGHT, what would happen?
Another Way of Putting It...
Suppose we had a block called:
If we wanted to solve the puzzle using the block above, what would we pick for N, s, d, and temp?
Another Way of Putting It...
Suppose we had a block called:
If we wanted to solve the puzzle using the block above, what would we pick for N, s, d, and temp?
Let’s Try This Out in Snap!
This is going to hurt your brain.
https://snap.berkeley.edu/project?user=joshhug&project=cs10_fa21_lec9_hanoi
Again: I do not expect you to deeply understand the 4 line solution.
Let’s Try This Out in Snap!
This is going to hurt your brain.
Starter code:
https://snap.berkeley.edu/project?user=joshhug&project=cs10_fa21_lec9_hanoi
Solution:
https://snap.berkeley.edu/project?user=joshhug&project=cs10_fa21_lec9_hanoi_complete
Closing Thoughts
Recursion adds no additional power to Snap! language.
When working on labs from here on out: Use scratch paper.
Extra Slides: More Verbose Animation of Towers of Hanoi
Solving the Towers of Hanoi
That animation was probably daunting, but the recursive solution is actually very simple.
Solving the Towers of Hanoi (Recursive Solution)
To Move N Disks from source to destination:
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
For the move top 4 disks call, s = LEFT, d = RIGHT, temp = MIDDLE
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 4 Disks from source to destination:
Source
temp
Destination
For the move top 3 disks call, s = LEFT, d = MIDDLE, temp = RIGHT
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 4 Disks from source to destination:
Source
temp
Destination
etc
For the move top 3 disks call, s = LEFT, d = MIDDLE, temp = RIGHT
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 4 Disks from source to destination:
Source
temp
Destination
For the move top 3 disks call, s = LEFT, d = MIDDLE, temp = RIGHT
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 4 Disks from source to destination:
Source
temp
Destination
Now back to moving 4 disks.
For the move top 3 disks call, s = LEFT, d = MIDDLE, temp = RIGHT
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
Key tricky point: For moving 4 disks, we’ll use the ‘source’ as the temp, and the ‘temp’ as the source.
For the move top 4 disks call, s = LEFT, d = RIGHT, temp = MIDDLE
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
For the move top 4 disks call, s = LEFT, d = RIGHT, temp = MIDDLE
Solving the Towers of Hanoi (Recursive Solution Demo).
To Move 5 Disks from source to destination:
Source
Destination
temp
For the move top 4 disks call, s = LEFT, d = RIGHT, temp = MIDDLE