Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The space complexity of this algorithm that loops over an array

I am asked to provide the asymptotic space and time complexity of the below algorithm in appropriate terms for an initial input number of arbitrary length (as distinct from a constant 12 digits).

1 for i = 2 to 12
2     if d[i] == d[i-1]
3        d[i] = (d[i] + 3) mod 10

This algorithm is apply for a number that has 12 digits, each digit is stored in the array d[] (so we have d[1], d[2],... d[12])

I figured out the time complexity is O(n) but how do I determine the space complexity?

like image 774
Hien Tran Avatar asked Sep 15 '13 04:09

Hien Tran


People also ask

What is the space complexity of a loop?

This is called Linear Space Complexity. If you have a loop variable I, then the required space complexity will be 1 word. If you have understood the concept of calculating the space complexity from the above example then following a similar approach you can calculate quadratic and other space complexity, as the complexity of an algorithm increases.

What is meant by space complexity of an algorithm?

Total amount of computer memory required by an algorithm to complete its execution is called as space complexity of that algorithm. Generally, when a program is under execution it uses the computer memory for THREE reasons.

What is the space complexity of an array of size n?

Ok, you have an array of size N. What about it? meaning, having some data stored does not have any meaning in terms of space complexity since there is no context like some code (algorithm) that uses this array (space). So you can't measure space complexity of nothing (no code to be run, just data sitting there).

What is space complexity in OO (n!) space complexity?

O (N!) Space Complexity: The space complexity of an algorithm quantifies the amount of space taken by an algorithm to run as a function of the length of the input. Consider an example: Suppose a problem to find the frequency of array elements.


1 Answers

Typically, to measure space complexity, you want to look at how much extra space is required to hold all the information necessary to execute the code. The challenge is that you can often reuse space across the lifetime of the code execution.

In this case, the code needs extra space to store

  • The values of temporary calculations in the loop,
  • The value of i, and
  • Miscellaneous data like the program counter, etc.

The last two of these take up O(1) space, since there's only one i and constant space for auxiliary data like the stack pointer, etc. So what about the first? Well, each iteration of the loop will need O(1) space for temporary variables, but notice that this space can get reused because after each loop iteration finishes, the space for those temporaries isn't needed anymore and can be reused in the next iteration. Therefore, the total space needed is just O(1).

(A note... are you sure the time complexity is O(n)? Notice that the number of iterations is a constant regardless of how large the array is.)

Hope this helps!

like image 194
templatetypedef Avatar answered Sep 20 '22 22:09

templatetypedef