Presentation is loading. Please wait.

Presentation is loading. Please wait.

IB Computer Science Unit 5 – Advanced Topics Recursion.

Similar presentations


Presentation on theme: "IB Computer Science Unit 5 – Advanced Topics Recursion."— Presentation transcript:

1 IB Computer Science Unit 5 – Advanced Topics Recursion

2 Recursion Example (Dictionary) Recursive vs. Iterative Functions Assignment Definition

3 The root of the word recursion is the word recur Recursion in computer programming defines a function in terms of itself A recursive algorithm is an algorithm that depends on a function calling itself You can imagine that this can be a tricky concept… what are some possible issues that may arise? So why bother? Recursive algorithms are powerful, elegant, sometimes difficult to understand, but more intuitive to code

4 Recursion Example Dictionary Lookup Say you’re looking up a word in a dictionary, and as you read the definition, you encounter another word that you don’t know You could put a sticky note onto a stack beside you on the desk with your Current page number, then go and look up the new word You continue with this for a while… need a new definition, follow that word, and need another definition until… While looking up the new word, you encounter yet another unknown word, Write down this page number on a sticky note, and add it to the top of the stack

5 Recursive Example Dictionary Lookup At some point a definition is read that does not require any new words to Be looked up What do you do? Go to the top of the sticky note stack, returns to the previous page number and Continues reading from there This is repeated, sequentially removing the top most note from the stack Until finally, you reach the original word… this has been a recursive approach!

6 Recursive Example Dictionary Lookup (Pseudocode) Notice how this function makes a call to itself method lookUp(word) { newWord = definitionOf(word) if(newWord == unknown) lookUp(newWord) else finishReading(word) }

7 Recursive vs. Iterative Functions A common method of structured programming is to divide a problem into smaller problems (divide and conquer), and this is the key to the design of many important algorithms Recursion works well for this approach When a function is called recursively, the computer language keeps track Of the various instances of the function by using a call stack

8 Recursive vs. Iterative Functions Lets compare two different implementations of the method for calculating the factorial of input n. A factorial is a math function used often in statistics; the factorial of a number n results when you multiply all integers from 1 up to that number A common way to implement this function is using a for loop

9 Recursive vs. Iterative Functions Factorial Iterative Function public static int factorial (int value) { int result = 1; for(int counter = 1; counter <= value; counter++) { result = result * counter; } return result; } Trace the method call factorial(5)

10 Recursive vs. Iterative Functions Creating a recursive method requires us to define at least one base case and then define rules to break down the other cases into smaller ones What is the base case for factorial? If the number is = 1, the result is 1 (base case) If the number is < 1, the result is invalid (base case) Otherwise, the result is the number multiplied by the factorial of the number less 1 (recursive case)

11 Recursive vs. Iterative Functions Factorial Recursive Function public static int factorial (int value) { if(value <= 0) return 0; if(value == 1) return 1; else return value * factorial(value -1); }

12 Recursion Both the recursive and the iterative methods produce the same results, and are equally useful The positives of recursion is that it is often an easier solution with shorter code (but sometimes harder debug). This relates to programming style Some negatives of recursion is that the recursive solution is not always seen right away, and often using recursion might consume more memory

13 Assignment Fibonacci


Download ppt "IB Computer Science Unit 5 – Advanced Topics Recursion."

Similar presentations


Ads by Google