2.4 Space Complexity¶
The space complexity is used to measure the growth trend of memory consumption as the scale of data increases for an algorithm solution. This concept is analogous to time complexity by replacing "runtime" with "memory space".
2.4.1 Algorithmic Correlation Space¶
The memory space used by algorithms during its execution include the following types.
- Input Space: Used to store the input data for the algorithm.
- Temporary Space: Used to store variables, objects, function contexts, and other data of the algorithm during runtime.
- Output Space: Used to store the output data of the algorithm.
In general, the "Input Space" is excluded from the statistics of space complexity.
The Temporary Space can be further divided into three parts.
- Temporary Data: Used to store various constants, variables, objects, etc., during the the algorithm's execution.
- Stack Frame Space: Used to hold 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 freed when the function returns.
- Instruction Space: Used to hold compiled program instructions, usually ignored in practical statistics.
When analyzing the space complexity of a piece of program, three parts are usually taken into account: Temporary Data, Stack Frame Space and Output Data.
Figure 2-15 Associated spaces used by the algorithm
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>>>,
}
/* Creating a Node structure */
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 calculation method for space complexity is pretty similar to time complexity, with the only difference being that the focus shifts from "operation count" to "space usage size".
On top of that, unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we have to make sure that there is enough memory space reserved for all possibilities incurred by input data.
Looking at the following code, the "worst" in worst-case space complexity has two layers of meaning.
- Based on the worst-case input data: when \(n < 10\), the space complexity is \(O(1)\); however, 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 during algorithm execution: for example, the program occupies \(O(1)\) space until the last line is executed; when the array
nums
is initialized, the program occupies \(O(n)\) space; thus the worst-case space complexity is \(O(n)\).
In recursion functions, it is important to take into count the measurement of stack frame space. For example in the following code:
- The function
loop()
calls \(n\) timesfunction()
in a loop, and each round offunction()
returns and frees stack frame space, so the space complexity is still \(O(1)\). - The recursion function
recur()
will have \(n\) unreturnedrecur()
during runtime, thus occupying \(O(n)\) of stack frame space.
2.4.3 Common Types¶
Assuming the input data size is \(n\), the figure illustrates common types of space complexity (ordered from low to high).
Figure 2-16 Common space complexity types
1. Constant Order \(O(1)\)¶
Constant order is common for constants, variables, and objects whose quantity is unrelated to the size of the input data \(n\).
It is important to note that memory occupied by initializing a variable or calling a function in a loop is released once the next iteration begins. Therefore, there is no accumulation of occupied space and the space complexity remains \(O(1)\) :
/* 函数 */
int func() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode node(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
func();
}
}
/* 函数 */
int function() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
final int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
function();
}
}
/* 函数 */
int Function() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void Constant(int n) {
// 常量、变量、对象占用 O(1) 空间
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
Function();
}
}
/* 函数 */
func function() int {
// 执行某些操作...
return 0
}
/* 常数阶 */
func spaceConstant(n int) {
// 常量、变量、对象占用 O(1) 空间
const a = 0
b := 0
nums := make([]int, 10000)
ListNode := newNode(0)
// 循环中的变量占用 O(1) 空间
var c int
for i := 0; i < n; i++ {
c = 0
}
// 循环中的函数占用 O(1) 空间
for i := 0; i < n; i++ {
function()
}
fmt.Println(a, b, nums, c, ListNode)
}
/* 函数 */
@discardableResult
func function() -> Int {
// 执行某些操作
return 0
}
/* 常数阶 */
func constant(n: Int) {
// 常量、变量、对象占用 O(1) 空间
let a = 0
var b = 0
let nums = Array(repeating: 0, count: 10000)
let node = ListNode(x: 0)
// 循环中的变量占用 O(1) 空间
for _ in 0 ..< n {
let c = 0
}
// 循环中的函数占用 O(1) 空间
for _ in 0 ..< n {
function()
}
}
/* 函数 */
function constFunc() {
// 执行某些操作
return 0;
}
/* 常数阶 */
function constant(n) {
// 常量、变量、对象占用 O(1) 空间
const a = 0;
const b = 0;
const nums = new Array(10000);
const node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (let i = 0; i < n; i++) {
const c = 0;
}
// 循环中的函数占用 O(1) 空间
for (let i = 0; i < n; i++) {
constFunc();
}
}
/* 函数 */
function constFunc(): number {
// 执行某些操作
return 0;
}
/* 常数阶 */
function constant(n: number): void {
// 常量、变量、对象占用 O(1) 空间
const a = 0;
const b = 0;
const nums = new Array(10000);
const node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (let i = 0; i < n; i++) {
const c = 0;
}
// 循环中的函数占用 O(1) 空间
for (let i = 0; i < n; i++) {
constFunc();
}
}
/* 函数 */
int function() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
final int a = 0;
int b = 0;
List<int> nums = List.filled(10000, 0);
ListNode node = ListNode(0);
// 循环中的变量占用 O(1) 空间
for (var i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (var i = 0; i < n; i++) {
function();
}
}
/* 函数 */
fn function() ->i32 {
// 执行某些操作
return 0;
}
/* 常数阶 */
#[allow(unused)]
fn constant(n: i32) {
// 常量、变量、对象占用 O(1) 空间
const A: i32 = 0;
let b = 0;
let nums = vec![0; 10000];
let node = ListNode::new(0);
// 循环中的变量占用 O(1) 空间
for i in 0..n {
let c = 0;
}
// 循环中的函数占用 O(1) 空间
for i in 0..n {
function();
}
}
/* 函数 */
int func() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
const int a = 0;
int b = 0;
int nums[1000];
ListNode *node = newListNode(0);
free(node);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
func();
}
}
// 函数
fn function() i32 {
// 执行某些操作
return 0;
}
// 常数阶
fn constant(n: i32) void {
// 常量、变量、对象占用 O(1) 空间
const a: i32 = 0;
var b: i32 = 0;
var nums = [_]i32{0}**10000;
var node = inc.ListNode(i32){.val = 0};
var i: i32 = 0;
// 循环中的变量占用 O(1) 空间
while (i < n) : (i += 1) {
var c: i32 = 0;
_ = c;
}
// 循环中的函数占用 O(1) 空间
i = 0;
while (i < n) : (i += 1) {
_ = function();
}
_ = a;
_ = b;
_ = nums;
_ = node;
}
2. Linear Order \(O(N)\)¶
Linear order is commonly found in arrays, linked lists, stacks, queues, and similar structures where the number of elements is proportional to \(n\):
/* 线性阶 */
void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
vector<int> nums(n);
// 长度为 n 的列表占用 O(n) 空间
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
unordered_map<int, string> map;
for (int i = 0; i < n; i++) {
map[i] = to_string(i);
}
}
/* 线性阶 */
void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new ArrayList<>();
for (int i = 0; i < n; i++) {
nodes.add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, String.valueOf(i));
}
}
/* 线性阶 */
void Linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = [];
for (int i = 0; i < n; i++) {
nodes.Add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, string> map = [];
for (int i = 0; i < n; i++) {
map.Add(i, i.ToString());
}
}
/* 线性阶 */
func spaceLinear(n int) {
// 长度为 n 的数组占用 O(n) 空间
_ = make([]int, n)
// 长度为 n 的列表占用 O(n) 空间
var nodes []*node
for i := 0; i < n; i++ {
nodes = append(nodes, newNode(i))
}
// 长度为 n 的哈希表占用 O(n) 空间
m := make(map[int]string, n)
for i := 0; i < n; i++ {
m[i] = strconv.Itoa(i)
}
}
/* 线性阶 */
function linear(n) {
// 长度为 n 的数组占用 O(n) 空间
const nums = new Array(n);
// 长度为 n 的列表占用 O(n) 空间
const nodes = [];
for (let i = 0; i < n; i++) {
nodes.push(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
const map = new Map();
for (let i = 0; i < n; i++) {
map.set(i, i.toString());
}
}
/* 线性阶 */
function linear(n: number): void {
// 长度为 n 的数组占用 O(n) 空间
const nums = new Array(n);
// 长度为 n 的列表占用 O(n) 空间
const nodes: ListNode[] = [];
for (let i = 0; i < n; i++) {
nodes.push(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
const map = new Map();
for (let i = 0; i < n; i++) {
map.set(i, i.toString());
}
}
/* 线性阶 */
void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
List<int> nums = List.filled(n, 0);
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = [];
for (var i = 0; i < n; i++) {
nodes.add(ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Map<int, String> map = HashMap();
for (var i = 0; i < n; i++) {
map.putIfAbsent(i, () => i.toString());
}
}
/* 线性阶 */
#[allow(unused)]
fn linear(n: i32) {
// 长度为 n 的数组占用 O(n) 空间
let mut nums = vec![0; n as usize];
// 长度为 n 的列表占用 O(n) 空间
let mut nodes = Vec::new();
for i in 0..n {
nodes.push(ListNode::new(i))
}
// 长度为 n 的哈希表占用 O(n) 空间
let mut map = HashMap::new();
for i in 0..n {
map.insert(i, i.to_string());
}
}
/* 哈希表 */
typedef struct {
int key;
int val;
UT_hash_handle hh; // 基于 uthash.h 实现
} HashTable;
/* 线性阶 */
void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int *nums = malloc(sizeof(int) * n);
free(nums);
// 长度为 n 的列表占用 O(n) 空间
ListNode **nodes = malloc(sizeof(ListNode *) * n);
for (int i = 0; i < n; i++) {
nodes[i] = newListNode(i);
}
// 内存释放
for (int i = 0; i < n; i++) {
free(nodes[i]);
}
free(nodes);
// 长度为 n 的哈希表占用 O(n) 空间
HashTable *h = NULL;
for (int i = 0; i < n; i++) {
HashTable *tmp = malloc(sizeof(HashTable));
tmp->key = i;
tmp->val = i;
HASH_ADD_INT(h, key, tmp);
}
// 内存释放
HashTable *curr, *tmp;
HASH_ITER(hh, h, curr, tmp) {
HASH_DEL(h, curr);
free(curr);
}
}
// 线性阶
fn linear(comptime n: i32) !void {
// 长度为 n 的数组占用 O(n) 空间
var nums = [_]i32{0}**n;
// 长度为 n 的列表占用 O(n) 空间
var nodes = std.ArrayList(i32).init(std.heap.page_allocator);
defer nodes.deinit();
var i: i32 = 0;
while (i < n) : (i += 1) {
try nodes.append(i);
}
// 长度为 n 的哈希表占用 O(n) 空间
var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);
defer map.deinit();
var j: i32 = 0;
while (j < n) : (j += 1) {
const string = try std.fmt.allocPrint(std.heap.page_allocator, "{d}", .{j});
defer std.heap.page_allocator.free(string);
try map.put(i, string);
}
_ = nums;
}
As shown in the Figure 2-17 , the depth of recursion for this function is \(n\), which means that there are \(n\) unreturned linear_recur()
functions at the same time, using \(O(n)\) size stack frame space:
Figure 2-17 Linear order space complexity generated by recursion function
3. Quadratic Order \(O(N^2)\)¶
Quadratic order is common in matrices and graphs, where the number of elements is in a square relationship with \(n\):
/* 平方阶 */
void quadratic(int n) {
// 矩阵占用 O(n^2) 空间
int[][] numMatrix = new int[n][n];
// 二维列表占用 O(n^2) 空间
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);
}
}
/* 平方阶 */
function quadratic(n: number): void {
// 矩阵占用 O(n^2) 空间
const numMatrix = Array(n)
.fill(null)
.map(() => Array(n).fill(null));
// 二维列表占用 O(n^2) 空间
const numList = [];
for (let i = 0; i < n; i++) {
const tmp = [];
for (let j = 0; j < n; j++) {
tmp.push(0);
}
numList.push(tmp);
}
}
/* 平方阶 */
void quadratic(int n) {
// 矩阵占用 O(n^2) 空间
List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));
// 二维列表占用 O(n^2) 空间
List<List<int>> numList = [];
for (var i = 0; i < n; i++) {
List<int> tmp = [];
for (int j = 0; j < n; j++) {
tmp.add(0);
}
numList.add(tmp);
}
}
/* 平方阶 */
void quadratic(int n) {
// 二维列表占用 O(n^2) 空间
int **numMatrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++) {
int *tmp = malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) {
tmp[j] = 0;
}
numMatrix[i] = tmp;
}
// 内存释放
for (int i = 0; i < n; i++) {
free(numMatrix[i]);
}
free(numMatrix);
}
// 平方阶
fn quadratic(n: i32) !void {
// 二维列表占用 O(n^2) 空间
var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);
defer nodes.deinit();
var i: i32 = 0;
while (i < n) : (i += 1) {
var tmp = std.ArrayList(i32).init(std.heap.page_allocator);
defer tmp.deinit();
var j: i32 = 0;
while (j < n) : (j += 1) {
try tmp.append(0);
}
try nodes.append(tmp);
}
}
As shown in the Figure 2-18 , the recursion depth of this function is \(n\), and an array is initialized in each recursion function with lengths \(n\), \(n-1\), \(\dots\), \(2\), \(1\), and an average length of \(n / 2\), thus occupying \(O(n^2)\) space overall:
Figure 2-18 Square-order space complexity generated by the recursion function
4. Exponential Order \(O(2^N)\)¶
Exponential order is common in binary trees. Looking at the Figure 2-19 , a "full binary tree" of degree \(n\) has \(2^n - 1\) nodes, occupying \(O(2^n)\) space:
// 指数阶(建立满二叉树)
fn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {
if (n == 0) return null;
const root = try mem_allocator.create(inc.TreeNode(i32));
root.init(0);
root.left = try buildTree(mem_allocator, n - 1);
root.right = try buildTree(mem_allocator, n - 1);
return root;
}
Figure 2-19 Exponential order space complexity generated by a full binary tree
5. Logarithmic Order \(O(\Log N)\)¶
Logarithmic order is commonly used in divide and conquer algorithms. For example, in a merge sort, given an array of length \(n\) as the input, each round of recursion divides the array in half from its midpoint to form a recursion tree of height \(\log n\), using \(O(\log n)\) stack frame space.
Another example is to convert a number into a string. Given a positive integer \(n\) with a digit count of \(\log_{10} n + 1\), the corresponding string length is \(\log_{10} n + 1\). Therefore, the space complexity is \(O(\log_{10} n + 1) = O(\log n)\).
2.4.4 Weighing Time And Space¶
Ideally, we would like to optimize both the time complexity and the space complexity of an algorithm. However, in reality, simultaneously optimizing time and space complexity is often challenging.
Reducing time complexity usually comes at the expense of increasing space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm speed is known as "trading space for time", while the opposite is called "trading time for space".
The choice between these approaches depends on which aspect we prioritize. In most cases, time is more valuable than space, so "trading space for time" is usually the more common strategy. Of course, in situations with large data volumes, controlling space complexity is also crucial.