[Data Structure] Space Complexity

Table of contents

1. Space complexity

2. Space complexity use cases


1. Space complexity

What I want to share in this chapter is space complexity, which is also a mathematical expression, which is a measure of the amount of storage space temporarily occupied by an algorithm during its operation.


Space complexity is not how many bytes the program occupies, because this is not very meaningful, so space complexity is calculated by the number of variables .

The space complexity calculation rules are basically similar to the practical complexity, and the big O asymptotic notation is also used.


Note: The stack space (storage parameters, local variables, some register information, etc.) required by the function at runtime has been determined during compilation, so the space complexity is mainly determined by the additional space explicitly requested by the function at runtime.

Not much nonsense, just look at the code to discuss

2. Space complexity use cases

Example 1

void BubbleSort(int* a, int n)
	{
		assert(a);
		for (size_t end = n; end > 0; --end)
		{
			int exchange = 0;
			for (size_t i = 1; i < end; ++i)
			{
				if (a[i - 1] > a[i])
				{
					Swap(&a[i - 1], &a[i]);
					exchange = 1;
				}
			}
			if (exchange == 0)
				break;
		}
	}

The above is the code of bubble sort. Can we see its space complexity?

It is not difficult to see here that three spaces have been opened up, namely exchange, end, and i. These are all constant variables, so the space complexity is O(1) ;

As mentioned in the above concept, the space complexity calculation is the additional space requested, but in fact, the sorting of these arrays cannot be counted as space consumption. We can also understand that the bubble sort algorithm only processes the existing data, sorts it, and regards it as a means of processing data, and does not require additional space consumption  , so its space complexity is O (1).

Example 2

Continue to observe the space complexity of Fibonacci

long long* Fibonacci(size_t n)
	{
		if (n == 0)
			return NULL;
		long long* fibArray = (long long*)malloc((n + 1) * sizeof(long long));
		fibArray[0] = 0;
		fibArray[1] = 1;
		for (int i = 2; i <= n; ++i)
		{
			fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
		}
		return fibArray;
	}

The difference from the previous Fibonacci is that malloc is used to directly apply for a space here

We applied for an array with a size of n+1*type, and calculated its valuation in the same way. It is not difficult to see that its space complexity is O(N)

Example 3

Continue to observe the space complexity of recursive functions

long long Fac(size_t N)
	{
		if (N == 0)
			return 1;
		return Fac(N - 1) * N;
	}

A stack frame will be created when the function recurses, and it may be easier to understand with a legend

 We can see that each time the function is called, it will create a stack frame and continue to call down until it calls Fac(1), and then returns layer by layer, so a total of N spaces have been opened up, so the space complexity is O. (N)

Example 4

Continue to increase the difficulty and observe the space complexity below

long long Fib(size_t N)
	{
		if (N < 3)
			return 1;
		return Fib(N - 1) + Fib(N - 2);
	}

 Data structure drawing is essential to understand the meaning of the question, we might as well continue to draw pictures to understand

A function like this is called straight down when calling the stack frame recursively, that is to say, after entering from Fib(N), it will call Fib(N-1), and then it will call down to Fib(N-2) , and then call Fib(N-3) downwards, and so on when Fib(1) is finally called, and then call the functions of other columns, similar to the following situation

 First call one to the deepest, and then call other functions and recursively share the same stack frame with the previous function, and reuse it.

That is to say, when studying the space complexity of recursion, we only need to study its depth.

Since it is reusing a piece of space, we might as well understand that only the part of the space in the red box is used, so its space complexity is O(N)

So according to the content of these two articles, we can know that time is gone forever, but space can be reused.

The content of space complexity is not very difficult. If the article is helpful to you, you may wish to triple support the following, thank you for reading.

Guess you like

Origin blog.csdn.net/wangduduniubi/article/details/130115245