Time and Space Complexity of Recursive Algorithms

Algorithm/Insights

Fibonacci Sequence: In the below screenshot, you can see that the function 'fibonacci(int n)' computes n'th number of fibonacci sequence. The fibonacci sequence is 0,1,1,2,3,5,8,13,21,... . As you can observe 0th number of this sequence is 0, 1st number of this sequence is 1 and so on. In general, if f(n) denotes n'th number of fibonacci sequence then f(n) = f(n-1) + f(n-2). For this recurrence relation, f(0) = 0 and f(1) = 1 are terminating conditions.


Time Complexity: Let us look at the recursion tree generated to compute the 5th number of fibonacci sequence.

In this recursion tree, each state(except f(0) and f(1)) generates two additional states and total number of states generated are 15. In general, total number of states are approximately equal to 2^n for computing n'th fibonacci number(f(n)). Notice that each state denotes a function call to 'fibonacci()' which does nothing but to make another recursive call. Therefore total time taken to compute nth number of fibonacci sequence is O(2^n).
Note that this does not always hold true and for more accurate time complexity analysis, you should be making use of master theorem. The purpose of this explanation is to give you a general idea about running time of recursive algorithms.   

Space Complexity: Computing space complexity of recursive algorithm is little bit tricky. We need to understand how the stack frames are generated in memory for recursive call sequence. 

Let's try to understand in general when the stack frames are generated and for how long are they kept in the memory? When a function 'f1' is called from function 'f0', stack frame corresponding to this function 'f1' is created. This stack frame is kept in the memory until call to function 'f1' is not terminated. This stack frame is responsible for saving the parameters of function 'f1', local variables in the function 'f1' and return address of caller function(function 'f0'). Now when this function 'f1' calls another function 'f2', stack frame corresponding to 'f2' is also generated and is kept in the memory until call to 'f2' is not terminated. When call to 'f2' is made, the call stack which has stack frames in it looks like following - 


Now when call to function 'f2' returns, the stack frame corresponding to 'f2' is deleted from memory since it is no longer required. Same is the case for stack frames of function 'f1' and function 'f0'.

Using this analogy for recursive call sequence, it should follow that maximum number of stack frames that could be present in memory at any point of time is equal to maximum depth of recursion tree. In the recursion tree, when the call corresponding to leaf node state is getting executed, its call sequence could be represented by the path from the root node in recursion tree to that leaf node. For example, in the recursion tree for computing 5th fibonacci number, when left and bottom most state f(1) is getting executed, the call sequence which led to this state would be f(5)->f(4)->f(3)->f(2)->f(1) and all the corresponding stack frames would be present in the memory and when f(1) is returned its stack frame would be removed from the memory(or call stack).

To conclude, space complexity of recursive algorithm is proportinal to maximum depth of recursion tree generated. If each function call of recursive algorithm takes O(m) space and if the maximum depth of recursion tree is 'n' then space complexity of recursive algorithm would be O(nm).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值