Is the time complexity of the empty algorithm O(0)?

后端 未结 14 2257
暗喜
暗喜 2020-12-12 21:29

So given the following program:


Is the time complexity of this program O(0)? In other words, is 0 O(0)?

I thought answering this in a separate question

相关标签:
14条回答
  • 2020-12-12 22:33

    It takes the same amount of time to run regardless of the input, therefore it is O(1) by definition.

    0 讨论(0)
  • 2020-12-12 22:33

    Big O is asymptotic notation. To use big O, you need a function - in other words, the expression must be parametrized by n, even if n is not used. It makes no sense to say that the number 5 is O(n), it's the constant function f(n) = 5 that is O(n).

    So, to analyze time complexity in terms of big O you need a function of n. Your algorithm always makes arguably 0 steps, but without a varying parameter talking about asymptotic behaviour makes no sense. Assume that your algorithm is parametrized by n. Only now you may use asymptotic notation. It makes no sense to say that it is O(n2), or even O(1), if you don't specify what is n (or the variable hidden in O(1))!

    As soon as you settle on the number of steps, it's a matter of the definition of big O: the function f(n) = 0 is O(0).

    Since this is a low-level question it depends on the model of computation. Under "idealistic" assumptions, it is possible you don't do anything. But in Python, you cannot say def f(x):, but only def f(x): pass. If you assume that every instruction, even pass (NOP), takes time, then the complexity is f(n) = c for some constant c, and unless c != 0 you can only say that f is O(1), not O(0).

    It's worth noting big O by itself does not have anything to do with algorithms. For example, you may say sin x = x + O(x3) when discussing Taylor expansion. Also, O(1) does not mean constant, it means bounded by constant.

    0 讨论(0)
提交回复
热议问题