Recursion
Making Big Problems Smaller
By Jeremy Lindblom
@jeremeamia
Slide 2
Slide 2 text
– some sarcastic jerkface
“
“
To understand recursion,
you must understand recursion.
Slide 3
Slide 3 text
Why Should You Care?
1. You’re Here! – You’re already here, so
I’m guessing you are interested.
2. Academics – Recursion is a key
concept in computer science and
mathematics.
3. Career – Technical interviews often
contain questions where recursive
solutions are a good answer.
Slide 4
Slide 4 text
Why Should You Care?
4. Open Source – A survey of 14 major
PHP projects yielded more than 300
examples of recursive functions.
5. Other Languages – Some languages in
the functional programming paradigm
rely solely on recursion for loops.
6. Parallelization – Recursive functions
are stateless and can be parallelized
easier.
Slide 5
Slide 5 text
Takeaways
① Using recursion (when appropriate)
can result in shorter, simpler code.
② Recursion allows you to solve the
whole problem, by only solving part of
the problem.
Recursion: Making Big Problems Smaller
Slide 6
Slide 6 text
Hello! I'm @jeremeamia
PHP Software Engineer at
Co-author of the AWS SDK for PHP
Co-organizer of the Seattle PHP User Group
Zend Education Advisory Board for 5.5 exam
Slide 7
Slide 7 text
What exactly is recursion?
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
O RLY? K, THX!
Slide 10
Slide 10 text
O RLY? K, THX!
And yes, I do see what you did there.
Slide 11
Slide 11 text
Recursion is easier
to visualize, IMO.
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
Fractals
Images
created with
math and
recursion
Mandelbrot
Recursive Function Anatomy
• Base Case(s)
• Recursive Call(s)
• Problem Size
Reduction
Slide 36
Slide 36 text
Anatomy: Base Case
Slide 37
Slide 37 text
Anatomy: Recursive Call
Slide 38
Slide 38 text
Anatomy: Reduce Problem Size
Slide 39
Slide 39 text
OK, but can’t you do that
without recursion?
Slide 40
Slide 40 text
Anything you can do with
recursion, you can also do
without recursion.
FYI
Slide 41
Slide 41 text
Iterative Factorial
Slide 42
Slide 42 text
Iterative solutions typically
are faster and require
less memory than recursive
solutions.
FYI
Slide 43
Slide 43 text
Iteration vs. Recursion
Iterative Factorial
Benchmark:
~4.25
µs
Result w/ large input:
INF
Slide 44
Slide 44 text
Iteration vs. Recursion
Iterative Factorial
Benchmark:
~4.25
µs
Result w/ large input:
INF
Recursive Factorial
Benchmark:
~10.50
µs
Result w/ large input:
Seg
fault:
11
Slide 45
Slide 45 text
Iteration vs. Recursion
Iterative Factorial
Benchmark:
~4.25
µs
Result w/ large input:
INF
Recursive Factorial
Benchmark:
~10.50
µs
Result w/ large input:
Seg
fault:
11
Yep, that's a stack overflow
Making Big Problems Smaller
① Using recursion (when appropriate)
can result in shorter, simpler code.
(that is sometimes faster, too)
vs.
Iterative (20 LOC) Recursive (9 LOC)
Slide 57
Slide 57 text
Making Big Problems Smaller
② Recursion allows you to solve the
whole problem, by only solving part of
the problem.
This logic only prints 1 node!
Slide 58
Slide 58 text
Making Big Problems Smaller
② Recursion allows you to solve the
whole problem, by only solving part of
the problem.
=
Slide 59
Slide 59 text
Recursion is…
The Adjustable
Crescent Wrench.
No matter what the
problem (nut/bolt)
size is, you perform
the same action.
Slide 60
Slide 60 text
Alright, I'm impressed now.
Recursion is pretty cool,
but…
Slide 61
Slide 61 text
Alright, I'm impressed now.
Recursion is pretty cool,
but…
Do you have any
practical examples?
Slide 62
Slide 62 text
General Examples
• Mathematical and geometric Series
• Tree/graph traversal
– DOM/XML/array traversing/building
– Directory structure traversing (-r)
– Parsing computer languages
– Resolving dependency graphs (DIC)
• “Divide-and-conquer” algorithms
Takeaways
① Using recursion (when appropriate)
can result in shorter, simpler code.
② Recursion allows you to solve the
whole problem, by only solving part of
the problem.
Recursion: Making Big Problems Smaller
Slide 69
Slide 69 text
Recursion
Making Big Problems Smaller
By Jeremy Lindblom
@jeremeamia
https://joind.in/10803