


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



A procedure that calls itself
If ( problem instance is simple / trivial ) Solve it directly Else
Solve small problem directly
Simplify problem into smaller subproblem(s) Recursively apply algorithm to subproblem(s) Calculate overall solution
Base case If array is empty, return false Recursive step If 1 st^ element of array is given value, return true Skip 1st^ element and recur on remainder of array
Base case If array is empty, return 0 Recursive step Skip 1st^ element and recur on remainder of array Add 1 to result
n! = n × n-1 × n-2 × n-3 × … × 3 × 2 × 1 0! = 1
Base case If n = 0, return 1 Recursive step Calculate the factorial of n- Return n × (the factorial of n-1)
Example – Factorial
int fact ( int n ) { if ( n == 0 ) return 1; // base case return n * fact(n-1); // recursive step }
Properties
State of current procedure is saved when procedure is recursively invoked Every procedure invocation gets own stack space
Use iteration with explicit stack to store state Algorithm may be simpler for one approach
Recursion vs. Iteration
int fact ( int n ) { if ( n == 0 ) return 1; return n * fact(n-1); }
Recursive algorithm is closer to factorial definition
int fact ( int n ) { int i, res; res = 1; for (i=n; i>0; i--) { res = res * i; } return res; }
Example – Towers of Hanoi
Move stack of disks between pegs Can only move top disk in stack Only allowed to place disk on top of larger disk
Example – Towers of Hanoi
Base case If n = 1, move disk from X to Y Recursive step
Iterative algorithm would take much longer to describe!
Recursion vs. Iteration
May be more efficient No additional function calls Run faster, use less memory
Possible Problems – Infinite Loop
If recursion not applied to simpler problem
int bad ( int n ) { if ( n == 0 ) return 1; return bad(n); }
Will infinite loop Eventually halt when runs out of (stack) memory Stack overflow
Possible Problems – Efficiency
If recomputing solutions for subproblems
Fibonacci numbers fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
Possible Problems – Efficiency
If n is 0 or 1, return 1 Else compute fibonacci(n-1) and fibonacci(n-2) Return their sum
Computes fibonacci(1) 2 n^ times
Iteration Dynamic programming Will examine different algorithm strategies later…
Examples of Recursive Algorithms
N-Queens
Place queens on a board such that every row and column contains one queen, but no queen can attack another queen
To place queens on NxN board Assume you’ve already placed K queens
Fractals
Construct shapes using a simple recursive definition with a natural appearance
Appears similar at all scales of magnification Therefore “infinitely complex” Not easily described in Euclidean geometry
Mandelbrot Set