introduction to data structures and algorithms - informatik 7

21
Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Introduction to Data Structures and Algorithms Chapter: Calculating Fibonacci Numbers

Upload: others

Post on 10-Feb-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Data Structures and Algorithms - Informatik 7

Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen

Introduction toData Structures and Algorithms

Chapter: Calculating Fibonacci Numbers

Page 2: Introduction to Data Structures and Algorithms - Informatik 7

Iterative sum of squaresItSum_Squares(m,n)Sum:=0for i = m to n doSum:= Sum + i * i

Recursion sum of squaresRecSum_Squares(m,n)if m < nthen

Sum:= m * m + RecSum_Squares(m+1,n)Sum:= m * m

Data Structures and Algorithms (26)

Iteration vs. Recursion

Example )1( 2222 where m ≤ n, n, m ∈ N

nmmin

mi

Page 3: Introduction to Data Structures and Algorithms - Informatik 7

Recursion combining two half‐solutions(method Divide-and-conquer)

RecSumSq(m,n)if m = nthen

RecSumSq:= m * melse

mid:= floor((m + n)/2)RecSumSq:= RecSumSq(m,mid)+ RecSumSq(mid+1,n)

Data Structures and Algorithms (26)

Iteration vs. Recursion

Example )1( 2222 where m ≤ n, n, m ∈ N

nmmin

mi

Page 4: Introduction to Data Structures and Algorithms - Informatik 7

Iteration vs. Recursion

355

RecSumSq(5,10)

110 245

RecSumSq(5,7)

RecSumSq(8,9)

61 145 100

25 36 64

81

RecSumSq(5,6) RecSumSq(7,7)

RecSumSq(8,10)

RecSumSq(10,10)

RecSumSq(5,5) RecSumSq(6,6) RecSumSq(8,8) RecSumSq(9,9)

25 36

Call Tree for Recursion RecSumSc (5,10)

Data Structures and Algorithms (28)

49

49 64 100

81

Page 5: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (29)

Fibonacci numbers fi are defined by the following recurrence

This leads to the sequence

Calculating Fibonacci Numbers

Page 6: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (30)

Problem:How can we compute the i-th Fibonacci number fi ?

We will present 3 sample solutions Recursive algorithm (fibrec) Iterative algorithm (fibiter) Iterative squaring algorithm (fibisq)

We will have to investigate how good these algorithms perform and which of these solutions is the “best solution”!

Calculating Fibonacci Numbers

Page 7: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (31)

1) A recursive algorithm fibrec

What does “recursive algorithm” mean?

Again the recursive definition:

For i=0, 1, ... compute

Calculating Fibonacci Numbers

Page 8: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (32)

Analysis of the recursive algorithm

We could find outthe exact runtime for each operation and use these values

Instead we typically procede as follows:We count the number of arithmetic operations performedwhen executing fibrec and consider this value as runtime(or cost or complexity) of fibrec: Arithmetic operations: additions, subtractions, multiplications, divisions

For simplicity here we assumethat all operations need the same amount of time for execution

Calculating Fibonacci Numbers

Page 9: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (33)

Analysis of the recursive algorithm

Let Crec(i) be the cost of computing fi using fibrec(i) i.e. Crec(i) =

number of arithmetic operations when computing fi using fibrec Then

The values of Crec(i) for small values (i=0, 1, 2, ...) are

Calculating Fibonacci Numbers

Page 10: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (34

Analysis of the recursive algorithm

A closed-form expression for the cost Crec(i) would be fine!

“Someone guesses” that (for i=0, 1, ...)

Try to proof this assumption!(Which method of proof would be your favorite choice?)

Calculating Fibonacci Numbers

Page 11: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (35)

Calculating Fibonacci Numbers

Analysis of the recursive algorithm

Use this resultto find information about fi Find lower and upper bounds Hint: Show that for i > 0

fi is positive fi is monotonically increasing

Result: For i > 2:

fi growing exponentially Crec(i) growing exponentially

2561610

26214451220

42.04

21.43

112

Upper bound(fi)

Lowerbound(fi)

i

3013 10)100(10 :Expl recC

Page 12: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (36)

2) An iterative algorithm fibiter

For i=0, 1, ... compute

Claim: fibiter(i) = fi for i=0, 1, ...

Calculating Fibonacci Numbers

fibiter(i)

if i=0 then return 0if i=1 then return 1

ppred := 0 ▻ pre-predecessorpred := 1 ▻ predecessor

for j := 2 to i docurr := pred+ppred ▻ currentppred := predpred := curr

return curr

Page 13: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (37)

Analysis of the iterative algorithm

Let Citer(i) be the cost of computing fi using fibiter(i)(remember: cost = “number of arithmetic operations”)

Then

This means: Linear complexity of fibiter ! Expl:

So fibiter is much better than fibrec

But: Is it possible to find an even better algorithm than fibiter ?

Calculating Fibonacci Numbers

199)200(200or 99)100(100 iteriter CiCi

Page 14: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (38)

3) An iterative squaring algorithm fibisq

Remark: We will see later what “iterative squaring” means

Let us start with some preparations:

Prove that the following holds for i=2, 3, ...

where

Calculating Fibonacci Numbers

Page 15: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (39)

Function pow implements the iterative squaring:

pow(a, n) = an for arbitrary a and n=1, 2, ...

Calculating Fibonacci Numbers

Page 16: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms 38)

Analysis of the runtime of pow

For given n there are recursice calls to function pow

Each call involves at most 1 integer subtraction (only if odd) 1 integer division 2 multiplications (of values of type(a)) (only 1 multiplication, if n even)

Calculating Fibonacci Numbers

1)1)(log(4 2 nCostpow

Page 17: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (41)

Calculating Fibonacci Numbers

The iterative squaring algorithm fibisq

For i=0, 1, ... compute

Claim: fibisq (i) = fi for i=0, 1, ...

2221

1211

22

P

withPmatrix of

pp

pp

pfi

Page 18: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (42)

Analysis of fibisq:

The only arithmetic operations involved are in the call pow(A, i-1) There are such calls, each involving at most

1 integer subtraction 1 integer division 2 multiplications of (2x2) matrices

(each involving 8 integer multiplications and 4 integer additions)summing up to at most 26 arithmetic operations per call

So the cost Cisq(i) of fibisq is:

Logarithmic complexity of fibisq! (“Divide and Conquer”)

Calculating Fibonacci Numbers

1)1)1(log(26)( 2 iiCisq

Page 19: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (43)

Interpretation

Cost comparison of for calculating the i-th Fibonacci number

i = 50:

i = 100:

i = 200:

i = 300:

i = 400:

Calculating Fibonacci Numbers

)( vs.)( vs.)( iCiCiC isqiterrec

157 )50( vs.49)50( vs.10)50(10 167 isqiterrec CCC

183 )100( vs.99)100( vs.10)100(10 3014 isqiterrec CCC

209 )200( vs.199)200( vs.10)200(10 6129 isqiterrec CCC

235 )300( vs.299)300( vs.10)300(10 9144 isqiterrec CCC

235 )400( vs.399)400( vs.10)400(10 12159 isqiterrec CCC

Page 20: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (44)

Interpretation

The runtimes of the three algorithms computing the same function(Fibonacci number) differ significantly!

If we assume that arithmetic operation takes 1 microsecond,the following table gives the maximal value ifor which fi can be computed in a given time (1 ms, …, 1 h)using the respective method (based on measurements):

Calculating Fibonacci Numbers

Page 21: Introduction to Data Structures and Algorithms - Informatik 7

Data Structures and Algorithms (45)

Is our analysis realistic?

We only counted arithmetic operations

We assumed that all arithmetic operations take the same time

The time to perform one arithmetic operation may depend on thevalue of the arguments involved

(it takes longer to add two 1000-digit numbers than to add two 10-digit numbers)

But the trend for large values of i is very clear!

Calculating Fibonacci Numbers