FIBONACCI NUMBERS AND RECURRENCES Lecture 26 CS2110 – Spring 2016 Fibonacci (Leonardo Pisano) 1170-1240? Statue in Pisa Italy.

Slides:



Advertisements
Similar presentations
Biography ( ) Fibonacci is a short for the Latin "filius Bonacci" which means "the son of Bonacci" but his full name was Leonardo of Pisa, or Leonardo.
Advertisements

Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Fibonacci Sequences Susan Leggett, Zuzana Zvarova, Sara Campbell
FIBONACCI Lecture 23 CS2110 – Spring 2015 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy And recurrences.
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, ,
DUYGU KANDEMİR
CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
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.
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.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Geometry: Similar Triangles. MA.912.D.11.5 Explore and use other sequences found in nature such as the Fibonacci sequence and the golden ratio. Block.
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.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Lecture 4,5 Mathematical Induction and Fibonacci Sequences.
Fibonacci Numbers (cont.) Pseudoprimes Shirley Moore CS 4390/5390 Fall September 12, 2013.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Fermat’s Little Theorem Fibonacci Numbers Shirley Moore CS4390/5390 Fall September 10,
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Leonardo Fibonacci.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
1 CMSC 341 Math Review. 2 Exponents Identities (X A ) B = X AB X A * X B = X A+B X A / X B = X A-B X A + X B  X A+B.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
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.
Emma Stephens, Charlotte Evans, Kenneth Mcilree, Lisa Yuan
7.4 Exploring recursive sequences fibonacci
Recursion.
CSE332: Data Abstractions Lecture 7: AVL Trees
Introduction to Algorithms: Divide-n-Conquer Algorithms
Data Structures – LECTURE Balanced trees
Euclidean Geometry
Fibonacci numbers and recurrences
Induction: One Step At A Time
The Golden Ratio and Fibonacci Numbers in Nature
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
CS201: Data Structures and Discrete Mathematics I
Induction: One Step At A Time
Induction: One Step At A Time
CS 3343: Analysis of Algorithms
Recursion (Continued)
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
CSE 373 Data Structures Lecture 5
Recursion Think ESCHER.
Divide and Conquer (Merge Sort)
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Recursion (Continued)
Fibonacci numbers Golden Ratio, recurrences
At the end of this session, learner will be able to:
CSC 380: Design and Analysis of Algorithms
Algorithms Recurrences.
Discrete Mathematics and its Applications
Induction: One Step At A Time
Discrete Mathematics and its Applications
Surprising Connections in Math: From the Golden Ratio to Fractals
Presentation transcript:

FIBONACCI NUMBERS AND RECURRENCES Lecture 26 CS2110 – Spring 2016 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy

Info about optional final on course website 2 We post course grade as soon after 10 May as possible. You answer quiz on CMS: Accept letter grade or take final? Walk into final room? You must complete the final. Take only 1 prelim? Must take final. Final may lower (rarely) or raise course grade. Conflict? Megan Gatch Quiet room / extra time. Megan Gatch Review session 1: Java. TBA Data structures, algorithms, concurrency. TBA

A7 grading is finished 3 A7 grading is finished.

Fibonacci function 4 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 about 1150 The so-called Fibonacci numbers in ancient and medieval India. Parmanad Singh, 1985

Fibonacci function (year 1202) 5 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) 6 Downloaded from wikipedia Fibonacci tiling Fibonacci spiral 0, 1, 1, 2, 3, 5, 8, 13, 21, …

fibonacci and bees 7 MB 1 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 8 The artichoke uses the Fibonacci pattern to spiral the sprouts of its flowers. topones.weebly.com/1/post/2012/10/the-artichoke-and-fibonacci.html The artichoke sprouts its leafs at a constant amount of rotation: degrees (in other words the distance between one leaf and the next is 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.

Fibonacci in Pascal’s Triangle 9 p[i][j] is the number of ways i elements can be chosen from a set of size j

Blooms: strobe-animated sculptures 10

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

LOUSY WAY TO COMPUTE: O(2^n) 12 /** Return fib(n). Precondition: n ≥ 0.*/ public static int f(int n) { if ( n <= 1) return n; return f(n-1) + f(n-2); } Calculates f(15) 8 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) 13 We can prove that T(n) is O(2 n ) 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(2 n ) T(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) 14 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(2) = a + T(1) + T(0) ≤ a + a * a * 2 0 = a * (4) = a * 2 2

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) 15 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(3) = a + T(2) + T(1) ≤ a + a * a * 2 1 = a * (7) ≤ a * 2 3 T(2) = a ≤ a * 2 2

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) 16 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(4) = a + T(3) + T(2) ≤ a + a * a * 2 2 = a * (13) ≤ a * 2 4 T(2) = a ≤ a * 2 2 T(3) = a ≤ a * 2 3

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) 17 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(5) = a + T(4) + T(3) ≤ a + a * a * 2 3 = a * (25) ≤ a * 2 5 T(2) = a ≤ a * 2 2 T(3) = a ≤ a * 2 3 WE CAN GO ON FOREVER LIKE THIS T(4) = a ≤ a * 2 4

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) 18 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(k) = a + T(k-1) + T(k-2) ≤ a + a * 2 k-1 + a * 2 k-2 = a * (1 + 2 k k-2 ) ≤ a * 2 k T(2) = a ≤ a * 2 2 T(3) = a ≤ a * 2 3 T(4) = a ≤ a * 2 4

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) 19 T(n) = N T(0) = a ≤ a * 2 0 T(1) = a ≤ a * 2 1 T(2) = a ≤ a * 2 2 T(3) = a ≤ a * 2 3 T(4) = a ≤ a * 2 4 T(5) = a ≤ a * 2 5 Need a formal proof, somewhere. Uses mathematical induction “Theorem”: all odd integers > 2 are prime 3, 5, 7 are primes? yes 9? experimental error 11, 13? Yes. That’s enough checking

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

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

The Parthenon 22

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

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

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

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(2 n ) to O(1) 26