Slide 1

Slide 1 text

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

Slide 17

Slide 17 text

Sierpinski’s Triangle

Slide 18

Slide 18 text

Sierpinski’s Triangle

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

“I eat recursion for breakfast!”

Slide 23

Slide 23 text

PHP Hypertext Preprocessor

Slide 24

Slide 24 text

PHP Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor

Slide 25

Slide 25 text

PHP Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor Hypertext Preprocessor

Slide 26

Slide 26 text

So… how about some code! (well, a little math first, actually)

Slide 27

Slide 27 text

Factorial — n!   20!  =  20  ×  19  ×  18  …  ×  2  ×  1   6!  =  720   5!  =  120   4!  =    24   3!  =      6  

Slide 28

Slide 28 text

Factorial — n!   20!  =  20  ×  19  ×  18  …  ×  2  ×  1  

Slide 29

Slide 29 text

Factorial — n!   20!  =  20  ×  19!  

Slide 30

Slide 30 text

Factorial — n!   20!  =  20  ×  19!   Hey look! Recursion!

Slide 31

Slide 31 text

Factorial — n!   n!  =  n  ×  (n-­‐1)!  

Slide 32

Slide 32 text

Factorial — n! n  ×      (n-­‐1)!  

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

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

Slide 46

Slide 46 text

Not Impressed with Recursion?

Slide 47

Slide 47 text

How about another example?

Slide 48

Slide 48 text

Tree Traversal ABCDEFGH

Slide 49

Slide 49 text

Tree Traversal

Slide 50

Slide 50 text

Traversal: Base Case

Slide 51

Slide 51 text

Traversal: Recursive Call

Slide 52

Slide 52 text

Traversal: Reduce Size

Slide 53

Slide 53 text

OK, but can’t you do that without recursion?

Slide 54

Slide 54 text

Iterative Tree Traversal

Slide 55

Slide 55 text

Iteration vs. Recursion Iterative Traversal Benchmark: ~54  µs   Recursive Traversal Benchmark: ~39  µs   w00t!  FTW!  

Slide 56

Slide 56 text

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

Slide 63

Slide 63 text

Let's Use "Recursed"

Slide 64

Slide 64 text

Recursive Methods in Your Phavorite Projects •  ZF2: 50+ •  Symfony: 75+ •  Laravel 4: 8+ •  Guzzle 3: 8+ •  Wordpress: 100+ •  Drupal 8: 25+ •  Joomla: 50+ •  Monolog: 2 •  Twig: 10+ •  SwiftMailer: 7+ •  PHPParser: 10+ •  AWS PHP SDK: 1+ •  Doctrine: 7+ •  CodeIgniter: 35+

Slide 65

Slide 65 text

Example 1 – WordPress

Slide 66

Slide 66 text

Example 2 – ZF2 Mail

Slide 67

Slide 67 text

Example 3 – Laravel Container

Slide 68

Slide 68 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 69

Slide 69 text

Recursion Making Big Problems Smaller By Jeremy Lindblom @jeremeamia https://joind.in/10803