I have been given a simply Python programme as such:
def boo(n):
if n == 1:
return 1
else:
return boo(n/2) + boo(n/2)
To boo, I have to find out the time complexity of the function. My idea is that since each n makes a call to two more boo's, the time complexity is O(2^n). However, it has been revealed that this is wrong. The answer has been revealed to me that the actual time complexity is O(n) without rationale given. My hunch is that boo(n/2) + boo(n/2) can be treated as 2*boo(n/2), but I am highly suspicious of this as I have seen similar scenarios before, and I vaguely remember that boo(n/2) + boo(n/2) cannot be equated to 2*boo(n/2) in terms of processing steps.
What would be a more appropriate way to understand this? I am still struggling with the ideas in order of growth (space and time complexity), so if anyone has any general advice or material that can possibly help me, I greatly welcome them. Thank you very much!
nis not a power of 2, this will recurse infinitely.boo(2048)callsboo(1024)twice, so doubling the input number doubles the number of operations. Which would make it O(n). Leaving aside the issue of using/instead of//.n * 2 - 1total calls, which isO(n)overall.boo(n/2) + boo(n/2)doesboo(n/2)twice, while2*boo(n/2)does it only once, so clearly they can't be equally complex (unless that complexity is constant, but then it would be independent ofn, so...).