






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, their definition, approaches, and formats. It includes examples of finding an element in an array, counting elements, calculating factorials, and solving the towers of hanoi problem. The document also covers the requirements for making recursion work and the concept of proof by induction. Recursive algorithms are compared to iterative algorithms, and the document concludes with a discussion of possible problems, such as infinite loops and inefficiency.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Satisfying ⇒ find any legal solution Optimization ⇒ find best solution (vs. cost metric)
Iterative ⇒ execute action in loop Recursive ⇒ reapply action to subproblem(s)
Recursive Algorithm
An algorithm that calls itself
Algorithm Format
Solve small problem directly
Simplify problem into smaller subproblem(s) Recursively apply algorithm to subproblem(s) Calculate overall solution
Example – Factorial
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 }
Requirements
Small version of problem solvable without recursion Strategy to simplify problem into 1 or more smaller subproblems Ability to calculate overall solution from solution(s) to subproblem(s)
Making Recursion Work
Recursion vs. Iteration
Higher overhead Time to perform function call Memory for activation records (call stack) May be simpler algorithm Easier to understand, debug, maintain Natural for backtracking searches Suited for recursive data structures Trees, graphs…
Example – Factorial
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
Recursive algorithm is simpler than iterative solution
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 – Inefficiency
If recomputing solutions for subproblems
Fibonacci numbers fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
Possible Problems – Inefficiency
If n is 0 or 1, return 1 Else compute fibonacci(n-1) and fibonacci(n-2) Return their sum
Computes fibonacci(1) 2n^ times
Iteration Dynamic programming Will examine different algorithm strategies later…