Worst-case running time of Euclid's algorithm continued, recursion tree for Fibonacci sequence
Today we will work on showing:
claim: for all n, n is at least log2(fib(n))
If the claim is true, there are infinitely many inputs on which gcd3(i,j) takes time proportional to log(i), so we can conclude
Note that, by algebra, the claim is equivalent to
claim: for all n, fib(n) is at most 2n
One way to show the claim is by induction on n:
base cases: For n = 0, fib(n) = 0 ≤ 2n. For n=1, fib(n) = 1 ≤ 2n.
induction step: For n > 1, fib(n) = fib(n-1) + fib(n-2) ≤ 2n-1 + 2n-2 ≤ 2*2n-1 = 2n.
Let's consider another way, to get some more intuition.
First, consider the complete binary tree of depth n. Here's the complete binary tree of depth 5:
This tree has depth 5, 25 leaves and 26-1 nodes.
Generally, the number of leaves in the complete binary tree of depth n is 2n.
Next let's consider the recursion tree for fib(n). Here's an example for n=8:
Once you convince yourself that this is true, you just need to convince yourself that there are at most 2n such nodes. To see this, note that if the tree was a complete binary tree of depth n-1, the number of leaves would be more than the number of such nodes.
We can conclude (from the observation) that
Given the upper bound we've already shown, we conclude that