View
255
Download
0
Embed Size (px)
Citation preview
1104/18/2304/18/23 21:3321:33 RecursionRecursion
RecursionRecursion
Recursion is when a function calls itself to Recursion is when a function calls itself to implement an algorithm.implement an algorithm.Really a paradigm for designing software.Really a paradigm for designing software.Advantages:Advantages: Some programming problems have very simple and Some programming problems have very simple and
elegant recursive solutions.elegant recursive solutions.
Disadvantages:Disadvantages: Additional memory and execution resources are Additional memory and execution resources are
needed.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.
2204/18/2304/18/23 21:3321:33 RecursionRecursion
Designing a Recursive AlgorithmDesigning a Recursive Algorithm
Three Major Steps:Three Major Steps: Divide the problem into two or more smaller Divide the problem into two or more smaller
problems that have the exact same form as problems that have the exact same form as the original problem.the original problem.
The algorithm calls itself to solve these The algorithm calls itself to solve these smaller problems.smaller problems.
The solutions to the smaller problems are The solutions to the smaller problems are combined to form the solution to the original combined to form the solution to the original problem.problem.
3304/18/2304/18/23 21:3321:33 RecursionRecursion
General and Base CasesGeneral and Base Cases
A problem cannot be split into smaller problems A problem cannot be split into smaller problems infinitely many times.infinitely many times.
In a recursive function, there will always be two In a recursive function, there will always be two cases: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 The base case is simple enough so that it can be
trivially solved. That solution is returned, rather than trivially solved. That solution is returned, rather than continuing to split the problem. This solution then continuing to split the problem. This solution then forms the bases for solving larger problems.forms the bases for solving larger problems.
4404/18/2304/18/23 21:3321:33 RecursionRecursion
Simple RecursionSimple Recursion
Simple recursive functions can be Simple recursive functions can be implemented using iteration (ie, regular implemented using iteration (ie, regular while/do/for loops).while/do/for loops).
Tail recursion refers to a recursive function Tail recursion refers to a recursive function where the last operation in the function is where the last operation in the function is to call itself (but no where else). These are to call itself (but no where else). These are always simple recursive functions.always simple recursive functions.
5504/18/2304/18/23 21:3321:33 RecursionRecursion
Complex RecursionComplex Recursion
Complex recursive functions can be Complex recursive functions can be implemented using iteration plus a stack to hold implemented using iteration plus a stack to hold partial results of problems. If a function cannot partial results of problems. If a function cannot complete until it has a solution to a sub-problem, complete until it has a solution to a sub-problem, it is complex.it is complex.If the language being used does not support If the language being used does not support formal parameters and local variables, it will not formal parameters and local variables, it will not support recursion. This is because the support recursion. This is because the parameters and local variables are kept on the parameters and local variables are kept on the system stack and managed for you. In this case, system stack and managed for you. In this case, you will need to implement a stack yourself.you will need to implement a stack yourself.
6604/18/2304/18/23 21:3321:33 RecursionRecursion
ExamplesExamples
See the following examples in the See the following examples in the textbook:textbook: Fibonacci Numbers Listing 12.6 on page 639Fibonacci Numbers Listing 12.6 on page 639 Binary Search Case Study on page 644Binary Search Case Study on page 644 Towers of Hanoi Case Study on page 649Towers of Hanoi Case Study on page 649