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 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?
Recursion - Example 5! = 5 * 4 * 3 * 2 * 1 Classic example is a factorial: n! 5! 5! = 5 * 4 * 3 * 2 * 1
Recursion - Example 5! = 5 * 4 * 3 * 2 * 1 NON – recursive definition But 4! = 4 * 3 * 2 * 1 5! = 5 * 4! 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 !
Recursion - Example TRIVIAL CASE 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
5! = 5 * 4! Can be solved Recursively 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 n! = n * (n-1)!
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]) this is done by testing for trivial cases (or other stopping conditions), and avoiding recursive calls in those cases.
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 Once you have a recursive description a recursive iimplementatio…
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
Recursion in NetLogo Draw a spiral from the outside in:
Recursion in NetLogo Iteratively Specify initial line length Interative Draw While (condition?), repeat Turtle draws a line Turtle turns right Reduce line length
GO TO MODEL
But we forgot something! 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!
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
GO to Code
Important Note For every recursive algorithm…… ….. There is an equivalent iterative (looping) algorithm!!
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