1 / 12

Problem Set 5: Problem 2

22C:021 Computer Science Data Structures. Problem Set 5: Problem 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.

Download Presentation

Problem Set 5: Problem 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 nth Fibonacci number at n-1th Location private static BigInteger[] answers;

  7. Initialize the “answers” array // Initializing answers int number = <fibonacci number to generate>; 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 46th 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 100th Number: 0.329 ms • For 1000th Number: 5.172 ms • The Naive version takes • So long that I did not evaluate ...

More Related