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

lecture 15 the fibonacci numbers
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Lecture 15: The Fibonacci Numbers

COMS10007 - Algorithms

  • Dr. Christian Konrad

20.04.2020

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 1 / 15

slide-2
SLIDE 2

The Fibonacci Numbers

Fibonacci Numbers F0 = F1 = 1 Fn = Fn−1 + Fn−2 for n ≥ 2 . 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

slide-3
SLIDE 3

source: wikipedia

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 3 / 15

slide-4
SLIDE 4

source: realworldmathematics at wordpress

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 3 / 15

slide-5
SLIDE 5

source: brian koberlein

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 3 / 15

slide-6
SLIDE 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

slide-7
SLIDE 7

Runtime Analysis

if n ≤ 1 then return n else return Fib(n−1) + Fib(n−2) Define Recurrence: T(n): number of recursive calls to Fib when called 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

slide-8
SLIDE 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) ≤ cn (we believe c ≤ 2)

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 6 / 15

slide-9
SLIDE 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) ≤ cn Substitute Guess into Recurrence: T(n) = 1 + T(n − 1) + T(n − 2) ≤ 1 + cn−1 + cn−2 It is required that 1 + cn−1 + cn−2 ≤ cn The additive 1 prevents us from getting a similar form as cn Try different guess: T(n) ≤ cn − 1

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 7 / 15

slide-10
SLIDE 10

Verification with the Substitution Method (2)

New Guess: T(n) ≤ cn − 1 T(n) = 1 + T(n − 1) + T(n − 2) ≤ 1 + (cn−1 − 1) + (cn−2 − 1) = cn−1 + cn−2 − 1 . Select smallest possible c: cn−1 + cn−2 = cn = c2 − c − 1 c = 1 + √ 5 2 ≈ 1.618033989 . Golden Ratio! Base Case: T(0) = T(1) = 1 c0 − 1 = 0 and c1 − 1 ≈ 0.61 ✗

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 8 / 15

slide-11
SLIDE 11

Verification with the Substitution Method (3)

Another New Guess: T(n) ≤ k · cn − 1 T(n) = 1 + T(n − 1) + T(n − 2) ≤ 1 + (k · cn−1 − 1) + (k · cn−2 − 1) = k

  • cn−1 + cn−2

− 1 . Select smallest possible c: c = 1+

√ 5 2

as before Base Case: T(0) = T(1) = 1 k · c0 − 1 = k − 1 and k · c1 − 1 > k − 1 We can hence select k = 2! We proved T(n) ≤ 2·( 1+

√ 5 2

)n−1. Hence T(n) ∈ O

  • ( 1+

√ 5 2

)n .

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 9 / 15

slide-12
SLIDE 12

Fibonacci Numbers: Closed-form Expression

Golden Ratio: φ = 1 + √ 5 2 ≈ 1.61803 Closed-form Expression: Fn = φn − (−φ)−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

slide-13
SLIDE 13

Experiments

0.2 0.4 0.6 0.8 1 5 10 15 20 25 30 35 40 45 50 secs

Exponential growth

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 11 / 15

slide-14
SLIDE 14

Experiments

5 10 15 20 25 30 35 5 10 15 20 25 30 35 40 45 50 secs

Exponential growth

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 11 / 15

slide-15
SLIDE 15

Experiments

  • 35
  • 30
  • 25
  • 20
  • 15
  • 10
  • 5

5 5 10 15 20 25 30 35 40 45 50 logarithmic scale to base (1+sqrt(5))/2

Logarithmic scale

  • Dr. Christian Konrad

Lecture 15: The Fibonacci Numbers 11 / 15

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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