2.4 Space complexity¶
Space complexity is used to measure the growth trend of the memory space occupied by an algorithm as the amount of data increases. This concept is very similar to time complexity, except that "running time" is replaced with "occupied memory space".
2.4.1 Space related to algorithms¶
The memory space used by an algorithm during its execution mainly includes the following types.
- Input space: Used to store the input data of the algorithm.
- Temporary space: Used to store variables, objects, function contexts, and other data during the algorithm's execution.
- Output space: Used to store the output data of the algorithm.
Generally, the scope of space complexity statistics includes both "Temporary Space" and "Output Space".
Temporary space can be further divided into three parts.
- Temporary data: Used to save various constants, variables, objects, etc., during the algorithm's execution.
- Stack frame space: Used to save the context data of the called function. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.
- Instruction space: Used to store compiled program instructions, which are usually negligible in actual statistics.
When analyzing the space complexity of a program, we typically count the Temporary Data, Stack Frame Space, and Output Data, as shown in Figure 2-15.
Figure 2-15 Space types used in algorithms
The relevant code is as follows:
class Node:
"""Classes"""
def __init__(self, x: int):
self.val: int = x # node value
self.next: Node | None = None # reference to the next node
def function() -> int:
"""Functions"""
# Perform certain operations...
return 0
def algorithm(n) -> int: # input data
A = 0 # temporary data (constant, usually in uppercase)
b = 0 # temporary data (variable)
node = Node(0) # temporary data (object)
c = function() # Stack frame space (call function)
return A + b + c # output data
/* Structures */
struct Node {
int val;
Node *next;
Node(int x) : val(x), next(nullptr) {}
};
/* Functions */
int func() {
// Perform certain operations...
return 0;
}
int algorithm(int n) { // input data
const int a = 0; // temporary data (constant)
int b = 0; // temporary data (variable)
Node* node = new Node(0); // temporary data (object)
int c = func(); // stack frame space (call function)
return a + b + c; // output data
}
/* Classes */
class Node {
int val;
Node next;
Node(int x) { val = x; }
}
/* Functions */
int function() {
// Perform certain operations...
return 0;
}
int algorithm(int n) { // input data
final int a = 0; // temporary data (constant)
int b = 0; // temporary data (variable)
Node node = new Node(0); // temporary data (object)
int c = function(); // stack frame space (call function)
return a + b + c; // output data
}
/* Classes */
class Node {
int val;
Node next;
Node(int x) { val = x; }
}
/* Functions */
int Function() {
// Perform certain operations...
return 0;
}
int Algorithm(int n) { // input data
const int a = 0; // temporary data (constant)
int b = 0; // temporary data (variable)
Node node = new(0); // temporary data (object)
int c = Function(); // stack frame space (call function)
return a + b + c; // output data
}
/* Structures */
type node struct {
val int
next *node
}
/* Create node structure */
func newNode(val int) *node {
return &node{val: val}
}
/* Functions */
func function() int {
// Perform certain operations...
return 0
}
func algorithm(n int) int { // input data
const a = 0 // temporary data (constant)
b := 0 // temporary storage of data (variable)
newNode(0) // temporary data (object)
c := function() // stack frame space (call function)
return a + b + c // output data
}
/* Classes */
class Node {
var val: Int
var next: Node?
init(x: Int) {
val = x
}
}
/* Functions */
func function() -> Int {
// Perform certain operations...
return 0
}
func algorithm(n: Int) -> Int { // input data
let a = 0 // temporary data (constant)
var b = 0 // temporary data (variable)
let node = Node(x: 0) // temporary data (object)
let c = function() // stack frame space (call function)
return a + b + c // output data
}
/* Classes */
class Node {
val;
next;
constructor(val) {
this.val = val === undefined ? 0 : val; // node value
this.next = null; // reference to the next node
}
}
/* Functions */
function constFunc() {
// Perform certain operations
return 0;
}
function algorithm(n) { // input data
const a = 0; // temporary data (constant)
let b = 0; // temporary data (variable)
const node = new Node(0); // temporary data (object)
const c = constFunc(); // Stack frame space (calling function)
return a + b + c; // output data
}
/* Classes */
class Node {
val: number;
next: Node | null;
constructor(val?: number) {
this.val = val === undefined ? 0 : val; // node value
this.next = null; // reference to the next node
}
}
/* Functions */
function constFunc(): number {
// Perform certain operations
return 0;
}
function algorithm(n: number): number { // input data
const a = 0; // temporary data (constant)
let b = 0; // temporary data (variable)
const node = new Node(0); // temporary data (object)
const c = constFunc(); // Stack frame space (calling function)
return a + b + c; // output data
}
/* Classes */
class Node {
int val;
Node next;
Node(this.val, [this.next]);
}
/* Functions */
int function() {
// Perform certain operations...
return 0;
}
int algorithm(int n) { // input data
const int a = 0; // temporary data (constant)
int b = 0; // temporary data (variable)
Node node = Node(0); // temporary data (object)
int c = function(); // stack frame space (call function)
return a + b + c; // output data
}
use std::rc::Rc;
use std::cell::RefCell;
/* Structures */
struct Node {
val: i32,
next: Option<Rc<RefCell<Node>>>,
}
/* Constructor */
impl Node {
fn new(val: i32) -> Self {
Self { val: val, next: None }
}
}
/* Functions */
fn function() -> i32 {
// Perform certain operations...
return 0;
}
fn algorithm(n: i32) -> i32 { // input data
const a: i32 = 0; // temporary data (constant)
let mut b = 0; // temporary data (variable)
let node = Node::new(0); // temporary data (object)
let c = function(); // stack frame space (call function)
return a + b + c; // output data
}
2.4.2 Calculation method¶
The method for calculating space complexity is roughly similar to that of time complexity, with the only change being the shift of the statistical object from "number of operations" to "size of used space".
However, unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we must ensure that there is enough memory space reserved under all input data.
Consider the following code, the term "worst-case" in worst-case space complexity has two meanings.
- Based on the worst input data: When \(n < 10\), the space complexity is \(O(1)\); but when \(n > 10\), the initialized array
nums
occupies \(O(n)\) space, thus the worst-case space complexity is \(O(n)\). - Based on the peak memory used during the algorithm's execution: For example, before executing the last line, the program occupies \(O(1)\) space; when initializing the array
nums
, the program occupies \(O(n)\) space, hence the worst-case space complexity is \(O(n)\).
In recursive functions, stack frame space must be taken into count. Consider the following code:
The time complexity of both loop()
and recur()
functions is \(O(n)\), but their space complexities differ.
- The
loop()
function callsfunction()
\(n\) times in a loop, where each iteration'sfunction()
returns and releases its stack frame space, so the space complexity remains \(O(1)\). - The recursive function
recur()
will have \(n\) instances of unreturnedrecur()
existing simultaneously during its execution, thus occupying \(O(n)\) stack frame space.
2.4.3 Common types¶
Let the size of the input data be \(n\), Figure 2-16 displays common types of space complexities (arranged from low to high).
Figure 2-16 Common types of space complexity
1. Constant order \(O(1)\)¶
Constant order is common in constants, variables, objects that are independent of the size of input data \(n\).
Note that memory occupied by initializing variables or calling functions in a loop, which is released upon entering the next cycle, does not accumulate over space, thus the space complexity remains \(O(1)\):
def function() -> int:
"""Function"""
# Perform some operations
return 0
def constant(n: int):
"""Constant complexity"""
# Constants, variables, objects occupy O(1) space
a = 0
nums = [0] * 10000
node = ListNode(0)
# Variables in a loop occupy O(1) space
for _ in range(n):
c = 0
# Functions in a loop occupy O(1) space
for _ in range(n):
function()
/* Function */
int func() {
// Perform some operations
return 0;
}
/* Constant complexity */
void constant(int n) {
// Constants, variables, objects occupy O(1) space
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode node(0);
// Variables in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
int c = 0;
}
// Functions in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
func();
}
}
/* Function */
int function() {
// Perform some operations
return 0;
}
/* Constant complexity */
void constant(int n) {
// Constants, variables, objects occupy O(1) space
final int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// Variables in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
int c = 0;
}
// Functions in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
function();
}
}
2. Linear order \(O(n)\)¶
Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to \(n\):
/* Linear complexity */
void linear(int n) {
// Array of length n occupies O(n) space
vector<int> nums(n);
// A list of length n occupies O(n) space
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(ListNode(i));
}
// A hash table of length n occupies O(n) space
unordered_map<int, string> map;
for (int i = 0; i < n; i++) {
map[i] = to_string(i);
}
}
/* Linear complexity */
void linear(int n) {
// Array of length n occupies O(n) space
int[] nums = new int[n];
// A list of length n occupies O(n) space
List<ListNode> nodes = new ArrayList<>();
for (int i = 0; i < n; i++) {
nodes.add(new ListNode(i));
}
// A hash table of length n occupies O(n) space
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, String.valueOf(i));
}
}
As shown in Figure 2-17, this function's recursive depth is \(n\), meaning there are \(n\) instances of unreturned linear_recur()
function, using \(O(n)\) size of stack frame space:
Figure 2-17 Recursive function generating linear order space complexity
3. Quadratic order \(O(n^2)\)¶
Quadratic order is common in matrices and graphs, where the number of elements is quadratic to \(n\):
/* Quadratic complexity */
void quadratic(int n) {
// Matrix occupies O(n^2) space
int[][] numMatrix = new int[n][n];
// A two-dimensional list occupies O(n^2) space
List<List<Integer>> numList = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> tmp = new ArrayList<>();
for (int j = 0; j < n; j++) {
tmp.add(0);
}
numList.add(tmp);
}
}
As shown in Figure 2-18, the recursive depth of this function is \(n\), and in each recursive call, an array is initialized with lengths \(n\), \(n-1\), \(\dots\), \(2\), \(1\), averaging \(n/2\), thus overall occupying \(O(n^2)\) space:
/* Quadratic complexity (recursive implementation) */
int quadraticRecur(int n) {
if (n <= 0)
return 0;
// Array nums length = n, n-1, ..., 2, 1
int[] nums = new int[n];
System.out.println("Recursion n = " + n + " in the length of nums = " + nums.length);
return quadraticRecur(n - 1);
}
Figure 2-18 Recursive function generating quadratic order space complexity
4. Exponential order \(O(2^n)\)¶
Exponential order is common in binary trees. Observe Figure 2-19, a "full binary tree" with \(n\) levels has \(2^n - 1\) nodes, occupying \(O(2^n)\) space:
Figure 2-19 Full binary tree generating exponential order space complexity
5. Logarithmic order \(O(\log n)\)¶
Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length \(n\) is recursively divided in half each round, forming a recursion tree of height \(\log n\), using \(O(\log n)\) stack frame space.
Another example is converting a number to a string. Given a positive integer \(n\), its number of digits is \(\log_{10} n + 1\), corresponding to the length of the string, thus the space complexity is \(O(\log_{10} n + 1) = O(\log n)\).
2.4.4 Balancing time and space¶
Ideally, we aim for both time complexity and space complexity to be optimal. However, in practice, optimizing both simultaneously is often difficult.
Lowering time complexity usually comes at the cost of increased space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm speed is known as "space-time tradeoff"; the reverse is known as "time-space tradeoff".
The choice depends on which aspect we value more. In most cases, time is more precious than space, so "space-time tradeoff" is often the more common strategy. Of course, controlling space complexity is also very important when dealing with large volumes of data.