Fibonacci Numbers and Recursion
Fibonacci Numbers and Recursion
Announcements
• Reading Quizzes:
– Out of 17
– Grades are going to be updated
– You still have until MidTerm
• Project 2 is out
• An iterative solution
FIBONACCI SEQUENCE
• F[0] = 0
• F[1] = 1
• F[2] = F[1] + F[0] = 1
• F[3] = F[2] + F[1] = 2
• F[4] = F[3] + F[2] = 3
• F[n] = F[n-1] + F[n-2]
/**
*----------------------------------------------------------
* the most straightforward algorithm to compute F[n]
*----------------------------------------------------------
*/
long fib1(int n) {
if (n <= 1) return n;
return fib1(n-1) + fib1(n-2);
}
ANALYSIS OF FIB1()
• Bottom-up iteration
– T[0] = T[1] = d
– T[2] = c + 2d
– T[3] = 2c + 3d
– T[4] = 4c + 5d
– T[5] = 7c + 8d
– T[6] = 12c + 13d
n n
( 1/ )
F [n] = p
5
p
1+ 5
= ⇡ 1.6
2
n n
C T [n] D
𝑇 𝑛 = Θ 𝜙&
long fib2(int n) {
// this is one implementation option
if (n <= 1) return n;
ArrayList<Long> A = new ArrayList<>();
A.add(new Long(0)); A.add(new Long(1));
for (int i=2; i<=n; i++) {
A.add(A.get(i-1)+ A.get(i-2));
}
return A.get(n);
}
long fib2(int n) {
if (n <= 1) return n;
long fib3(int n) {
if (n <= 1) return n;
long a=0, b=1, temp = 0;
for (int i=2; i<= n; i++) {
temp = a + b; // F[i] = F[i-2] + F[i-1]
a = b; // a = F[i-1]
b = temp; // b = F[i]
}
return temp;
}
n
F [n + 1] 1 1 1
=
F [n] 1 0 0
• Want
n
1 1
1 0
long power2(int n) {
long ret;
if (n == 0) return 1;
if (n % 2 == 0) {
ret = power2(n/2);
return ret * ret;
} else {
ret = power2((n-1)/2);
return base * ret * ret;
}
}
• Recursion is
– powerful!
• Thinking recursively
• Recursion is:
– A problem-solving approach, that can ...
– Generate simple solutions to ...
– Certain kinds of problems that ...
– Would be difficult to solve in other ways
38
Recursive Algorithm for Finding the
Length of a String
44
Proving a Recursive Method Correct
45
Proving a Recursive Method Correct (2)
Show that
• If all smaller problems are solved correctly,
• Then original problem is also solved correctly
46
The Stack and Activation Frames
RECURSIVE DEFINITIONS OF
MATHEMATICAL FORMULAS
Recursive Definitions of Mathematical
Formulas
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
Recursive Algorithm for Calculating xn
(cont.)
double power(double x, int n) {
if (n == 0)
return 1;
else if (n > 0)
return x * power(x, n – 1);
else
return 1.0 / power(x, -n);
}
Recursive Algorithm for Calculating gcd
(cont.)
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
• It is a straightforward process to turn such a function
into an iterative one
58
Iterative factorial function
int factorial_iter(int n) {
int result = 1;
for (int k = 1; k <= n; k++)
result = result * k;
return result;
}
Efficiency of Recursion
• Memory usage
– A recursive version can require significantly
more memory that an iterative version
because of the need to save local variables
and parameters on a stack
Fibonacci Numbers (cont.)
Inefficient
An O(n) Recursive fibonacci function
Efficient
Examples
• Towers of Hanoi
65
Towers of Hanoi
• http://www.mathsisfun.com/games/towerofha
noi.html
67
Problem Inputs and Outputs
Recursive Algorithm for Towers of Hanoi
BACKTRACKING
72
Backtracking
• Backtracking:
– Systematic trial and error search for solution to a
problem
– Example:Finding a path through a maze
• In walking through a maze, probably walk a path
as far as you can go
– Eventually, reach destination or dead end
– If dead end, must retrace your steps
– Loops: stop when reach place you’ve been before
• Backtracking systematically tries alternative paths
and eliminates them if they don’t work
Backtracking(2)
74
Finding a Path through a Maze
• Problem
– Use backtracking to find a display the path
through a maze
– From each point in a maze you can move to
the next cell in a horizontal or vertical
direction if the cell is not blocked
Analysis (cont.)