Does anyone explain to me what the trace makes the Fibonacci code recursive in Java?

1

Good morning. I am trying to draw the trace that follows this recursive fibonacci Java code to see exactly how recursivity works in this case. I do not quite understand the exact operation in this particular case since in the return it is assumed that it is returned and it leaves the function in which it is but in the return recursively enters 2 methods that in turn if it is not 0 or one returns again 2 recursive methods coming out of that method in question.

The code is as follows:

public int fibonacci(int n) {

  if ((n == 0) || (n == 1)) // base cases
      return n;
    else
      // recursion step
      return fibonacci(n - 1) + fibonacci(n - 2);
}

Could someone explain to me the trace that follows the program and on each pass the result of n at the end of each function for each time it is called?

Does the return really cause that there is not a stack leaving the function and calling in turn to 2 functions as a tail recursion?

    
asked by Lorthas 01.03.2017 в 16:26
source

3 answers

2

To understand the algorithm, we can start with the base cases and go up:

fibonacci(0) --> 0
fibonacci(1) --> 1

For fibonacci(2) we have to apply the part of else :

fibonacci(2) --> fibonacci(1) + fibonacci(0)

To calculate the sum, it is necessary to know the result of the two calls that, in the case of addition, are evaluated from left to right. Therefore we have the following sequence:

fibonacci(2)
--> fibonacci(1) + fibonacci(0)
--> 1 + fibonacci(0)
--> 1 + 0
--> 1

For fibonacci(3) :

fibonacci(3)
--> fibonacci(2) + fibonacci(1)
--> (fibonacci(1) + fibonacci(0)) + fibonacci(1)
--> (1 + fibonacci(0)) + fibonacci(1)
--> (1 + 0) + fibonacci(1)
--> 1 + fibonacci(1)
--> 1 + 1
--> 2

This last case you can see that fibonacci(1) is invoked twice. The higher the whole number, the more times the calculations will be repeated. (An optimization mode would save the result for later calculations ( "memoization" )).

The process is quite linear, first one function and then the other. There is no mix of stacks since the functions are not invoked simultaneously.

Doing an exercise of imagination, to calculate fibonacci(n) you do not need more than n levels of recursion, which is the same, no more than n "stackframes" . p>     

answered by 01.03.2017 / 17:21
source
5

Suppose we enter the number 3.

Since it is not 0 nor 1 it passes to else {

Once inside the else we are sure that the result will be the sum of fibonacci(n-1) + fibonacci(n-2) , exemplifying with a diagram it could be as follows

--------? (3)

------ / ------ \

----? (2) ------? (1)

Where do we not know the result "?" but if the number we are going to enter, the first fibonacci of the diagram of the second line fibonacci(n-1) is executed, which is fibonacci(2) , since it is not 0 nor 1 passes to else where it will be executed again fibonacci(n-1) + fibonacci(n-2) , the diagram would fit us now like this

--------? (3)

------ / ------ \

----? (2) ------? (1)

- / ---- \

-? (1) ---? (0)

Executing the first fibonacci of the 3 line fibonacci(n-1) that is 2 - 1 = 1 fibonacci(1) , when executing it and being 1, we return n that is 1, then we know that returns fibonacci(1)

--------? (3)

------ / ------ \

----? (2) ------? (1)

- / ---- \

-1 (1) ---? (0)

When executing the second fibonnacci of the third line fibonacci(n-2) which is 2 - 2 = 0 fibonacci(0) will return 0, since it returns n and n

--------? (3)

------ / ------ \

----? (2) ------? (1)

- / ---- \

-1 (1) --- 0 (0)

Now we know that fibonacci(2) returns fibonacci(1) + fibonacci(0) which is 1 + 0 = 1

--------? (3)

------ / ------ \

---- 1 (2) ------? (1)

- / ---- \

-1 (1) ---? (0)

Pasamo to the second fibonacci of the second line we know that% co_of% = 1, then.

--------? (3)

------ / ------ \

---- 1 (2) ------ 1 (1)

- / ---- \

-1 (1) --- 0 (0)

Again we know that fibonacci(1) returns fibonacci(3) which is 1 + 1 =

-------- 2 (3)

------ / ------ \

---- 1 (2) ------ 1 (1)

- / ---- \

-1 (1) --- 0 (0)

But as they said it would be better to debugge or with simple prints you can serve

    
answered by 01.03.2017 в 16:52
0

To see the trace by yourself you could use the java debugger or if you can not show in the console what you do:

public int fibonacci(int n) {

  if ((n == 0) || (n == 1)) // base cases
       System.out.println("Devuelvo +"n)
       return n;
    else
       System.out.println("Entro en el else con +"n)      
        // recursion step
      return fibonacci(n - 1) + fibonacci(n - 2);
}

Although you can use other methods for the fibonacci series

public class fibonacci
{
public static void main (String[] args)
{
int a=0,b=1,c,hasta,i;
cantidad=15; //indica la cantidad de numeros en la serie fibonacci


for(i=0;i<hasta;i++)
{
c=a+b;
a=b;
b=c;
System.out.print(" "+a);
}
}

} 
    
answered by 01.03.2017 в 16:35