This algorithm presented is very similarread observation at the end of the reply an implementation of the algorithm to calculate the number n
of the Fibonacci sequence, which in turn has linear temporal complexity (o(n)
). So, you could say that the expected behavior of T(n)
is a linear function, in the format T(n) = a*n + b
, for general cases. However, there are special cases: any number with n <= 1
has T(n) = 2
. How do I know this? Taking the table test. See how it went to n = 1
:
- checks position -1 >= 2 (
n >= 2
)
- return position -1 value (
return n
)
I’m considering that the goto
be part of the primitive comparison operation, something like jumpelse n >= 2, LABEL_ELSE
Now, what would it be like for another case? For a generic case of n
? Let’s test with n = 4
:
- checks position -1 >= 2 (
n >= 2
)
- assigns 1 to heading 0 (
ant = 1
)
- assigns 1 to position 1 (
preant = 1
)
- assigns position -1 value to position 3 (
i = n
)
- check if position 3 >= 3 (
i >= 3
)
- sum value of position 0 and value of position 1 (
ant + preant
)
- gives result to position 2 (
atual = ant + preant
, and the sum has been considered above)
- assigns position 1 to position 0 (
preant = ant
)
- assigns the value of position 2 to position 1 (
ant = preant
)
- decreases to position 3 (
i--
)
- check position value 3 >= 3 (
i >= 3
)
- sum value of position 0 and value of position 1 (
ant + preant
)
- gives result to position 2 (
atual = ant + preant
, and the sum has been considered above)
- assigns position 1 to position 0 (
preant = ant
)
- assigns the value of position 2 to position 1 (
ant = preant
)
- decreases to position 3 (
i--
)
- check position value 3 >= 3 (
i >= 3
)
- returns position 2 value (
return atual
)
So they gave 18 operations to n = 4
. And to n = 5
?
- checks position -1 >= 2 (
n >= 2
)
- assigns 1 to heading 0 (
ant = 1
)
- assigns 1 to position 1 (
preant = 1
)
- assigns position -1 value to position 3 (
i = n
)
- check if position value 3 >= 0 (
i >= 0
)
- sum value of position 0 and value of position 1 (
ant + preant
)
- gives result to position 2 (
atual = ant + preant
, and the sum has been considered above)
- assigns position 1 to position 0 (
preant = ant
)
- assigns the value of position 2 to position 1 (
ant = preant
)
- decreases to position 3 (
i--
)
- checks position value 3 >= 0 (
i >= 0
)
- sum value of position 0 and value of position 1 (
ant + preant
)
- gives result to position 2 (
atual = ant + preant
, and the sum has been considered above)
- assigns position 1 to position 0 (
preant = ant
)
- assigns the value of position 2 to position 1 (
ant = preant
)
- decreases to position 3 (
i--
)
- checks position value 3 >= 0 (
i >= 0
)
- sum value of position 0 and value of position 1 (
ant + preant
)
- gives result to position 2 (
atual = ant + preant
, and the sum has been considered above)
- assigns position 1 to position 0 (
preant = ant
)
- assigns the value of position 2 to position 1 (
ant = preant
)
- decreases to position 3 (
i--
)
- checks position value 3 >= 0 (
i >= 0
)
- returns position 2 value (
return atual
)
Twenty-four operations. From here, we were able to get the value of a
in T(n) = a*n + b
and then we’ll find out b
:
T(5) = a*5 + b
T(4) = a*4 + b
T(5) = 24
T(4) = 18
T(5) - T(4) = a*5 + b - (a*4 + b) = a
T(5) - T(4) = 24 - 18 = 6
a = 6
18 = a*4 + b = 6*4 + b = 24 + b
b = -6
So we have to, to n >= 2
, T(n) = 6*n - 6
. Therefore:
This algorithm, although very similar to a classical implementation to the algorithm that calculates the numbers in the Fibonacci sequence, contains an error that disqualifies it as such. See, for example, the portamento for n = 2
:
- checks position -1 >= 2 (
n >= 2
)
- assigns 1 to heading 0 (
ant = 1
)
- assigns 1 to position 1 (
preant = 1
)
- assigns position -1 value to position 3 (
i = n
)
- check if position 3 >= 3 (
i >= 3
)
- returns position 2 value (
return atual
)
In not a moment position 2 (representing the variable atual
) has been filled. This indicates that at this time the return of the function is undefined. This is prohibited in some programming languages, but the C compilers I know allow it. In many cases, the return will be a memory junk, any value, which can sometimes even match the appropriate value. At other times, some interpreters I’ve seen around initialize variables with 0. Also, see the output result for some numbers:
fib(0) => 0
fib(1) => 1
fib(2) => undefined
fib(3) => 2
fib(4) => 3
fib(5) => 5
There are some solutions to fix the algorithm:
- make 2 a special case and force return 1
- do
preant = 0, ant = 1
and the loop until i >= 2
The first solution would add 1 extra step in special cases (and none in the general case, if the algorithm is well written); it would also change the composition of the function, being constant in the interval (-infty, 2]
and linear in [3, +infty)
(the original is constant in the range (-infty, 1]
and linear in [2, +infty)
).
The second solution does not change the number of steps in special cases, nor the intervals that define the behavior of the function, but would change the linear part; it would come out of 6*n - 6
for 6*n
, since at least one loop (which in turn has 6 operations) will always be executed.
What is the
T(n)
?– Jefferson Quesado
T(n) is the calculation of the algorithm considering the primitive operations
– Brenda Xavier