Skip to content

Commit 099efc0

Browse files
authored
Merge pull request #69 from ananya0504/63/Space-Complexity
Improved the lesson - Added more theory, examples, formatting
2 parents b16cc59 + b530f96 commit 099efc0

File tree

1 file changed

+75
-25
lines changed

1 file changed

+75
-25
lines changed

lessons/space-complexity.md

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,102 @@ section: "Space & Time Complexity"
66
description: "learn about time and space complexity for various algorithms"
77
---
88

9-
Now that we have learned about 'time' aspect of performance analysis of an algorithm, let's move on to memory aspect of the same.
9+
Now that we have learned about the 'time' aspect of performance analysis of an algorithm, let's move on to memory aspect of the same.
1010

11-
***Space complexity of an algorithm is basically the amount of memory it needs to run to completion.***
12-
13-
It is a way to establish approximate relationship between size of input data and primary memory used by the algorithm to produce the expected result.
11+
***Space complexity of an algorithm is basically the amount of memory it needs to run to completion, ie, to execute and produce the result.***
1412

1513
Calculation of space complexity used to hold much more significance in early days of computing than it does now. This is because most machines today have large memories and the user does not need to worry about running out of memory for running a program or two. But it is a crucial estimate where the physical memory is limited or closely monitored.
1614

17-
## **Calculation of Space Complexity**
15+
*Above all, it’s necessary to mention that space complexity depends on a variety of things such as the programming language, the compiler, or even the machine running the algorithm.*
16+
17+
## Memory Usage while Execution
18+
While executing, an algorithm uses memory space for three reasons:
19+
- **Instruction Space**
20+
-- It's the amount of memory used to save the compiled version of instructions.
21+
22+
- **Environmental Stack**
23+
-- Sometimes an algorithm(function) may be called inside another algorithm(function). In such a situation, the current variables are pushed onto the system stack, where they wait for further execution and then the call to the inside algorithm(function) is made.\
24+
Ex. If a function A() calls function B() inside it, then all the variables of the function A() will get stored on the system stack temporarily, while the function B() is called and executed inside the function A().
25+
26+
- **Data Space**
27+
-- Amount of space used by the variables and constants.
28+
29+
30+
31+
So in general for any algorithm, the memory may be used for the following: - `Variables (Data Space)`, `Program Instruction (Instruction Space)` and `Execution (Environmental Space)`.
32+
But while calculating the Space Complexity of any algorithm, we usually consider only Data Space and we neglect the Instruction Space and Environmental Stack.
33+
34+
## Calculation of Space Complexity
1835

1936
An algorithm's space can be categorized into 2 parts:\
20-
**1)Fixed Part** which is independent of characteristics of input and output.\
37+
**1) Fixed Part** - It is independent of the characteristics of input and output.\
2138
It includes instruction(code) space, space for simple variables, fixed-size component variables and constants.\
22-
**2)Variable Part** which depends on instance characteristics.\
39+
**2) Variable Part** - It depends on instance characteristics.\
2340
It consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, the space needed by referenced variables, and the recursion stack space.
2441

25-
Thus, space requirement S(P) of any algorithm P is:
26-
> S(P) = c + Sp (Instance characteristics), where c is constant
42+
Sometimes, ***Auxiliary Space*** is confused with Space Complexity. The Auxiliary Space is the extra space or the temporary space used by the algorithm during it's execution.
43+
44+
`Space Complexity = Auxiliary Space + Input space`
45+
46+
> Thus, space requirement S(M) of any algorithm M is:
47+
> S(M) = c + Sm (Instance characteristics), where c is constant
2748
28-
While analyzing space complexity, we primarily concentrate on estimating Sp.
29-
Consider following algorithm:
49+
50+
While analyzing space complexity, we primarily concentrate on estimating Sm.
51+
Consider the following algorithm:
52+
53+
```java
54+
public int sum(int a, int b) {
55+
return a + b;
56+
}
3057
```
31-
Algorithm Calc(a, b, c)
32-
{
33-
return a + b - c*a + b/c - a;
58+
In this particular method, three variables are used and allocated in memory:
59+
1. The first `int` argument, a
60+
2. The second `int` argument, b
61+
3. The returned sum result which is also an `int` like a and b
62+
63+
In Java, a single integer variable occupies `4` bytes of memory. In this example, we have three integer variables. Therefore, this algorithm always takes 12 bytes of memory to complete (3*4 bytes).
64+
65+
> We can clearly see that the space complexity is constant, so, it can be expressed in big-O notation as O(1).
66+
67+
Now let us see another example -
68+
```java
69+
public int sumArray(int[] array) {
70+
int size = array.length;
71+
int sum = 0;
72+
for (int i = 0; i < size; i++) {
73+
sum += array[i];
74+
}
75+
return sum;
3476
}
3577
```
36-
Here, problem instance depends only upon *a*,*b* and *c* which can be considered constant(fixed part). Thus ***Sp=0***. So, the space required by *Calc* algorithm is constant. In other words, complexity is O(1).
78+
Again, let’s list all variables present in the above code:
79+
1. Array – the function’s only argument – the space taken by the array is equal to 4n bytes where n is the length of the array
80+
2. The `int` variable, size
81+
3. The `int` variable, sum
82+
4. The `int` iterator, i
3783

84+
The total space needed for this algorithm to complete is 4n + 4 + 4 + 4 (bytes). The highest order is of n in this equation. Thus, the space complexity of that code snippet is O(n).
3885
When the program consists of loops (In case of Iterative algorithms), it will have linear space complexity or O(n).
39-
While dealing with operations on data structures, we can say that space complexity depends on size of the data structure. For example, if an array stores N elements, its space complexity is O(n). A program with an array of N arrays will have space complexity O(n^2) and so on.
4086

41-
Space complexity analysis also takes into account the size of recursion stack in case of recursive algorithms. for example,
42-
```
43-
Algorithm Fact(n)
87+
> While dealing with operations on data structures, we can say that space complexity depends on size of the data structure. Ex, if an array stores N elements, its space complexity is O(N). A program with an array of N arrays will have space complexity O(N^2) and so on.
88+
89+
Now, the space complexity analysis also takes into account the size of recursion stack in case of recursive algorithms.
90+
Consider the code below -
91+
```java
92+
Algorithm fact(n)
4493
{
45-
if (n<=0) return 1;
46-
else return n * (n - 1);
94+
if (n<=0)
95+
return 1;
96+
else
97+
return n * (n - 1);
4798
}
4899
```
49-
In this case there are 3 statements (1 if & 2 return statements). The depth of recursion is *n + 1*. Thus the recursion stack space needed is >=3(n+1). So we can say, space complexity is O(n) i.e. linear.
50-
51-
***NOTE: Space complexity might differ from machine to machine based on the programming language, the compiler etc.***
52100

101+
In this case there are 3 statements ( an `if` statement & 2`return` statements). The depth of recursion is *n + 1*. Thus the recursion stack space needed is >=3(n+1). So we can say, space complexity is O(n) i.e. linear.
53102

54-
## **Space Complexities of Common Algorithms**
103+
## Space Complexities of Common Algorithms
104+
The space complexities of various algorithms is given below -
55105
| Algorithm | Space Complexity |
56106
| --------- | ---------------- |
57107
| Linear Search | O(1) |

0 commit comments

Comments
 (0)