0% found this document useful (0 votes)
24 views14 pages

1 Intro 2 Fibonaccinumbers3

The document introduces the Fibonacci sequence and algorithms for computing Fibonacci numbers. It shows that a naive recursive algorithm is very slow. A new algorithm called FibList is presented that pre-computes the Fibonacci numbers up to the desired index n in an array, running in linear time O(n) and polynomial time complexity. This improved algorithm demonstrates how the choice of algorithm can dramatically impact performance.

Uploaded by

ronaldo.zani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

1 Intro 2 Fibonaccinumbers3

The document introduces the Fibonacci sequence and algorithms for computing Fibonacci numbers. It shows that a naive recursive algorithm is very slow. A new algorithm called FibList is presented that pre-computes the Fibonacci numbers up to the desired index n in an array, running in linear time O(n) and polynomial time complexity. This improved algorithm demonstrates how the choice of algorithm can dramatically impact performance.

Uploaded by

ronaldo.zani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Intro:

Fibonacci Numbers III


Daniel Kane
Department of Computer Science and Engineering
University of California, San Diego

Data Structures and Algorithms


Algorithmic Toolbox
Learning Objectives
Compute Fibonacci numbers efficiently.
Definition

0, n = 0,


Fn = 1, n = 1,

Fn1 + Fn2, n > 1 .

Algorithm

FibRecurs(n)
if n 1:
return n
else:
return FibRecurs(n 1) + FibRecurs(n 2)

Too slow!
Another Algorithm

Imitate hand computation:


0, 1
Another Algorithm

Imitate hand computation:


0, 1, 1
0+1=1
Another Algorithm

Imitate hand computation:


0, 1, 1, 2
0+1=1
1+1=2
Another Algorithm

Imitate hand computation:


0, 1, 1, 2, 3
0+1=1
1+1=2
1+2=3
Another Algorithm

Imitate hand computation:


0, 1, 1, 2, 3, 5
0+1=1
1+1=2
1+2=3
2+3=5
Another Algorithm

Imitate hand computation:


0, 1, 1, 2, 3, 5, 8
0+1=1
1+1=2
1+2=3
2+3=5
3+5=8
New Algorithm
FibList(n)
create an array F [0 . . . n]
F [0] 0
F [1] 1
for i from 2 to n:
F [i] F [i 1] + F [i 2]
return F [n]
New Algorithm
FibList(n)
create an array F [0 . . . n]
F [0] 0
F [1] 1
for i from 2 to n:
F [i] F [i 1] + F [i 2]
return F [n]

T (n) = 2n + 2. So T (100) = 202.


Easy to compute.
Summary
Introduced Fibonacci numbers.
Naive algorithm takes ridiculously long
time on small examples.
Improved algorithm incredibly fast.
Summary
Introduced Fibonacci numbers.
Naive algorithm takes ridiculously long
time on small examples.
Improved algorithm incredibly fast.

Moral: The right algorithm makes


all the difference.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy