
















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
An introduction to recursive algorithms, a problem-solving strategy where a procedure calls itself. The approach involves simplifying complex problems into smaller instances and solving them recursively, with the solutions combined to solve the original problem. The recursive algorithm format, examples such as finding an element in an array, counting elements, and calculating factorials, and the properties and advantages of recursion over iteration. It also discusses the towers of hanoi problem and the importance of designing correct recursive algorithms.
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Recursion
A procedure that calls itself
Solve it directly
Else
Simplify problem instance into smallerinstance(s) of the original problem
Solve smaller instance using same algorithm
Combine solution(s) to solve original problem
Example – Find
Base case
If array is empty, return false Recursive step
If 1
st
element of array is given value, return true
Skip 1
st
element and recur on remainder of array
Example – Count
Base case
If array is empty, return 0 Recursive step
Skip 1
st
element and recur on remainder of array
Add 1 to result
Example – Factorial
if ( n == 0 ) return 1;
// base case
return n * fact(n-1);
// recursive step
Properties
State of current procedure is saved whenprocedure 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
Higher overhead
Time to perform function call Memory for call stack May be simpler algorithm
Easier to understand, debug, maintain Natural for backtracking searches Suited for recursive data structures
Trees, graphs…
Example – Towers of Hanoi
Move stack of disks between pegs Can only move top disk(s) in stack Only allowed to place disk on top of larger disk
Recursion vs. Iteration
May be more efficient
No additional function calls Run faster, use less memory
Making Recursion Work
Base case is
Recognized correctly Solved correctly
Recursive case
Solves 1 or more simpler subproblems Can calculate solution from solution(s) tosubproblems
Proof By Induction
Base case
Prove theorem is true for n = 0, and
Inductive step
Assume theorem is true for n
(inductive hypothesis) Prove theorem must be true for n+
Types of Recursion
Single recursive call at end of function Exampleint tail( int n ) {
… return function( tail(n-1) ); } Can easily transform to iteration (loop)
Possible Problems – Infinite Loop
If recursion not applied to simpler problemint 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)