/Lecture2 /Lecture4
###### Euclid's algorithm, recursion trees for fibonacci

In /Lecture2 we showed:

For fun, can you convince yourself that the following is true?

*Worst-case running time of Euclid's algorithm continued, recursion tree for Fibonacci sequence*

- worst-case running time of gcd1(i,j) is proportional to i,
- worst-case running time of gcd2(i,j) is proportional to i,
- worst-case running time of gcd3(i,j) is
*at most*proportional to log(i), - gcd3(fib(n), fib(n-1)) takes time proportional to n.

Today we will work on showing:
**claim:** for all n, n is at least log_{2}(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

- worst-case running time of gcd3(i,j) is
*at least*proportional to i.

- worst-case running time of gcd3(i,j) is proportional to i.

Note that, by algebra, the claim is equivalent to
**claim:** for all n, fib(n) is at most 2^{n}

One way to show the claim is by induction on n:

**base cases:** For n = 0, fib(n) = 0 ≤ 2^{n}. For n=1, fib(n) = 1 ≤ 2^{n}.

**induction step:** For n > 1, fib(n) = fib(n-1) + fib(n-2) ≤ 2^{n-1} + 2^{n-2} ≤ 2*2^{n-1} = 2^{n}.

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, 2^{5} leaves and 2^{6}-1 nodes.

Generally, the number of leaves in the complete binary tree of depth n is 2^{n}.

Next let's consider the recursion tree for fib(n). Here's an example for n=8:

**observation**: for all n, fib(n) equals the number of nodes in the recursion tree with label "1"

Once you convince yourself that this is true, you just need to convince yourself that there
are at most 2^{n} 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

- fib(n) ≤ 2
^{n}

- log
_{2}fib(n) ≤ n .

- n ≥ log
_{2}fib(n)

- gcd3(fib(n), fib(n-1)) takes time at least proportional to n,

- gcd3(i, j) takes time at least proportional to log(i).

Given the upper bound we've already shown, we conclude that

- the worst-case running time of gcd3(i, j) is proportional to log(i).

For fun, can you convince yourself that the following is true?

**claim**: the number of nodes labeled "1" in the tree is*at least*2^{n/2-1}.