Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 would shed some light on this question.

EDIT: Lots of good answers here! We all agree that 0 is O(1). The question is, is 0 O(0) as well?

like image 627
jyoungdev Avatar asked Jul 09 '10 00:07

jyoungdev


People also ask

What is o1 complexity?

Constant Complexity - O(1)An algorithm has constant time complexity if it takes the same time regardless of the number of inputs. ( Reading time: under 1 minute) If an algorithm's time complexity is constant, it means that it will always run in the same amount of time, no matter the input size.

What is the time complexity of an empty for loop?

Theoretically, it's O(n) , where n is the number of loops, even if there's no task inside the loop. But, in case of some compiler, the compiler optimizes the loop and doesn't iterates n times. In this situation, the complexity is O(1) .

What does O in time complexity mean?

So, the time complexity is constant: O(1) i.e. every time a constant amount of time is required to execute code, no matter which operating system or which machine configurations you are using.

Is Big O time or space complexity?

Space complexity of an algorithm is commonly expressed using Big O (O(n)) notation. Many algorithms have inputs that can vary in size, e.g., an array.


1 Answers

From Wikipedia:

A description of a function in terms of big O notation usually only provides an upper bound on the growth rate of the function.

From this description, since the empty algorithm requires 0 time to execute, it has an upper bound performance of O(0). This means, it's also O(1), which happens to be a larger upper bound.

Edit:

More formally from CLR (1ed, pg 26):

For a given function g(n), we denote O(g(n)) the set of functions

O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all nn0 }

The asymptotic time performance of the empty algorithm, executing in 0 time regardless of the input, is therefore a member of O(0).

Edit 2:

We all agree that 0 is O(1). The question is, is 0 O(0) as well?

Based on the definitions, I say yes.

Furthermore, I think there's a bit more significance to the question than many answers indicate. By itself the empty algorithm is probably meaningless. However, whenever a non-trivial algorithm is specified, the empty algorithm could be thought of as lying between consecutive steps of the algorithm being specified as well as before and after the algorithm steps. It's nice to know that "nothingness" does not impact the algorithm's asymptotic time performance.

Edit 3:

Adam Crume makes the following claim:

For any function f(x), f(x) is in O(f(x)).

Proof: let S be a subset of R and T be a subset of R* (the non-negative real numbers) and let f(x):S ->T and c ≥ 1. Then 0 ≤ f(x) ≤ f(x) which leads to 0 ≤ f(x) ≤ cf(x) for all x∈S. Therefore f(x) ∈ O(f(x)).

Specifically, if f(x) = 0 then f(x) ∈ O(0).

like image 192
13 revs Avatar answered Sep 20 '22 19:09

13 revs