$30 off During Our Annual Pro Sale. View Details »

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. 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.
  2. 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.
  3. 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
  4. 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
  5. PHP Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor PHP Hypertext

    Preprocessor Hypertext Preprocessor Hypertext Preprocessor
  6. Factorial — n!   20!  =  20  ×  19  ×

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

     18  …  ×  2  ×  1  
  8. 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  
  9. 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
  10. Iteration vs. Recursion Iterative Traversal Benchmark: ~54  µs   Recursive

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

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

    problem (nut/bolt) size is, you perform the same action.
  15. 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
  16. 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+
  17. 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