`

Time complexity of recursive fibonacci algorithm

 
阅读更多

One thing you need to keep in mind about finding the time complexity of a recursive function is recurrence relation.

Below is a C function for finding the fibonacci sequence (same as yours):

 int fib(int n)
 {
   if((n==1) ||(n==2))return 1;
   return (fib(n-1)+fib(n-2));
 }

Converting the above code into a Recurrence Relation:

 T(0) = 0 ......base case
 T(n) = 2T(n-1) + 1

Sol'n

 T(n) = 2T(n-1) + 1
      = 2(2T(n-2) + 1) + 1
      = 4(2T(n-3) + 1) + 1 + 2
      :
      :
      = 2^k T(n-k) + ∑ 2^i
      = 2^k T(n-k) + 2^(k-1)
      = 2^k T(n-k) + (2^k) - 1           using 1-r^n / 1-r

Now,

 n-k = 0
 n=k

Finally,

 T(n) = 2^n T(n-n) + 2^n - 1
      = 2^n T(0) + 2^n - 1
      = 0 + 2^n - 1
      = 2^n - 1

Order:

 O(2^n)  .....Time Complexity

time complexity --- O(2^n)
space complexity-----O(n)
draw recursive tree and calculate its height or depth that will be space complexity

First tree is normal recursion tree, second one is by memoization.

 

Reference:

http://cs.stackexchange.com/questions/14733/complexity-of-recursive-fibonacci-algorithm

http://cs.stackexchange.com/questions/13055/time-complexity-and-space-complexity-in-recursive-algorithm

分享到:
评论

相关推荐

    Time complexity of HSO algorithm

    Calculating Hypervolume is one of the ... In this paper,We have gotten HSO algorithm's time complexity by the path problem of combinatorial mathematics, and described the mapping between the two issues.

    算法的复杂性Complexity of Algorithms

    算法的复杂性是计算理论中的一个重要概念,它研究了算法求解问题所需的资源量与问题规模之间的关系。这种资源主要指的是时间(时间复杂性)和空间(空间复杂性)。算法复杂性的研究对于计算机科学、数学、统计物理学...

    The Complexity of Boolean Functions

    《布尔函数的复杂性》是Ingo Wegener教授在1987年出版的一部专著,该书深入探讨了布尔函数的计算复杂性理论,是计算机科学领域中的一本经典著作。本书由John Wiley & Sons Ltd.和B.G. Teubner出版社联合发行,并在多...

    Dynamic Programming - 01

    For example, if we write simple recursive solution for Fibonacci Numbers, we get exponential time complexity and if we optimize it by storing solutions of subproblems, time complexity reduces to ...

    Complexity of Algorithms

    算法复杂性是计算机科学中的一个核心概念,它探讨了算法执行效率的问题,即算法在解决特定问题时所需的时间和空间资源。《算法复杂性》这本由波士顿大学的Peter Gács和耶鲁大学的László Lovász撰写的讲义,深入...

    Algorithm and Complexity

    ### 知识点总结:《算法与复杂度》 #### 一、背景介绍与书籍概述 本书《算法与复杂度》由Herbert S. Wilf撰写,是关于计算机科学领域中算法及其复杂度分析的经典著作之一。作者为宾夕法尼亚大学教授,在计算机科学...

    complexity of algorithms

    ### 算法复杂度概览 #### 引言与预备知识 《算法复杂度》是一本由波士顿大学的Peter Gács教授和耶鲁大学的László Lovász教授编写的讲义,该讲义是1999年春季学期的课程资料。本书旨在为读者提供一个系统而深入...

    Algorithm And Complexity 《算法和复杂度》(英文版)

    ### 知识点总结:《算法与复杂度》 #### 一、背景介绍与书籍概述 本书《算法与复杂度》由Herbert S. Wilf撰写,主要关注于算法的设计与分析及其复杂度评估。作者Herbert S. Wilf来自宾夕法尼亚大学,该书的版权...

    [Book]Theory of Computational Complexity

    Theory of Computational Complexity offers a thorough presentation of the fundamentals of complexity theory, including NP-completeness theory, the polynomial-time hierarchy, relativization, and the ...

    Low Complexity Joint Hybrid Precoding Algorithm for Millimeter Wave MIMO Systems

    Millimeter wave (mmWave) ... In this paper, we transform the hybrid precoding problem and develop a low complexity joint hybrid precoding algorithm. First, the analog precoder and combiner are ob

    Measuring Cyclomatic Complexity Of Python Code

    这就是 cyclomatic complexity(圈复杂度)的概念发挥作用的地方。圈复杂度是一种软件度量标准,用于估算程序的复杂程度,它通过计算控制流图的边数来确定。这个指标可以提供关于代码中分支和循环数量的信息,从而...

    Theory of Computation Formal Languages, Automata, and Complexity (高清扫描版)

    Theory of Computation covers regular, context-free, and general phrase-structure languages along with their associated automata, computability in the context of Turing machines, partial recursive ...

    juisai.zip_time complexity

    相控阵天线的方向图(切比雪夫加权),LZ复杂度反映的是一个时间序列中,雅克比迭代求解线性方程组课设。

    sequences of games:a tool for taming the complexity of security proofs

    本文介绍了一种在密码学领域中用于组织安全证明的技术——序列游戏(Sequences of Games)。该技术通过将复杂的证明过程拆解为一系列相对简单且有序的游戏,帮助研究者更清晰地理解和验证这些证明。序列游戏的应用...

    算法设计英文版课件:Chapter 2 The complexity of Algorithms

    Chapter 2 of the "Algorithm Design" course material delves into the complexity of algorithms and the lower bounds of problems. This chapter explores the fundamental concepts associated with analyzing ...

    The.MIT.Press.Once.Upon.an.Algorithm.How.Stories.Explain.Computing.0262036630

    Picture a computer scientist, staring at a screen and clicking away frantically on a keyboard, hacking into a ... Something to think about next time we execute the algorithm of getting up in the morning.

    评估算法的时间复杂度(time complexity)的技巧小结

    时间复杂度(time complexity),是评估算法好坏的一个指标,关于它的本质,简单概括就是:时间复杂度是一个算法的输入和它运行所需的时间之间的函数特征。 我们把这个输入称之为问题规模,而这个函数特征,并不是一...

Global site tag (gtag.js) - Google Analytics