# Cs141 Home/Lecture3

Cs141 Home | Cs141 Home | recent changes | Preferences

/Lecture2 /Lecture4

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

###### Euclid's algorithm, recursion trees for fibonacci
In /Lecture2 we showed:
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 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

worst-case running time of gcd3(i,j) is at least proportional to i.
Given the matching upper bound, we will be able to conclude:
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 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:

observation: for all n, fib(n) equals the number of nodes in the recursion tree with label "1"
You can see this by induction.

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

fib(n) ≤ 2n
Taking logs (base 2) of each side,
log2 fib(n) ≤ n .
This implies
n ≥ log2 fib(n)
Thus, n is at least log(fib(n)). From this and the observation that
gcd3(fib(n), fib(n-1)) takes time at least proportional to n,
we conclude that, on infinitely many inputs,
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 2n/2-1.
To see this, compare the number to of these nodes to the number of nodes in the tree at depth floor(n/2) (that is, at distance floor(n/2) from the top).

Cs141 Home | Cs141 Home | recent changes | Preferences