Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to.

Similar presentations


Presentation on theme: "10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to."— Presentation transcript:

1 10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to smaller versions of itself A powerful, elegant, efficient way to solve certain classes of programming problems A recursive function calls itself

2 10/14/2015cosc237/recursion2 Recursive definitions Relative: x is a relative of y iff (if and only if) x is y’s parent x is y’s child x is y’s spouse x is a relative of a relative of y

3 10/14/2015cosc237/recursion3 Recursive definitions cont’d Natural numbers 1 is a natural number if i is an natural number, then i+1 is a natural number Definition in which a problem is expressed in terms of a smaller version of itself Has one or more base cases

4 10/14/2015cosc237/recursion4 Example 1: void message() //infinite recursive function { System.out.println("This is a recursive function.)"; message(); }

5 10/14/2015cosc237/recursion5 Example 2: void message(int times) { if (times > 0) { System.out.println("This is a recursive function.)"; message(times -1); }

6 10/14/2015cosc237/recursion6 message (4); // Each call creates an activation record or frame. 1st call times: 4 2nd call times: 3 3rd call times: 2 4th call times: 1 5th call times: 0

7 10/14/2015cosc237/recursion7 Recursion requires 1. base case 2. general case

8 8 General format for Many Recursive Functions if (some easily-solved condition) // base case solution statement else // general case recursive function call

9 10/14/2015cosc237/recursion9 Base part – states one or more cases that satisfy the definition –No base->infinite recursion recursive part –states how to apply the definition to satisfy other cases –with repeated applications, reduces to base implicitly: nothing else satisfies the definition

10 10/14/2015cosc237/recursion10 Example #3: Find the sum of all int numbers between 1 and n. Sum(5) = 1 + 2 + 3 + 4 + 5 = 15 Base case: Sum (1) = 1 General Case: Sum (2) = 1 + 2 = Sum(1) + 2 = Sum (n-1) + n

11 11 Finding the Sum of the Numbers from 1 to n int Summation ( int n ) { if ( n == 1)// base case return 1 ; else// general case return ( n + Summation ( n - 1 ) ) ; } 11

12 10/14/2015cosc237/recursion12 Exercises Write a function using recursion to print numbers from n to 0. Write a function using recursion to print numbers from 0 to n. (You just need to shift one line in the program above)

13 10/14/2015cosc237/recursion13 Solution: //n to 0 void Print(int n) { System.out.println(n ); if (n <= 0) return; Print(n-1); } //0 to n void Print(int n) { if (n > 0) Print(n-1); System.out.println(n ); }

14 10/14/2015cosc237/recursion14 Example #4: Write a recursive function to find n factorial. n! = n * (n-1) * (n-2)... 3 * 2 * 1 Factorial(5) = 120 = 5 * 4 * 3 * 2 * 1 Base case: if (n == 1) return 1; General case: n! = n * (n-1) * (n-2)... 3 * 2 * 1 = n * [(n-1) * (n-2)... 3 * 2 * 1] = n * (n-1)! OR: Factorial(n) = n * Factorial(n-1)

15 10/14/2015cosc237/recursion15 Factorial n! = n x (n-1)! int factorial(int num) { if (num == 0) return num; else return num * Factorial(num – 1); }

16 16 Recursive Factorial Method (continued)

17 10/14/2015cosc237/recursion17 Example #5: Write a recursive function to find x n. Base case: 2 0 = 1 General Case: 2 n = 2 * 2 n-1

18 10/14/2015cosc237/recursion18 power function int Power(int x, int n) // n >0 { if (n == 1) return x; // base case else return x * Power(x,n-1); }

19 10/14/2015cosc237/recursion19 Power, n<0 Note that 2 -3 = 1/2 3 = 1/8 In general, x n = 1/ x -n for non-zero x, and integer n<0.

20 10/14/2015cosc237/recursion20 Power float Power (float x, int n) { if (n == 0) // base case return 1; else if (n > 0) // first general case return (x * Power(x, n - 1)); else // second general case return (1.0 / Power(x, - n)); }

21 10/14/2015cosc237/recursion21 Use the box method to trace Activation record Represent each call to the function during the course of the execution by a new box Local environment

22 22 Tracing a Recursive Method After completing a recursive call: –Control goes back to the calling environment –Recursive call must execute completely before control goes back to previous call –Execution in previous call begins from point immediately following recursive call

23 10/14/2015cosc237/recursion23 void mystery(char param1, char param2) { if (param1 != param2) { param1++; param2--; Mystery(param1, param2); System.out.println(param1); System.out.println(param2); } Show all output resulting from the following invocation:mystery('A','G'); Under what circumstances does mystery result in infinite recursion? Exercises

24 10/14/2015cosc237/recursion24 int TermUnknown (int someInt) // This is a classic number theory problem for which // termination for all input is uncertain. { if (someInt == 1) return 0; if (someInt % 2 == 1) return 1 + TermUnknown(3 * someInt +1); return 100 + TermUnknown(someInt/2); } What value does each of the following invocations return? a. TermUnknow(1); b. TermUnknown(4); c. TermUnknown(3);

25 25 Largest Value in Array public static int largest(int[] list, int lowerIndex, int upperIndex) { int max; if (lowerIndex == upperIndex) return list[lowerIndex]; else { max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return list[lowerIndex]; else return max; }

26 26 Largest Value in Array (continued)

27 10/14/2015cosc237/recursion27 Recursive Functions Recursive function - invokes itself directly or indirectly –direct recursion - function is invoked by a statement in its own body –indirect recursion - one function initiates a sequence of function invocations that eventually invokes the original A->B->C->A

28 10/14/2015cosc237/recursion28 When a function is invoked - activated activation frame - collection of information maintained by the run-time system(environment) –current contents of local automatic variables –current contents of all formal parameters –return address –pushed on stack

29 29 Linear Search array a two subscripts, low and high Key Return -1 if key is not found in the elements a[low...high]. Otherwise, return the subscript where key is found

30 30 // Recursive definition int LinearSearch ( /* in */ const int a[ ], /* in */ int low, /* in */ int high, /* in */ int key ) { if ( a [ low ] == key ) // base case return low ; else if ( low == high) // second base case return -1 ; else // general case return LinearSearch( a, low + 1, high, key ) ; } 30

31 31 Function BinarySearch( ) l BinarySearch that takes sorted array a, and two subscripts, low and high, and a key as arguments. It returns -1 if key is not found in the elements a[low...high], otherwise, it returns the subscript where key is found l BinarySearch can be written using iteration or using recursion

32 32 // Iterative definition int BinarySearch (int a[ ], int low, int high,int key ) { int mid; while ( low <= high ) { // more to examine mid = (low + high) / 2 ; if ( a [ mid ] == key ) // found at mid return mid ; else if ( key < a [ mid ] ) // search in lower half high = mid - 1 ; else // search in upper half low = mid + 1 ; } return -1 ; // key was not found } 32

33 33 // Recursive definition int BinarySearch (int a[ ], int low, int high, int key) { int mid = (low + high) / 2 ; if ( low > high ) // base case -- not found return -1; if ( a [ mid ] < key ) // search in upper half return BinarySearch( a, mid + 1, high, key ) ; else if ( key < a [ mid ] ) // search in lower half return BinarySearch ( a, low, mid - 1, key ); else return mid; } 33

34 10/14/2015cosc237/recursion34 "One Small Step/Complete the Solution" Each invocation of a recursive function performs one small step towards the entire solution and makes a recursive call to complete the solution.

35 10/14/2015cosc237/recursion35 Recursive Functions Non-value-returning –invoked as separate stand-alone statement value-returning –invoked in an expression –often embedded within return statement

36 36 Recursion or Iteration? Two ways to solve particular problem: –Iteration or Recursion Every loop can be replaced by recursion not every recursion can be replaced by a loop Iterative control structures use looping to repeat a set of statements Tradeoffs between two options: –Sometimes recursive solution is easier –Recursive solution is often slower

37 10/14/2015cosc237/recursion37 Factorial(loop version) int LoopFactorial (int n) { int i; int fact = 1; for (i = 1; i <= n; i++) fact = fact * i; return fact; }

38 10/14/2015cosc237/recursion38 Factorial - table lookup int factorial[8] = {1,1,2,6,24,120,720,5040}; faster, can simplify algorithms more memory space

39 Java Programming: Program Design Including Data Structures 39 Programming Example: Decimal to Binary public static void decToBin(int num, int base) { if (num > 0) { decToBin(num / base, base); System.out.print(num % base); }

40 Java Programming: Program Design Including Data Structures 40 Programming Example: Decimal to Binary

41 10/14/2015cosc237/recursion41 When to Use Recursion If the problem is stated recursively if recursive algorithm is less complex than recursive algorithm if recursive algorithm and nonrecursive algorithm are of similar complexity - nonrecursive is probably more efficient consider table-driven code

42 10/14/2015cosc237/recursion42 References: [1] Programming and Problem Solving with C++, third edition, by Neal Dale, Chip Weems, and Mark Headington, Jones and Bartlett Publishers, 2002. [2] Data Abstraction and Structures Using C++, by Mark R. Headington, and David D. Riley, Jones and Bartlett Publishers, 1997. [3] Starting Out with C++, by Tony Gaddis, Scott Jones Publishers, 2004. Java Programming: [4] Program Design Including Data Structures, D.S. Malik


Download ppt "10/14/2015cosc237/recursion1 Recursion A method of defining a concept which refers to the concept itself A method of solving a problem by reducing it to."

Similar presentations


Ads by Google