Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Set 5: Problem 2 22C:021 Computer Science Data Structures.

Similar presentations


Presentation on theme: "Problem Set 5: Problem 2 22C:021 Computer Science Data Structures."— Presentation transcript:

1 Problem Set 5: Problem 2 22C:021 Computer Science Data Structures

2 What needs to be done? Implement a Fibonacci Number Generator – A naive Fibonacci number generator Implement an optimized Fibonacci number generator – That remembers results of sub-problems already solved Evaluate Performance

3 Fibonacci Number Generator: Naive public static int fibonacci(int n) { if((n == 1) || (n == 2)) return 1; else return fibonacci (n-1) + fibonacci (n-2); }

4 Fibonacci Number Generator: Optimized We need: – An array “answers” that stores results of solved sub- problems – A type that can handle large numbers ● Fibonacci numbers grow fast, integers and longs run out of range – A way to check if a sub-problem has already been solved – Only need to recurse when necessary

5 The BigInteger Data Type Available under java.util namespace Can store really large values – Check Java Documentation for more Details about this type ● http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger. html

6 The “answers” array What should be the size of the “answers” array – The size n of this array should be equal to the Fibonacci number we've been asked to generate – This array stores the n th Fibonacci number at n-1 th Location private static BigInteger[] answers;

7 Initialize the “answers” array // Initializing answers int number = ; answers = new BigInteger[number]; // The first two numbers in series are 1 answers[0] = new BigInteger("1"); answers[1] = new BigInteger("1"); // Set all others to zeros to mark them as // not-computed for(int i = 2; i < number; i++) answers[i] = new BigInteger("0") ;

8 Optimized Fibonacci Number Generator public static BigInteger fastFibonacci(int n) { if((n == 1) || (n == 2)) return answers[0]; if(answers[n-1].compareTo(zero) != 0) return answers[n-1]; if(answers[n-2].compareTo(zero) == 0) answers[n-2] = fastFibonacci(n-1); if(answers[n-3].compareTo(zero) == 0) answers[n-3] = fastFibonacci(n-2); return answers[n-2].add(answers[n-3]); }

9 Optimized Fibonacci Number Generator When asked to generate a number – We check if the number requested has already been computed and return it if it has been. – Then, we check if the required numbers have already been computed (n – 1 and n - 2) – If they have, we straightaway use their values – If they haven't, we call the generator number on each of the missing required numbers – Once we have both the value, we add them and return the sum

10 Performance Comparisons How fast is the optimized version? – To generate the 46 th Fibonacci Number ● The unoptimized version takes 885490.7 ms = Approx 15 minutes ● The optimized version takes 0.145 ms

11 Performance Comparison

12 Other Performance Stats The Optimized version takes – For 100 th Number: 0.329 ms – For 1000 th Number: 5.172 ms The Naive version takes – So long that I did not evaluate...


Download ppt "Problem Set 5: Problem 2 22C:021 Computer Science Data Structures."

Similar presentations


Ads by Google