r/AskComputerScience • u/Leading-Fail-7263 • Mar 23 '26
Complexity seems un-rigorous, what am I missing?
Are the actual parameters of complexity, namely n (input size) and time (steps) subjective? Input size could be in terms of character length, items in an array, or anything that I could make up. And steps - who defines what a step is? Unless we model the program down to a Turing machine, how do we reach a universal consensus on what a step is?
And, if you're saying that it is subjective and up to the definer to decide -- doesn't that enable you to warp any programme to have the complexity you want? How does such a fundemental principle work with no crystal clear sub-definitions?
18
u/notjrm Mar 23 '26
It is, in a sense, subjective, but I would not say it is un-rigorous. Complexity depends on the representation of your data, yes. See for instance this Stackexchange discussion. I like Gilles' example:
The way to solve this problem is simply to make your modelling choices explicit: say how your data is represented, say what you consider to be an elementary step, etc.
15
u/thewiirocks Mar 23 '26
Complexity is a measurement of scaling, not of runtime. The goal is to understand how the algorithm changes performance as the complexity ramps up, not predict how well it will run at a certain scale.
For example, you might observe someone say that their algorithm takes three steps instead of one, therefore it’s O(3). This is incorrect. If it’s always three steps, then the scale is invariant and is O(1). Why is it O(1)? Because O(1) is a constant line across the graph. It is unaffected by scale. O(3) being a straight line slightly higher on the graph is irrelevant. We only care about the shape of the line.
Similarly, you might have different parts of the algorithm that scale differently. e.g. O(n) in one part and O(n2) in another part. The temptation is to try and come up with an equation like O(n + n2).
While this is not entirely wrong (e.g. we often talk about the average case of hash tables being O(m + 1)), it’s generally the wrong way to look at it. The worst performing part of the algorithm is going to dominate the scaling. Therefore, we “snap” to the worst performer and say it’s O(n2) in such cases.
So no, not arbitrary. Just generalized and unconcerned with precision.
4
u/Leverkaas2516 Mar 23 '26
Where you write "anything that I could make up", I would instead say "whatever is appropriate to the implementation." It is subjective to a degree, but the selection of what N means is guided by quite a lot of convention and prior art.
The question is largely resolved when you remember that the whole idea is communication between engineers and comparison between different algorithms for a given problem. Warping it to "have any complexity you want" serves no purpose. It's a lot like ordering a steak medium-rare. It's not an exact specification, but if you flout the conventions, you don't get what you want.
3
u/assumptioncookie Mar 23 '26
It's up to you to define n. When there's not a very obvious clear thing (like the length of an array you're sorting) you have to specify explicitly. A step can be any O(1) operation. You can just consider the graph with n on the x-axis and time on the y-axis and see what functions it will never outgrow.
2
u/Great-Powerful-Talia Mar 23 '26
You have to communicate the parameter you're using, either explicitly or by using the obvious one. If you go "my algorithm is O(1)" (with regard to the size of the elements) and that meaning doesn't make sense in context, you're just being deliberately misleading in a way that borders on lying.
2
u/miniatureconlangs Mar 25 '26
Scott Aaronson has written a bit about other resources besides time and space, e.g. ink (counting how often a symbol must be replaced), or head reversals. Turns out some are more interesting than others.
2
u/[deleted] Mar 23 '26
i think that is the reason, why big O notation is used.
since the runtime depends on the encoding of the input and the implementation/model of computation, the exact runtime might differ but asymptotically they are similar
1
u/Beregolas Mar 23 '26
No, but they come in different degrees of usefulness.
For most problems there is an intuitive answer to this. If we are sorting an array for example, the only thing that really makes sense is the amount of items in the array. If we are talking about a String, we normally mean the number of characters in this string* (*careful: in reality this is not always true, as unicode is doing unicode things, but for algorithm design we can simplify a character to be a u8 or something. In theory there is no difference between theory and reality)
And when we talk about graphs... we actually don't normally use n. At least at my university, we defaulted to v, e and or a. v meaning vertices, e meaning edges, and a meaning arcs. (arcs are directed edges) So in cases where "n" would be confusing, because we are talking about one of multiple input values, we actually do distinguish.
And while v, e and a are shorthands you need to know for them to make sense, there are algorithms that have a runtime complexity of O(n^2+m) or something. One example I could pull out of my ass for this would be: Consider a list of m matrixes of size n*n. You want to compute the matrix of size n*n so that for each element e_i_j with 0<i<n+1 and 0<j<n+1 of this new matrix contains the minimum value found at each e_i_j of any matrix. Basically: Treat all n^2 fields of the matrixes as unsorted lists and find the minimum for each, which takes O(n) time, where n is the length of said list.
There are several definitions of what counts as a step, and they actually differ depending on what you are interested in / in which context you are looking at the runtime complexity. In one DSA class we actually defined a set of "steps" an algorithm could take. We even wrote a proper grammar for them. I think they were along the lines of "assign a value to a variable, read a variable and perform "basic" operators (like +, -, <, >, etc.)" But you will soon learn: This actually is not at all important for complexity.
The "easy" answer to what a step is is btw: Anything that requires the "tracked variable", mostly n, to work.
Let's look at an example:
As long as we can agree on the fact, that at least 1 step is being taken in each of my comments, we get the same conclusion: O(n^2), where n is the length of the list. The thing is, we could argue, that even if NO "step" is being performed inside of my comments, the fact that we are even looping over the variables means, that the algorithm is O(n^2) (because compiler optimizations are not part of an algorithm, again, theory vs. reality).
I mean, you could say that, but not in a way that is grounded in facts, or supported by complexity theory. The thing is: Of course you can lie, or use data to mislead. You can do that with ANYTHING. Literally. As long as you define "health" as "having a heart rate of no more than 120 BPM", every dead person is perfectly healthy. As long as you say "Let's look at the runtime complexity of merge sort with n=the height of the eiffel tower", of course it's in O(1). Complexity theory is not the reason people can lie and say bullshit ;)
1
u/Scharrack Mar 23 '26
Not subjective but case dependent, at least if you want it to have any meaning. Essentially the notation gives you information about how an algorithm scales with the chosen parameter, for example there is a search tree where searches on average scale logarithmic with the size of the Data you are searching through while at worst case being constant with key length.
1
u/not-just-yeti Mar 23 '26
Think of the input size is being ng the #bits of the input. But if you want it to be the # of
int32s in the input (“the size of the array”), that’s within a constant factor, so gets washed out in big-Oh. And typically any reasonable input-format differs only by constant factors. (But /u/notjrm cites an interesting example where an alternate input format “could” make a big-Oh difference.)Similarly, a Turing Machine nails down number-of-steps, as you mention. But that’s within a constant factor of any assembly language, which’ll be a constant factor of the C code, etc. [Okay, you don’t want to use a Turing Machine as your abstract model; instead look at “Random Access Machine” model.] And “number of times through some loop” is often clearly within the same big-Oh, so that may well be a reasonable “number of steps”.
And at the end of the day, one might want “running time of this algorithm, in milliseconds“. But of course you’d have to totally specify your hardware, cache, OS version, etc for that to even be well-defined. And since the run-time behavior of (say) insert-sort has some intrinsic properties that aren’t hardware-dependent, you can think of big-Oh as giving us the aspects of the algorithm that don’t change as our hardware’s specs improve over the years.
1
u/Objective_Mine MSCS, CS Pro (10+) Mar 23 '26 edited Mar 23 '26
You're right that what exactly is defined as a single "step" can be ambiguous, but that generally turns out not to be a problem.
Even if what can be done in a single step differs between different machines, they're typically only different by a constant factor.
Let's say that one machine has an instruction that loads a value, increments it, and stores the new value all in a single step. Another machine requires three separate steps instead. However, that still means just means multiplying or dividing the complexity by a constant of three.
Both asymptotic notation and problem complexity classes have been formally and rigorously defined but in such a way that constant coefficients don't matter.
If you have a function with linear growth, it's still linear even if you multiply it by 1/2 or 2 or 1000. Quadratic growth is similarly quadratic regardless of any constant coefficients.
Although that's more of an intuitive explanation, the formal definitions of asymptotic notation also reflect this: constant coefficients don't change asymptotic complexity.
An exception to this is of course that due to the non-random access nature of the Turing machine tape, Turing machines may require N steps in order to do what a random access machine could do in a single step. So when going between Turing machines and random access machines (or similar real-world computers), it's true that asymptotic complexities can turn out different.
When the complexity of an algorithm is expressed in asymptotic notation, a random access model is generally assumed. And as long as you stay within random access-style machines, asymptotic complexities don't generally depend on the exact set of allowed instructions. If someone wanted to discuss big O complexity for a Turing machine instead, they'd have to specifically make that clear. It's generally not unclear which one is being discussed.
Many of the complexity classes used in computational complexity theory have been explicitly defined for Turing machines instead, but the class definitions are in many cases flexible enough (yet still formal and rigorous) that it turns out not to matter.
As far as what you consider as the "input size" goes: constant-factor differences again don't matter. The complexity of an algorithm generally doesn't change if you consider you input size to be in bits instead of characters. Even though a single Unicode code point may take up to 32 bits, the number of bits in a string still differs from the number of code points by at most a constant factor of 32. A speedup or slowdown of 32 times can of course be practically significant but it doesn't change asymptotic complexity: e.g. linear-time is still linear-time.
In some cases what exactly you're measuring as your input size could be ambiguous, though. If you have an algorithm for graphs and you analyze its complexity in relation to the size of the graph, what exactly does the "size of the graph" mean? Number of vertices? Number of edges?
In such a case the complexity result should be explicit about what's being considered as the input size. The complexity of graph algorithms often depends on both the number of vertices and edges, so the complexity of a graph algorithm is often expressed in terms of both. For example Dijkstra's algorithm has a time complexity of Θ(|E| + |V| log |V|) where |E| is the number of edges and |V| is the number of vertices in the graph.
And even if the complexity of an algorithm only depended on, say, the number of vertices, you should still make it explicit that your "N" is the number of vertices.
If the complexity result didn't make it explicit whether the size of the input is in terms of edges or vertices, that would indeed be ambiguous.
1
u/Aaron1924 Mar 23 '26
This is one place where CS and SE differ somewhat. In theoretical computer science, the runtime complexity is always given in terms of the size of the input, measures in symbols on the tape, which usually corresponds to bits. For algorithms that work on arrays (e.g. sorting algorithms), you can usually assume that the element in the array have a fixed size, so the size of the input ends up being the length of the array (times some constant). For algorithms that take a number (e.g. prime check), the size is the logarithm of the input number; this is why the trial division is considered exponential time.
In software engineering, it is often a detour to calculate the size of the input to give the runtime bound correctly. It is much easier for both implementers and users of library function to give runtime complexity relative to properties of the input directly, e.g. stating trial division has having O(n^2) runtime, where "n" is the input to the function.
1
u/FlamingSea3 Mar 23 '26
I wouldn't consider steps a parameter to complexity. A step in one algorithm doesn't always line up nicely with a step in another -- even if the algorithms perform the same task. For example, sorting algorithms usually have steps as 'compares' and 'moves'. But a comparison in a radix sort is a whole different beast than a comparison in a bubble sort. I'd agree that steps are arbitrary - and since O(n) notation drops constants from the terms the relative amount of work in each step doesn't matter. Just how quickly the number of steps grows as the input size increases.
Speaking of input size, these parameters are products of analyzing the algorithm. Said analysis usually amounts to figuring out how many times each expression in the algorithm gets executed, and what factors change that.
If you have a loop over the inputs, everything inside will execute roughly once for each input or O(n). Put another loop inside, multiply by n and get O(n^2). Realize that comparing strings has another loop inside, multiply by the average length of each input k - getting us O(k*n^2). Then remember that you're sorting books by author and title, and while those can get long, you have 1000s of books, so the number of books dominates the time taken, getting us back to O(n^2).
Point being, whatever parameters you choose for your big O notation, you need to be able to justify why you choose them; and also the absence of a more impactful parameter.
And then you go and try to benchmark your algorithm and find that you have several helpful gremlins making it a lot harder to figure out the performance. Such as the compiler optimizing large swaths of your algorithm away because it realized that some of the arguments were constant; the compiler deciding to take both branches because the branch misprediction cost on the target cpu takes twice as long as just performing both branches; the cpu doing superscalar shenanigans that mess with your assumptions about how code is executed; bad locality (temporal and spacial) in your memory accesses making the cache unable to speed up memory; and Windows deciding that now's a good time to decompress the update it just downloaded.
Probably should have led with this: Big O notation is just an estimate on how the algorithm scales.
1
u/PassionatePossum Mar 23 '26 edited Mar 23 '26
What defines a step depends on the machine model that is used.
The most common one is the RAM model. If you talk about computational complexity this is usually what you are using. In this model, every memory access is a step.
Obviously this model is not suitable for every application. For distributed systems you often use a different model that is geared towards I/O efficiency where one step means loading items into a local memory and all operations you do inside local memory are “free”.
And of course algorithms who are optimal in one machine model are not necessarily optimal in another.
In a sense these models are arbitrary. You can come up with something completely different. But they are good starting points to compare algorithms.
1
u/ericbythebay Mar 23 '26
Complexity is an approximation. If you want to know how long it actually takes, you have to measure it.
1
u/DTux5249 Mar 23 '26
Not subjective, just imprecise. Imprecision isn't a bad thing, because this is fundamentally comparative.
No operation is gonna take the exact same amount of time repeatedly. But we don't care about about the exact amount of time taken; we care about how well the model stands up to problem size changes.
But you're right that depending on the problem, things can break down. Division is technically an O(n²) operation, not constant. But we consider it O(1) for most applications because dividing a number by 2 is trivial, and won't take long even if you don't just bitshift the thing.
In general, these are simply comparative metrics for making DSA choices. Not rigorous tools for calculating the actual time taken by an algorithm. If you're CRUDing the fuck out of a set of values, and don't care much about relative indexing, you use a hashtable; not a skiplist. The way we justify that decision to people formally is time complexity.
1
u/trejj Mar 23 '26 edited Mar 23 '26
Yes, that is correct, and you are allowed to. Input size should reflect whatever you want to measure the complexity with respect to.
If you are interested in how runtime grows as a function of number of items in an array, then you will take N to be the number of items in an array.
If you are interested in how runtime grows as a function of the length of the items in the array (maybe cumulative/summed over all elements), then you take that to be N.
N will be whatever is relevant to the analysis at hand. It is rigorous, but the meaning of N can freely change from one analysis to the next, depending on what you are interested in analysing for that particular case at hand.
For example, if you have a list of items and you want to find a maximum.. typically the interesting part there is to analyze the runtime as a function of the list length, so N will then be the length of the array.
But, if you have two numbers and you want to find the maximum of those two... (e.g. maybe you might be implementing a max() function in Verilog for realisation in FPGA or other hardware circuits) .. then it will be interesting to place N to be the length of the two numbers, in terms of bits. (either max # bits of the two inputs, or sum of bits, the analysis actually works out to be the same)
So what N is can depend on what is interesting, and what the computational model of the hardware at hand is.
You can! The beauty of complexity theory is that it does not matter. You can make a step to be "every assembly instruction" to go to the lowest level, or you can make a step be "every time I change the value of a CPU register or memory", or you can make a step be "every time I perform a computation on one of the input elements".
A step is typically any action in any fictional abstract machine that is computing the algorithm at hand. But often in analysis many actions of the machine can be ignored, since those won't have an effect to the analysis, and to avoid making the analysis obtuse.
No, the reason for this is that you are performing an analysing relative to the input. When you are calculating on any abstract machine of your choosing, you cannot escape that it needs to access the data to operate on it.
For example, if you have N elements of input data, and try to find a maximum value of the data set. This must take at least N steps, since your abstract machine needs to access each element at least once.
In another abstract machine, you might need, say, 10 steps to access a single element of input data (as opposed to just one step above). Then to find the maximum value, you would use 10*N steps at least.
But the way that complexity classes are calculated, all constant factors melt away. So both 10*N ∈ O(N) and N ∈ O(N).
You could of course try to "cheat" and state that in your own abstract machine, you have an instruction that accesses all input elements in one step, and returns the maximum value. Running that single operation would give you an operational runtime of O(1) for finding the maximum value. But, while pedantically true, such an abstract machine would not match the performance characteristics of any abstract machine that we know how to build in real world, so it would not be particularly applicable or interesting to state.
(Quantum computers are one example of a situation where the above kind of play happens, with Grover's Search algorithm)
1
u/Key_Net820 Mar 24 '26
Yes it is rigorous. What you're saying is also valid in that you can consider nested variables. For example, If we consider a function that simply counts 1 to N, this runs in O(N) time when you think of it in terms of integers. But when you think in terms of bits, you need 2^n bits to store an integer of size N, so the complexity is O(2^n) in terms of bits.
Both are correct, but it's just a matter of what variable are you considering the function of.
1
u/Crazy-Willingness951 Mar 24 '26
Look at "cyclomatic complexity" and tools that measure it.
1
u/buzzon Mar 23 '26
No, it's not subjective.
Input size of array in bytes and input size of array in elements only differ by a constant factor, so their complexity class would not change.
A step is a minimum singular step of a typical computer you would run your program on, and if you want to run it on another machine, they would only differ by a constant factor as well, if each operation of one computer can be emulated in a finite and fixed number of steps.
1
u/esaule Mar 23 '26
No, it is not subjective. Your machine model defines what a step is. Typically we use the RAM model to do the analysis. If you pick a different machine model, you need to specify.
For what parameter you use, you can use any function of the input.