Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Recursion: Making Big Problems Smaller

Recursion: Making Big Problems Smaller

Recursion is a key concept in programming, and can be used to simplify code and create elegant solutions. It's also has a reputation of being a little hard to learn. I will show you that it is not as difficult as you think, and that you can take a formulaic approach to writing recursive functions. Doing so will allow you to solve big problems, by dividing them up into smaller, easier ones.

Jeremy Lindblom

January 09, 2014
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. Recursion
    Making Big Problems Smaller
    By Jeremy Lindblom
    @jeremeamia

    View Slide

  2. – some sarcastic jerkface


    To understand recursion,
    you must understand recursion.

    View Slide

  3. 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.

    View Slide

  4. 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.

    View Slide

  5. 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

    View Slide

  6. 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

    View Slide

  7. What exactly is recursion?

    View Slide

  8. View Slide

  9. O RLY? K, THX!

    View Slide

  10. O RLY? K, THX!
    And yes, I do see what you did there.

    View Slide

  11. Recursion is easier
    to visualize, IMO.

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. Fractals
    Images
    created with
    math and
    recursion
    Mandelbrot

    View Slide

  17. Sierpinski’s Triangle

    View Slide

  18. Sierpinski’s Triangle

    View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. “I eat recursion for breakfast!”

    View Slide

  23. PHP Hypertext Preprocessor

    View Slide

  24. PHP Hypertext Preprocessor
    PHP Hypertext Preprocessor Hypertext Preprocessor

    View Slide

  25. PHP Hypertext Preprocessor
    PHP Hypertext Preprocessor Hypertext Preprocessor
    PHP Hypertext Preprocessor Hypertext Preprocessor Hypertext Preprocessor

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  29. Factorial — n!  
    20!  =  20  ×  19!  

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  33. View Slide

  34. View Slide

  35. Recursive Function Anatomy
    •  Base Case(s)
    •  Recursive Call(s)
    •  Problem Size
    Reduction

    View Slide

  36. Anatomy: Base Case

    View Slide

  37. Anatomy: Recursive Call

    View Slide

  38. Anatomy: Reduce Problem Size

    View Slide

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

    View Slide

  40. Anything you can do with
    recursion, you can also do
    without recursion.
    FYI

    View Slide

  41. Iterative Factorial

    View Slide

  42. Iterative solutions typically
    are faster and require
    less memory than recursive
    solutions.
    FYI

    View Slide

  43. Iteration vs. Recursion
    Iterative Factorial
    Benchmark:
    ~4.25  µs  
    Result w/ large input:
    INF  

    View Slide

  44. 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  

    View Slide

  45. 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

    View Slide

  46. Not Impressed with Recursion?

    View Slide

  47. How about another
    example?

    View Slide

  48. Tree Traversal
    ABCDEFGH

    View Slide

  49. Tree Traversal

    View Slide

  50. Traversal: Base Case

    View Slide

  51. Traversal: Recursive Call

    View Slide

  52. Traversal: Reduce Size

    View Slide

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

    View Slide

  54. Iterative Tree Traversal

    View Slide

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

    View Slide

  56. 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)

    View Slide

  57. 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!

    View Slide

  58. Making Big Problems Smaller
    ② Recursion allows you to solve the
    whole problem, by only solving part of
    the problem.
    =

    View Slide

  59. Recursion is…
    The Adjustable
    Crescent Wrench.
    No matter what the
    problem (nut/bolt)
    size is, you perform
    the same action.

    View Slide

  60. Alright, I'm impressed now.
    Recursion is pretty cool,
    but…

    View Slide

  61. Alright, I'm impressed now.
    Recursion is pretty cool,
    but…
    Do you have any
    practical examples?

    View Slide

  62. 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

    View Slide

  63. Let's Use "Recursed"

    View Slide

  64. 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+

    View Slide

  65. Example 1 – WordPress

    View Slide

  66. Example 2 – ZF2 Mail

    View Slide

  67. Example 3 – Laravel Container

    View Slide

  68. 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

    View Slide

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

    View Slide