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

118 Upvotes

64 comments sorted by

View all comments

Show parent comment

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.

2

u/TwillAffirmer Sep 19 '26 edited Sep 19 '26

The function mapping (input -> number of operations) has a lot more than two variables. An input of size n is n dimensional, so it has n variables, and that increases with n. In general the input space is infinite dimensional. But that's not how we do asymptotic analysis.

When we talk about asymptotic analysis with big-O and Θ, we are talking about functions Z -> Z mapping (size of input -> number of operations). These are functions of one variable. For a given algorithm, there are several such functions of interest.

Yes, you CAN talk about just the worst-case and best-case time complexity, or average case, being specific to say which you mean, and that's what reputable sources trying to be correct will do. Just to say the "time complexity" is underspecified. And if you want to adopt the convoluted convention that the "time complexity is O(n)" means it's O(n) for every such function Z->R for the algorithm, you won't find it any textbook. It's the same as the worst-case complexity, so you should just say "worst case complexity" and then use Θ.

The legitimate reason to use O(n) in computing is usually when there's a specific function but you don't know exactly how it behaves for large n. For example, we know that the time complexity of an optimal algorithm for matrix multiplication is O(n2.371177). It's not Θ(n2.371177) because we don't know the optimal algorithm, O(n2.371177) is simply the worst-case complexity of the best algorithm known. Another time you might use O(n) is if the exact complexity depends on a lot of log factors or log(log()) factors and is verbose to state, so you simply round up the polynomial part.

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.