2
$\begingroup$

After reading the question and answers on Time complexity of min() and max()? I would like to clear up some confusion on the relation between time complexity and size of input.

First, I've noticed most of the time people omit what exactly n represents in big O notation. Usually it's related to the input, but it may be the value or length in bits or something else. Am I right that it's usually inferred from context?

When is a function O(1) vs O(n)? I had thought that only functions that do not depend on the input would be O(1) for example

foo()
{
  return "bar"
}

Or accessing certain data structures like an array arr[0] or a map would have operations of O(1). But in the linked question some answers stated this is only true if the size is known. What difference does this make? Even if the size is known, some operations may take O(n). For example given in an array of 100 elements you know the value 50 appears and would like to find its index. That would be O(n) because the array would have to be traversed. So why do some people say knowing the size makes it constant time?

$\endgroup$
2
$\begingroup$

First, I've noticed most of the time people omit what exactly n represents in big O notation.

Yes, that is indeed a bad habit. In general, when you analyze complexity, you must specify:

  • Are you talking about space complexity, step complexity, time complexity?
  • Are you talking about best-case, worst-case, amortized worst-case, average case, expected case?
  • What is your machine model? (I.e. what operations exist?)
  • What is your cost model? (How long do those operations take?)
  • What are you actually counting? (Comparisons, stores, loads, arithmetic operations, bit operations, etc.)
  • How do you define "length of the input"? (Number of elements, number of bits, etc.)
  • What is the distribution of your inputs? (In case of average or expected complexity.)

Usually it's related to the input, but it may be the value or length in bits or something else.

For data structures, it is usually the number of elements. For strings, it already gets trickier: is it the number of bytes, number of octets, number of `chars, number of code points, number of characters, number of graphemes, number of grapheme clusters?

One thing that catches many people out is that for numbers, it is usually not the magnitude, it is the length (i.e. the number of "digits" or bits), which is the logarithm of the magnitude.

Am I right that it's usually inferred from context?

Indeed. However, as mentioned above, for numbers, it is often inferred wrong.

The reason why we usually use the length of numbers is that arithmetic operations depend on the number of digits, not on the magnitude of the number: adding 1+1 take the same time as adding 4+4, but adding 10+10 takes twice as long. Adding 4999+5000 doesn't take 5000 times as long as adding 1+1, it only takes 4 times as long.

For example given in an array of 100 elements you know the value 50 appears and would like to find its index. That would be O(n) because the array would have to be traversed.

No, it would not. O(1) means the number of comparisons (which is what we are usually counting when talking about "searching") is bounded by a constant. In your example, you will never need more than 100 comparisons, therefore the number of comparisons is bounded by 100, which is a constant.

Therefore, the time complexity is O(100), which is the same as O(1).

$\endgroup$
2
  • $\begingroup$ Still don't get it. With that reasoning wouldn't everything have time complexity of O(1) because there will always be a constant number of operations? For example binary search is O(logn) but using your argument, on a certain input it will always take the same operations therefore O(1). $\endgroup$
    – northerner
    Mar 14 at 9:12
  • $\begingroup$ Just look at the definition of Big-O: f ∈ O(g) if there exist constants c, N_0 > 0, such that for all n > N_0, f(n) <= c × g(n). Just set N_0 = 101. $\endgroup$ Mar 14 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.