Recursion is defined as a function that calls itself. It solves a larger problem by breaking it down into smaller instances of the same problem. A recursive function must have a base case, where it stops calling itself, and it builds solutions up the call stack until the base case is reached. Examples of recursive functions include calculating factorials, Fibonacci sequences, and tree/graph traversals. While powerful, recursion risks stack overflow errors if not implemented properly.
9. ”
“Walking with me in the middle of town
he suddenly remarked to me, “Daddy,
not every boat has a lifeboat, has it?” I
said "How come?” “Well, the lifeboat
could have a smaller lifeboat, but then
that would be without one.”
- Edsger W. Dijkstra
10.
11.
12. In everyday life...
Cleaning the apartment:
◉ If this isn’t the last room in the apartment:
If this room is messy, clean it.
◉ Otherwise, move to next room.
14. “Robot, walk iteratively”
◉ Calculate the number of steps between
you and the specified wall
◉ Walk that many steps
◉ Stop
15. “Robot, walk
recursively”◉ If there is a wall in front of you, stop.
◉ Otherwise, take a step forward.
◉ Go to the beginning of the instructions.
16. Qualities of a
recursive function
◉ Solves a big problem by solving smaller parts in
the same way
◉ Has a base case
◉ Calls itself
◉ Executes itself by adding those calls to the stack
33. factorial(5)
● Is 5 <= 1? No.
● Return value of
5 * factorial(4)
5 * factorial(4) - waiting...
34. factorial(5)
● Is 5 >= 1? No.
● Return value of
5 * factorial(4)
Is value of factorial(4)
known?
5 * factorial(4) - waiting...
35. factorial(5)
● Is 5 >= 1? No.
● Return value of
5 * factorial(4)
Is value of factorial(4)
known? No.
Wait until value is found...
5 * factorial(4) - waiting...
41. Now we know value of
factorial(1), can return
2 * factorial(1)
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
42. Now we know value of
factorial(1), can return
2 * factorial(1)
This value gets returned to the
function that called it in the
first place...
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
43. That function was factorial(2)!
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
44. That function was factorial(2)!
Now that factorial(2) gets the
value of factorial(1), it can
return the value of
2 * factorial(1)...5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
45. Which gets returned to
factorial(3), the function that
called it.
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
46. This bubbling back of values
continues until the original
general case function gets
back the value it’s waiting for,
value of factorial(4)...
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
48. ◉ Sudoku board solver: having to make a guess, then
another guess inside that guess case…
◉ Tree problems
◉ “...the solution to a problem depends on solutions to
smaller instances of the same problem”
Lots of problems can
be solved
recursively...
49. ◉ Stack overflow
◉ Web development: waiting on a stack of calls
to resolve can create a slow user experience
◉ Can ‘feel’ complicated
… but… recursion isn’t
always the best choice.
50. Classic problems:
Factorial
3! => 3 * 2! ...
Fibonacci sequence
Each item is the sum
of the previous two
items
Sorting algorithms
Merge sort, quicksort
Palindrome
rotor == rotor
51. Recap:
a recursive function...
◉ Solves the big problem by solving small parts
◉ Has a base case
◉ Calls itself
◉ Executes itself by adding those calls to the stack
53. Credits
◉ Presentation template by SlidesCarnival
◉ Plant illustrations from Köhler's Medizinal-Pflanzen in naturgetreuen at BHL
◉ Quora, “How should I explain recursion to a 4-year-old?”
◉ StackOverflow!
◉ YouTube, Computerphile: “What on Earth is Recursion?”