1 / 17

New Mexico Computer Science For All

New Mexico Computer Science For All. Recursion Maureen Psaila-Dombrowski. Recursion . What is Recursion? It is a concept/method used in computer science and mathematics Recursive problem: The problem can be described as a reduced or smaller form of the same problem

russ
Download Presentation

New Mexico Computer Science For All

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. New Mexico Computer Science For All Recursion Maureen Psaila-Dombrowski

  2. Recursion • What is Recursion? • It is a concept/method used in computer science and mathematics • Recursive problem: The problem can be described as a reduced or smaller form of the same problem • Sometimes the problem gets so small that the solution of the small problem is trivial  can solved recursively or has a recursive implementation • Recursion occurs in computer science when you use a recursive implementation • ….WHAT?

  3. Recursion - Example • Classic example is a factorial: n!  5! 5! = 5 * 4 * 3 * 2 * 1

  4. Recursion - Example But 4! = 4 * 3 * 2 * 1 5! = 5 * 4! 5! = 5 * 4 * 3 * 2 * 1 NON – recursive definition Recursive definition SO… The problem can be broken down into a smaller or reduced version of the same problem    Maybe it can be solved recursively !

  5. Recursion - Example • Look for the smallest MOST TRIVIAL form of the problem: 5! = 5 * 4 * 3 * 2 * 1  5! = 5 * 4! but 4! = 4 * 3 * 2 * 1 and 4! = 4 * 3!  since 3! = 3 * 2 * 1 but 3! = 3 * 2 * 1 and 3! = 3 * 2!  since 2! = 2 * 1 but 2! = 2 * 1!  since 1! = 1 TRIVIAL CASE

  6. Recursion - Example • So 5! = 5 * 4 * 3 * 2 * 1 Or 5! = 5 * 4! Can be solved Recursively • Or in general n! = n * n-1 * n-2 * …* 3 * 2 * 1 Or n! = n * (n-1)!

  7. Recursion in Computer Languages • Implemented by calling a function or procedure in the body of that same function or procedure. • must be prevented from consuming excessive computing resources • Factorial Psuedocode factorial N if N <=1 (then factorial = 1) (else factorial = N * factorial[N-1])

  8. Recursive Programming • Pros • Recursive description --- simple, elegant, and easy to explain and understand. • Recursive implementation --- straightforward to build and verify. • Cons • Can be difficult to understand at first. • In some programming languages not as efficient as iteratively • Can be difficult to program • circularity  infinite loop  MUST PUT IN A STOP

  9. Why use Recursion? • So if can be less efficient and difficult to understand and program, why use recursion? • The problem/program may be easier to understand, solve and program using recursion • When you have a recursive description  recursive solution is the most direct solution path • Usually creating an easy to verify code is more important than creating the most efficient code

  10. Recursion in NetLogo • Draw a spiral from the outside in:

  11. Recursion in NetLogo • Iteratively • Specify initial line length Interative Draw • While (condition?), repeat • Turtle draws a line • Turtle turns right • Reduce line length

  12. GO TO MODEL

  13. Recursion in NetLogo • Recursively • Specify initial line length Recursive Draw • Turtle draws a line • Turtle turns right • Draw a new line with a reduced length But we forgot something! We Forgot The STOP!

  14. Recursion in NetLogo • Recursively • Specify initial line length Recursive Draw • If (condition?) STOP • Turtle draws a line • Turtle turns right • draw a new live with a reduced length

  15. GO to Code

  16. Important Note For every recursive algorithm…… ….. There is an equivalent iterative (looping) algorithm!!

  17. Summary • Recursion: CS method • The solution depends on the solution to smaller instances of the same problem • In most programming languages  a function or procedure calling itself in the code • Pros: shorter, easier to understand, more elegant • Cons: often not as efficient as iterative and can be difficult to program • Why use recursion? • The problem/program is easier to understand using recursion • The problem is easier to solve using recursion

More Related