19

I understood the basic that if I have a function like this:

int sum(int x, int y, int z) {
  int r = x + y + z;
  return r;
}

it requires 3 units of space for the parameters and 1 for the local variable, and this never changes, so this is O(1).

But what if I have a function like this:

void add(int a[], int b[], int c[], int n) {
    for (int i = 0; i < n; ++i) {
        c[i] = a[i] + b[0]
    }
}

Which requires N units for a, M units for b and L units for c and 1 unit for i and n. So it will need N+M+L+1+1 amount of storage.

So what will the big-O for space complexity here? The one which takes higher memory? I.e. if N takes more higher momery than M and L (from much higher means suppose larger than 10**6) - so is it safe to say space complexity is O(N) or not like we do for time complexity ?

But if all three i.e a, b, c are not very much different

Like this function

void multiply(int a[], int b[], int c[][], int n) { 
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            c[i] = a[i] + b[j];
        }
    }
}

Then what will be the space complexity? O(N+M+L)? Or still the biggest one?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
  • 3
    When we talk about space complexity, typically we mean _auxiliary_ space needed – not space for the inputs themselves. – Matt Ball May 13 '15 at 16:21
  • Space complexity includes both Auxiliary space and space used by input. Right ? – Ankur Anand May 13 '15 at 16:24
  • 1
    @AnkurAnand Technically, yes. But many use the term to just mean auxiliary space complexity. Specifically you'd like to know things like, "If I pass this large data set through 100 functions, how much more memory did I take up, and garbage did I create?" – btilly May 13 '15 at 17:01

2 Answers2

15

When we talk about space complexity, we don't consider the space used by the input.

This allows us to talk about algorithms which are constant space, O(log n) space etc. If we started counting the input, then all algorithms will be at least linear space!

The standard multi-tape Turing machine definition of space complexity also does not count the output.

The input is read only and output is write only and do not count towards the space complexity.

So to answer your question: look for what memory your method allocates, including stack space for recursion/local variables etc, and that will determine the space complexity.

15

The space complexity of an algorithm or data structure is the maximum amount of space used at any one time, ignoring the space used by the input to the algorithm. Therefore space complexity of all three examples in your question is O(1)

user3311298
  • 335
  • 5
  • 12