MCS-208 · Unit 1

Analysis of Algorithms

Section 7 of 8

Block 1 · Unit 1Analysis of Algorithms12 min · Section 7 of 8

1.7Calculating Space Complexity

Covers IGNOU MCS-208 §1.4

Time is not the only cost. A program that needs more memory than the machine has will not run at all. This page shows how to count memory for iterative programs and, more carefully, for recursive ones.

Why memory matters

If an algorithm is slow, it still finishes, just later. If it needs more memory than is available, it fails (for example with a stack overflow or an allocation error). On the other hand, memory, unlike time, can be reused: when a function returns, its memory is freed for the next call.

It helps to split memory into two parts (Horowitz, Sahni and Mehta): a fixed part (code, simple variables, constants) that does not depend on nn, and a variable part (arrays whose size depends on nn, and the recursion stack).

Iterative programs

For a program without recursion, look at the declarations and allocations: how many variables, how long are the arrays, what does malloc request?

  • Summing an array of nn numbers with a loop needs only i and sum: auxiliary space O(1)O(1).
  • Copying the array into a new array of size nn needs O(n)O(n) auxiliary space.
  • An n×nn \times n table needs O(n2)O(n^2).

Recursive programs

Every active call needs a stack frame: space for its arguments, local variables and return address. So the space of a recursive algorithm is (space per frame) × (maximum recursion depth), the largest number of calls active at the same time. A function that calls itself once per level down to depth nn (like a recursive factorial) uses O(n)O(n) space.

Binary recursion

A binary-recursive function may call itself twice. The study material’s example:

Binary recursion f(n)
1. If n equals 0 or 1, return 1.
2. Recursively calculate f(n-1).
3. Recursively calculate f(n-2).
4. Return the sum of the results of steps 2 and 3.

The GCD algorithm

The study material’s GCD algorithm repeatedly subtracts the smaller number from the larger:

GCD by subtraction
while m > 0:
    if n > m: swap m and n
    m = m - n
return n

Space in complexity theory

On a Turing machine (a theoretical computer with a tape for memory), the space complexity is the maximum number of tape cells used for any input of length nn. The class PSPACE contains the decision problems that can be solved using a polynomial amount of memory, with no limit on time.

Key takeaways

  • Space complexity = peak memory in use, as a function of nn; memory is reused after calls return.
  • Iterative programs: count variables and allocated arrays.
  • Recursive programs: space = frame size × maximum recursion depth.
  • Binary recursion f(n−1)+f(n−2)f(n-1) + f(n-2): exponential time, Θ(n)\Theta(n) space.
  • GCD by subtraction: O(1)O(1) space, between 1 and max⁡(m,n)\max(m, n) iterations.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the space complexity of a recursive factorial function that calls itself on n−1n - 1 until n=0n = 0?

Show answer

Answer: B. O(n)O(n)

The calls for n,n−1,…,0n, n-1, \dots, 0 are all active at the deepest point: n+1n + 1 frames of constant size. O(1)O(1) would be right for the loop version, not the recursive one.

Question 2 · multiple choice

For the binary recursion with f(0)=f(1)=1f(0) = f(1) = 1, how many calls in total does computing f(5)f(5) make?

Show answer

Answer: D. 15

Let C(n)C(n) be the number of calls: C(0)=C(1)=1C(0) = C(1) = 1 and C(n)=1+C(n−1)+C(n−2)C(n) = 1 + C(n-1) + C(n-2). So C(2)=3C(2) = 3, C(3)=5C(3) = 5, C(4)=9C(4) = 9, C(5)=15C(5) = 15, as the program output shows. 8 is the value of f(5)f(5), not the number of calls.

Question 3 · multiple choice

For the same recursion, what is the largest number of calls active at the same time when computing f(30)f(30)?

Show answer

Answer: A. 30

The deepest chain is f(30)→f(29)→⋯→f(1)f(30) \to f(29) \to \dots \to f(1): 30 frames. 2,692,537 is the total number of calls over the whole run, which measures time, not space.

Question 4 · multiple choice

How many loop iterations does GCD by subtraction take for m=48m = 48, n=18n = 18?

Show answer

Answer: B. 5

(48,18) → (30,18) → (12,18) → swap, (6,12) → swap, (6,6) → (0,6): five subtractions, and the GCD is 6. Answering 6 confuses the GCD with the iteration count.

Question 5 · multiple choice

A loop computes the sum of an array of nn integers that is already in memory, using variables i and sum. What is its auxiliary space?

Show answer

Answer: C. O(1)O(1)

Only two extra variables are used, whatever nn is. The array itself is input, so it is not auxiliary space; counting it gives O(n)O(n) total space.

Question 6 · short answer

What is the space complexity of the Euclid (GCD) algorithm in the study material, and why? Is space complexity always more critical than time complexity?

Show worked answer

The space complexity is O(1)O(1): the algorithm stores only the integers mm, nn and a temporary tt for swapping, and this does not depend on the values of mm and nn. Space can be critical because a program that needs more memory than the machine has cannot run at all, while a slow program still finishes eventually. But neither is always more critical: memory can be reused during execution, and in practice the right choice depends on the limits of the system (for example, a real-time system has strict time limits). Often we trade one for the other.

↑ ↓ move↵ openTab switch type