# Recursion - CSS342 Recursion Professor Munehiro Fukuda...

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

CSS342: Recursion 1 CSS342: Recursion Professor: Munehiro Fukuda

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

View Full Document
CSS342: Recursion 2 Topics Day 1: Lecture Basic recursions 1. Sum 2. Fact 3. Print a number in any base 4. Binary search 5. Fibonacci sequence Tracing recursions Recursive applications Towers of Hanoi Backtrack (8 queens) Efficiency of recursion Day 2: Lab Work Drawing Fractal Figures with Recursion Ruler Koch Curves Sierpinski Arrowhead Gosper or Hilbert Curves
CSS342: Recursion 3 Basic Recursions Break a problem into smaller identical problems Each recursive call solves an identical but smaller problem. Stop the break-down process at a special case whose solution is obvious, (termed a base case a base case ) Each recursive call tests the base case to eventually stop. Otherwise, we fall into an infinite recursion. Basic Recursions

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

View Full Document
CSS342: Recursion 4 Example 1: The Sum of the First N Positive Integers Definition: sum(n) = n + (n-1) + (n-2) + … + 1 for any integer n > 0 Recursive relation; sum(n) = n + [(n-1) + (n-2) + … + 1] = n + sum(n-1) Looks so nice, but how about n == 1? sum(1) = 1 + sum(0), but the argument to sum( ) must be positive Final recursive definition: sum(n) = 1 if n = 1 (Base case) = n + sum(n-1) if n > 1 (Recursive call) Basic Recursions
CSS342: Recursion 5 Recursive Definition of sum(n) int sum(int n) { if (n == 1) // base case return 1; else return n + sum(n-1); // recursive call A } How should I test this function’s correctness? Use the box method: A box has each function’s local environment ex. cout << sum(3); 1. Arguments 2. Local variables 3. Place holder for the value returned by a called function 4. A return value n = 3 A: sum(n-1) = ? return ? Basic Recursions

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

View Full Document
CSS342: Recursion 6 Box trace of sum(3) n = 3 A: sum(n-1)=? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 n = 3 A: sum(n-1)= ? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 Each box corresponds to a function’s activation record or stack. cout << sum(3); 1 3 3 6 Basic Recursions
CSS342: Recursion 7 Example 2: The Factorial of n Definition: factorial(n) = n * (n-1) * (n-2) * … * 1 for any integer n > 0 factorial(0) = 1 Recursive relation; factorial(n) = n * [(n-1) * (n-2) * … * 1] = n * factorial(n-1) Looks so nice, but how about n = 1? factorial(1) = 1 * factorial(0) but the argument to factorial( ) must be positive Final recursive definition: factorial(n) = 1 if n = 1 = n *factorial(n-1) if n > 1 Basic Recursions

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

View Full Document
8 Recursive Definition of facotrial(n) int fact(int n) { if (n == 1) // base case return 1; else return n * fact(n-1); // recursive call A } Trace its execution using the box method: ex. cout << fact(3); n = 3 A: fact(n-1) = ? return ?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 37

Recursion - CSS342 Recursion Professor Munehiro Fukuda...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online