These math proofs are so insanely over my head, but I really want to understand them. I tried Jane Street's socratic tutor skill (https://github.com/janestreet/agent_skills/blob/master/socra...) against this pdf and here's what I got:
- Matrix multiplication is multiplying pairs of numbers together then summing them (you probably know this already)
- Without any optimization, this process is o(n^3)
- You can make this faster if you share a multiplication or sum across multiple places
- By sharing sums / multiplication operations the maximum efficiency is o(n^2.25)
I studied Economics, not CS so I'm a bit hazy on what big o actually means, to be honest.
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.
“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.
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.