So when we do an iterative solution to find the nth number in a Fibonacci sequence, we run a for loop (n-2) times. This would mean that the time complexity would be O(n). Is this correct or would it actually be pseudo-polynomial depending on the number of bits of the input, much like the Knapsack problem?
2 Answers
Here, I assume Fib(n) is an iterative version of a program that computes Fibonacci numbers. Perhaps something like:
def Fib(n):
a, b = 0, 1
for _ in xrange(n):
a, b = b, a + b
return a
"Fib(n) is pseudo-polynomial" means in this context that computing Fib is bounded by a polynomial of its argument, n, but isn't bounded by a polynomial function of the size of the argument, log(n). That's true in this case.
"Fib(n) is O(n)" is a statement about the running time of Fib with respect to the value of its argument. There's sometimes ambiguity what "n" is, but here there's none -- it's the input to Fib, otherwise "n" would refer to two different things in the original statement. That's true here (although see the technical side-note below).
"Fib is O(n)" is ambiguous. There are people who will tell you that n clearly refers to the argument, and there's others who will tell you that n always refers to the size of the argument. The truth is that it's ambiguous and if it's not clear in context you should say what you mean (or ask what it means if you hear it and are confused). One context where it's not ambiguous is when you're talking about classes of P/NP problems -- there it's assumed that complexities are always relative to the size of the input.
A technical side-note
The iterative version of Fib(n) performs O(n) arithmetic operations, but whether it's O(n) time depends on your computational model, and specifically whether it can perform arbitrary integer arithmetic operations in O(1) time. Personally, I'd be careful and say "Fib(n) performs O(n) arithmetic operations" rather than "Fib(n) is O(n)" -- and if you plot the running time of Fib(n), you'll find it's not linear time in practice, as real bignum implementations are certainly not O(1) for all basic operations.

- 54,811
- 11
- 92
- 118
-
Hi Paul, thank you for the response. I am new to Algorithms actually and don't understand P/NP and all that. I was just doing a MOOC and there they were teaching the Knapsack problem and said that for the bag weight W input, and n being the number of objects, the complexity looks like O(nW). However for W, it is actually pseudo polynomial as it depends on the size of the input. That is why I thought if it was similar for Fib iterative version where we also take an input n that is the nth Fib number we are to find. Here also will it be pseudo polynomial for the same reason as Knapsack? – Ayan Majumdar May 17 '16 at 06:12
-
Yes, it's technically correct to say it's pseudo polynomial. – Paul Hankin May 17 '16 at 06:15
-
In a strict sense Big O should be calculated on the basis of the size of the input. By that criteria, iterative fibonacci is psuedo-polynomial because it is not linear on the size of it's input. In practice that means that if you calculate fib(10000) you're going to need a very large number and you'll overflow. – AminM Sep 04 '22 at 05:28
Yes, it is infact O(n). The time complexity of Knapsack Problem is a really weird one and is an exception.

- 2,063
- 1
- 21
- 39
-
Well that is what I don't understand. Why and how is it an exception? I am new to Algorithms... – Ayan Majumdar May 17 '16 at 06:07