{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cp2_020

# cp2_020 - CSIS1122B Computer Programming II Module 2...

This preview shows pages 1–11. Sign up to view the full content.

CSIS1122B Computer Programming II Module 2 – Recursion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Overview What is recursion? Why recursion? Implementing recursion. Improving performance of recursion. [O1: Programming Techniques]
What is Recursion? ( Divide-and-Conquer ) We can call sub- routines (functions) to help us solving some sub-tasks. char x, n; cin >> x >> n; cout << exp(x, n) << endl; // exp(x, n) computes x^n char x, n; cin >> x >> n; cout << exp(x, n) << endl; // exp(x, n) computes x^n // pre: x, n are integers, n >= 0 // post: x^n is returned. double exp(int x, int n) { //... } // pre: x, n are integers, n >= 0 // post: x^n is returned. double exp(int x, int n) { //... }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CP2_020:4 Recursive Functions There is a special type of functions called recursive functions recursive functions A recursive function is a function that calls itself during execution. e.g., double exp(int x, int n) { if (n == 0) return 1; /* else */ return x * exp(x, n – 1); } double exp(int x, int n) { if (n == 0) return 1; /* else */ return x * exp(x, n – 1) ; }
CP2_020:5 Recursion When solving a problem, one key technique is to break a big problem into smaller ones. Sometimes, at least one sub-problem is very similar to the original problem except that the problem size is smaller. e.g., to compute exp(x,n), we can compute x * exp(x, n – 1) This is very similar to the original problem, except that the problem size is reduced by one. By doing one extra multiplication, we can use the result of smaller problem to solve a larger problem.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CP2_020:6 Why Recursion? Recursion is a very powerful problem solving tool Once recursive structure of a problem is identified , the corresponding recursive function is very easy to code Their code are usually shorter than their iterative counterparts. Correctness of recursive function is easier to proof based on Mathematical Induction.
CP2_020:7 How to Use Recursion? Key: identify the recursive structure of the problem. However, there is no single method to identify the recursive structure of all problems. :-( We need experience! The usual case is that we can express the original problem into similar and smaller ones .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CP2_020:8 Recursive Structure Examples y n = y n-1 * y n! = n * (n-1)! gcd(a, b) = gcd(b, a%b) F n = F n-1 + F n-2 (Fibonacci number) All permutation of some characters = {permutations begin with the 1 st char} U {permutations begin with the 2 nd char} U .. {permutations begin with the last char}
CP2_020:9 Base Cases Only recursive structure is not enough. We must have base cases the cases which the solution can be readily available. e.g., y 0 = 1 1! = 1, 0! = 1 gcd(a, 0) = a F 0 = 0, F 1 = 1 Permutation of 0 chars is an empty set. Otherwise, the recursion continues forever and never ends.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CP2_020:10 Writing a Recursive Function Once the recursive structure and base cases are found, the recursive function has this structure: RType MyFunc(Problem p) { if (is_base_case(p)) { // solve it directly ...
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}