-3

Let's say I have an Algorithm A which is a combination of 2 algorithms A1 and A2.

The algorithm A consists of carrying out algorithm A1 and A2 one after another. The input to algorithm A are 2 arrays of length m and n respectively.

The goal of algorithm A1 is to modify the 2 arrays. Overall, the space complexity of A1 is O(m^2 + n^2).

Then the modified array is sent to A2. And I do not need the auxiliary space occupied by algorithm A1. The space complexity of A2 is O(mn).


So what should be the overall space complexity of the whole algorithm (A1+A2)? Should it just be O(m^2 + n^2 + mn)?

However, the space occupied by A1 and A2 do not happen at the same time and space complexity is all about the worst case space occupied at a point in time. So should it be more appropriate to write O(max(m^2+n^2,mn))?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Buna
  • 45
  • 4

1 Answers1

1

In a sense you are right, the space complexity overall should be O(m^2 + n^2 + mn). However since mn < max(m^2, n^2) you can drop it and write is as O(m^2 + n^2).

Sorin
  • 11,863
  • 22
  • 26