9

This is slightly confusing to me. What should be my approach of solving a given problem when the constraint is as follows:

1) Without using extra space: For e.g.: If I want to sort a given array, I have few ways of doing it. Bubble sort, which keeps on swapping ( just loops, no recursion). I believe this is said to be without using extra space. What is the case if I use a recursion to sort the elements. Is it the same as "without using extra space", or the stack used is counted in the Space complexity of the algorithm?

2) In O(1) space: What is the meaning of O(1) space? Does it mean constant space. Now if it is constant space then please comment on the following cases:

a) If I am swapping in bubble sort with the help of the third variable. Isn't it an extra space and it will not depend on the size of input so it is in constant space.

b) Moreover if I am using count sort being applied on natural numbers, where it doesn't really require the amount of space proportional to the total numbers, do we consider it to be in constant space O(1).

Please explain the difference if any. Thanks

dharam
  • 7,882
  • 15
  • 65
  • 93

4 Answers4

5

According to Fortnow & Homer (2003),

The space complexity of the computation is [...] taken to be the amount of space used on the work tapes.

Sorting algorithm are all O(n) space in the least, since it needs the space to store all the inputs (no matter on memory or on disk). Therefore, even for bubble sort, the space complexity is still O(n).

However, sometimes, we are not interested in the overall space complexity (esp. in the case above), but we want to know the additional space used by the algorithm. For bubble sort, we can say that it uses constant amount of additional space.

Recursion is quite a special case where we have to consider stack. We are storing the state when we recurse, and we call the recursive function many times based on the input. As the number of recursion level depends on the input size, the space complexity must take into consideration the stack space usage.

I'm not sure if O(1) space algorithm is common or not, but Cycle Finding algorithm is one of such example. The algorithm by itself only use space for exactly 2 "pointers". Extra spaces used by the function whose cycle to be find should be counted separately.

In case of counting sort, the space complexity depends on the size of the input n (the count) and the maximum input value k. The 2 parameters are independent of each other, hence the space complexity is O(n + k). The additional space used can be defined as O(k).

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • From your source: *"This is a (deterministic or nondeterministic) Turing machine whose input is written on a special read-only input tape. Computation is carried out on read-write work tapes which are initially blank. The space complexity of the computation is then taken to be the amount of space used on the work tapes."* It is clear that the "work tapes" are distinct from the "input tape", and so the size of the input tape is not counted towards "the space complexity of the computation". The section this is quoted from is explicitly making this point, to say why O(log n) space is meaningful. – kaya3 Feb 09 '22 at 02:55
2

"No extra space" implies some amount of space, usually exactly n, is available via the input, and no more should be used, although in an interview I never care if the candidate uses O(1) extra. Honestly you would be hard-pressed in any modern language to avoid O(1) extra space for almost any trivial action you could take.

The stack counts when giving bounds on algorithms' space complexity.

O(1) means constant.

Counting sort uses at minimum O(k) space, where k is the largest possible key magnitude. Therefore, theoretically if we are talking about integers on a fixed number of bits, that is a constant. That is also why a radix sort is sometimes said to be a linear time sort.

Judge Mental
  • 5,209
  • 17
  • 22
  • To be clear, counting sort uses O(k) **additional** space, but O(n + k) space overall, since you have to take even the space to store the input into consideration. Radix sort is somewhat different from counting sort, please don't confuse between the 2. – nhahtdh Jun 04 '12 at 21:12
1

O(1) : It is defined as the , in which input is bounded by a fixed constant number .we can compare this to when we have given integer range to be entered are between -10^5 to 10^5. So in breif we can say it signify bound on the values to be input .

O(n) : It is just opposite of the above when we have no condition on the input . As example , when we have to enter a string , then there is no condition on the length of string we entered

Ritik Kamboj
  • 806
  • 6
  • 10
1

Before Answering the question, I just want to explain the meaning of extra constant space, For an algorithm to take constant extra space, the extra variables used to solve it should not change with the input size, for example, In bubble sort, the extra variable that we will use is the temp variable, it doesn't change with the size of the input array, we will always use the exact number of extra variables for any size.

Now coming to your first question: while dealing with recursion, the space complexity of the recursive algorithm is proportional to a maximum depth of recursion tree generated, so even if we are using a constant number of extra variables, the number of times these extra variable gets created will depend on the recursive call, which in turn depends on the size of the arr, which is not constant.

In count-sort, we are taking O(N+k) extra space to perform the sort, the N here will vary with inputs and so will K, so the extra space used will not be constant but O(n+k)

Hope this helps!!