Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do recursive calls count into space complexity?

When an algorithm doesn't use more than a constant amount of auxiliary memory but does have O(log(N)) recursive calls (each one taking some extra space on the stack), is that algorithm's space complexity O(1) or O(log(N))?

like image 746
NPS Avatar asked Mar 19 '23 06:03

NPS


1 Answers

If the recursive algorithm is not exploiting tail recursion, then, yes, a straightforward implementation would be using O(log(N)) space. This is because the runtime must keep O(log(N)) stack frames, each of O(1) size, in memory at once.

like image 89
Timothy Shields Avatar answered Mar 26 '23 02:03

Timothy Shields