Presentation on theme: "New Mexico Computer Science For All"— Presentation transcript:
1 New Mexico Computer Science For All RecursionMaureen Psaila-Dombrowski
2 Recursion What is Recursion? It is a concept/method used in computer science and mathematicsRecursive problem: The problem can be described as a reduced or smaller form of the same problemSometimes the problem gets so small that the solution of the small problem is trivial can solved recursively or has a recursive implementationRecursion occurs in computer science when you use a recursive implementation….WHAT?
3 Recursion - Example 5! = 5 * 4 * 3 * 2 * 1 Classic example is a factorial:n! 5!5! = 5 * 4 * 3 * 2 * 1
4 Recursion - Example 5! = 5 * 4 * 3 * 2 * 1 NON – recursive definition But ! = 4 * 3 * 2 * 15! = 5 * 4!Recursive definitionSO… 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 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 * 1but 3! = 3 * 2 * 1 and 3! = 3 * 2! since 2! = 2 * 1but 2! = 2 * 1! since 1! = 1TRIVIAL CASE
6 5! = 5 * 4! Can be solved Recursively Recursion - ExampleSo5! = 5 * 4 * 3 * 2 * 1Or5! = 5 * 4! Can be solved RecursivelyOr in generaln! = n * n-1 * n-2 * …* 3 * 2 * 1n! = 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 resourcesFactorial Psuedocodefactorial Nif 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.
8 Recursive Programming ProsRecursive description --- simple, elegant, and easy to explain and understand.Recursive implementation --- straightforward to build and verify.ConsCan be difficult to understand at first.In some programming languages not as efficient as iterativelyCan be difficult to programcircularity infinite loop MUST PUT IN A STOPOnce you have a recursive description a recursive iimplementatio…
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 recursionWhen you have a recursive description recursive solution is the most direct solution pathUsually creating an easy to verify code is more important than creating the most efficient code
10 Recursion in NetLogoDraw a spiral from the outside in:
11 Recursion in NetLogo Iteratively Specify initial line length Interative DrawWhile (condition?), repeatTurtle draws a lineTurtle turns rightReduce line length
13 But we forgot something! Recursion in NetLogoRecursivelySpecify initial line lengthRecursive DrawTurtle draws a lineTurtle turns rightDraw a new line with a reduced lengthBut we forgot something!We Forgot The STOP!
14 Recursion in NetLogo Recursively Specify initial line length Recursive DrawIf (condition?) STOPTurtle draws a lineTurtle turns rightdraw a new live with a reduced length
16 Important NoteFor 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 problemIn most programming languages a function or procedure calling itself in the codePros: shorter, easier to understand, more elegantCons: often not as efficient as iterative and can be difficult to programWhy use recursion?The problem/program is easier to understand using recursionThe problem is easier to solve using recursion