Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.

Similar presentations


Presentation on theme: "Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline."— Presentation transcript:

1 Recursion

2 What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline

3 A recursive method is a method that directly or indirectly calls itself. What is recursion? 5! = 5 × 4 × 3 × 2 × 1 n! = n × n − 1 × n − 2 ×... × 1 (n − 1)! = n − 1 × n − 2 ×... × 1 factorial(n) = n × factorial(n − 1) factorial(1) = 1 Example: definition of factorials

4 What happens if we call int x = factorial(4)?. Example: recursive implementation of factorial { if (n == 1) return 1; return n * factorial(n - 1); } public int factorial(int n) return 4 * factorial (3); factorial(4)= 24 return 3 * factorial (2); factorial(3)= 6 return 2 * factorial (1); factorial(2) = 2 Return 1; factorial(4)= 1 Eventually, x is assigned the return value of factorial(4), i.e. 24

5 How can a method F solve a problem by calling itself? The key: F calls itself on a simpler problem! Problems solvable by recursion can be reduced into one or more smaller and similar problems. In general, recursion has 2 cases: –Induction case: solved by reducing the problem. –Base case: simple enough, requires no more reduction. The basics of recursion

6 The rules of recursion Always have at least one case that can be solved without recursion. 1. Base case To avoid infinite recursion, any recursive call should progress towards the base case. 2. Make progress Always assume that the recursive call works. 3. You gotta believe Never duplicate work by solving the same instance of a problem in separate recursive calls. 4. Compound interest rule

7 Example: –Consider factorial(n). –Assume factorial(n − 1) computes the correct result. –What must be done to get factorial(n) from factorial(n − 1)? –Multiply by n. This is true whether you’re computing factorial(4) or factorial(400000).You don’t have to trace this 400000 times to convince yourself! “Believing” in the induction case

8 What happens if we call bad(n) with any value of n > 0? Infinite recursion! How NOT to do recursion { if (n == 0) return 0; return bad(n * 3); } public static int bad (int n)

9 The Java Virtual Machine keeps track of method calling using activation records. An activation record contains relevant info, such as: –Values of parameters –Values of local variables –Which line of code was being executed Every time a method call is made, a new activation record is created. What would make a good data structure for a collection of these records? A Stack! How does recursion work?

10 When method S is called, an activation record for S is pushed onto the stack, making it the currently active method. When the method returns, the stack is popped and the activation record that is the new top of the stack contains the restored values. OutlineActivation record stack

11 On “my” computer, calling s(12516) can’t be handled! Why? The computer runs outs of memory storing the activation record stack! A simple loop would have worked... Too much recursion! { if (n == 1) return 1; else return s (n - 1); } public static long s (int n)

12 How do we prove this is true? We can easily test it for N = 1, perhaps even for 2 <= N <= 10. However, this is not proof that the theorem holds for all N >= 1. Induction: the mathematical basis for recursion For P any integer N >= 1, the sum of the first N integers, given by,,equals N(N + 1)/2. Theorem Mathematical induction is a method for proving theorems. Example:

13 Prove it. Proving the theorem with induction

14 The Fibonacci sequence F 0, F 1,..., F i is a very famous sequence of numbers. It is defined as follows: –F0 = 0 –F1 = 1 –F i = F i-1 + F i-2 Thus: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... Fibonacci numbers

15 This works. However, it’s really slow. Computing F40 takes almost a minute on some modern PCs! To compute fibo(n), we recursively compute fibo(n-1) and fibo(n- 2). However, we already computed fibo(n-2) in the process of computing fibo(n-1). Computing Fibonacci recursively { if( n <= 1 ) return n; else return fibo( n - 1 ) + fibo( n - 2 ); } public static long fibo(int n)

16 F40 makes over 300 million recursive calls. The growth rate is exponential! Remember the last rule of recursion: Never duplicate work by solving the same instance of a problem in separate recursive calls. The solution: remember the previous Fibonacci numbers. Wasting time...

17 Storing the result of previous computation: this technique is called dynamic programming or memorization. There is a more (space) efficient implementation. How? Remembering the previous numbers { if (n <= 1) return n; long result[] = new long[n + 1]; result[0] = 0; result[1] = 1; for (int ii = 2; ii <= n; ii++) result[ii] = result[ii - 1] + result[ii - 2]; return result[n]; } public static long fibo2 (int n)

18 Remembering just the last two numbers { if (n <= 1) return n; long fib1 = 0; long fib2 = 1; long result; for (int ii = 2; ii <= n; ii++) {result = fib2 + fib1; fib1 = fib2; fib2 = result;} return result; } public static long fibo3 (int n)

19 An efficient recursive implementation { return fiboHelp(0,1,n) } public static long fibo4 (int n) { if (n == 0) return x; else if ( n == 1 ) return y; else return fiboHelp (y, x+y, n-1); } static long fiboHelp (long x, long y, int n)

20 Recursion is a powerful problem solving tool and has many uses. The foundation of recursive functions is mathematical induction, a way of proving theorems. The rules of recursion: –Base case. –Progress towards the base case. –Always assume the recursive call works. –Never duplicate work by solving the same instance of a problem in separate recursive calls. Dynamic programming: saving the results of previous computation. Summary


Download ppt "Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline."

Similar presentations


Ads by Google