问题
How is computational complexity defined if the algorithm:
- ...yields many results? As a total (then an algorithm producing a set of
k
cannot be faster than O(k) ) or per element (then the estimate must be multiplied to compare it with non-set-producing algorithms)?- What about storage complexity - does an estimate reflect if the entire set needs to be present in memory at once or each consecutive element can be produced and discarded?
- ...has multiple parameters? A separate figure for each parameter or something combined?
An example that suits both cases is selecting k
elements from N
. E.g. is there a difference in the estimate depending on whether ~k
or ~N
steps are required?
I'd like to see some hard evidence: the formal definition of the term in these cases and/or how these ambiguities are eliminated in CS papers rather than just random thoughts and/or personal experience. The goal is to work out a complete, state-of-the-art solution to eliminate these ambiguities in my (and others') texts once and for all.
The questions that had me puzzled about this are: Unique (non-repeating) random numbers in O(1)?, How do you efficiently generate a list of K non-repeating integers between 0 and an upper bound N, Algorithm to select a single, random combination of values?, Efficiently selecting a set of random elements from a linked list.
回答1:
These questions are usually answered by context.
You are right that an algorithm takes at least O(k) time when returning k elements. At least if it returns the elements at once. If the algorithm is invoked multiple times to get the result one element at a time, the stated time complexity may relate to per-element time. Amortized Complexity may help in these cases. For example, the union-find data structure has an amortized time complexity of O(alpha(n)) per operation. Space complexity usually does not include the space to store the result. But again, this should be clear from context.
For multiple parameters (or other independent or dependent variables), complexities are usually stated in a single expression. E.g., querying interval trees requires O(n + m) time, where n is the number of elements in the tree and m is the number of returned elements. Other variables may include data distribution or other characteristics.
回答2:
According to the answer at CS.SE:
- Time complexity and space complexity shall be reported separately and separately for each variable: "O(k) and O(n) time, O(1) space".
- If the time/space doesn't depend on some of the variables but not all, this can be told in plain text or with something like O(n0) for brevity (there's no universal convention here)
- Whether all the results are returned at the end or one by one is reflected by refferring to the algorithm as streaming (online) (one by one) or batch (all at the end)
- For a streaming algorithm, its delay can also be described:
For instance, a O(log n) delay algorithm returns results one at a time, taking at most O(log n) steps (running time) to output each result.
- For a streaming algorithm, its delay can also be described:
来源:https://stackoverflow.com/questions/39420745/computational-complexity-for-the-case-of-many-answers-or-multiple-parameters