7.2 Recursive Definitions of Mathematical Formulas

Slides:



Advertisements
Similar presentations
Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods.
Advertisements

Recursion Chapter 7.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 5.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion Chapter 7.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.
Recursion Chapter 5.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
RECURSION Chapter 5. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Recursion. Midterm Exam Wednesday,November 4, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Version 1.0.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Topic 6 Recursion.
Chapter 15 Recursion.
Introduction to Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 17 Recursion.
Recursion Chapter 12.
Chapter 15 Recursion.
Recursion Chapter 10.
Recursion Chapter 5.
Programming with Recursion
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm design and Analysis
Recursion Data Structures.
24 Searching and Sorting.
Basics of Recursion Programming with Recursion
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ICS103 Programming in C Lecture 11: Recursive Functions
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 12.
Recursive Thinking.
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

7.2 Recursive Definitions of Mathematical Formulas 7.1 Recursive Thinking 7.2 Recursive Definitions of Mathematical Formulas 7.3 Recursive Search Chapter 7 – Recursion

Attendance Quiz #23 Recursion

Tip #25: Compiler Warnings Recursion Many programmers routinely ignore compiler warnings. If the problem were serious, it would be an error, right? Not so in C++! Sloppy programming may lead to erroneous program behavior, followed by a lot of difficult debugging. class B { public: virtual void func() const; }; class D: public B virtual void func(); warning: D::func() hides B:func() "Of course, that's what it's supposed to do!" int main() { B* d = new D(); d->func(); return 0; } Take compiler warnings seriously and strive to compile warning-free at the maximum warning level supported by your compiler. Don't become dependent on compiler warnings, because different compilers warn about different things – porting to a new compiler may eliminate warning messages you've come to rely on.

7.1, pgs. 404-411 7.1 Recursive Thinking Steps to Design a Recursive Algorithm Proving That a Recursive Function Is Correct Tracing a Recursive Function The Stack and Activation Frames 7.1, pgs. 404-411

Recursive Thinking General Recursive Algorithm: Recursion General Recursive Algorithm: Step 1 involves a test for what is called the base case: a value of n for which the problem can be solved easily Whenever a split occurs, we revisit Step 1 for each new problem to see whether it is a base case or a recursive case if problem can be solved directly for the current value of n 2. then Solve it. else 3. Recursively apply the algorithm to one or more problems involving smaller values of n. 4. Combine the solutions to the smaller problems to get the solution to the original problem. Step 3 is the recursive case, because there we recursively apply the algorithm Because the value of n for each recursive case is smaller than the original value of n, each recursive case makes progress towards a base case

Design a Recursive Algorithm Recursion Design: Recognize a base case and provide a solution to it. Devise a strategy to split the problem into smaller versions of itself while making progress toward a base case. Combine the solutions to the smaller problems to solve the larger problem. Find the length of a string: /** Recursive function for length of string @param str The string @return The length of the string */ int size(string str) { if (str == "") return 0; return 1 + size(str.substr(1)); }

Forward (Example: H e l l o) Reverse (Example: o l l e H) Pair up. Write a function to recursively print a string of characters (space delimited): Forward (Example: H e l l o) Reverse (Example: o l l e H) /** Recursive function print_chars post: Display in forward order @param str The string */ void print_chars(string str) { } /** Recursive function print_chars_reverse post: Display in reverse order @param str The string */ void print_chars_reverse(string str) { }

Printing String Characters Recursion /** Recursive function print_chars post: The string is displayed @param str The string */ void print_chars(string str) { if (str == "") return; // base case cout << str.at(0); // output 1st character print_chars(str.substr(1)); // output 1 less character } /** Recursive function print_chars_reverse post: Display in reverse order @param str The string */ void print_chars_reverse(string str) { if (str == "") return; // base case print_chars(str.substr(1)); // output 1 less character cout << str.at(0); // output 1st character }

Printing String Characters Recursion /** Recursive function print_chars post: The string is displayed @param str The string @param result The accumulated result */ void print_chars(string str, string& result) { if (str == "") return; // base case result += str.at(0); // output 1st character print_chars(str.substr(1), result); // output 1 less character } /** Wrapper function print_chars post: The string is displayed @param str The string */ string print_chars(string str) { string result = ""; print_chars(str, result); // get result return result; }

The Stack and Activation Frames Recursion C++ maintains a stack on which it saves new information in the form of an activation frame. The activation frame contains storage for function arguments. local variables (if any). the return address of the instruction that called the function. Whenever a new function is called (recursive or otherwise), C++ pushes a new activation frame onto the stack.

Tracing a Recursive Function Recursion The process of returning from recursive calls and computing the partial results is called unwinding the recursion. #include <iostream> using namespace std; int size(string str) { if (str == "") return 0; return 1 + size(str.substr(1)); } int main() cout << size("ace"); return 0;

Run-Time Stack - Activation Frames Recursion int main() { cout << size("ace"); return 0; } int size(string str) { if (str == "") return 0; return size(str.substr(1)); }

7.2, pgs. 412-419 7.2 Recursive Definitions of Mathematical Formulas Recursion Versus Iteration Tail Recursion or Last-Line Recursion Efficiency of Recursion 7.2, pgs. 412-419

Recursive Mathematical Formulas Recursion Mathematicians often use recursive definitions of formulas that lead naturally to recursive algorithms Examples include: factorials powers greatest common divisors sorts int factorial(int n) { if (n == 0) return 1; return (n * factorial(n – 1)); }

Recursion Downside Recursion downside Recursion Versus Iteration Base case never reached. Factorial with a negative argument - throw an invalid_argument exception if n is negative. If your program does not terminate properly, you will eventually get a run-time error when there is no more memory available for your program to execute more function calls. Recursion Versus Iteration There are similarities between recursion and iteration. In iteration, a loop repetition condition determines whether to repeat the loop body or exit from the loop. In recursion, the condition usually tests for a base case. There is always an iterative solution to a problem that is solvable by recursion. A recursive algorithm may be simpler than an iterative algorithm and thus easier to design, code, debug, and maintain.

Tail Recursion Recursion Most of the recursive algorithms and functions so far are examples of tail recursion or last-line recursion. In these algorithms, there is a single recursive call and it is the last line of the function, such as in the factorial function. Straightforward process to turn a recursion function with a tail into an iterative solution: /** Recursive factorial function */ int factorial(int n) { if (n == 0) return 1; return n * factorial(n – 1); } /** 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 Readability: Comparing our two factorial algorithms, the iterative function may be slightly less readable than the recursive function, but not much. Generally, if it is easier to conceptualize an algorithm using recursion, then use a recursive function, because the reduction in efficiency does not outweigh the advantage of readable code that is easy to debug. Execution time: In terms of efficiency, both algorithms are O(n), because the number of loop repetitions or recursive calls increases linearly with n. The iterative version is probably faster (but not significantly), because the overhead for a function call and return would be greater than the overhead for loop repetition (testing and incrementing the loop control variable). 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 f(n) = f(n-1) + f(n-2) f(0) = 1 f(1) = 1 Recursion f(n) = f(n-1) + f(n-2) f(0) = 1 f(1) = 1 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… int Fibonacci(int n) { if (n < 2) return 1; /* base case */ return (Fibonacci(n-1) + Fibonacci(n-2)); } Because of the redundant function calls, the time required to calculate fibonacci(n) increases exponentially with n. If n is 100, there are approximately 2100 activation frames (1030). If you could process one million activation frames per second, it would still take 1024 seconds (3  1016 years) to compute fibonacci(100).

7.3 Recursive Search Design of a Recursive Linear Search Algorithm Implementation of Linear Search Design of Binary Search Algorithm Efficiency of Binary Search Implementation of Binary Search Testing Binary Search 7.3, pgs. 420-426

Recursive vector Search Recursion Searching a vector can be accomplished using recursion. The simplest way to search is a linear search: Examine one element at a time starting with the first element or the last element to see whether it matches the target. On average, approximately n/2 elements are examined to find the target in a linear search. If the target is not in the vector, all n elements are examined. Base cases for recursive search: Empty vector, target can not be found; result is -1. First element of the vector matches the target; result is the subscript of first element. The recursive step searches the rest of the vector, excluding the first element. What is the order of complexity? O(n).

Recursive vector Search Recursion /** Recursive linear search function @param items The vector being searched @param target The item being searched for @param first The position of the current first element @return The subscript of target if found; otherwise -1 */ template<typename T> int linear_search(const vector<T>& items, const T& target, size_t first) { if (first == items.size()) return -1; if (target == items[first]) return first; return linear_search(items, target, first + 1); } /** Wrapper for recursive linear search function */ template<typename T> int linear_search(const std::vector<T>& items, const T& target) { return linear_search(items, target, 0); }

Design of a Binary Search Algorithm Recursion A binary search can be performed only on an array or vector that has been sorted. Base cases: The range of the vector is empty. The element being examined matches the target. Rather than looking at the first element, a binary search compares the middle element for a match with the target. If the middle element does not match the target, a binary search excludes the half of the vector (within which the target would not be found.) What is the order of complexity? O(log n).

Binary Search Algorithm Recursion

Efficiency of Binary Search Recursion At each recursive call we eliminate half the vector elements from consideration, making a binary search O(log n). A vector of size 16 would search vectors of length 16, 8, 4, 2, and 1, making 5 probes in the worst case. 16 = 24 5 = log2 16 + 1 A doubled vector size would require only 6 probes in the worst case. 32 = 25 6 = log2 32 + 1 A vector with 32,768 elements requires only 16 probes! A vector of 65,536 elements increases the number of required probes to 17.

Recursive Binary Search Recursion template <typename T> int binary_search(const vector<T>& items, int first, int last, const T& target) { if (first > last) return -1; // (base case #1) size_t middle = (first + last) / 2; // next probe if (target == items[middle]) return middle; //success (base case #2) if (target < items[middle]) return binary_search(items, first, middle - 1, target); else return binary_search(items, middle + 1, last, target); } /** Wrapper for recursive binary search function */ template<typename T> int binary_search(const vector<T>& items, const T& target) { return binary_search(items, 0, items.size() - 1, target); }

18 – Sequential Containers

Recursive Thinking Recursion Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that are difficult to solve by other means. Recursion reduces a problem into one or more simpler versions of itself. Recursive Algorithm to Process Nested Figures: 1. if there is one figure in the nest 2. Do whatever is required to the figure. else 3. Do whatever is required to the outer figure in the nest. 4. Process the nest of figures inside the outer figure in the same way.

Recursive Thinking Consider searching for a target value in an vector. Recursion Consider searching for a target value in an vector. Assume the vector elements are sorted in increasing order. We compare the target to the middle element and, if the middle element does not match the target, search either the elements before the middle element or the elements after the middle element. Instead of searching n elements, we search n/2 elements. Recursive Algorithm to Search an vector if the vector is empty Return -1 as the search result else if the middle element matches the target Return the subscript of the middle element as the result else if the target is less than the middle element Recursively search the vector elements preceding the middle element and return the result else Recursively search the vector elements following the

Run-Time Stack - Activation Frames Recursion An office tower has an employee on each level each with the same list of instructions The employee on the bottom level carries out part of the instructions, calls the employee on the next level up and is put on hold. The employee on the next level completes part of the instructions and calls the employee on the next level up and is put on hold. The employee on the next level completes part of the instructions and calls the employee on the next level up and is put on hold. And so on until the top level is reached. When the employee on the top level finishes the instructions, that employee returns an answer to the employee below. The employee below resumes, and when finished, returns an answer to the employee below. The employee below resumes, and when finished, returns an answer to the employee below. The employee below resumes, and when finished, returns an answer to the employee below, and so on . . . Eventually the bottom is reached, and all instructions are executed.

Run-Time Stack - Activation Frames Recursion

Calculating xn Recursion You can raise a number to a power that is greater than 0 by repeatedly multiplying the number by itself. Recursive definition: xn = x  xn-1 Base case: x0 = 1 /** Recursive power function @param x The number being raised to a power @param n The exponent @return x raised to the power n */ double power(double x, int n) { if (n == 0) return 1; if (n > 0) return x * power(x, n – 1); else return 1.0 / power(x, -n); }

Calculating GCD Recursion The greatest common divisor (gcd) of two numbers is the largest integer that divides both numbers The gcd of 20 and 15 is 5 The gcd of 38 and 18 is 2 The gcd of 17 and 97 is 1 /** Recursive gcd function @param m The larger number (m > 0) @param n The smaller number (n > 0) @return Greatest common divisor of m and n */ int gcd(int m, int n) { if (m % n == 0) return n; if (m < n) return gcd(n, m); // Transpose arguments else return gcd(n, m % n); }

Calculating GCD Correct? Recursion How do we verify that our algorithm is correct? Base case correct? The base case is “n is a divisor of m” The solution is n (n is the greatest common divisor), which is correct Does recursion make progress to base case? Both arguments in each recursive call are smaller than in the previous call and The new second argument is always smaller than the new first argument (m % n must be less than n) Eventually a divisor will be found or the second argument will become 1 (which is a base case because it divides every integer)

Tail Recursion Recursion Straightforward process to turn a recursion function with a tail into an iterative solution: /** Recursive factorial function */ int factorial(int n) { if (n == 0) return 1; return n * factorial(n – 1); } /** Iterative factorial function */ int factorial_iter(int n) { int result = 1; for (int k = 1; k <= n; k++) result = result * k; return result; }

Proving Recursive Correctness Recursion Proof by induction Prove the theorem is true for a base case of (usually) n = 0 or n = 1 Show that if the theorem is assumed true for k, then it must be true for k+1 Recursive proof is similar to induction Verify a base case is recognized and solved correctly Verify that each recursive case makes progress towards a base case; that is, any new problems generated are smaller versions of the original problem. Verify that if all smaller problems are solved correctly, then the original problem also is solved correctly

Fibonacci Numbers f(n) = f(n-1) + f(n-2) f(0) = 1 f(1) = 1 Recursion f(n) = f(n-1) + f(n-2) f(0) = 1 f(1) = 1 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… int Fibonacci(int n) { if (n < 2) return 1; /* base case */ return (Fibonacci(n-1) + Fibonacci(n-2)); }

Call Tree for Fibonacci Recursion main() Fibonacci(4) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) Fibonacci(1) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 1 Fibonacci(1) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) Fibonacci(2) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) 2 Fibonacci(2) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) 2 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) Fibonacci(3) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + + 2 1 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) 3 2 Fibonacci(3) Fibonacci(2) + + 2 1 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() Fibonacci(4) + 3 2 Fibonacci(3) Fibonacci(2) + + 2 1 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Call Tree for Fibonacci Recursion main() 5 Fibonacci(4) + 3 2 Fibonacci(3) Fibonacci(2) + + 2 1 1 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) int Fibonacci(int n) { if (n < 2) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } int main() cout << "Fibonacci(4) = " << Fibonacci(4);

Fibonacci Numbers O(n) Recursion Because of the redundant function calls, the time required to calculate fibonacci(n) increases exponentially with n. If n is 100, there are approximately 2100 activation frames (1030). If you could process one million activation frames per second, it would still take 1024 seconds (3  1016 years) to compute fibonacci(100). /** Recursive O(n) function to calculate Fibonacci numbers @param fib_current The current Fibonacci number @param fib_previous The previous Fibonacci number @param n The count of Fibonacci numbers left to calculate @return The value of the Fibonacci number calculated so far */ int fibo(int fib_current, int fib_previous, int n) { if (n == 1) return fib_current; return fibo(fib_current + fib_previous, fib_current, n – 1); }

Fibonacci Numbers O(n) Recursion /** Fibonacci sequence @param fib_current Current # @param fib_previous Previous # @param n # left to calculate @return Current Fibonacci value */ int fibo(int current, int previous, int n) { if (n < 2) return current; return fibo(current + previous, current, n - 1); } int fibonacci(int n) return fibo(1, 1, n); int main() cout << "Fibonacci(4) = " << fibonacci(4); fibonacci(4) 5 fibo(1, 1, 4) 5 fibo(2, 1, 3) 5 fibo(3, 2, 2) 5 fibo(5, 3, 1)

Recursive vector Search Recursion

Trace of Binary Search Recursion

Testing Binary Search You should test vectors with Recursion You should test vectors with an even number of elements an odd number of elements duplicate elements Test each vector for the following cases: the target is the element at each position of the vector, starting with the first position and ending with the last position the target is less than the smallest vector element the target is greater than the largest vector element the target is a value between each pair of items in the vector