upvote
I can help explain big O, it's a super weird concept:

Let's say you want to know the speed of an algorithm. That's a hard thing to do. First, there's the problem that it's different speeds on different computers. Then there's the problem that speed will be relative to the size of the thing you are processing. Then, there's the issue that what you really want is easy ways to compare algorithms, so you don't really want the exact speed of an algorithm but a quick estimate. Finally, you want to make it mathematically provable.

Big O is so weird because of the last two. If you want a quick estimate, you probably want it to be handwavy. But if you want it mathematically provable, then you don't. It feels like these two should be impossible to fit together. But Big O makes it work by putting algorithms into different buckets. If two algorithms are in the same bucket, you handwave minor differences in speed. But which bucket that it's in is mathematically provable.

The way Big O works is that you build an equation of how long it takes given some input with size n, then you take the largest term and you ignore any constant multipliers and smaller terms, with precise mathematical terminology that lets you do so. So the run time of some algorithm is 3 * n^2 + n, then it would be O(n^2), as that is the largest term.

The perfect place to see this in action is in sorting algorithms. They tend to fall into three main buckets - O(n^2) for slow algorithms that are easy to create, O(n lg n) for fast algorithms that are optimal, and O(n) for super fast algorithms that only work if the data you are sorting has particular properties. So simply by knowing which of these three buckets your sorting algorithm is in, I can tell if you are using a bad algorithm, using a good one, or making assumptions about your data that may or may not be true.

reply
That’s one of the best explanation I’ve read about Big O ever, thank you for writing this.

That’s a great skill to be able to explain challenging concepts cleanly.

reply
Thank you for this
reply
replying so i can find this on my home computer later
reply
You can save HN comments as favorites on their individual page (click on the comment’s time indication, then you get the option to “favorite”). No need to spam the thread.
reply
fantastic explanation
reply
On your last point: when talking about algorithms in the abstract, we still use similar structures to those used in programming; loops and branches (if/else) are the most critical in many of the common complexity analysis cases (big-O, little-O, big-Theta).

Big-O specifically refers to the worst-case time or space complexity of an algorithm. Assume that your algorithm operates on an input of n elements. Then:

- A straight iteration over every element (`for item in items: print(item)`) runs in linear time: O(n) for n elements.

- If your algorithm has a nested loop, and each of the loops has a bound which is either literally n, or derived from n, this is quadratic time (O(n^2)). For example, this naïve search for duplicates:

    for item in items:
        for dupe_candidate in items:
            if item == dupe_candidate:
                print(f"{item} is a dupe!")
This extends into higher powers (O(n^3), O(n^4), etc)) with deeper nesting of loops, and if inner loops are dependent upon other input parameters, you can use a separate variable for that (O(n^m) for example).

- There are also cases where algorithms run in sub-linear time. Two very common cases for that are logarithmic time (O(log(m, n)), which often comes up in algorithms over height-balanced search trees of arity m, and constant time (O(1)), which means that regardless of the input size, the algorithm will run in the same amount of time (which sounds great, until you realize that for large enough values of 1 this is actually really slow -- so you often see these constant-time algorithms find niche applications at large scales where even a very large 1 is still small).

From a math perspective, this is really just modelling a function's behaviour over a given input range, as well as when it tends towards infinity. Understanding your algorithm's time and space complexity as well as the input domain are a very common first step towards identifying the data structures and algorithms which will make up your application.

reply
Ah yes, I knew this in the past and you just helped me remember, thanks.

So basically, this is 2.25 nested loops!

reply
So one loop can essentially be cut into a quarter. Ie vertical and horizontal symmetry?
reply
Correct
reply
I've found asking for "a Bartosz Ciechanowski style explainer for..." and tasking it with a big model on high settings usually produces something that can teach you concepts.
reply
“Big o notation” are the search terms you’re looking for.

It’s a notation for quantifying the complexity of algorithms, by how the number of operations needed scales with the size of the input (often “n”). For example, greeting every person when you enter a room is O(n), because the number of greetings increases linearly with the number of people in the room. But having every person in a room greet every other person in a room is O(n^2), as the number of greetings scales quadratically with the number of people.

reply
And for anyone googling along at home: in this case, google also "big-theta" (bounded above and below, up to constants), and note that the hn headline at time of writing--"Matrix Multiplication is Θ(n^2.25)"--is not quite right.
reply