Space Complexity of an Algorithm
Space Complexity of an Algorithm
Space complexity of an algorithm refers to the amount of memory or storage that the algorithm requires as a function of the input size. Just as time complexity measures how the runtime of an algorithm grows with input size, space complexity measures how the memory requirements of an algorithm increase as the size of the input grows.
Key Concepts
- Input Size: The space complexity depends on the size of the input. For example, in a sorting problem, the space complexity may depend on the number of elements to be sorted.
- Auxiliary Space: This refers to the extra space or temporary space used by the algorithm, excluding the space used by the input. For example, if an algorithm uses additional data structures like arrays, lists, or queues, the space required by these structures is considered auxiliary space.
- Memory Usage: Space complexity is concerned with the total memory needed, including both the input and the auxiliary space, for the algorithm to execute.
Common Space Complexities
Space Complexity | Growth Rate | Example |
---|---|---|
O(1) – Constant Space | Does not depend on the input size. The memory usage remains constant. | Swapping two variables without using any additional data structures. |
O(n) – Linear Space | Memory usage grows linearly with the input size. If the input size doubles, the memory usage doubles. | Creating a new array to store all elements of the input array. |
O(n²) – Quadratic Space | Memory usage grows with the square of the input size. | Creating a 2D matrix based on the input size. |
O(log n) – Logarithmic Space | Memory usage grows logarithmically with the input size, often used in recursive algorithms. | A recursive algorithm like binary search divides the problem into halves. |
Analyzing Space Complexity
When analyzing the space complexity of an algorithm, we focus on the following:
- Memory for input: This is usually considered separate from the auxiliary space. For example, if you’re sorting an array, the array is the input, and it’s not usually counted in the auxiliary space.
- Auxiliary Space: This includes additional data structures or variables used during the execution of the algorithm.
Example of Space Complexity
Constant Space Complexity (O(1)) Example:
def find_max(arr):
max_value = arr[0] # Constant space for max_value
for num in arr:
if num > max_value:
max_value = num
return max_value
The function uses a single variable `max_value` to keep track of the maximum value found so far. This is constant space, O(1). The space complexity of this algorithm is O(1) because the space used does not depend on the input size, only on the fixed amount of space needed to store the maximum value.
Linear Space Complexity (O(n)) Example:
def duplicate_array(arr):
result = [] # Creating a new array to store the duplicate
for num in arr:
result.append(num)
return result
The function creates a new array `result` which has the same size as the input array `arr`. The space complexity is O(n) because the amount of memory used by the algorithm grows linearly with the input size.
Recursive Space Complexity (O(n)) Example:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
In this recursive algorithm, each function call adds a frame to the call stack. The maximum depth of the recursion is equal to n, and so the space complexity is O(n) due to the call stack.
Best, Worst, and Average Case Space Complexities
- Worst-case space complexity refers to the maximum memory required by the algorithm for the largest input size.
- Best-case space complexity refers to the minimum memory required for the smallest input size.
- Average-case space complexity considers the average amount of memory needed for all possible inputs.
Space complexity helps in understanding how much memory an algorithm needs as its input size grows. By analyzing space complexity, you can determine the efficiency of an algorithm in terms of memory usage and compare different algorithms to choose the one that performs best in terms of both time and space.