Fibonacci Numbers
Last updated
Last updated
A mathematical series of numbers as Fibonacci numbers as to provide a foundation for building a Golden ratio number, could be computed in a recurrence relation:
Given function Fn to denote the nth position of a Fibonacci sequence; And,
F1 = F2 = 1; Fn = Fn-1 + Fn-2
By adopting top-down recursive approach,
which comes down to a recurrence relation: Τ(n) = Τ(n-1) + Τ(n-2) + Ο(1) ⩾ ϕn
Τ(n) ⩾ 2 ⋅ Τ(n-2) + Ο(1) ⩾ 2n/2
Hence, it takes exponential time to compute the nth fibonacci number, wherein lots of repetitive computations are being performed; Specifically in the following diagram,
Figure 1. Fibonacci Numbers in Recursive Computations
To improve upon that, a memoized version of such algorithm is introduced:
Therefore, FIBONACCI(k) only takes one recursion, ∀ k ∈ n; and all memoized calls use Θ(1) time. Thus, the time complexity is Ο(n), space complexity is Ο(1)
What about we don't want to have recursions? Then, we can use an extra linear space to store the FIB(n - 1) and FIB(n - 2) when computing FIB(n), which is to use iteration in replacement of recursion and having the same time complexity but avoid the use of recursion, which is also called bottom-up DP.