lecture 15 the fibonacci numbers
play

Lecture 15: The Fibonacci Numbers COMS10007 - Algorithms Dr. - PowerPoint PPT Presentation

Lecture 15: The Fibonacci Numbers COMS10007 - Algorithms Dr. Christian Konrad 20.04.2020 Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 1 / 15 The Fibonacci Numbers Fibonacci Numbers F 0 = 0 = 1 F 1 F n = F n 1 + F n 2


  1. Lecture 15: The Fibonacci Numbers COMS10007 - Algorithms Dr. Christian Konrad 20.04.2020 Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 1 / 15

  2. The Fibonacci Numbers Fibonacci Numbers F 0 = 0 = 1 F 1 F n = F n − 1 + F n − 2 for n ≥ 2 . 0 1 1 2 3 5 8 13 21 34 55 89 . . . Why are they important? Fibonacci heaps (data structure) Appear in analysis of algorithms (e.g. Euclid’s algorithm) Appear everywhere in nature Interesting and instructive computational problem Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 2 / 15

  3. source: wikipedia Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 3 / 15

  4. source: realworldmathematics at wordpress Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 3 / 15

  5. source: brian koberlein Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 3 / 15

  6. Computing the Fibonacci Numbers Na¨ ıve Algorithm Require: Integer n ≥ 0 if n ≤ 1 then return n else return Fib ( n − 1) + Fib ( n − 2) Fib ( n ) What is the runtime of this algorithm? Runtime: Without recursive calls, runtime is O (1) Hence, runtime is O (“number of recursive calls”) Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 4 / 15

  7. Runtime Analysis if n ≤ 1 then Define Recurrence: return n T ( n ): number of recursive else calls to Fib when called return Fib ( n − 1) + Fib ( n − 2) with parameter n T (0) = T (1) = 1 T ( n ) = 1 + T ( n − 1) + T ( n − 2) , for n ≥ 2 . How to Solve this Recurrence? We will use the recursion tree technique to obtain a guess for an upper bound We will verify the guess with the substitution method Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 5 / 15

  8. Recursion Tree for T Observe: Each node contributes 1 Hence, T ( n ) equals number of nodes Number of levels of recursion tree: n Our guess: T ( n ) ≤ c n (we believe c ≤ 2) Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 6 / 15

  9. Verification with the Substitution Method Recall: T (0) = T (1) = 1 T ( n ) = 1 + T ( n − 1) + T ( n − 2) , for n ≥ 2 . Our guess: T ( n ) c n ≤ Substitute Guess into Recurrence: T ( n ) = 1 + T ( n − 1) + T ( n − 2) ≤ 1 + c n − 1 + c n − 2 It is required that 1 + c n − 1 + c n − 2 ≤ c n The additive 1 prevents us from getting a similar form as c n Try different guess: T ( n ) ≤ c n − 1 Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 7 / 15

  10. Verification with the Substitution Method (2) New Guess: T ( n ) ≤ c n − 1 T ( n ) = 1 + T ( n − 1) + T ( n − 2) 1 + ( c n − 1 − 1) + ( c n − 2 − 1) = c n − 1 + c n − 2 − 1 . ≤ Select smallest possible c : c n − 1 + c n − 2 = c n c 2 − c − 1 0 = √ 1 + 5 c = ≈ 1 . 618033989 . Golden Ratio! 2 Base Case: T (0) = T (1) = 1 c 0 − 1 = 0 and c 1 − 1 ≈ 0 . 61 ✗ Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 8 / 15

  11. Verification with the Substitution Method (3) Another New Guess: T ( n ) ≤ k · c n − 1 T ( n ) = 1 + T ( n − 1) + T ( n − 2) 1 + ( k · c n − 1 − 1) + ( k · c n − 2 − 1) ≤ c n − 1 + c n − 2 � � = − 1 . k √ Select smallest possible c : c = 1+ 5 as before 2 Base Case: T (0) = T (1) = 1 k · c 0 − 1 = k − 1 and k · c 1 − 1 > k − 1 � We can hence select k = 2! √ √ � ) n � We proved T ( n ) ≤ 2 · ( 1+ 5 ( 1+ 5 ) n − 1. Hence T ( n ) ∈ O . 2 2 Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 9 / 15

  12. Fibonacci Numbers: Closed-form Expression Golden Ratio: √ φ = 1 + 5 ≈ 1 . 61803 2 Closed-form Expression: φ n − ( − φ ) − n F n = . √ 5 Why not compute Fibonacci Numbers this way? Floating point operations, precision Large numbers involved Impractical Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 10 / 15

  13. Experiments 1 secs 0.8 0.6 0.4 0.2 0 0 5 10 15 20 25 30 35 40 45 50 Exponential growth Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 11 / 15

  14. Experiments 35 secs 30 25 20 15 10 5 0 0 5 10 15 20 25 30 35 40 45 50 Exponential growth Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 11 / 15

  15. Experiments 5 logarithmic scale to base (1+sqrt(5))/2 0 -5 -10 -15 -20 -25 -30 -35 0 5 10 15 20 25 30 35 40 45 50 Logarithmic scale Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 11 / 15

  16. Why is this Algorithm so slow? Discussion: We compute solutions to subproblems many times ( T ( i ) is computed often, for most values of i ) How can we avoid this? Dynamic Programming! Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 12 / 15

  17. Dynamic Programming Solution Dynamic Programming (will be discussed in more detail later) Store solutions to subproblems in a table Compute table bottom up Require: Integer n ≥ 0 if n ≤ 1 then return n else A ← array of size n A [0] ← 1, A [1] ← 1 for i ← 2 . . . n do A [ i ] ← A [ i − 2] + A [ i − 1] return A [ n ] DynPrgFib ( n ) Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 13 / 15

  18. Dicussion Analysis: DynPrgFib() runs in time O ( n ) It uses space Θ( n ) since it uses an array of size n Can we reduce the space to O (1)? Improvement: Observe that when T ( i ) is computed, the values T (1) , T (2) , . . . , T ( i − 3) are no longer needed Only store the last two values of T Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 14 / 15

  19. Improved Algorithm Require: Integer n ≥ 0 if n ≤ 1 then return n else a ← 0 b ← 1 for i ← 2 . . . n do c ← a + b a ← b b ← c return c ImprovedDynPrgFib ( n ) Correctness: via loop invariant! Dr. Christian Konrad Lecture 15: The Fibonacci Numbers 15 / 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend