11

Consider the Kadane's algorithm for finding maximum subarray within an array:

def max_subarray(numbers):
    """Find the largest sum of any contiguous subarray."""
best_sum = 0
current_sum = 0
for x in numbers:
    current_sum = max(0, current_sum + x)
    best_sum = max(best_sum, current_sum)
return best_sum

The algorithm requires constant space to execute, apparently. But still, it accepts a list of n elements as input. So is its space complexity O(n) or O(1)?

Eugene Yarmash
  • 275
  • 3
  • 8

5 Answers5

25

It depends on the chosen convention. I often prefer the convention that considers that the input is not part of the space complexity, for different reasons:

  • the space complexity of a function answer the question "do I have enough memory to finish my computation?". In that aspect, I consider that when calling a function, its argument is already written somewhere in the memory, and therefore there is no need to consider it for the future computation (we already know that there is enough space for the input).
  • the complexity classes L and NL (problems that can be solved deterministically and non-deterministically respectively in logarithmic space) make no sense if you consider the input as part of the space complexity (otherwise any space complexity would always be at least linear).
Nathaniel
  • 18,309
  • 2
  • 30
  • 58
6

Auxiliary Space: The extra space that is taken by an algorithm temporarily to finish its work

Space Complexity: Space complexity is the total space taken by the algorithm with respect to the input size plus the auxiliary space that the algorithm uses.

Referred from here

i.e., space complexity of a program on the whole also includes the space taken up by the input values. Space Complexity = Auxiliary space + Space used up by input values.

But when we compare two algorithms that have the same end goal with similar input types, often the space taken up by the input is disregarded. Only the auxiliary space of the algorithm is considered.

Hence it is said that Kadane's algorithm runs in constant space i.e. O(1)

subasri_
  • 169
  • 4
4

No, the input does not count in the space complexity. The data has to be supplied anyway and if we counted it, no $o(n)$ complexity (in particular $O(1)$) would be possible.

1

Complexity analysis is usually done to compare algorithms that process the same input. In this case, it makes little sense to include the space of the input data, because it's the same for all algorithms being compared and doesn't affect the comparison.

However, we might want to compare algorithms that process different forms of input, e.g. arrays versus linked lists. In this case, the space used by the input is a function of the algorithm, so we should include the input space.

Barmar
  • 473
  • 2
  • 9
0

While the question has been mostly answered by other people, I want to make a note that in the algorithm described, you have to keep track of the index you are at to iterate to the next array element. This index tracker, at last position, grows as input grows, so it is not quite O(1) in terms of space, but O(lg(n)) (since you can keep the binary representation of the index).

Although, again, saying O(1) is convention in some places.

Pozay
  • 1
  • 1