Fibonacci numbers

 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References see the Weiss text, pages 183-185

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition the Fibonacci sequence F is defined inductively as follows:

F0 is equal to 0;
F1 is equal to 1;
Fn is equal to Fn-1 + Fn-2, otherwise

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition
bullet Motivations and connections Fibonacci originally defined this sequence to characterize the growth in size of rabbit populations;
But there are enough other applications that a qurterly journal is devoted to the study of Fibonacci (and related) sequences

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition
bullet Motivations and connections
bullet Straight-forward program for Fib(n) the value of the Fibonacci number Fn can be computed recrsively as follows:
public static long fib(int n) {
if (n <= 1)
    return n
else 
    return fib(n-1) + fib(n-2)
}

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition
bullet Motivations and connections
bullet Straight-forward program for Fib(n)
bullet Analyzing recursive calls the running time of recursive programs can be analyzed using recurrence equations; we will study these techniques in more depth when we develop divide-and-conquer algorithms (see the Weiss text, page 200 and following)

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition
bullet Motivations and connections
bullet Straight-forward program for Fib(n)
bullet Analyzing recursive calls
bullet Time analysis for fib the time consumed in computing fib is clearly proportional to the number of calls made to fib; this turns out to grow exponentially in n, so that for n=40, over 300 million calls are made

Control bar


















































 

CS 241: Data Structures
Lecture #2: Two Sample Problems in Analysis

Fibonacci numbers
bullet References
bullet Definition
bullet Motivations and connections
bullet Straight-forward program for Fib(n)
bullet Analyzing recursive calls
bullet Time analysis for fib
bullet Space analysis for fib the above implementation of Fibonacci never uses much space, since each call uses only a small amount of local memory, and the number of calls pending at any given time is bounded by n

Control bar