Introduction to Recursion
July 5th, 2018
Announcements
https://snap.berkeley.edu/snapsource/snap.html#present:Username=jkpvecino&ProjectName=LEC%20-%20Vee%20%2B%20Downup
https://snap.berkeley.edu/snapsource/snap.html#present:Username=jkpvecino&ProjectName=LEC%20-%20Vee%20%2B%20Downup
Definition of Recursion
An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.
Recursive solutions involve 2 major parts:
Downup Breakdown
Divide: There’s the first and last word, and the middle words.
Invoke: call downup on all but the last letter of the input word.
Combine: Join: the word, the downup of the smaller input word, and the word.
Base
case
Recur-
sive
case
You already know recursion!
Drawing Hands (1948), M.C. Escher
Row Count Demo
Row Count in Snap!
Revisiting Recursion
An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.
Recursive solutions involve 2 major parts:
Row Count in Snap! - complete breakdown
Base
case
Recur-
sive
case
Divide: There’s the person name, and there’s the rest of the row behind them.
Invoke: call row count on the rest of the row.
Combine: Add 1 for the person name to the solution to “how many are behind them”.
Familiar Recursion
thanks to shutterstock
Problem: Make a tree
Base case: Size is really small? Make a leaf instead, or don’t make anything
Recursive case: Make some wood (trunk), and make two smaller trees to the left and right
What Do You Think?
Practice Makes Perfect
Recursion is no more powerful than iteration! Often leads to cleaner and better code though.
Base case: simple, immediately solvable version of the problem.
Recursive case:
“Trust the recursion!”
Review
Citations
This presentation was modified from earlier versions created by Dan Garcia, Steven Traversi, and Yifat Amir.