What is
Recursion?
Hello!
I’m Esther
You can find me on Github at @mindplace
Defined in terms
of itself
Let’s see some
examples...
This sentence
has 26 letters in
it.
This sentence starts with
the word "this" and ends
with the word "word."
”
“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
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.
Recursion and
programming
“Robot, walk iteratively”
◉ Calculate the number of steps between
you and the specified wall
◉ Walk that many steps
◉ Stop
“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.
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
Example:
factorials
2! => 2 * 1
3! => 3 * 2 * 1
3! => 3 * 2!
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
?
1
2
2
6
Let’s see this
with a stack!
factorial(5)
factorial(5)
● Is 5 <= 1?
5 * factorial(4) - waiting...
factorial(5)
● Is 5 <= 1? No.
● Return value of
5 * factorial(4)
5 * factorial(4) - waiting...
factorial(5)
● Is 5 >= 1? No.
● Return value of
5 * factorial(4)
Is value of factorial(4)
known?
5 * factorial(4) - waiting...
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...
factorial(4)
● Is 4 <= 1?
5 * factorial(4) - waiting...
4 * factorial(3) ...
factorial(4)
● Is 4 <= 1? No.
And so on, until...
5 * factorial(4) - waiting...
4 * factorial(3) ...
factorial(1)
● Is 1 <= 1?
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
factorial(1)
● Is 1 <= 1? Yes!
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
factorial(1)
● Is 1 <= 1? Yes!
● Return 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
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
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
That function was factorial(2)!
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
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) ...
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
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
...and finally returns the
answer:
120
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
=> 6
=> 24
◉ 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...
◉ 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.
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
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
Thanks!
Any questions?
You can find me at @mindplace
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?”
Go forth!

Recursion

  • 1.
  • 2.
    Hello! I’m Esther You canfind me on Github at @mindplace
  • 3.
  • 4.
  • 5.
    This sentence has 26letters in it.
  • 6.
    This sentence startswith the word "this" and ends with the word "word."
  • 9.
    ” “Walking with mein 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
  • 12.
    In everyday life... Cleaningthe apartment: ◉ If this isn’t the last room in the apartment: If this room is messy, clean it. ◉ Otherwise, move to next room.
  • 13.
  • 14.
    “Robot, walk iteratively” ◉Calculate the number of steps between you and the specified wall ◉ Walk that many steps ◉ Stop
  • 15.
    “Robot, walk recursively”◉ Ifthere is a wall in front of you, stop. ◉ Otherwise, take a step forward. ◉ Go to the beginning of the instructions.
  • 16.
    Qualities of a recursivefunction ◉ 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
  • 17.
  • 18.
  • 19.
    3! => 3* 2 * 1
  • 20.
    3! => 3* 2!
  • 21.
    def factorial(n) return 1if n <= 1 n * factorial(n - 1) end
  • 23.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
    factorial(5) ● Is 5<= 1? 5 * factorial(4) - waiting...
  • 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...
  • 36.
    factorial(4) ● Is 4<= 1? 5 * factorial(4) - waiting... 4 * factorial(3) ...
  • 37.
    factorial(4) ● Is 4<= 1? No. And so on, until... 5 * factorial(4) - waiting... 4 * factorial(3) ...
  • 38.
    factorial(1) ● Is 1<= 1? 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 39.
    factorial(1) ● Is 1<= 1? Yes! 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 40.
    factorial(1) ● Is 1<= 1? Yes! ● Return 1 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 41.
    Now we knowvalue 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 knowvalue 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 wasfactorial(2)! 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1
  • 44.
    That function wasfactorial(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 returnedto 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 backof 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
  • 47.
    ...and finally returnsthe answer: 120 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1 5 * factorial(4) - waiting... 4 * factorial(3) ... => 2 => 6 => 24
  • 48.
    ◉ Sudoku boardsolver: 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
  • 52.
    Thanks! Any questions? You canfind me at @mindplace
  • 53.
    Credits ◉ Presentation templateby 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?”
  • 54.