Complexity for recursive functions - Time and Space

荒凉一梦 提交于 2019-12-01 05:34:05

问题


I was interested in knowing how to calculate the time and space complexity of recursive functions like permutation, fibonacci(described here)

In general we can have recursion at many places than just at permutaion or recursion, so I am looking for approach generally followed to calculate tmie ans space complexity

Thank you


回答1:


Take a look at http://www.cs.duke.edu/~ola/ap/recurrence.html




回答2:


Time complexity and space complexity are the two things that characterize the performance of an algorithm. Nowadays, as space is relatively inexpensive, people bother mostly about time complexity, and time complexity is mostly expressed in terms of a recurrence relation.

Consider binary search algorithm (Search for an element in an array): Each time middle element(mid) is selected and compared with the element(x) to be searched. If (mid > x), search the lower sub-array, otherwise search the upper sub-array. If there are n elements in an array, and let T(n) represents the time complexity of the algorithm, then T(n) = T(n/2) + c, where c is a constant. With given boundary conditions, we can solve for T(n), in this case, it would be T(n) = log(n), with T(1) = 1.



来源:https://stackoverflow.com/questions/4326634/complexity-for-recursive-functions-time-and-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!