r/computerscience • • Sep 17 '26

Help Why use Big O notation?

If someone asks for big O time complexity of an algorithm but expects only the minimum of the possible big Os then is that even Big O notation anymore? cuz if the big o time complexity of an o(n) algorithm is asked then according to the condition of big O notation O(n square) would also be a valid answer

117 Upvotes

64 comments sorted by

View all comments

207

u/qlkzy Sep 17 '26

Like any upper bound, you can always satisfy it in some sense by giving a ridiculous upper bound, but that isn't useful.

If I ask "can you estimate an upper bound for the weight of your luggage" and you say "a hundred tons", that is technically correct, but that isn't helpful for packing a car.

Any question in mathematics that asks "find an upper bound" implies "find a tight upper bound".

16

u/Significant_Virus142 Sep 17 '26

Then why don't they ask theta, has O become the convention or theta isn't right either

60

u/[deleted] Sep 17 '26

[removed] — view removed comment

16

u/not-just-yeti Sep 17 '26 edited Oct 05 '26

It doesn’t have Theta.

Once you define what your function is, it likely has a Theta. "Given a number n, the worst-case running time over all inputs of size n" is well-defined. And, best-case and average-case are both well-defined. But agreed, if you just say "the running time of the algorithm on some input of size n but otherwise I won't tell you which input I'm thinking of", that is not a function because there when n=17 there might be multiple "answers".

CS folk often forget that Big-Oh etc. are great for any function on numbers; it doesn't have to be worst-case running time, or average-case-memory-usage; big-Oh can be sensible for (e.g.) "expected number of independent customer-reviews needed to be 95% sure of their average being within 1/n of the distribution's true average". I've seen it used to bound the error on the approximation in the Prime Number Theorem here.

Btw, mathematicians will point out that there are some functions which have a big-Oh and a big-Omega, but no big-Theta. E.g. the size of a maximal graph matching, over all graphs with n nodes: when n is even it's clearly n/2, and for n odd it's zero. So the graph alternates between n/2 and 0, and has no asymptote.

3

u/massive_cerebrum Sep 18 '26

Theta has nothing to do with best case. It's a mathematical concept used for a single function.

That function could be the worst case running time, best case running time or something else.

So for insertion sort, the worst case is actually theta(n2) and best case is theta(n).

Theta, Big O and Omega are not defined for algorithms, but rather for mathematical functions. What we typically mean is Big O of the runtime of an algorithm.

5

u/umop_aplsdn Sep 17 '26

No, this is misinformation. Big O and little O are completely orthogonal to worst and best-case. Most algorithm analysis focuses only on worst-case analysis (because best-case is often trivial).

16

u/UniqueSignificance77 Sep 17 '26

This, so many people in this thread do not know what Theta means.

It only exists if upper and lower bounds are in a constant factor of each other and the reason we do not use it is often NOT due to computer scientists not knowing the tightest bounds.

A linear search will NOT have a Theta because it is Omega(1) and O(n).

2

u/TwillAffirmer Sep 19 '26

You are being confidently incorrect. A linear search has a worst case time complexity of Θ(n) and a best case time complexity of Θ(1).

O(n) and Ω(1) do NOT refer to worst case or best case time complexities. Θ, O, and Ω are all applied to a single, definite, fully determined function of one variable. The worst case time complexity is one function of n, and the best case time complexity is a different function of n, and you can use Θ, O, and Ω to describe either of those two functions.

1

u/UniqueSignificance77 Sep 19 '26 edited Sep 19 '26

This is an interpretation problem.

The common interpretation is the number of operations the program does. Let's call the function mapping (input -> number of operations) f. For inputs where the list has length n, f has two free variables. The list L with len(L) = n and the search number s.

Regardless of what values are assigned to these two free variables, the number of operations performed by linear search is Ω(1) and O(n) where the bounds are correct over all lists L of length n and all search numbers s.

A Θ(g(n)) bound will not exist here because the tightest upper and lower bounds are not within a constant factor of each other such that it's correct for all such "cases".

Talking about the upper bound over this family using big O implicitly encodes "worst case" since this bound must hold true for all such free variables. Similarly Ω and best case.

Sure, you CAN talk about just the worst-case time complexity being Θ(n) and the best-case time complexity being Θ(1), but that's specifically ONLY talking about the maximum and minimum over these assignments and I do expect people to be specific if they're talking about only these.

When people normally talk about time complexity without explicitly mentioning specific cases, they care about bounds which hold on the whole set.

→ More replies (1)

4

u/not-just-yeti Sep 17 '26 edited Sep 17 '26

A linear search will NOT have a Theta because it is Omega(1) and O(n).

I agree with your gist, but be careful about phrasing: What is your function exactly? "The number of items needed to look at, to see if the collection of n items includes a particular target item?" That's not actually a function: when I input n=17 (say), there are many possible outputs (17 of them). That's why we usually make the function not "run-time on input of size n", but the "worst-case run-time over all inputs of size n" (or the average-case, or whatever). Once you clarify your function, it does have a big-Theta. (E.g., restricted to worst-case-behavior it's exactly linear, so it's bounded both above and below by n.)

But I agree with you that people usually leave this off, either assuming worst-case (or probably not really realizing they haven't defined their function for us). And in that way, you can still make sense of big-Oh and big-Omega ("I don't care which input of size 17 you use, the result is bounded above by 17" — essentially worst- or best-case without saying the words), but big-Theta wouldn't be sensible.

4

u/CattleHot9774 Sep 17 '26

in this case it'd be correct to say the best case runtime is theta(1) and worst case theta(n), right?

2

u/not-just-yeti Sep 17 '26

Yes! And the big-Theta (rather than big-Oh) is communicating that each of these is a tight bound.

→ More replies (1)

8

u/garanglow Sep 17 '26

This is inaccurate. The running time is usually defined as worst case running time.

In that sense, insertion sort runs in Theta(n2).

2

u/TwillAffirmer Sep 19 '26

An insertion sort has a worst case time complexity of Θ(n^2) and a best case time complexity of Θ(n).

O(n) does NOT refer to "worst case" and Θ(n) does NOT mean that best case and worst case are asymptotically the same. Θ, O, and Ω are all applied to a single, definite, fully determined function of one variable. The worst case time complexity is one function of n, and the best case time complexity is a completely different function of n, and you can pick either of those functions and use Θ, O, and Ω to describe it. You can't use a single expression O(n^2) to say that best and worst case are both bounded by k*n^2, because they are two different functions, and big O notation only applies to one function at a time.

I think it is this misconception that has led to the overuse of big O. In most cases, Θ is more specific and appropriate.

4

u/Aminumbra Sep 17 '26

This is (generally) false and it is baffling that people contradicting you are downvoted.

  • In the vast majority of cases, and almost always when there are no further precisions, the complexity function of an algorithm f(n) is the complexity of the algo in the worst-case over instances of size n. Other notions are studied/interesting too (average case (for uniform data or not ...), amortized complexity, etc), but we generally talk about worst-case complexity.
  • Now, we are only concerned with giving an "asymptotic estimate" of this function f:

    • Saying f(n) = O(g(n)) simply gives an upper bound on f. And usually, we are indeed interested in non-trivial upper bounds, and it is generally implicit that you are giving "the best upper bound that you can prove".
    • Saying that f(n) = Θ(n) gives both an upper and a lower bound on the worst-case complexity. This is in fact what we usually want, and OP is right. The reason we teach/say/use "big O" is mostly historical, convenience and inertia (O is not a weird letter, Theta is, sometimes it is impossible to prove a Theta so you need to know what the O(...) notation means anyway, etc).

Now, there are functions which don't admit (non-trivial, you can always write f = Θ(f) but that's useless ...) Theta, as their growth rate does not fall into a "neat" class, and there are problems for which we do not have one either: there are some "common" methods which give bounds such as "O(n^(1+epsilon)) for any epsilon but not O(n), for example.

→ More replies (10)

10

u/JoJoModding Sep 17 '26

Because we often can't find an actual tight bound. Sure, list sorting is in Theta(n log n) but it's one of the few problems where we can actually do this.

For example for matrix multiplication, it is not known what the fastest algorithm ist. It is known to be Omega(n²). Since the fastest currently known algorithm has a runtime of about O(2^(2.371177)). But this is an upper bound, in a few years they will probably found a slightly lower value for the exponent.

We care about the best currently known upper bound. And upper bounds means big-O.

7

u/Vim2K Sep 17 '26

Sometimes big theta and omega are interesting too, big O is just usually easier to work with than theta, and the upper bound of complexity typically represents a more important constraint on a program or system than the lower bound.

2

u/PersonalityIll9476 Sep 17 '26

That's not what Theta means. Theta means it is bounded above *and* below by that complexity, so it is exactly that. If I write "f(n) is Theta(n^2)" then it can not grow slower than n^2 nor faster, so it is basically a constant times a quadratic polynomial.

If you want to be precise, you could reply by saying "this function is o(n^2)" (note the little-oh) for upper bounds that are not tight.

People are just asking for the best known upper bound, colloquially. That's all.

2

u/FireworksWorks Sep 17 '26

It became a convention since, for some cases a theta is not proven. But we can still talk about the TIGHTEST upper bound, meaning the lowest upperbound we know of (which might be the theta, but also might not be).

So since researchers did this through out the years, it has become the convention for daily speak about Big O

1

u/attatest Sep 19 '26

More people should

1

u/Interesting_Debate57 Sep 20 '26

It's about the best known upper bound and the best known lower bound. Those are sometimes identical, I'm which case neither can ever be improved. But for most things that aren't sorting or reducible to sorting, there's a gap, so we know it takes at least as long as the lower bound and at worst as long as the upper bound, but we may not know either the best algorithm or the tightest bound for that algorithm, so we state what we do know instead.

Also keep in mind that there are conditions attached to these bounds. The well known bound for sorting is for a series of binary comparisons of data that doesn't have many or any duplicates in the data. Lots of data (like a list of a billion 4-digit years of birth) isn't unique and there are specialized algorithms because it's a more narrowly specified problem than the general case.

→ More replies (2)

2

u/Sjoerdiestriker Sep 18 '26

Any question in mathematics that asks "find an upper bound" implies "find a tight upper bound

I don't think this holds true. For instance, there are plenty of things you could prove from realising n! Is bounded above by nn, even if this is (both in absolute and relative terms) nowhere near a tight bound.

1

u/projexion_reflexion Sep 21 '26

Of course if big O has the n as an exponent or factorial, your algorithm is useless. 

1

u/Sjoerdiestriker Sep 22 '26

That isn't true necessarily either. Big O for algorithms just tells you how the problem scales with size, not how expensive it is for any finite size. It's perfectly possible for an algorithm to scale exponentially or superexponentially, yet still be cheap enough to be easily computable for any practical size of problem you might want to throw it at.

Likewise, it's perfectly possible to have an O(n) or O(1) algorithm that's way too expensive for any practical size of problem to ever actually execute. It'd just mean it wouldn't get much more expensive than that very quickly.

Lastly, big O is going to depend heavily on what you are actually using as a measure of size. An algorithm that scales linearly with the size of an input number scales exponentially with the number of digits of that input number, for instance.