SlideShare a Scribd company logo
A recursive implementation and its
problems for calculating the
Fibonacci sequence of length N.
Data Structures and Algorithms
Presented By: Professor Lili Saghafi
proflilisaghafi@gmail.com
@Lili_PLSSimon Fraser University
SFU
May 2019
Agenda
• Understand the definition of the Fibonacci
numbers.
• Understand the definition of the Recursive
/ Recursive Functions
• Show that the naive algorithm for
computing them is slow.
• Efficiently create algorithms to compute
large Fibonacci numbers.
Definition: It is the integer sequence in which every
number after the first two is the sum of the two
preceding numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
The Fibonacci Sequence
• It is the integer sequence in which every
number after the first two is the sum of
the two preceding numbers.
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
…
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
puzzle that Fibonacci posed
• Leonardo of Pisa, known as Fibonacci.
• Fibonacci considers the growth of an idealized
(biologically unrealistic) rabbit population, assuming that:
– a single newly born pair of rabbits (one male, one female) are
put in a field;
– rabbits are able to mate at the age of one month so that at the
end of its second month a female can produce another pair of
rabbits;
– rabbits never die and a mating pair always produces one new
pair (one male, one female) every month from the second month
on.
• The puzzle that Fibonacci posed was: how many pairs
will there be in one year?
we have the following recursive definition for the nth Fibonacci number,
F0=0 F1=1
Fn:=Fn−1+Fn−2
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Examples
Computing Fibonacci Numbers
Recursion
• Have you ever seen a set of Russian
dolls? At first, you see just one figurine,
usually painted wood, that looks
something like this:
• You can remove the top half of the first
doll, and what do you see inside? Another,
slightly smaller, Russian doll!
• You can remove that doll and separate its
top and bottom halves. And you see yet
another, even smaller, doll:
• And once more:
• And you can keep going. Eventually you
find the teeniest Russian doll. It is just one
piece, and so it does not open:
We started with one big Russian doll, and we saw smaller and smaller Russian
dolls, until we saw one that was so small that it could not contain another.
Recursion
What do Russian dolls have to do
with algorithms?
• Just as one Russian doll has within it a smaller
Russian doll, which has an even smaller
Russian doll within it, all the way down to a tiny
Russian doll that is too small to contain
another, we'll see how to design an algorithm to
solve a problem by solving a smaller instance of
the same problem, unless the problem is so
small that we can just solve it directly.
• We call this technique recursion.
• Recursion has many, many applications
What is Recursive Function
• A recursive function is a function that
calls itself during its execution.
• This enables the function to repeat itself
several times, outputting the result at the
end of each iteration.
Example of a recursive function
function Count (integer N)
if (N <= 0) return "Must be a Positive
Integer";
if (N > 9) return "Counting Completed";
else return Count (N+1);
end function
• The function Count() uses recursion to
count from any number between 1 and 9,
to the number 10.
• For example, Count(1) would return
2,3,4,5,6,7,8,9,10.
• Count(7) would return 8,9,10.
• The result could be used as a roundabout
way to subtract the number from 10.
Downside
• Recursive functions are common in computer
science because they allow programmers to
write efficient programs using a minimal
amount of code.
• The downside is that they can cause infinite
loops and other unexpected results if not written
properly.
• For example, in previous example, the function
is terminated if the number is 0 or less or greater
than 9.
• If proper cases are not included in the function to
stop the execution, the recursion will repeat
forever, causing the program to crash, or worse
yet, hang the entire computer system.
Question?
• How you can compute any term of the
series of Fibonacci numbers in any
programming languages using recursive
functions?
Naive Algorithm
• Recursive functions are those functions
which, basically, call themselves.
• The most basic method to do This is
because the computing power required to
calculate larger terms of the series is
immense.
• The number of times the function is called
causes a stack overflow in most
languages.
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Basic Algorithm
• A recursive function F (F for Fibonacci): to
compute the value of the next term.
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
…
• Our function will take n as an input,
• which will refer to the nth term of the
sequence that we want to be computed.
• F(4) should return the fourth term of the
sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, 144, …
• The code should, regardless the language, look
something like this:
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
• The term 0 of the sequence will be considered
to be 0, so the first term will be 1; the second
term, 1; the third term, 2; and so on
• Term is the position of the number
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
• If it gets 0 as an input, it returns 0.
• If it gets 1, it returns 1.
• If it gets 2… in that case it falls into the
else statement, which will call the function
again for terms 2–1 (1) and 2–2 (0).
• That will return 1 and 0, and the two
results will be added, returning 1.
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
Problem with recursive functions
• If you wanted the 100th term of the
sequence.
• The function would call itself for the 99th
and the 98th, which would themselves call
the function again for the 98th and 97th,
and 97th and 96th terms…and so on.
• It would be really slow.
Downside
• Recursion allows for the design of powerful,
elegant algorithms, but it uses up time and
space for the call stack.
– Efficiency can sometimes be improved by eliminating
recursion.
– A tail-recursive algorithm can easily be converted into
a loop.
– If the algorithm is only returning a value (as opposed
to modifying an existing data structure), redundant
computation can be avoided by storing the results
of previous invocation in a table.
Recursive Algorithm for Fibonacci
numbers
It is the integer sequence in which every number after the first
two is the sum of the two preceding numbers
What are the number of lines of
code executed
• Let T(n) denote the
number of lines of
code executed by
FibRecurs(n).
number of lines of code executed
•Let T(n) denote the number of lines of code executed by FibRecurs(n).
Running Time
T(n) denote the number of lines of code executed by
FibRecurs(n).
If n>=2
Question !
• How many digits do you think that T(100)
has?
A- Less than 10
B- 10 to 20
C- 20 to 30
T is for the number of line of code to
be executed.
Answer
• How many digits do you think that T(100)
has?
C- 20 to 30
T is for the number of line of code to be
executed.
Running Time
T is for the number of line of code
to be executed.
T of n is at least to the length of
Fibonacci numbers
Why slow?
Look at all recursive calls this
algorithm needs
We need recursive
call to n-1 and for
doing that we need n-
2 and n-3
We need recursive call
to n-2 and for doing that
we need n-3 and n-4
And it just keep going………and create
a big tree of recursive calls
Because it seems that we compute
things over and over
Every time we ask to do it since it is a new recursive call , we
compute the whole thing over and over
Lets blow up the tree , F n-4
computes 5 times!
It is not right!
• just because it looks right, it doesn’t mean
it is.
• not every problem that looks like a
recursion-solvable should be solved with
recursion
Recursive Algorithm
Very Slow
Efficient Algorithm
Just look at the last two
element and add them
New Algorithm
When I filled out the entire
list , I am going to return the
last element
Need to create an array to store
all the numbers
First element of array set to 0
Second element of array set to 1
I runs from 2 to n
We need to set i
element to SUM of two
i-1 and i-2
How fast is this New Algorithm?
How many lines of code?
3 lines of code at the
beginning and one
return statement
means 4 line code
We have 4 statement that went through n-1
time and each time we have to execute 2
lines of code so adding everything we
reach that T(n)=2n+2 and for n=100 it
takes 202 line of code.
Summary
Conclusion
• The right algorithm makes all the
difference.
Python
def F(n): if n == 0:
return 0 if n == 1:
return 1 else:
return F(n-1) + F(n-2)
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
Java
public static int F(int n) { if(n == 0) { return
0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
C++
int F(int n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
JavaScript
function F(n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
A recursive implementation and its
problems for calculating the
Fibonacci sequence of length N.
Data Structures and Algorithms
Presented By: Professor Lili Saghafi
proflilisaghafi@gmail.com
@Lili_PLS

More Related Content

PPTX
Hamiltonian path
PDF
Chap7 2 Ecc Intro
PPTX
Depth first search [dfs]
PPT
Greedy with Task Scheduling Algorithm.ppt
PPTX
Ford Fulkerson Algorithm
PPTX
Hamilton path and euler path
PDF
Lecture: Context-Free Grammars
PPT
Kleene's theorem
Hamiltonian path
Chap7 2 Ecc Intro
Depth first search [dfs]
Greedy with Task Scheduling Algorithm.ppt
Ford Fulkerson Algorithm
Hamilton path and euler path
Lecture: Context-Free Grammars
Kleene's theorem

What's hot (20)

PPT
Greedy algorithms
PDF
Bellman ford
PDF
Logistic Regression in Python | Logistic Regression Example | Machine Learnin...
PPT
Discrete mathematics
PPT
The Floyd–Warshall algorithm
PDF
All pairs shortest path algorithm
PPTX
Asymptotic Notations
PPT
Dynamic programming
PPTX
Propositional logic
PDF
07 Analysis of Algorithms: Order Statistics
PDF
Algorithms Lecture 7: Graph Algorithms
PPTX
Traveling salesman problem
PPTX
Counting, pigeonhole, permuntation, Permutations and Combination ,Binomial T...
PPT
Amortized Analysis of Algorithms
PDF
2.03 bayesian estimation
PDF
Lecture Notes-Finite State Automata for NLP.pdf
PDF
Parse Tree
PPTX
Travelling salesman problem
PPT
Breadth first search
Greedy algorithms
Bellman ford
Logistic Regression in Python | Logistic Regression Example | Machine Learnin...
Discrete mathematics
The Floyd–Warshall algorithm
All pairs shortest path algorithm
Asymptotic Notations
Dynamic programming
Propositional logic
07 Analysis of Algorithms: Order Statistics
Algorithms Lecture 7: Graph Algorithms
Traveling salesman problem
Counting, pigeonhole, permuntation, Permutations and Combination ,Binomial T...
Amortized Analysis of Algorithms
2.03 bayesian estimation
Lecture Notes-Finite State Automata for NLP.pdf
Parse Tree
Travelling salesman problem
Breadth first search
Ad

Similar to Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi (20)

PDF
Python Programming unit5 (1).pdf
PDF
chapter1.pdf ......................................
PDF
Recursion.pdf
PPT
Recursion C programming exercises_ Recursion - w3resource.ppt
PPTX
Recurrence relationships
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PPT
Lecture 1 and 2 of Data Structures & Algorithms
PPTX
Unit 1.pptx
PDF
01 Notes Introduction Analysis of Algorithms Notes
PPTX
PDF
Design & Analysis Of Algorithm
PDF
Sure interview algorithm-1103
PDF
BCS401 ADA First IA Test Question Bank.pdf
DOCX
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
PDF
Analysis of Algorithms
PDF
Design & Analysis of Algorithms Lecture Notes
PDF
Dynamic programing
Python Programming unit5 (1).pdf
chapter1.pdf ......................................
Recursion.pdf
Recursion C programming exercises_ Recursion - w3resource.ppt
Recurrence relationships
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Lecture 1 and 2 of Data Structures & Algorithms
Unit 1.pptx
01 Notes Introduction Analysis of Algorithms Notes
Design & Analysis Of Algorithm
Sure interview algorithm-1103
BCS401 ADA First IA Test Question Bank.pdf
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
Analysis of Algorithms
Design & Analysis of Algorithms Lecture Notes
Dynamic programing
Ad

More from Professor Lili Saghafi (20)

PDF
Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
PPTX
Software Engineering
PDF
Introduction to Quantum Computing Lecture 1: Fundamentals
PDF
Software Engineering_Agile Software Development By: Professor Lili Saghafi
PDF
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
PDF
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
PDF
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
PDF
Introduction to Blockchain Technology By Professor Lili Saghafi
PDF
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
PDF
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
PPT
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
PDF
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
PDF
Machine learning by using python By: Professor Lili Saghafi
PDF
What is digital humanities ,By: Professor Lili Saghafi
PDF
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
PDF
Data Science unit 2 By: Professor Lili Saghafi
PDF
Data science unit 1 By: Professor Lili Saghafi
PDF
Data Scientist By: Professor Lili Saghafi
PDF
New Assessments in Higher Education with Computers by: Prof Lili Saghafi
Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
Software Engineering
Introduction to Quantum Computing Lecture 1: Fundamentals
Software Engineering_Agile Software Development By: Professor Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi
Data Scientist By: Professor Lili Saghafi
New Assessments in Higher Education with Computers by: Prof Lili Saghafi

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PDF
Pre independence Education in Inndia.pdf
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
TR - Agricultural Crops Production NC III.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
PPH.pptx obstetrics and gynecology in nursing
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
GDM (1) (1).pptx small presentation for students
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
Pre independence Education in Inndia.pdf

Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi

  • 1. A recursive implementation and its problems for calculating the Fibonacci sequence of length N. Data Structures and Algorithms Presented By: Professor Lili Saghafi proflilisaghafi@gmail.com @Lili_PLSSimon Fraser University SFU May 2019
  • 2. Agenda • Understand the definition of the Fibonacci numbers. • Understand the definition of the Recursive / Recursive Functions • Show that the naive algorithm for computing them is slow. • Efficiently create algorithms to compute large Fibonacci numbers.
  • 3. Definition: It is the integer sequence in which every number after the first two is the sum of the two preceding numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
  • 4. The Fibonacci Sequence • It is the integer sequence in which every number after the first two is the sum of the two preceding numbers. • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
  • 6. puzzle that Fibonacci posed • Leonardo of Pisa, known as Fibonacci. • Fibonacci considers the growth of an idealized (biologically unrealistic) rabbit population, assuming that: – a single newly born pair of rabbits (one male, one female) are put in a field; – rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits; – rabbits never die and a mating pair always produces one new pair (one male, one female) every month from the second month on. • The puzzle that Fibonacci posed was: how many pairs will there be in one year?
  • 7. we have the following recursive definition for the nth Fibonacci number, F0=0 F1=1 Fn:=Fn−1+Fn−2
  • 11. Recursion • Have you ever seen a set of Russian dolls? At first, you see just one figurine, usually painted wood, that looks something like this:
  • 12. • You can remove the top half of the first doll, and what do you see inside? Another, slightly smaller, Russian doll!
  • 13. • You can remove that doll and separate its top and bottom halves. And you see yet another, even smaller, doll:
  • 14. • And once more:
  • 15. • And you can keep going. Eventually you find the teeniest Russian doll. It is just one piece, and so it does not open: We started with one big Russian doll, and we saw smaller and smaller Russian dolls, until we saw one that was so small that it could not contain another.
  • 16. Recursion What do Russian dolls have to do with algorithms? • Just as one Russian doll has within it a smaller Russian doll, which has an even smaller Russian doll within it, all the way down to a tiny Russian doll that is too small to contain another, we'll see how to design an algorithm to solve a problem by solving a smaller instance of the same problem, unless the problem is so small that we can just solve it directly. • We call this technique recursion. • Recursion has many, many applications
  • 17. What is Recursive Function • A recursive function is a function that calls itself during its execution. • This enables the function to repeat itself several times, outputting the result at the end of each iteration.
  • 18. Example of a recursive function function Count (integer N) if (N <= 0) return "Must be a Positive Integer"; if (N > 9) return "Counting Completed"; else return Count (N+1); end function
  • 19. • The function Count() uses recursion to count from any number between 1 and 9, to the number 10. • For example, Count(1) would return 2,3,4,5,6,7,8,9,10. • Count(7) would return 8,9,10. • The result could be used as a roundabout way to subtract the number from 10.
  • 20. Downside • Recursive functions are common in computer science because they allow programmers to write efficient programs using a minimal amount of code. • The downside is that they can cause infinite loops and other unexpected results if not written properly. • For example, in previous example, the function is terminated if the number is 0 or less or greater than 9. • If proper cases are not included in the function to stop the execution, the recursion will repeat forever, causing the program to crash, or worse yet, hang the entire computer system.
  • 21. Question? • How you can compute any term of the series of Fibonacci numbers in any programming languages using recursive functions?
  • 22. Naive Algorithm • Recursive functions are those functions which, basically, call themselves. • The most basic method to do This is because the computing power required to calculate larger terms of the series is immense. • The number of times the function is called causes a stack overflow in most languages.
  • 24. Basic Algorithm • A recursive function F (F for Fibonacci): to compute the value of the next term. • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … • Our function will take n as an input, • which will refer to the nth term of the sequence that we want to be computed. • F(4) should return the fourth term of the sequence.
  • 25. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … • The code should, regardless the language, look something like this: function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2) • The term 0 of the sequence will be considered to be 0, so the first term will be 1; the second term, 1; the third term, 2; and so on • Term is the position of the number
  • 26. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … • If it gets 0 as an input, it returns 0. • If it gets 1, it returns 1. • If it gets 2… in that case it falls into the else statement, which will call the function again for terms 2–1 (1) and 2–2 (0). • That will return 1 and 0, and the two results will be added, returning 1. function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2)
  • 27. Problem with recursive functions • If you wanted the 100th term of the sequence. • The function would call itself for the 99th and the 98th, which would themselves call the function again for the 98th and 97th, and 97th and 96th terms…and so on. • It would be really slow.
  • 28. Downside • Recursion allows for the design of powerful, elegant algorithms, but it uses up time and space for the call stack. – Efficiency can sometimes be improved by eliminating recursion. – A tail-recursive algorithm can easily be converted into a loop. – If the algorithm is only returning a value (as opposed to modifying an existing data structure), redundant computation can be avoided by storing the results of previous invocation in a table.
  • 29. Recursive Algorithm for Fibonacci numbers It is the integer sequence in which every number after the first two is the sum of the two preceding numbers
  • 30. What are the number of lines of code executed • Let T(n) denote the number of lines of code executed by FibRecurs(n).
  • 31. number of lines of code executed •Let T(n) denote the number of lines of code executed by FibRecurs(n).
  • 32. Running Time T(n) denote the number of lines of code executed by FibRecurs(n). If n>=2
  • 33. Question ! • How many digits do you think that T(100) has? A- Less than 10 B- 10 to 20 C- 20 to 30 T is for the number of line of code to be executed.
  • 34. Answer • How many digits do you think that T(100) has? C- 20 to 30 T is for the number of line of code to be executed.
  • 35. Running Time T is for the number of line of code to be executed. T of n is at least to the length of Fibonacci numbers
  • 36. Why slow? Look at all recursive calls this algorithm needs We need recursive call to n-1 and for doing that we need n- 2 and n-3 We need recursive call to n-2 and for doing that we need n-3 and n-4 And it just keep going………and create a big tree of recursive calls
  • 37. Because it seems that we compute things over and over Every time we ask to do it since it is a new recursive call , we compute the whole thing over and over
  • 38. Lets blow up the tree , F n-4 computes 5 times!
  • 39. It is not right! • just because it looks right, it doesn’t mean it is. • not every problem that looks like a recursion-solvable should be solved with recursion
  • 41. Efficient Algorithm Just look at the last two element and add them
  • 42. New Algorithm When I filled out the entire list , I am going to return the last element Need to create an array to store all the numbers First element of array set to 0 Second element of array set to 1 I runs from 2 to n We need to set i element to SUM of two i-1 and i-2
  • 43. How fast is this New Algorithm? How many lines of code? 3 lines of code at the beginning and one return statement means 4 line code We have 4 statement that went through n-1 time and each time we have to execute 2 lines of code so adding everything we reach that T(n)=2n+2 and for n=100 it takes 202 line of code.
  • 45. Conclusion • The right algorithm makes all the difference.
  • 46. Python def F(n): if n == 0: return 0 if n == 1: return 1 else: return F(n-1) + F(n-2) function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2)
  • 47. Java public static int F(int n) { if(n == 0) { return 0; } if(n == 1) { return 1; } else { return F(n-1) + F(n-2); }} function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2)
  • 48. C++ int F(int n) { if(n == 0) { return 0; } if(n == 1) { return 1; } else { return F(n-1) + F(n-2); }} function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2)
  • 49. JavaScript function F(n) { if(n == 0) { return 0; } if(n == 1) { return 1; } else { return F(n-1) + F(n-2); }} function F(n) if n = 0 return 0 if n = 1 return 1 else return F(n-1) + F(n-2)
  • 50. A recursive implementation and its problems for calculating the Fibonacci sequence of length N. Data Structures and Algorithms Presented By: Professor Lili Saghafi proflilisaghafi@gmail.com @Lili_PLS