Good. I am trying to draw the trace that follows this recursive fibonacci code in Java to see how exactly the recursion works in this case. I do not quite understand the exact operation in this specific case since in the return it is supposed to return and exit the function in which it is, but in the return it recursively enters 2 methods that in turn if it is not 0 or one returns again 2 recursive methods exiting that method in question.
The code is the following:
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 the program follows and in each pass the result of n at the end of each function for each time it is called?
The return really does not have a stack leaving the function and calling in turn 2 functions as a tail recursion?
Suppose we enter the number 3.
Since it is neither 0 nor 1, it goes to
else {
Once inside
else
we are sure that the result will be the sum offibonacci(n-1) + fibonacci(n-2)
, exemplifying with a diagram it could well be as follows--------?(3)
------/------\
----?(2)------?(1)
Where we do not know the result "?" but if the number that we are going to enter, the first fibonacci of the diagram of the second line is executed,
fibonacci(n-1)
which isfibonacci(2)
, since it is not 0 or 1, it passes toelse
where it will be executed againfibonacci(n-1) + fibonacci(n-2)
, the diagram would now be like this--------?(3)
------/------\
----?(2)------?(1)
--/----\
-?(1)---?(0)
Executing the first fibonacci of the 3rd line,
fibonacci(n-1)
which is 2 - 1 = 1fibonacci(1)
, when executed and being 1, it returns n, which is 1, so we already know what it returnsfibonacci(1)
--------?(3)
------/------\
----?(2)------?(1)
--/----\
-1(1)---?(0)
Executing the second fibonnacci of the third line,
fibonacci(n-2)
which is 2 - 2 = 0fibonacci(0)
, will return 0, since it returns n and n--------?(3)
------/------\
----?(2)------?(1)
--/----\
-1(1)---0(0)
Now we know that it
fibonacci(2)
returnsfibonacci(1) + fibonacci(0)
which is 1 + 0 = 1--------?(3)
------/------\
----1(2)------?(1)
--/----\
-1(1)---?(0)
We go to the second fibonacci of the second line that we know
fibonacci(1)
= 1, so.--------?(3)
------/------\
----1(2)------1(1)
--/----\
-1(1)---0(0)
Again we know that it
fibonacci(3)
returnsfibonacci(2) + fibonacci(1)
which is 1 + 1 =--------23)
------/------\
----1(2)------1(1)
--/----\
-1(1)---0(0)
But as they said, the best thing would be to debug or with simple prints it can help you
To understand the algorithm, we can start with the base cases and work our way up:
For
fibonacci(2)
we have to apply the part ofelse
: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:
For
fibonacci(3)
:In this last case you can see that it
fibonacci(1)
is invoked twice. The larger the integer, the more times the calculations will be repeated. (An optimization way would be to save the result for later calculations ( "memoization" )).The process is fairly linear, first one function and then the other. There is no mixing of stacks since the functions are not called simultaneously.
Doing an exercise of imagination, to calculate
fibonacci(n)
you don't need more thann
recursion levels, which is the same, no more thann
"stackframes" .To see the trace yourself you could use the java debugger or if you can't show what you're doing in the console:
Although you can use other methods for the fibonacci series