This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Using Recursion To Solve Problems Recursion is the term for "calling yourself". Some algorithms can be simply implemented by having a method call itself. Consider the Fibonacci series, which looks like this: Number 0 1 2 3 4 5 6 7 8 9 10 ... Fibonacci 0 1 1 2 3 5 8 13 21 34 55 ... Values The next number in the Fibonacci series is the sum of the two previous Fibonacci numbers. Expressed in the form of an algorithm we would say: F(0) = 0 F(1) = 1 F(n>1) = F(n1) + F(n2) A Conventional Iterative Solution If we wanted to calculate the Fibonacci value for a number n, one way to do it would be to generate the whole Fibonacci series up to n, and then return the last number of the series. We could use a looping (iteration) approach, like this: public long fibonacci(int n) { long fibonacciSeries = new long[n+1]; // make array just large enough to hold series if (n == 0) fibonacciSeries[0] = 0; if (n > 0) fibonacciSeries[1] = 1; for (int i = 2; i <= n; i++) fibonacciSeries[i] = fibonacciSeries[i1]+ fibonacciSeries[i2]; return fibonacciSeries[n]; } A Recursive Solution Or, we could write a piece of code that looks very much like the algorithm itself, and take advantage of the operation of the software system to calculate the answer: public long fibonacci(int n) { if (n <= 1) return n; // "base cases" else return fibonacci(n1) + fibonacci(n2); } We see that calling this method with n = 0 or 1 returns n (as the algoritm states), and calling with n = 2 returns (F(1) = 1) plus (F(0) = 0) which is 1. Calling with n = 3 then returns (F(2) = 1) plus (F(1) = 1) which is 2. And so on, which again is what the algorithm states. Note that the recursive solution code is more brief than the iterative solution. In fact the code is a direct statement of the algorithm! When the algorithm expression refers to itself, we can use recursion in our code to implement the algorithm. A Picture Of Recursion With recursion , we are counting on the call process of the computer to hold local variable values on the stack when a method is called. When the called method returns, you have the value returned by the called method plus whatever you had on your local stack when you made the call. Actually, since the same thread is making the calls, we are working on the same stack, and the computer creates an extension to the stack whenever our code makes a call. When a method returns, the stack extension made for that call is erased. So a thread's stack grows as it makes calls and shrinks as methods return! Let's take a look at the stack as recursive execution proceeds. These pictures are of the stack SPACE for a thread, not the time spent in a method. The local variables are allocated in the stack extension, and that is what makes the extension grow as the execcution continues. As we see above, when a method returns to it's caller, it's extension of the thread stack is erased and all of it's local variables are lost. But the returned value is delivered to (received by, allocated on) the caller's stack extension. is delivered to (received by, allocated on) the caller's stack extension....
View
Full
Document
This note was uploaded on 01/25/2010 for the course ECE 309 taught by Professor Bowman during the Spring '09 term at N.C. State.
 Spring '09
 BOWMAN
 Algorithms, Recursion

Click to edit the document details