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

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Garfield AP Computer Science
Recursion.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Analysis of Recursive Algorithms
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
Lecture 16: Big-Oh Notation
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Recursion.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Recursion.
Stacks and Queues Introduction to Computing Science and Programming I.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
M180: Data Structures & Algorithms in Java
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Lecture 13 Recursion part 2 Richard Gesick. Advanced Recursion Sometimes recursion involves processing a collection. While it could be done using 'foreach'
11-7 Multiplying Integers Warm Up Find each product ,600 14,000.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Dynamic Programming.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Lecture 151 Programming & Data Structures Dynamic Programming GRIFFITH COLLEGE DUBLIN.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Recursion.
1 A Case Study: Percolation Percolation. Pour liquid on top of some porous material. Will liquid reach the bottom? Applications. [ chemistry, materials.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Rod cutting Decide where to cut steel rods:
Recursion notes Chapter 8.
Java 4/4/2017 Recursion.
Repetition-Counter control Loop
Introduction to Programming
Dynamic Programming.
Dynamic Programming.
Lecture 18 Recursion part 2 Richard Gesick.
Algorithms: Design and Analysis
Java
22C:21 Discrete Structures
Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,
Presentation transcript:

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

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

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); }

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

The BigInteger Data Type Available under java.util namespace Can store really large values – Check Java Documentation for more Details about this type ● html

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;

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") ;

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]); }

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

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

Performance Comparison

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