Live data from Hacker News

Big-O notation explained by a self-taught programmer

justin.abrah.ms

51–60 of 80 posts

Re: Big-O notation explained by a self-taught programmer

#51
post #46

This sort of contributes to giving self-taught programmers a rather bad name. The writeup is good, but the idea that Big O is "scary" is just absurd. It's an elementary concept that every working programmer should be familiar with, regardless of whether they're self-taught. Algorithms are not "scary". If you can't reason about algorithms, you may not be a very good programmer yet. To be clear, I really appreciate the…

but the idea that Big O is "scary" is just absurd. To you. I never studied things like these in school and getting to a point of "Oh that's what that means" was long and arduous as it pertained to a lot of scientific literature. Plain English, it seems, isn't in the tool set for a lot of very smart people who, coincidentally, feel that it's their duty to Explain All the Things . It's unfortunate that so many of them…

I'm as autodidactic as they come (high school drop out, self-taught in math, comp sci, psychology) and I to think this is elementary stuff.

The correct way to "understand these things" is to learn the math, the theory, and do the exercises.

Would you take a scientist seriously if they didn't understand what the scientific method was?

You're right. English is not the tool to use here in explaining theory, it's not capable of it. Mathematics and programming languages are!

Re: Big-O notation explained by a self-taught programmer

#52
post #9

O(N) is read "Order of N" because the O function is also known as the Order function. I think this is because we're doing approximation, which deals in "orders of magnitude". It's a different meaning of "order", that has to do with the shape of the size-vs-time curve. It's the same meaning as the order of a polynomial ("x" is linear or 1st order, "x^2" is quadratic or 2nd order, etc). but Big-O is all about the appro…

Thanks for your comment. I must admit that my lack of mathematical background makes your clarification difficult to understand. I've not heard the term "order of a polynomial". If you wrote up a post and emailed me at justin@abrah.ms explaining the concept, I'd happily link it in this article. :)

I'm self taught too. Pick up Knuths books and start going through them, stop and learn the math if you don't understand it. His first book in particular begins with some mathematical foundations that will dramatically change how you reason about your programs.

This stuff isn't scary. It's thick but learnable and you can even develop an appreciation for its elegance.

Re: Big-O notation explained by a self-taught programmer

#53
post #51
post #46

Earlier quoted context omitted.

but the idea that Big O is "scary" is just absurd. To you. I never studied things like these in school and getting to a point of "Oh that's what that means" was long and arduous as it pertained to a lot of scientific literature. Plain English, it seems, isn't in the tool set for a lot of very smart people who, coincidentally, feel that it's their duty to Explain All the Things . It's unfortunate that so many of them…

I'm as autodidactic as they come (high school drop out, self-taught in math, comp sci, psychology) and I to think this is elementary stuff. The correct way to "understand these things" is to learn the math, the theory, and do the exercises. Would you take a scientist seriously if they didn't understand what the scientific method was? You're right. English is not the tool to use here in explaining theory, it's not cap…

It's unfortunate you've read my post as a claim to avoid actually learning these.

I was pointing out that the "x is elementary" attitude doesn't help the process and I thought (from the video at least) some empathy in communication is warranted. It's ridiculous to expect clarity when no such thing exists in the language used to describe an idea in the first place. The OP went to the trouble of making such a post. Maybe it wasn't perfect, but it gets the ball rolling.

Whatever inadequacies can be corrected with feedback and I for one would like to see more people engage in humane explanations for things pertaining to their expertise.

Big-O is "technical". Understanding of it comes with clear explanations.

No where did I claim anything contrary to : "The correct way to "understand these things" is to learn the math, the theory, and do the exercises." What I claimed was that language can soften the barrier to learning these as I imagine it did for you.

Re: Big-O notation explained by a self-taught programmer

#54
post #47
post #19

Earlier quoted context omitted.

Note that there are other bounding functions, like bounded from the bottom (which still isn't the same thing as worst-case). See https://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachm... . Speaking of worst-case (or best-case, average-case, etc.) scenarios, how does big O notation relate? The variables inside an O() notation as far as I know refer only to the size of the input, so when we say that finding a val…

Here's an answer of mine on Quora you might find useful: https://www.quora.com/Algorithms/How-can-I-determine-whether... There are two things going on. First, when we talk about Big-O we're not talking about the "worst case scenario." Big-O gives us an upper bound on the worst case scenario, but the actual worst case scenario might be better. Big-O means "no worse than" not "as bad as." When most people say Big-O the…

It's rather unclear from your example that f(n) is in fact itself a description of the runtime of an algorithm, not an algorithm itself.

Re: Big-O notation explained by a self-taught programmer

#55
post #50
post #47

Earlier quoted context omitted.

Here's an answer of mine on Quora you might find useful: https://www.quora.com/Algorithms/How-can-I-determine-whether... There are two things going on. First, when we talk about Big-O we're not talking about the "worst case scenario." Big-O gives us an upper bound on the worst case scenario, but the actual worst case scenario might be better. Big-O means "no worse than" not "as bad as." When most people say Big-O the…

It sounds to me like you're mixing up two things: "worst case performance" and "asymptotic performance." You say > Big-O is concerned with worst case performance. Colloquially, f∈O(g) means that "Eventually, f performs no worse than g." Aren't these two different concepts? Worst case performance deals with the worst possible input of any given input size (like pathological inputs to Quicksort), while asymptotic perfo…

Let's be precise. I'm being more precise here in my comment than I was on Quora.

Big-O and related notations are ways of categorizing functions. O(n^2) for example is actually a set of functions, which is why I wrote f ∈ O(n^2) rather than something like f = O(n^2) or f(n) = O(n^2). That is, f is a member of some set of functions which all satisfy a particular, precisely-defined property.

To understand that property, first, let's get rid of the idea of "performance" because asymptotic analysis has nothing to do with "performance" per se and predates even the first precise definitions of things like "algorithm" or "computability." The notation itself was invented in the late 19th century.

Instead, let's just talk about "upper bounds." If we have a function it's easy to talk about upper bounds. For example,

  f(x) = sin(x)
is bounded above by 1, 1.5, 10, 100, 80457, and an infinitude of other numbers for any real number x. It's bounded below by -1.

Now, in this case, it's east for us to see that not only is

  sin(x) 
but also that

  max { sin(x) : x is real } = 1
So in this sense the upper bound of 1 is strict. 2 is also an upper bound in the sense that

  sin(x) 
but it's not strict. There are other upper bounds which are strictly smaller than 2, e.g., 1.5. So, we can say that "the value of sin(x) for real x is no greater than 2," but we can't say that it "is" 2.

So, to answer your point before diving deeper, Big-O is about "worst case performance" in this sense. By itself it doesn't tell us what the worst case performance is. Instead, it gives us an upper bound on the worst case performance. It says "the worst case performance is no worse than FOO." The actual worst case performance might be better.

Big-Θ is the asymptotic equivalent to "this is a tight upper bound."

I'll skip further development of this for now and jump back to the issue of algorithms. The issue is this: given an algorithm with input of length N, we want to say something about how long it takes to run.

This means that the function we're analyzing isn't "QuickSort(n)". What does that even mean? The input of QuickSort is an array of integers and it returns a sorted array of integers. How can an array of anything be greater than or equal to n^2? So that's one way in which the CS vernacular equivocates -- we're not really talking about QuickSort we're talking about some other function:

  T(n) = the amount of time it takes QuickSort to run given an input of length n
We're then talking about bounds on this other function T, asymptotic or otherwise.

But now we're in a pickle because what does "the amount of time it takes QuickSort to run given an input of length n" mean? There are many inputs of length n. If we're talking about just arrays of integers of length n, there are n! if all we care about is relative ordering and not the actual values in the array. If we care about the actual values in the array then there are an infinitude of inputs of length n.

There are a few ways we can handle this. Let's re-define T(n) like so:

  T(x) = the amount of time it takes QuickSort to run given input x
One way is the "worst case" method. This says, ok, look at this function:

  W(n) = max { T(x) : x is a valid input to QuickSort and len(x) == n }
We can now do Big-O, bounds, asymptotic analysis, etc. on W(n). This is what we mean when we say the worst case is O(n^2). It means W ∈ O(n^2).

Another way is the "average case" method. This says, ok, look at this function:

  A(n) = avg { T(x) : x is a valid input to QuickSort and len(x) == n }
This is tricky if there are in principle an infinite number of valid inputs of a given length. There are various ways of handling this issue. For something like QuickSort we can see that it's really only the ordering that matters, i.e., for the purposes of QuickSort [1,10,5] is the same operation-wise as [-50, 80, 0], so there are only n! inputs we really need to check for a given n.

Yet another way is the "best case" method, which looks at

  B(n) = min { T(x) : x is a valid input to QuickSort and len(x) == n }
So, given an algorithm we can derive these three functions and then answer Big-O questions about them. We're never answering Big-O questions about the algorithm per se, although we can get away with equivocating when W(n), A(n), and B(n) are always equal or it's obvious we only care about one of them.

For simple examples this is often the case, e.g., calculating the nth Fibonacci number in the standard iterative way has best, average, and worse case performance of O(n).

To make matters worse, most people say Big-O but mean Big-Θ, or at the very least aren't clear when they mean one or the other. So, when one says "worst case performance" and we have W(n), A(n), and B(n) all being the same, it can be particularly confusing.

Depending on the algorithm in question which it might be understood what we care about one more than the others. For example, if worst case inputs are particularly pathological we might talk as if we mean the performance of the algorithm per se but really be talking about A(n). However, if "bad" inputs are common we might really be talking about W(n).

Re: Big-O notation explained by a self-taught programmer

#56
post #50
post #47

Earlier quoted context omitted.

Here's an answer of mine on Quora you might find useful: https://www.quora.com/Algorithms/How-can-I-determine-whether... There are two things going on. First, when we talk about Big-O we're not talking about the "worst case scenario." Big-O gives us an upper bound on the worst case scenario, but the actual worst case scenario might be better. Big-O means "no worse than" not "as bad as." When most people say Big-O the…

It sounds to me like you're mixing up two things: "worst case performance" and "asymptotic performance." You say > Big-O is concerned with worst case performance. Colloquially, f∈O(g) means that "Eventually, f performs no worse than g." Aren't these two different concepts? Worst case performance deals with the worst possible input of any given input size (like pathological inputs to Quicksort), while asymptotic perfo…

I'd summarize my other comment this way.

Asymptotic analysis only makes sense in the context of functions whose inputs are real numbers (possibly integers) and whose outputs are real numbers. When we want to do asymptotic analysis of algorithms we need to derive functions amenable to this analysis and for a given algorithm there is more than one derived function we might care to look at.

But in any Big-O situation the person is always, always, always talking about a function from the real line to the real line, with perhaps many layers of confusion and equivocation in between. You should be able to suss out what function they're "really" talking about.

Phrases like "worst case", "average case", and "best case" are shorthand for three of the most common derived functions.

If you want, think of a function T which takes as its input an algorithm and an input to that algorithm and returns its running time.

  T(QuickSort)(x) = running time of QuickSort given x as its input
then

  W(QuickSort)(n) = max { T(QuickSort)(x) : length(x) == n }
We're then in the business of doing asymptotic analysis on W(QuickSort), A(QuickSort), and B(QuickSort).

Re: Big-O notation explained by a self-taught programmer

#57
post #47

Earlier quoted context omitted.

Here's an answer of mine on Quora you might find useful: https://www.quora.com/Algorithms/How-can-I-determine-whether... There are two things going on. First, when we talk about Big-O we're not talking about the "worst case scenario." Big-O gives us an upper bound on the worst case scenario, but the actual worst case scenario might be better. Big-O means "no worse than" not "as bad as." When most people say Big-O the…

It's rather unclear from your example that f(n) is in fact itself a description of the runtime of an algorithm, not an algorithm itself.

Right, yes, that's the main thing to understand and it's also the thing that most explanations obscure. I tried to be clear about that, but wasn't clear enough. See my follow-up comment.

Re: Big-O notation explained by a self-taught programmer

#58
I don't really know why Big-O notation is so common , even though Big O is the upper bound . For me it is more practical and logical to use the Big Θ (Theta) notation as it provides a tighter bounder which is more understand able. Also Big O is very misleading to the new comers, as they are usually confused when they see something like O(n) = O(n^2) which is perfectly valid , as the Big O notation is only the upper bound albeit it will be a loose upper bound.

For all we care , we can write the Big O of

for ( i = 0 ; i as O( n!) , it won't be mathematically wrong but again it would be very misleading and loose :). So my advice to everyone is to use the Big Θ notation

As f(x)= Big Θ(g(x)) when f(x) = Big O ( g(x) ) and Big Ω(g(x)) .

Here for those that don't know what Big Ω(g(x)) (read Big omega) is, it is a lower bound . In English that would be that your loop will execute/iterate at least g(x) times.

Now before people get any more confused Big Θ(g(x)) is a tight bound , that means that your code/loop will run at least C1 * (g(x)) and at most C2 * (g(x)) . where C1 and C2 are two constants .

If anyone is interested they should really read CLRS. It has an excellent chapter on calculating and explaining the time complexities.

Re: Big-O notation explained by a self-taught programmer

#59
post #58

I don't really know why Big-O notation is so common , even though Big O is the upper bound . For me it is more practical and logical to use the Big Θ (Theta) notation as it provides a tighter bounder which is more understand able. Also Big O is very misleading to the new comers, as they are usually confused when they see something like O(n) = O(n^2) which is perfectly valid , as the Big O notation is only the upper b…

Theta is much harder to type and slightly harder to write. Besides, people are trying to be reasonable about their bounds.

Re: Big-O notation explained by a self-taught programmer

#60
post #58

I don't really know why Big-O notation is so common , even though Big O is the upper bound . For me it is more practical and logical to use the Big Θ (Theta) notation as it provides a tighter bounder which is more understand able. Also Big O is very misleading to the new comers, as they are usually confused when they see something like O(n) = O(n^2) which is perfectly valid , as the Big O notation is only the upper b…

Of course O(n) is not equal to O(n^2). O(n) is a set, and O(n^2) is a different set.
Post reply on HN