Fibonacci numbers and recurrences

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Fibonacci Sequences Susan Leggett, Zuzana Zvarova, Sara Campbell
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
FIBONACCI Lecture 23 CS2110 – Spring 2015 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy And recurrences.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Analysis of Recursive Algorithms
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
FIBONACCI NUMBERS 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, ,
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013.
CS 3343: Analysis of Algorithms
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Fermat’s Little Theorem Fibonacci Numbers Shirley Moore CS4390/5390 Fall September 10,
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
FIBONACCI NUMBERS AND RECURRENCES Lecture 26 CS2110 – Spring 2016 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy.
Emma Stephens, Charlotte Evans, Kenneth Mcilree, Lisa Yuan
7.4 Exploring recursive sequences fibonacci
CSE332: Data Abstractions Lecture 7: AVL Trees
Analysis of Algorithms and Inductive Proofs
Introduction to Algorithms: Divide-n-Conquer Algorithms
Data Structures – LECTURE Balanced trees
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
CS 3343: Analysis of Algorithms
Fibonacci numbers Golden Ratio, recurrences
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Recursion (Continued)
Fibonacci numbers Golden Ratio, recurrences
Recursion (Continued)
Lecture Outline for Recurrences
Fibonacci numbers Golden Ratio, recurrences
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS 201 Fundamental Structures of Computer Science
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Recursion (Continued)
Searching CLRS, Sections 9.1 – 9.3.
CSE 373 Data Structures Lecture 5
Searching, Sorting, and Asymptotic Complexity
Divide-and-Conquer 7 2  9 4   2   4   7
Recursion (Continued)
Fibonacci numbers Golden Ratio, recurrences
At the end of this session, learner will be able to:
Algorithms Recurrences.
Induction: One Step At A Time
Surprising Connections in Math: From the Golden Ratio to Fractals
Presentation transcript:

Fibonacci numbers and recurrences (Leonardo Pisano) 1170-1240? Statue in Pisa Italy Fibonacci numbers and recurrences Lecture 23 CS2110 – Fall 2015

Prelim 2 Thursday at 5:30 and 7:30 5:30. Statler Auditorium. Last name M..Z 7:30. Statler Auditorium. Last name A..L If you completed assignment P2Conflict and did not hear from us, just go to your desired time 26 people 7:30 -> 5:30 07 people 5:30 -> 7:30 If you are AUTHORIZED BY CORNELL to have a quiet place or extend time and completed P2COnflict, go to Gates 405, starting from 5:15.

About implementing heaps N is size of heap /** Add e with priority p to the priority queue. * Throw an illegalArgumentException if e is already in the queue. * Expected time is O(log N), worst-case time is O(N). */ public void add(E e, double p) { if (b.contains(e)) throw new …); b.add(size,e); size= size+1; int ni= b.indexOf(e); } O(N) operation O(N) operation -------------- size-1;

Operations size, isEmpty, get, set, iterator, and listIterator run in constant time. Operation add runs in amortized constant time, that is, adding n elements requires O(n) time. All the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Creating unnecessary objects In bubble-up b.set(k, child); map.put(child, new Info(k, p_child)); BETTER: b.set(k, child); map.get(child).index= k;

More effective presentation 1. Put { on line before, with space before 2. Put space after if, else, while do, etc. 3. If the “then-part” of an if statement returns or throws, do not use else. public E peek() { if(b.size() == 0) { throw new PC…(); } else return b.get(0); public E peek() { if (b.size() == 0) { throw new PC…(); } return b.get(0);

Fibonacci function fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) for n ≥ 2 0, 1, 1, 2, 3, 5, 8, 13, 21, … In his book in 1202 titled Liber Abaci Has nothing to do with the famous pianist Liberaci But sequence described much earlier in India: Virahaṅka 600–800 Gopala before 1135 Hemacandra about1150 The so-called Fibonacci numbers in ancient and medieval India. Parmanad Singh, 1985

Fibonacci function (year 1202) fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) for n ≥ 2 /** Return fib(n). Precondition: n ≥ 0.*/ public static int f(int n) { if ( n <= 1) return n; return f(n-1) + f(n-2); } 0, 1, 1, 2, 3, 5, 8, 13, 21, …

Fibonacci function (year 1202) Downloaded from wikipedia Fibonacci spiral Fibonacci tiling 0, 1, 1, 2, 3, 5, 8, 13, 21, …

fibonacci and bees MB 1 Male bee has only a mother FB 1 FB MB 2 FB MB FB 3 FB MB FB FB MB 5 FB MB FB FB MB FB MB FB 8 MB: male bee, FB: female bee Male bee has only a mother Female bee has mother and father The number of ancestors at any level is a Fibonnaci number

Fibonacci in nature The artichoke uses the Fibonacci pattern to spiral the sprouts of its flowers. The artichoke sprouts its leafs at a constant amount of rotation: 222.5 degrees (in other words the distance between one leaf and the next is 222.5 degrees). You can measure this rotation by dividing 360 degrees (a full spin) by the inverse of the golden ratio. We see later how the golden ratio is connected to fibonacci. topones.weebly.com/1/post/2012/10/the-artichoke-and-fibonacci.html

Fibonacci in Pascal’s Triangle 1 1 2 3 5 1 1 2 3 4 5 6 7 8 8 1 1 13 21 1 2 1 1 3 1 1 4 6 1 5 10 6 15 20 7 21 35 1 1 1 1 1 1 p[i][j] is the number of ways i elements can be chosen from a set of size j

Blooms: strobe-animated sculptures www.instructables.com/id/Blooming-Zoetrope-Sculptures/

Uses of Fibonacci sequence in CS Fibonacci search Fibonacci heap data strcture Fibonacci cubes: graphs used for interconnecting parallel and distributed systems

Recursion for fib: f(n) = f(n-1) + f(n-2) f(18) + f(19) f(16) + f(17) + f(17) + f(18) f(14)+f(15) + f(15)+F(16) f(15)+f(16) + f(16)+f(17) Calculates f(15) four times! What is complexity of f(n)?

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a “Recurrence relation” for the time T(1) = a It’s just a recursive function T(n) = a + T(n-1) + T(n-2) We can prove that T(n) is O(2n) It’s a “proof by induction”. Proof by induction is not covered in this course. But we can give you an idea about why T(n) is O(2n) T(n) <= c*2n for n >= N

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = a + T(n-1) + T(n-2) T(n) <= c*2n for n >= N T(2) = <Definition> a + T(1) + T(0) ≤ <look to the left> a + a * 21 + a * 20 T(0) = a ≤ a * 20 T(1) = a ≤ a * 21 = <arithmetic> a * (4) = <arithmetic> a * 22

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = T(n-1) + T(n-2) T(n) <= c*2n for n >= N T(3) = <Definition> a + T(2) + T(1) ≤ <look to the left> a + a * 22 + a * 21 T(0) = a ≤ a * 20 T(1) = a ≤ a * 21 = <arithmetic> a * (7) T(2) = a ≤ a * 22 ≤ <arithmetic> a * 23

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = T(n-1) + T(n-2) T(n) <= c*2n for n >= N T(4) = <Definition> a + T(3) + T(2) T(0) = a ≤ a * 20 ≤ <look to the left> a + a * 23 + a * 22 T(1) = a ≤ a * 21 = <arithmetic> a * (13) T(2) = a ≤ a * 22 ≤ <arithmetic> a * 24 T(3) = a ≤ a * 23

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = T(n-1) + T(n-2) T(n) <= c*2n for n >= N T(5) = <Definition> a + T(4) + T(3) T(0) = a ≤ a * 20 ≤ <look to the left> a + a * 24 + a * 23 T(1) = a ≤ a * 21 T(2) = a ≤ a * 22 = <arithmetic> a * (25) T(3) = a ≤ a * 23 ≤ <arithmetic> a * 25 T(4) = a ≤ a * 24 WE CAN GO ON FOREVER LIKE THIS

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = T(n-1) + T(n-2) T(n) <= c*2n for n >= N T(k) = <Definition> a + T(k-1) + T(k-2) T(0) = a ≤ a * 20 ≤ <look to the left> a + a * 2k-1 + a * 2k-2 T(1) = a ≤ a * 21 T(2) = a ≤ a * 22 = <arithmetic> a * (1 + 2k-1 + 2k-2) T(3) = a ≤ a * 23 ≤ <arithmetic> a * 2k T(4) = a ≤ a * 24

Recursion for fib: f(n) = f(n-1) + f(n-2) T(0) = a T(1) = a T(n) = T(n-1) + T(n-2) T(n) <= c*2n for n >= N Need a formal proof, somewhere. Uses mathematical induction T(0) = a ≤ a * 20 T(1) = a ≤ a * 21 “Theorem”: all odd integers > 2 are prime 3, 5, 7 are primes? yes 9? experimental error 11, 13? Yes. That’s enough checking T(2) = a ≤ a * 22 T(3) = a ≤ a * 23 T(4) = a ≤ a * 24 T(5) = a ≤ a * 25

The golden ratio a > 0 and b > a > 0 are in the golden ratio if (a + b) / a = a/b call that value ϕ ϕ2 = ϕ + 1 so ϕ = (1 + sqrt(5)) /2 = 1.618 … 1.618…. ratio of sum of sides to longer side = ratio of longer side to shorter side a 1 b

The golden ratio How to draw a golden rectangle a b golden rectangle

The Parthenon

Can prove that Fibonacci recurrence is O(ϕn) We won’t prove it. Requires proof by induction Relies on identity ϕ2 = ϕ + 1

Linear algorithm to calculate fib(n) /** Return fib(n), for n >= 0. */ public static int f(int n) { if (n <= 1) return 1; int p= 0; int c= 1; int i= 2; // invariant: p = fib(i-2) and c = fib(i-1) while (i < n) { int fibi= c + p; p= c; c= fibi; i= i+1; } return c + p;

Logarithmic algorithm! f0 = 0 f1 = 1 fn+2 = fn+1 + fn 0 1 fn fn+1 0 1 f0 fn = so: = 1 1 fn+1 fn+2 1 1 f1 fn+1 You know a logarithmic algorithm for exponentiation —recursive and iterative versions n Gries and Levin/ Computing a Fibonacci number in log time. IPL 2 (October 1980), 68-69.

Constant-time algorithm! Define φ = (1 + √5) / 2 φ’ = (1 - √5) / 2 The golden ratio again. Prove by induction on n that fn = (φn - φ’n) / √5 We went from O(2n) to O(1)