Download presentation
Presentation is loading. Please wait.
1
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself
2
Fall 2009ACS-1805 Ron McFadyen2 Indefinite Repetition In programs where a count of repetitions is not known (indefinite), we can use one of two repetition control mechanisms: Recursion While statement Many of the pieces we use to create a program are identified by using special words. For example, Do in order Do together If/Else Loop Recursion is not a program statement with a special word that identifies it as part of the programming language. Recursion means that a method (or a function) calls itself.
3
Fall 2009ACS-1805 Ron McFadyen3 Examples of recursion The natural numbers: 1 is in {N} if n is in {N}, then n + 1 is in {N}
4
Fall 2009ACS-1805 Ron McFadyen4 Ancestors For example, the following is a recursive definition of a person's ancestors: * One's parents; * Your parents ancestors (recursion step).
5
Fall 2009ACS-1805 Ron McFadyen5 Fibonacci number sequence Fibonacci number sequence: F(n) = F(n − 1) + F(n − 2). Base cases: F(0) = 0 F(1) = 1.
6
Fall 2009ACS-1805 Ron McFadyen6 Humour Recursive humor A common geeky joke is the following "definition" of recursion. recursion see “recursion” In the index of a book, on page 189 recursion22, 45, 80, 189
7
Fall 2009ACS-1805 Ron McFadyen7 Factorials n! = n (n - 1)! where n >=0 3!=3*2! 4!=4*3! 2!=2*1! Base cases: 0! = 1 1! = 1 A base case tells us when recursion can end
8
Fall 2009ACS-1805 Ron McFadyen8 N! n! parameter n, an integer If n=0 return 1 Else if n=1 return 1 Else return n * (n-1)! Recursive call
9
Fall 2009ACS-1805 Ron McFadyen9 n factorial.a2w
10
Fall 2009ACS-1805 Ron McFadyen10 n factorial.a2w Number penguin.n! ( [123] n) No variables If ( n == 0 ) Return 1 Else If ( n == 1 ) Return 1 Else Do Nothing Return ( ( n * ( penguin.n! ( ( n - 1 ) ) ) ) ) Call n! again with the argument n-1 Once (n-1)! is computed, this returns the value n * (n-1)! to whoever called
11
Fall 2009ACS-1805 Ron McFadyen11 The call stack A stack in computer science is an area of memory where information can be placed, used, and then removed. The call stack keeps track of method and function calls Operations: push, pop, get Information is pushed onto the stack Information is popped off the stack The top entry is accessible … lifo: last in, first out pushA new entry goes onto the stack… the stack becomes larger with the new entry on top
12
Fall 2009ACS-1805 Ron McFadyen12 The call stack When a method/function is called an entry goes on the stack push When a method/function finishes the top entry is popped and execution resumes using the information in the new top entry pop If a function just finished, the value it returns is “plugged” into the spot where it was invoked.
13
Fall 2009ACS-1805 Ron McFadyen13 The call stack Our sample program will begin with my first method First of all, information for my first method is pushed onto the stack: locations for its parameters, locations for its variables, the currently executing instruction Execution begins: x is assigned the value 4 penguin.n! is invoked … Now, information for penguin.n! is pushed onto the stack and it begins to execute … let’s illustrate:
14
Fall 2009ACS-1805 Ron McFadyen14 Another example – shark/goldfish Previously this world used a loop which executed until the shark was close enough to the goldfish Now consider : CHASER If the distance between the shark and the goldfish > 0.5 Do together shark swims toward goldfish goldfish flees call CHASER Shark swims … Goldfish flees … ChaseRecursion.a2w Recursive call
15
Fall 2009ACS-1805 Ron McFadyen15 Example – shark/goldfish
16
Fall 2009ACS-1805 Ron McFadyen16 The method begins with an if statement to determine if a base condition is met. If the base condition is met the method terminates; if it is not then we invoke the method again on a smaller problem. The general form of the algorithm we have just used is: MethodX: if base condition is satisfied exit else do something call MethodX Example – shark/goldfish
17
Fall 2009ACS-1805 Ron McFadyen17 To formulate a recursive solution we need: to understand some base conditions where the problem has a known answer or solution to be able to express a solution in terms of smaller problems. As the problems get smaller and smaller, we eventually converge to one of the base conditions. Example – shark/goldfish
18
Fall 2009ACS-1805 Ron McFadyen18 More general forms of recursion Suppose there is something to do both before and after the recursive call: if a base condition is satisfied the method teminates otherwise do something A recursive call do something B
19
Fall 2009ACS-1805 Ron McFadyen19 More general forms of recursion Suppose there is more than one recursive call: if a base condition is satisfied the method teminates else recursive call do something recursive call See mischief.a2wmischief.a2w
20
Fall 2009ACS-1805 Ron McFadyen20 mischief.a2w
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.