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.

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

January 09, 2014
Tweet

Transcript

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

  2. – some sarcastic jerkface “ “ To understand recursion, you

    must understand recursion.
  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.
  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.
  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
  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
  7. What exactly is recursion?

  8. None
  9. O RLY? K, THX!

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

    you did there.
  11. Recursion is easier to visualize, IMO.

  12. None
  13. None
  14. None
  15. None
  16. Fractals Images created with math and recursion Mandelbrot

  17. Sierpinski’s Triangle

  18. Sierpinski’s Triangle

  19. None
  20. None
  21. None
  22. “I eat recursion for breakfast!”

  23. PHP Hypertext Preprocessor

  24. PHP Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor

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

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

    actually)
  27. Factorial — n!   20!  =  20  ×  19  ×

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

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

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

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

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

  33. None
  34. None
  35. Recursive Function Anatomy •  Base Case(s) •  Recursive Call(s) • 

    Problem Size Reduction
  36. Anatomy: Base Case

  37. Anatomy: Recursive Call

  38. Anatomy: Reduce Problem Size

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

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

    without recursion. FYI
  41. Iterative Factorial

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

    recursive solutions. FYI
  43. Iteration vs. Recursion Iterative Factorial Benchmark: ~4.25  µs   Result

    w/ large input: INF  
  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  
  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
  46. Not Impressed with Recursion?

  47. How about another example?

  48. Tree Traversal ABCDEFGH

  49. Tree Traversal

  50. Traversal: Base Case

  51. Traversal: Recursive Call

  52. Traversal: Reduce Size

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

  54. Iterative Tree Traversal

  55. Iteration vs. Recursion Iterative Traversal Benchmark: ~54  µs   Recursive

    Traversal Benchmark: ~39  µs   w00t!  FTW!  
  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)
  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!
  58. Making Big Problems Smaller ② Recursion allows you to solve the

    whole problem, by only solving part of the problem. =
  59. Recursion is… The Adjustable Crescent Wrench. No matter what the

    problem (nut/bolt) size is, you perform the same action.
  60. Alright, I'm impressed now. Recursion is pretty cool, but…

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

    you have any practical examples?
  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
  63. Let's Use "Recursed"

  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+
  65. Example 1 – WordPress

  66. Example 2 – ZF2 Mail

  67. Example 3 – Laravel Container

  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
  69. Recursion Making Big Problems Smaller By Jeremy Lindblom @jeremeamia https://joind.in/10803