basic complexity confusion - performance

I have an algorithm which takes in a 2D array and uses no extra space. So is the space complexity of the algorithm O(n^2) (because I am processing the entire input array) or O(1) (since the algorithm does not use any extra space apart from the input)
In particular, in this question http://www.careercup.com/question?id=4959773472587776 , it does not matter if we use 2 extra 1-dimensional arrays right, since anyway the input space complexity is O(n^2).
Thanks!

Auxiliary space complexity does not include the input space whereas the space complexity does.
For auxiliary space complexity analysis, only consider the extra memory consumption. If your algorithm does not use any extra space then the auxiliary space complexity is O(1).
If the input has size m ( = n x n), and you use 2 arrays of size n, then the auxiliary space complexity will be O(n) (or O(logm)).
For space complexity, since you count the input size, you are right, using 2 arrays will not change the space complexity.

Related

What is the space complexity of duplicating the input?

I have some strings as input and I need to manipulate their individual characters, which I do more efficiently by first splitting the strings into arrays of characters.
So in short, my algorithm is duplicating the input. What would be its space complexity?
The same space complexity as the original problem.
If you have an input of n characters arranged in strings, that carries a spatial complexity of O(n). All you do is arrange them in some different way and duplicating the space you need: 2n and thus having a complexity of O(2n).
However O(n) is equivalent to O(kn), being k any constant. Doesn't mean both problems consume the same memory but complexity wise they are equivalent.

Why space complexity of sum of all elements in an array is O(n)?

Below Algorithm is for sum of all elements in an array.
function sumofnumbers(a[],n)
{
sum=0
for(i=0 to n)
sum=sum+a[i]
print(sum)
}
Can anyone help me out with this Algorithm analysis. why the space complexity of above algorithm is O(n)?
According to me this must be O(1). Because we are not considering any extra space over here. We are using given space to calculate sum of elements in array. for example in some sorting technique to sort elements we take extra array.
According to me
Formula to calculate Space complexity:
Space complexity = Input size + Auxillary space
Every algorithm contains it's required/own input size that is whatever space required by program to execute. So we have to consider only extra space(Auxillary space) required by program to execute that is other than Input size to calculate Space complexity.
So I applied this concept is it True??
And when to consider Both parameter(i.e Input size and Auxillary Space) to calculate Space complexity? because in some example like Linear Search i seen that some times they are not using Input size ?
Thank you!!
I believe you are correct. The space complexity does not change with increasing values of N - hence O(1).
Reference https://www.baeldung.com/cs/space-complexity
If the input is large, the total sum can be large too, which results in non-constant space.
i.e. 112345 ^ 1235634 cannot fit in a constant 64-bit int. So, you need larger space to hold the sum (could be str, or list, or linked-list).

Space Complexity of an algorithm when no extra space is used

Consider an algorithm that uses no extra variables except the given input.
How to represent the space complexity in BigO Notation?
O(1)
Where it requires a constant amount of additional space namely 0.

Difference between Auxiliary Space and Space Complexity of Heap Sort?

Difference between Auxiliary Space and Space Complexity of Heap Sort?
My attempt:
As explained here:
If we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criteria than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort and Heap Sort use O(1) auxiliary space. Space complexity of all these sorting algorithms is O(n) though.
I googled the space complexity of Heap Sort, I found the space complexity is O(1).
My question is:
Is that explanation correct? What is difference between Auxiliary Space and Space Complexity?
Auxiliary should be intended as to all the memory that is not used to store the original input.
Heap Sort input is an array of unordered elements and it works by rearranging them in place meaning that no (or a constant amount of it i.e. not depening on the size on the input array) auxiliary space is used (the heap is built using the input array - http://www.algostructure.com/sorting/heapsort.php).
Talking about space complexity you should also take into account the space used by the input and the auxiliary one, so in this sense, the heap sort has space complexity of O(n)+O(1) (n for the input and 1 as auxiliary space).
To be fair if you want you could also consider the space used on the stack (recursive implementation of heap sort use that space, though it should be only O(logn), see here for more details).
By the way, auxiliary space of merge-sort can also be O(1) since exists a version of merge-sort which sorts the array in place (How to sort in-place using the merge sort algorithm?).

Space complexity of algorithm to copy a list into a HashSet

What is the space complexity for an algorithm which places each element from a list into a HashSet? Is it O(n), where n is the size of the list or is it O(k), where k is the number of unique elements in the list. Since the HashSet only grows when we add unique elements to it it seems to me that the latter is correct.
Space complexity of any algorithm takes into account the size of the input. It is a measure of the maximum working memory that will be needed during the execution of the algorithm. So for O(n) size input the space complexity has to be at least O(n). Source
So given the algorithm used O(n) just for the input, and it isn't a really bad implementation i.e. it uses constant amount of space as it iterates over the list and we know that k < n, so input size will always be the dominating factor in space complexity. So overall the space complexity will be O(n).

Resources