Fibonacci Sequence 维基百科

\(F(n) = F(n-1)+F(n-2)\),其中 \(F(0)=0, F(1)=1\),即该数列由 0 和 1 开始,之后的数字由相邻的前两项相加而得出。

递归

def fibonacci(n):
    assert n >= 0, 'invalid n'
    if n < 2: return n
    return fibonacci(n - 1) + fibonacci(n -2)

递归方法的时间复杂度为高度为 \(n-1\) 的不完全二叉树的节点数,所以近似为 \(O(2^n)\)

数学求解方法:

\(T(n) = T(n-1) + T(n-2) \qquad (n>1)\)

设 \(f(n)\) 为参数为 n 时的时间复杂度 \(f(n) = f(n-1) + f(n-2)\)

转化为求二阶常系数齐次差分方程,设通解为 \(f_{n} = C_{1}f_{n-1} + C_{2}f_{n-2}\)

设有特解 \(f_{n} = \lambda\),\(\lambda\) 为非零待定常数,将 \(\lambda\) 代入方程,易得特征方程 \(\lambda ^{2} = \lambda + 1\),则 \(λ = \frac{1\pm \sqrt{5}}{2}\)

再根据 \(f(0) = 0, f(1) = 1.\) 求出 \(C_{1}\) 和 \(C_{2}\)

得出通项公式

\[
f(n) = \frac{1}{\sqrt{5}}[(\frac{1+ \sqrt{5}}{2})^{n} - (\frac{1- \sqrt{5}}{2})^{n}]
\]

当 \(n->\infty\) 时, \(\left |(\frac{1- \sqrt{5}}{2})^{n} \right |<1\),所以趋于零。

时间复杂度为 \(O((\frac{1+ \sqrt{5}}{2})^{n})\) ,约等于 \(O(1.618^{n})\) ,即指数级复杂度 \(O(2^n)\),递归算法空间复杂度取决于递归的深度,显然为 \(O(n)\)。

迭代

def fibonacci(n):
    assert n >= 0, 'invalid n'
    if n == 0: return 0
    a, b = 0, 1
    for _ in range(n - 1):
        a, b = b, a+b
    return b

时间复杂度 \(O(n)\),空间复杂度 \(O(1)\)

矩阵

\(F(n)\) 和 \(F(n - 1)\) 写成一个 2 x 1 的矩阵,然后对其进行变形。

\(\begin{bmatrix}F_{n}\\F_{n-1}\end{bmatrix}=\begin{bmatrix}F_{n-1}+F_{n-2}\\F_{n-1}\end{bmatrix}=\begin{bmatrix}1\times F_{n-1}+1\times F_{n-2}\\1\times F_{n-1}+0\times F_{n-2}\end{bmatrix}=\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}\times\begin{bmatrix}F_{n-1}\\F_{n-2}\end{bmatrix}=...=\begin{bmatrix}1&1\\1&0\end{bmatrix}^{n-1}\times\begin{bmatrix}F_{1}\\F_{0}\end{bmatrix}=\begin{bmatrix}1&1\\1&0\end{bmatrix}^{n-1}\times\begin{bmatrix}1\\0\end{bmatrix}\)

因此要求 \(F_{n}\),只要对这个二阶方阵求 \(n - 1\) 次方,最后取结果方阵第一行第一列的数字就可以了。

等式中的矩阵 \(\begin{bmatrix}1&1\\1&0\end{bmatrix}\) 被称为斐波那契数列的 Q- 矩阵。

通过 Q- 矩阵,我们可以利用如下公式进行计算:

\(F_{n} = Q^{n-1}_{1,1}\)

如此一来,计算斐波那契数列的问题就转化为了求 \(Q^{n-1}\) 的问题。

\[
A^{n} = \begin{cases} A\cdot (A^{2})^{\frac{n-1}{2}}, & \text{if $n$ is odd} \\\ (A^{2})^{\frac{n}{2}}, & \text{if $n$ is even} \\ \end{cases}
\]

可见时间复杂度满足 \(T(n) = T(n / 2) + O(1)\)
Master 定理 可得

时间复杂度 \(O( \log n)\) ,空间复杂度显然为 \(O(1)\)

from numpy import matrix

def MatrixPower(mat, n):
    assert n > 0, 'invalid n'
    res = None
    temp = mat
    while True:
      if n & 1:
        if res is None: res = temp
        else: res = res * temp
      n >>= 1
      if n == 0: break
      temp = temp * temp
    return res

def fibonacci(n):
    assert n >= 0, 'invalid n'
    if n < 2: return n  # F(0) = 0, F(1) = 1
    mat = matrix([[1, 1], [1, 0]], dtype=object)
    mat = MatrixPower(mat, n - 1)
    return mat[0, 0]

通项公式法

def fibonacci(n):
    root_Five = 5**0.5
    result = (((1 + root_Five) / 2)**n - ((1 - root_Five) / 2)**n) / root_Five
    return int(result)

显然,该方法的时空复杂度均为 \(O(1)\) , 但使用公式计算的方法由于有大量的浮点运算,在 n 增大时浮点误差不断增大会导致返回结果不正确甚至数据溢出。

方法 时间复杂度 空间复杂度
递归 \(O(2^{n})\) \(O(n)\)
迭代 \(O(n)\) \(O(1)\)
矩阵 \(O(log^{n})\) \(O(1)\)
公式 \(O(1)\) \(O(1)\)

Computational Complexity of Fibonacci Sequence / 斐波那契数列的时空复杂度的更多相关文章

  1. ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)

    1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...

  2. 10、end关键字和Fibonacci series: 斐波纳契数列

    # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 #复合赋值表达式,a,b同时赋值0和1 while b < 10: print(b ...

  3. [Amazon] Program for Fibonacci numbers 斐波那契数列

    The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21 ...

  4. Fibonacci series(斐波纳契数列)的几种常见实现方式

    费波那契数列的定义: 费波那契数列(意大利语:Successione di Fibonacci),又译费波拿契数.斐波那契数列.斐波那契数列.黄金切割数列. 在数学上,费波那契数列是以递归的方法来定义 ...

  5. 【算法】Fibonacci(斐波那契数列)相关问题

    一.列出Fibonacci数列的前N个数 using System; using System.Collections.Generic; using System.Linq; using System ...

  6. 509. Fibonacci Number斐波那契数列

    网址:https://leetcode.com/problems/fibonacci-number/ 原始的斐波那契数列 运用自底向上的动态规划最佳! 可以定义vector数组,但是占用较多内存空间 ...

  7. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

  8. Fibonacci(斐波那契数列)的第N位数

    无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).. ...

  9. 【LeetCode每天一题】Fibonacci Number(斐波那契数列)

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

随机推荐

  1. Mysql使用事务

    DECLARE t_error INTEGER DEFAULT 0; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; START TR ...

  2. util之Map

    1.定义 Map<String, Integer> map = new HashMap<String,Integer>(); 2.判断map中是否存在某个键的值: if(map ...

  3. HTML的链接标签

    网页的链接标签 文本超链接 图像超链接 格式:<a href="path" target="目标窗口位置" >链接文本或图像</a> 锚 ...

  4. AcWing 830. 单调栈

    https://www.acwing.com/problem/content/832/ #include <iostream> using namespace std; ; int stk ...

  5. MySQL连接池详解

    使用场景数据库连接是一种关键的.有限的.昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池正是针对 ...

  6. pycharm pro与你同在

    下载激活码和激活依赖的jar包,地址(https://www.lanzous.com/b00t4aneb密码:67t9)按照步骤操作即可第一步:正确安装 Pycharm 软件(版本2019.1.3 p ...

  7. codeforces 1204C Anna, Svyatoslav and Maps(floyd+dp)

    题目链接:http://codeforces.com/problemset/problem/1204/C 给定一组序列,P1,P2,P3...Pm,这是一组合法路径的序列,即任意的Pi和Pi+1之间有 ...

  8. 对C#继承、多态的理解

    11月3日 阴天 前两天看某位大牛写的程序,对于C#多态有困惑,今天一大早来查阅了不少资料,自认为有了一个基本的认知,记录下来,一扫今天这阴霾的天气 ------------------------- ...

  9. ubuntu16.04无法打开终端

    最近将自带的python3.5更改为默认的python3.6,所以就出现了终端打不开的情况,以下是我的解决办法: 首先,按ctrl+alt+F1进入命令行模式,也就是无图形截面,这时候会让你输入用户名 ...

  10. ASA映射80端口到公网

    1.测试拓扑: 2.测试目的:Web Server:192.168.1.100/24 GW:192.168.1.254Internet:200.1.1.2/24 映射的地址:200.1.1.3 3.配 ...