cosc236/recursion1 Recursion Recursive method calls itself Recursion frequently occurs in mathematics Recursive definition –2 parts base part (basis) recursive part (inductive part)
cosc175/recursion2 Recursion 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
cosc175/recursion3 Power public static int power(int x, int n) { if (n == 0) return 1;//base case else return x * power(x,n-1); // recursive case }
cosc175/recursion4 Power public static int power(int x, int n) { // n >= 0 if (n < 0) throw new IllegalArgumentException(“negative exponent”); else if (n == 0) return 1;//base case else return x * power(x,n-1); // recursive case }
cosc175/recursion5 Factorial n! 0! = 0 1! = 1 For n > = 1, n! = n * (n-1)! Exa: –6! = 6 * 5! –6 * 5 * 4! –6 * 5 * 4 * 3! –6 * 5 * 4 * 3 * 2! –6 * 5 * 4 * 3 * 2 * 1
cosc175/recursion6 Factorial – N! N! = N * (N-1)! public static int factorial(int n) // n >= 0 { if (n == 0) return 0; else if (n == 1) return 1; else return n * factorial(n – 1); }
cosc175/recursion7 Recursive methods Recursive method - invokes itself directly or indirectly –direct recursion - method is invoked by a statement in its own body –indirect recursion - one method initiates a sequence of method invocations that eventually invokes the original A->B->C->A
cosc175/recursion8 Why use Recursion? Any loop can be rewritten using recursion Recursion requires more memory Recursion more complex Some problems are naturally recursive Recursive solution may be shorter Recursive solution may be more efficient –Quicksort, dynamic memory not every recursion can be replaced by a loop
cosc175/recursion9 Recursion When a method 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
cosc175/recursion10 public static void echoReversed() { String s= console.next(); char c = (char)s.charAt(0); if (c == '\n') System.out.println(); else { echoReversed(); System.out.print(b); }
cosc175/recursion11 Infinite Recursion no base condition public static void infRec() { if ( ) { infRec(); }
cosc175/recursion12 Factorial(loop version) public static int loopFactorial (int n) { int i; int fact = 1; for (i = 1; i <= n; i++) fact = fact * i; return fact; }
cosc175/recursion13 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
Example public static int mystery (int number) { if (number == 0) return number; else return (number + mystery (number – 1); } 1.Identify the base case 2.Identify the general case 3.What is the result of mystery(0) 4.What is the result of mystery(5); cosc175/recursion14
Example public static void mystery (char param1, char param2) { if (param1 != param2) { param1++; param2--; mystery(param1, param2); System.out.println(param1); System.out.println(param2); } 1.Show all output resulting from the following invocation : Mystery('A','G'); 2.Under what circumstances does Mystery result in infinite recursion? cosc175/recursion15
public static mystery(int n) { if (n <= 1) System.out.println(n); else { mystery(n/2); System.out.println(“, “ + n); } cosc175/recursion16 mystery(1); mystery(2); mystery(3); mystery(4); mystery(16); mystery(30);