0

How to calculate the space complexity in terms of recursion? For example, is it equal to the maximum recursion stack at all points of program execution? or do (memory) released/returned recursive calls also count? Thanks.

prin
  • 35
  • 3
  • It's equal to the maximum space occupied at any instant. If you use X amount of space, then free it, then use X space again, then free it, then X again, etc, and do that Y times, it's still only O(X). But if you use X amount, then don't free it, then X again, still don't free anything, then X again, etc, and do that Y times, then suddenly you're using Ө(X Y) space. – Stef Dec 28 '21 at 20:37
  • So there would be a huge amount of difference in space complexity between these two functions: `function f(n) { use X amount of space; f(n-1); free space; }` and `function g(n) { use X amount of space; free space; g(n-1); }` – Stef Dec 28 '21 at 20:40
  • This similar question has a few longer answers: [What are the fundamentals of calculating space complexity in loops?](https://stackoverflow.com/questions/70573130/what-are-the-fundamentals-of-calculating-space-complexity-in-loops) – Stef Jan 04 '22 at 14:02

0 Answers0