Presentation is loading. Please wait.

Presentation is loading. Please wait.

16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.

Similar presentations


Presentation on theme: "16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm."— Presentation transcript:

1 16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm for designing software. Advantages: Some programming problems have very simple and elegant recursive solutions. Some programming problems have very simple and elegant recursive solutions.Disadvantages: Additional memory and execution resources are needed. Additional memory and execution resources are needed. Some languages do not support recursion. Some languages do not support recursion. Recursion can be a tricky concept to master. Recursion can be a tricky concept to master.

2 26/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Designing a Recursive Algorithm Three Major Steps: Divide the problem into two or more smaller problems that have the exact same form as the original problem. Divide the problem into two or more smaller problems that have the exact same form as the original problem. The algorithm calls itself to solve these smaller problems. The algorithm calls itself to solve these smaller problems. The solutions to the smaller problems are combined to form the solution to the original problem. The solutions to the smaller problems are combined to form the solution to the original problem.

3 36/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion General and Base Cases A problem cannot be split into smaller problems infinitely many times. In a recursive function, there will always be two cases: The general case is split down into smaller cases. The general case is split down into smaller cases. The base case is simple enough so that it can be trivially solved. That solution is returned, rather than continuing to split the problem. This solution then forms the bases for solving larger problems. The base case is simple enough so that it can be trivially solved. That solution is returned, rather than continuing to split the problem. This solution then forms the bases for solving larger problems.

4 46/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Simple Recursion Simple recursive functions can be implemented using iteration (ie, regular while/do/for loops). Tail recursion refers to a recursive function where the last operation in the function is to call itself (but no where else). These are always simple recursive functions.

5 56/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Complex Recursion Complex recursive functions can be implemented using iteration plus a stack to hold partial results of problems. If a function cannot complete until it has a solution to a sub-problem, it is complex. If the language being used does not support formal parameters and local variables, it will not support recursion. This is because the parameters and local variables are kept on the system stack and managed for you. In this case, you will need to implement a stack yourself.

6 66/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Examples See the following examples in the textbook: Fibonacci Numbers Listing 12.6 on page 639 Fibonacci Numbers Listing 12.6 on page 639 Binary Search Case Study on page 644 Binary Search Case Study on page 644 Towers of Hanoi Case Study on page 649 Towers of Hanoi Case Study on page 649

7 76/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Reading This same material is also covered in the textbook in: Section 6.6 Section 6.6 Chapter 12 Chapter 12


Download ppt "16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm."

Similar presentations


Ads by Google